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