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