]> git.saurik.com Git - wxWidgets.git/blame - interface/wx/arrstr.h
committing the slightly-modified patch by R.U.10 for documenting some public function...
[wxWidgets.git] / interface / wx / arrstr.h
CommitLineData
23324ae1
FM
1/////////////////////////////////////////////////////////////////////////////
2// Name: arrstr.h
e54c96f1 3// Purpose: interface of wxArrayString
23324ae1
FM
4// Author: wxWidgets team
5// RCS-ID: $Id$
6// Licence: wxWindows license
7/////////////////////////////////////////////////////////////////////////////
8
b7c50e37
FM
9/**
10 @todo
11 the following functions are not documented; do they need to be?
12 WXDLLIMPEXP_BASE int wxCMPFUNC_CONV wxStringSortAscending(wxString*, wxString*);
13 WXDLLIMPEXP_BASE int wxCMPFUNC_CONV wxStringSortDescending(wxString*, wxString*);
14*/
15
23324ae1
FM
16/**
17 @class wxArrayString
7c913512 18
39fb8056
FM
19 wxArrayString is an efficient container for storing wxString objects.
20
21 It has the same features as all wxArray classes, i.e. it dynamically expands
22 when new items are added to it (so it is as easy to use as a linked list),
23 but the access time to the elements is constant, instead of being linear in
24 number of elements as in the case of linked lists. It is also very size
25 efficient and doesn't take more space than a C array @e wxString[] type
26 (wxArrayString uses its knowledge of internals of wxString class to achieve this).
27
28 This class is used in the same way as other dynamic arrays(), except that no
71f8a117 29 ::WX_DEFINE_ARRAY declaration is needed for it.
39fb8056
FM
30 When a string is added or inserted in the array, a copy of the string is created,
31 so the original string may be safely deleted (e.g. if it was a @e wxChar *
32 pointer the memory it was using can be freed immediately after this).
33 In general, there is no need to worry about string memory deallocation when using
23324ae1 34 this class - it will always free the memory it uses itself.
7c913512 35
39fb8056
FM
36 The references returned by wxArrayString::Item, wxArrayString::Last or
37 wxArrayString::operator[] are not constant, so the array elements may
38 be modified in place like this:
7c913512 39
23324ae1
FM
40 @code
41 array.Last().MakeUpper();
42 @endcode
7c913512 43
39fb8056
FM
44 @note none of the methods of wxArrayString is virtual including its
45 destructor, so this class should not be used as a base class.
46
47 Although this is not true strictly speaking, this class may be considered as
48 a specialization of wxArray class for the wxString member data: it is not
49 implemented like this, but it does have all of the wxArray functions.
7c913512 50
b7c50e37
FM
51 @todo what about stl? how does it integrate?
52
23324ae1
FM
53 @library{wxbase}
54 @category{containers}
7c913512 55
39fb8056 56 @see wxArray, wxString, @ref overview_string
23324ae1
FM
57*/
58class wxArrayString : public wxArray
59{
60public:
23324ae1 61 /**
39fb8056 62 Default constructor.
23324ae1
FM
63 */
64 wxArrayString();
39fb8056
FM
65
66 /**
b7c50e37 67 Copy constructor.
39fb8056 68 */
7c913512 69 wxArrayString(const wxArrayString& array);
39fb8056
FM
70
71 //@{
72 /**
71f8a117 73 Constructor from a C string array. Pass a size @a sz and an array @a arr.
39fb8056 74 **/
7c913512
FM
75 wxArrayString(size_t sz, const char** arr);
76 wxArrayString(size_t sz, const wchar_t** arr);
23324ae1
FM
77 //@}
78
79 /**
71f8a117 80 Constructor from a wxString array. Pass a size @a sz and array @a arr.
39fb8056
FM
81 */
82 wxArrayString(size_t sz, const wxString* arr);
83
84 /**
85 Destructor frees memory occupied by the array strings. For performance
23324ae1
FM
86 reasons it is not virtual, so this class should not be derived from.
87 */
88 ~wxArrayString();
89
90 /**
4cc4bfaf 91 Appends the given number of @a copies of the new item @a str to the
23324ae1 92 array and returns the index of the first new item in the array.
39fb8056 93
39fb8056 94 @see Insert()
23324ae1 95 */
4cc4bfaf 96 size_t Add(const wxString& str, size_t copies = 1);
23324ae1
FM
97
98 /**
76e9224e 99 Preallocates enough memory to store @a nCount items.
39fb8056 100
76e9224e
FM
101 This function may be used to improve array class performance before
102 adding a known number of items consecutively.
23324ae1
FM
103 */
104 void Alloc(size_t nCount);
105
106 /**
107 Clears the array contents and frees memory.
39fb8056
FM
108
109 @see Empty()
23324ae1
FM
110 */
111 void Clear();
112
113 /**
39fb8056 114 Empties the array: after a call to this function GetCount() will return 0.
3c4f71cc 115 However, this function does not free the memory used by the array and so
39fb8056
FM
116 should be used when the array is going to be reused for storing other strings.
117 Otherwise, you should use Clear() to empty the array and free memory.
23324ae1
FM
118 */
119 void Empty();
120
121 /**
122 Returns the number of items in the array.
123 */
328f5751 124 size_t GetCount() const;
23324ae1
FM
125
126 /**
39fb8056 127 Search the element in the array, starting from the beginning if @a bFromEnd
71f8a117 128 is @false or from end otherwise. If @a bCase, comparison is case sensitive
39fb8056
FM
129 (default), otherwise the case is ignored.
130
b7c50e37 131 This function uses linear search for wxArrayString.
23324ae1
FM
132 Returns index of the first item matched or @c wxNOT_FOUND if there is no match.
133 */
8d483c9b 134 int Index(const wxString& sz, bool bCase = true, bool bFromEnd = false) const;
23324ae1
FM
135
136 /**
4cc4bfaf 137 Insert the given number of @a copies of the new element in the array before the
71f8a117 138 position @a nIndex. Thus, for example, to insert the string in the beginning of
39fb8056
FM
139 the array you would write:
140
141 @code
142 Insert("foo", 0);
143 @endcode
3c4f71cc 144
71f8a117 145 If @a nIndex is equal to GetCount() this function behaves as Add().
23324ae1
FM
146 */
147 void Insert(const wxString& str, size_t nIndex,
148 size_t copies = 1);
149
150 /**
151 Returns @true if the array is empty, @false otherwise. This function returns the
71f8a117 152 same result as GetCount() == 0 but is probably easier to read.
23324ae1 153 */
8d483c9b 154 bool IsEmpty() const;
23324ae1
FM
155
156 /**
8d483c9b 157 Return the array element at position @a nIndex. An assert failure will
23324ae1
FM
158 result from an attempt to access an element beyond the end of array in debug
159 mode, but no check is done in release mode.
39fb8056
FM
160
161 @see operator[] for the operator version.
23324ae1 162 */
8d483c9b 163 wxString& Item(size_t nIndex) const;
23324ae1
FM
164
165 /**
166 Returns the last element of the array. Attempt to access the last element of
167 an empty array will result in assert failure in debug build, however no checks
168 are done in release mode.
169 */
8d483c9b 170 wxString& Last() const;
23324ae1
FM
171
172 /**
173 Removes the first item matching this value. An assert failure is provoked by
174 an attempt to remove an element which does not exist in debug build.
39fb8056
FM
175
176 @see Index()
23324ae1
FM
177 */
178 void Remove(const wxString& sz);
179
180 /**
4cc4bfaf 181 Removes @a count items starting at position @a nIndex from the array.
23324ae1
FM
182 */
183 void RemoveAt(size_t nIndex, size_t count = 1);
184
185 /**
76e9224e
FM
186 Releases the extra memory allocated by the array.
187 This function is useful to minimize the array memory consumption.
39fb8056 188
76e9224e 189 @see Alloc()
23324ae1
FM
190 */
191 void Shrink();
192
39fb8056
FM
193 /**
194 Sorts the array in alphabetical order or in reverse alphabetical order if
195 @a reverseOrder is @true. The sort is case-sensitive.
39fb8056 196 */
39fb8056
FM
197 void Sort(bool reverseOrder = false);
198
23324ae1 199 /**
4cc4bfaf 200 Sorts the array using the specified @a compareFunction for item comparison.
71f8a117 201 @a CompareFunction is defined as a function taking two @e const wxString
3c4f71cc 202 parameters and returning an @e int value less than, equal to or greater
39fb8056 203 than 0 if the first string is less than, equal to or greater than the
23324ae1 204 second one.
39fb8056
FM
205
206 Example:
207 The following example sorts strings by their length.
208
209 @code
210 static int CompareStringLen(const wxString& first, const wxString& second)
211 {
212 return first.length() - second.length();
213 }
3c4f71cc 214
39fb8056 215 ...
3c4f71cc 216
39fb8056 217 wxArrayString array;
3c4f71cc 218
39fb8056
FM
219 array.Add("one");
220 array.Add("two");
221 array.Add("three");
222 array.Add("four");
3c4f71cc 223
39fb8056
FM
224 array.Sort(CompareStringLen);
225 @endcode
23324ae1 226 */
7c913512 227 void Sort(CompareFunction compareFunction);
23324ae1
FM
228
229 /**
230 Compares 2 arrays respecting the case. Returns @true if the arrays have
231 different number of elements or if the elements don't match pairwise.
232 */
328f5751 233 bool operator !=(const wxArrayString& array) const;
23324ae1
FM
234
235 /**
236 Assignment operator.
237 */
8d483c9b 238 wxArrayString& operator=(const wxArrayString&);
23324ae1
FM
239
240 /**
241 Compares 2 arrays respecting the case. Returns @true only if the arrays have
242 the same number of elements and the same strings in the same order.
243 */
328f5751 244 bool operator ==(const wxArrayString& array) const;
23324ae1
FM
245
246 /**
8d483c9b 247 Return the array element at position @a nIndex. An assert failure will
39fb8056
FM
248 result from an attempt to access an element beyond the end of array in
249 debug mode, but no check is done in release mode.
250
251 This is the operator version of the Item() method.
23324ae1 252 */
8d483c9b 253 wxString& operator[](size_t nIndex) const;
23324ae1
FM
254};
255
256
b7c50e37
FM
257/**
258 @class wxSortedArrayString
b7c50e37
FM
259
260 wxSortedArrayString is an efficient container for storing wxString objects
261 which always keeps the string in alphabetical order.
262
263 wxSortedArrayString uses binary search in its wxArrayString::Index() function
264 (instead of linear search for wxArrayString::Index()) which makes it much more
265 efficient if you add strings to the array rarely (because, of course, you have
266 to pay for Index() efficiency by having Add() be slower) but search for them
267 often. Several methods should not be used with sorted array (basically, all
268 those which break the order of items) which is mentioned in their description.
269
270 @todo what about STL? who does it integrates?
271
272 @library{wxbase}
273 @category{containers}
274
275 @see wxArray, wxString, @ref overview_string
276*/
277class wxSortedArrayString : public wxArrayString
278{
279public:
280
281 /**
282 Copy constructor. Note that when an array is assigned to a sorted array,
283 its contents is automatically sorted during construction.
284 */
285 wxArrayString(const wxArrayString& array);
286
287 /**
288 @copydoc wxArrayString::Add()
289
290 @warning
291 For sorted arrays, the index of the inserted item will not be, in general,
292 equal to GetCount() - 1 because the item is inserted at the correct position
293 to keep the array sorted and not appended.
294 */
295 size_t Add(const wxString& str, size_t copies = 1);
296
297
298 /**
299 @copydoc wxArrayString::Index()
300
3c4f71cc 301 This function uses binary search for wxSortedArrayString, but it ignores
b7c50e37
FM
302 the @a bCase and @a bFromEnd parameters.
303 */
304 int Index(const wxString& sz, bool bCase = true,
fadc2df6 305 bool bFromEnd = false) const;
b7c50e37
FM
306
307 /**
308 @warning this function should not be used with sorted arrays because it
309 could break the order of items and, for example, subsequent calls
310 to Index() would then not work!
311 */
312 void Insert(const wxString& str, size_t nIndex,
313 size_t copies = 1);
314
b02f1c86 315 //@{
b7c50e37
FM
316 /**
317 @warning this function should not be used with sorted array because it could
318 break the order of items and, for example, subsequent calls to Index()
b02f1c86
FM
319 would then not work! Also, sorting a wxSortedArrayString doesn't make
320 sense because its elements are always already sorted.
b7c50e37
FM
321 */
322 void Sort(bool reverseOrder = false);
b7c50e37 323 void Sort(CompareFunction compareFunction);
b02f1c86 324 //@}
b7c50e37
FM
325};
326
e54c96f1 327
23324ae1
FM
328// ============================================================================
329// Global functions/macros
330// ============================================================================
331
39fb8056
FM
332/** @ingroup group_funcmacro_string */
333//@{
334
23324ae1 335/**
4cc4bfaf 336 Splits the given wxString object using the separator @a sep and returns the
23324ae1 337 result as a wxArrayString.
39fb8056 338
4cc4bfaf 339 If the @a escape character is non-@NULL, then the occurrences of @a sep
39fb8056 340 immediately prefixed with @a escape are not considered as separators.
23324ae1
FM
341 Note that empty tokens will be generated if there are two or more adjacent
342 separators.
7c913512 343
e54c96f1 344 @see wxJoin()
3950d49c
BP
345
346 @header{wx/arrstr.h}
23324ae1
FM
347*/
348wxArrayString wxSplit(const wxString& str, const wxChar sep,
39fb8056 349 const wxChar escape = '\\');
23324ae1
FM
350
351/**
39fb8056
FM
352 Concatenate all lines of the given wxArrayString object using the separator
353 @a sep and returns the result as a wxString.
354
4cc4bfaf 355 If the @a escape character is non-@NULL, then it's used as prefix for each
71f8a117 356 occurrence of @a sep in the strings contained in @a arr before joining them
3950d49c
BP
357 which is necessary in order to be able to recover the original array
358 contents from the string later using wxSplit().
39fb8056
FM
359
360 @see wxSplit()
3950d49c
BP
361
362 @header{wx/arrstr.h}
23324ae1
FM
363*/
364wxString wxJoin(const wxArrayString& arr, const wxChar sep,
39fb8056 365 const wxChar escape = '\\');
23324ae1 366
39fb8056 367//@}
3950d49c 368