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