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