]>
Commit | Line | Data |
---|---|---|
23324ae1 FM |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: dynarray.h | |
e54c96f1 | 3 | // Purpose: interface of wxArray<T> |
23324ae1 FM |
4 | // Author: wxWidgets team |
5 | // RCS-ID: $Id$ | |
6 | // Licence: wxWindows license | |
7 | ///////////////////////////////////////////////////////////////////////////// | |
8 | ||
9 | /** | |
23324ae1 | 10 | @wxheader{dynarray.h} |
7c913512 | 11 | |
0c1fe6e9 | 12 | This section describes the so called @e "dynamic arrays". This is a C |
23324ae1 | 13 | array-like type safe data structure i.e. the member access time is constant |
0c1fe6e9 BP |
14 | (and not linear according to the number of container elements as for linked |
15 | lists). However, these arrays are dynamic in the sense that they will | |
16 | automatically allocate more memory if there is not enough of it for adding | |
17 | a new element. They also perform range checking on the index values but in | |
18 | debug mode only, so please be sure to compile your application in debug | |
19 | mode to use it (see @ref overview_debugging for details). So, unlike the | |
20 | arrays in some other languages, attempt to access an element beyond the | |
21 | arrays bound doesn't automatically expand the array but provokes an | |
22 | assertion failure instead in debug build and does nothing (except possibly | |
23 | crashing your program) in the release build. | |
24 | ||
25 | The array classes were designed to be reasonably efficient, both in terms | |
26 | of run-time speed and memory consumption and the executable size. The speed | |
27 | of array item access is, of course, constant (independent of the number of | |
28 | elements) making them much more efficient than linked lists (wxList). | |
29 | Adding items to the arrays is also implemented in more or less constant | |
30 | time, but the price is preallocating the memory in advance. In the | |
31 | "memory management" function section, you may find some useful hints about | |
32 | optimizing wxArray memory usage. As for executable size, all wxArray | |
33 | functions are inline, so they do not take @e any space at all. | |
7c913512 | 34 | |
23324ae1 | 35 | wxWidgets has three different kinds of array. All of them derive from |
0c1fe6e9 BP |
36 | wxBaseArray class which works with untyped data and can not be used |
37 | directly. The standard macros WX_DEFINE_ARRAY(), WX_DEFINE_SORTED_ARRAY() | |
38 | and WX_DEFINE_OBJARRAY() are used to define a new class deriving from it. | |
39 | The classes declared will be called in this documentation wxArray, | |
40 | wxSortedArray and wxObjArray but you should keep in mind that no classes | |
41 | with such names actually exist, each time you use one of the | |
42 | WX_DEFINE_XXXARRAY() macros, you define a class with a new name. In fact, | |
43 | these names are "template" names and each usage of one of the macros | |
44 | mentioned above creates a template specialization for the given element | |
45 | type. | |
46 | ||
47 | wxArray is suitable for storing integer types and pointers which it does | |
48 | not treat as objects in any way, i.e. the element pointed to by the pointer | |
49 | is not deleted when the element is removed from the array. It should be | |
50 | noted that all of wxArray's functions are inline, so it costs strictly | |
51 | nothing to define as many array types as you want (either in terms of the | |
52 | executable size or the speed) as long as at least one of them is defined | |
53 | and this is always the case because wxArrays are used by wxWidgets | |
54 | internally. This class has one serious limitation: it can only be used for | |
55 | storing integral types (bool, char, short, int, long and their unsigned | |
56 | variants) or pointers (of any kind). An attempt to use with objects of | |
57 | @c sizeof() greater than @c sizeof(long) will provoke a runtime assertion | |
58 | failure, however declaring a wxArray of floats will not (on the machines | |
59 | where @c "sizeof(float) <= sizeof(long)"), yet it will @b not work, please | |
60 | use wxObjArray for storing floats and doubles. | |
61 | ||
62 | wxSortedArray is a wxArray variant which should be used when searching in | |
63 | the array is a frequently used operation. It requires you to define an | |
64 | additional function for comparing two elements of the array element type | |
65 | and always stores its items in the sorted order (according to this | |
66 | function). Thus, its Index() function execution time is @c "O(log(N))" | |
67 | instead of @c "O(N)" for the usual arrays but the Add() method is slower: | |
68 | it is @c "O(log(N))" instead of constant time (neglecting time spent in | |
69 | memory allocation routine). However, in a usual situation elements are | |
70 | added to an array much less often than searched inside it, so wxSortedArray | |
71 | may lead to huge performance improvements compared to wxArray. Finally, it | |
72 | should be noticed that, as wxArray, wxSortedArray can be only used for | |
73 | storing integral types or pointers. | |
74 | ||
75 | wxObjArray class treats its elements like "objects". It may delete them | |
76 | when they are removed from the array (invoking the correct destructor) and | |
77 | copies them using the objects copy constructor. In order to implement this | |
78 | behaviour the definition of the wxObjArray arrays is split in two parts: | |
79 | first, you should declare the new wxObjArray class using the | |
80 | WX_DECLARE_OBJARRAY() macro and then you must include the file defining the | |
81 | implementation of template type: @<wx/arrimpl.cpp@> and define the array | |
82 | class with the WX_DEFINE_OBJARRAY() macro from a point where the full (as | |
83 | opposed to 'forward') declaration of the array elements class is in scope. | |
84 | As it probably sounds very complicated here is an example: | |
7c913512 | 85 | |
23324ae1 | 86 | @code |
0c1fe6e9 | 87 | #include <wx/dynarray.h> |
7c913512 | 88 | |
0c1fe6e9 BP |
89 | // We must forward declare the array because it is used |
90 | // inside the class declaration. | |
23324ae1 FM |
91 | class MyDirectory; |
92 | class MyFile; | |
7c913512 | 93 | |
0c1fe6e9 BP |
94 | // This defines two new types: ArrayOfDirectories and ArrayOfFiles which |
95 | // can be now used as shown below. | |
23324ae1 FM |
96 | WX_DECLARE_OBJARRAY(MyDirectory, ArrayOfDirectories); |
97 | WX_DECLARE_OBJARRAY(MyFile, ArrayOfFiles); | |
7c913512 | 98 | |
23324ae1 FM |
99 | class MyDirectory |
100 | { | |
0c1fe6e9 BP |
101 | // ... |
102 | ArrayOfDirectories m_subdirectories; // All subdirectories | |
103 | ArrayOfFiles m_files; // All files in this directory | |
23324ae1 | 104 | }; |
7c913512 | 105 | |
0c1fe6e9 | 106 | // ... |
7c913512 | 107 | |
0c1fe6e9 | 108 | // Now that we have MyDirectory declaration in scope we may finish the |
23324ae1 FM |
109 | // definition of ArrayOfDirectories -- note that this expands into some C++ |
110 | // code and so should only be compiled once (i.e., don't put this in the | |
111 | // header, but into a source file or you will get linking errors) | |
0c1fe6e9 | 112 | #include <wx/arrimpl.cpp> // This is a magic incantation which must be done! |
23324ae1 | 113 | WX_DEFINE_OBJARRAY(ArrayOfDirectories); |
7c913512 | 114 | |
23324ae1 FM |
115 | // that's all! |
116 | @endcode | |
7c913512 | 117 | |
0c1fe6e9 | 118 | It is not as elegant as writing this: |
7c913512 | 119 | |
23324ae1 | 120 | @code |
0c1fe6e9 | 121 | typedef std::vector<MyDirectory> ArrayOfDirectories; |
23324ae1 | 122 | @endcode |
7c913512 | 123 | |
0c1fe6e9 BP |
124 | But is not that complicated and allows the code to be compiled with any, |
125 | however dumb, C++ compiler in the world. | |
7c913512 | 126 | |
0c1fe6e9 BP |
127 | Remember to include @<wx/arrimpl.cpp@> just before each |
128 | WX_DEFINE_OBJARRAY() ocurrence in your code, even if you have several in | |
129 | the same file. | |
7c913512 | 130 | |
23324ae1 | 131 | Things are much simpler for wxArray and wxSortedArray however: it is enough |
0c1fe6e9 | 132 | just to write: |
7c913512 | 133 | |
23324ae1 FM |
134 | @code |
135 | WX_DEFINE_ARRAY_INT(int, ArrayOfInts); | |
136 | WX_DEFINE_SORTED_ARRAY_INT(int, ArrayOfSortedInts); | |
137 | @endcode | |
7c913512 | 138 | |
0c1fe6e9 BP |
139 | There is only one @c DEFINE macro and no need for separate @c DECLARE one. |
140 | For the arrays of the primitive types, the macros | |
23324ae1 | 141 | @c WX_DEFINE_ARRAY_CHAR/SHORT/INT/SIZE_T/LONG/DOUBLE should be used |
0c1fe6e9 BP |
142 | depending on the sizeof of the values (notice that storing values of |
143 | smaller type, e.g. shorts, in an array of larger one, e.g. @c ARRAY_INT, | |
144 | does not work on all architectures!). | |
145 | ||
146 | ||
147 | @section array_macros Macros for Template Array Definition | |
148 | ||
149 | To use an array you must first define the array class. This is done with | |
150 | the help of the macros in this section. The class of array elements must be | |
151 | (at least) forward declared for WX_DEFINE_ARRAY(), WX_DEFINE_SORTED_ARRAY() | |
152 | and WX_DECLARE_OBJARRAY() macros and must be fully declared before you use | |
153 | WX_DEFINE_OBJARRAY() macro. | |
154 | ||
155 | - WX_DEFINE_ARRAY() | |
156 | - WX_DEFINE_EXPORTED_ARRAY() | |
157 | - WX_DEFINE_USER_EXPORTED_ARRAY() | |
158 | - WX_DEFINE_SORTED_ARRAY() | |
159 | - WX_DEFINE_SORTED_EXPORTED_ARRAY() | |
160 | - WX_DEFINE_SORTED_USER_EXPORTED_ARRAY() | |
161 | - WX_DECLARE_EXPORTED_OBJARRAY() | |
162 | - WX_DECLARE_USER_EXPORTED_OBJARRAY() | |
163 | - WX_DEFINE_OBJARRAY() | |
164 | - WX_DEFINE_EXPORTED_OBJARRAY() | |
165 | - WX_DEFINE_USER_EXPORTED_OBJARRAY() | |
166 | ||
167 | To slightly complicate the matters even further, the operator "->" defined | |
168 | by default for the array iterators by these macros only makes sense if the | |
169 | array element type is not a pointer itself and, although it still works, | |
170 | this provokes warnings from some compilers and to avoid them you should use | |
171 | the @c _PTR versions of the macros above. For example, to define an array | |
172 | of pointers to @c double you should use: | |
173 | ||
174 | @code | |
175 | WX_DEFINE_ARRAY_PTR(double *, MyArrayOfDoublePointers); | |
176 | @endcode | |
177 | ||
178 | Note that the above macros are generally only useful for wxObject types. | |
179 | There are separate macros for declaring an array of a simple type, such as | |
180 | an int. | |
181 | ||
182 | The following simple types are supported: | |
183 | - @c int | |
184 | - @c long | |
185 | - @c size_t | |
186 | - @c double | |
187 | ||
188 | To create an array of a simple type, simply append the type you want in | |
189 | CAPS to the array definition. | |
190 | ||
191 | For example, you'd use one of the following variants for an integer array: | |
192 | ||
193 | - WX_DEFINE_ARRAY_INT() | |
194 | - WX_DEFINE_EXPORTED_ARRAY_INT() | |
195 | - WX_DEFINE_USER_EXPORTED_ARRAY_INT() | |
196 | - WX_DEFINE_SORTED_ARRAY_INT() | |
197 | - WX_DEFINE_SORTED_EXPORTED_ARRAY_INT() | |
198 | - WX_DEFINE_SORTED_USER_EXPORTED_ARRAY_INT() | |
199 | ||
7c913512 | 200 | |
23324ae1 | 201 | @library{wxbase} |
0c1fe6e9 | 202 | @category{containers} |
7c913512 | 203 | |
b1db61e1 | 204 | @see @ref overview_container, wxList<T>, wxVector<T> |
23324ae1 | 205 | */ |
7c913512 | 206 | class wxArray<T> |
23324ae1 FM |
207 | { |
208 | public: | |
23324ae1 | 209 | /** |
0c1fe6e9 BP |
210 | @name Constructors and Destructors |
211 | ||
212 | Array classes are 100% C++ objects and as such they have the | |
213 | appropriate copy constructors and assignment operators. Copying wxArray | |
214 | just copies the elements but copying wxObjArray copies the arrays | |
215 | items. However, for memory-efficiency sake, neither of these classes | |
216 | has virtual destructor. It is not very important for wxArray which has | |
217 | trivial destructor anyhow, but it does mean that you should avoid | |
218 | deleting wxObjArray through a wxBaseArray pointer (as you would never | |
219 | use wxBaseArray anyhow it shouldn't be a problem) and that you should | |
220 | not derive your own classes from the array classes. | |
23324ae1 | 221 | */ |
0c1fe6e9 | 222 | //@{ |
23324ae1 FM |
223 | |
224 | /** | |
0c1fe6e9 | 225 | Default constructor. |
23324ae1 | 226 | */ |
0c1fe6e9 | 227 | wxArray(); |
23324ae1 | 228 | /** |
0c1fe6e9 | 229 | Default constructor initializes an empty array object. |
23324ae1 | 230 | */ |
0c1fe6e9 | 231 | wxObjArray(); |
23324ae1 | 232 | /** |
0c1fe6e9 BP |
233 | There is no default constructor for wxSortedArray classes - you must |
234 | initialize it with a function to use for item comparison. It is a | |
235 | function which is passed two arguments of type @c T where @c T is the | |
236 | array element type and which should return a negative, zero or positive | |
237 | value according to whether the first element passed to it is less than, | |
238 | equal to or greater than the second one. | |
23324ae1 | 239 | */ |
0c1fe6e9 | 240 | wxSortedArray(int (*)(T first, T second)compareFunction); |
23324ae1 FM |
241 | |
242 | /** | |
0c1fe6e9 BP |
243 | Performs a shallow array copy (i.e. doesn't copy the objects pointed to |
244 | even if the source array contains the items of pointer type). | |
23324ae1 | 245 | */ |
0c1fe6e9 | 246 | wxArray(const wxArray& array); |
23324ae1 | 247 | /** |
0c1fe6e9 BP |
248 | Performs a shallow array copy (i.e. doesn't copy the objects pointed to |
249 | even if the source array contains the items of pointer type). | |
23324ae1 | 250 | */ |
0c1fe6e9 BP |
251 | wxSortedArray(const wxSortedArray& array); |
252 | /** | |
253 | Performs a deep copy (i.e. the array element are copied too). | |
254 | */ | |
255 | wxObjArray(const wxObjArray& array); | |
23324ae1 | 256 | |
23324ae1 | 257 | /** |
0c1fe6e9 BP |
258 | Performs a shallow array copy (i.e. doesn't copy the objects pointed to |
259 | even if the source array contains the items of pointer type). | |
23324ae1 | 260 | */ |
0c1fe6e9 BP |
261 | wxArray& operator=(const wxArray& array); |
262 | /** | |
263 | Performs a shallow array copy (i.e. doesn't copy the objects pointed to | |
264 | even if the source array contains the items of pointer type). | |
265 | */ | |
266 | wxSortedArray& operator=(const wxSortedArray& array); | |
267 | /** | |
268 | Performs a deep copy (i.e. the array element are copied too). | |
269 | */ | |
270 | wxObjArray& operator=(const wxObjArray& array); | |
23324ae1 FM |
271 | |
272 | /** | |
0c1fe6e9 BP |
273 | This destructor does not delete all the items owned by the array, you |
274 | may use the WX_CLEAR_ARRAY() macro for this. | |
23324ae1 | 275 | */ |
0c1fe6e9 BP |
276 | ~wxArray(); |
277 | /** | |
278 | This destructor does not delete all the items owned by the array, you | |
279 | may use the WX_CLEAR_ARRAY() macro for this. | |
280 | */ | |
281 | ~wxSortedArray(); | |
282 | /** | |
283 | This destructor deletes all the items owned by the array. | |
284 | */ | |
285 | ~wxObjArray(); | |
286 | ||
287 | //@} | |
288 | ||
23324ae1 FM |
289 | |
290 | /** | |
0c1fe6e9 BP |
291 | @name Memory Management |
292 | ||
293 | Automatic array memory management is quite trivial: the array starts by | |
294 | preallocating some minimal amount of memory (defined by | |
295 | @c WX_ARRAY_DEFAULT_INITIAL_SIZE) and when further new items exhaust | |
296 | already allocated memory it reallocates it adding 50% of the currently | |
297 | allocated amount, but no more than some maximal number which is defined | |
298 | by the @c ARRAY_MAXSIZE_INCREMENT constant. Of course, this may lead to | |
299 | some memory being wasted (@c ARRAY_MAXSIZE_INCREMENT in the worst case, | |
300 | i.e. 4Kb in the current implementation), so the Shrink() function is | |
301 | provided to deallocate the extra memory. The Alloc() function can also | |
302 | be quite useful if you know in advance how many items you are going to | |
303 | put in the array and will prevent the array code from reallocating the | |
304 | memory more times than needed. | |
23324ae1 | 305 | */ |
0c1fe6e9 | 306 | //@{ |
23324ae1 FM |
307 | |
308 | /** | |
0c1fe6e9 BP |
309 | Preallocates memory for a given number of array elements. It is worth |
310 | calling when the number of items which are going to be added to the | |
311 | array is known in advance because it will save unneeded memory | |
312 | reallocation. If the array already has enough memory for the given | |
313 | number of items, nothing happens. In any case, the existing contents of | |
314 | the array is not modified. | |
23324ae1 | 315 | */ |
0c1fe6e9 | 316 | void Alloc(size_t count); |
23324ae1 | 317 | |
23324ae1 | 318 | /** |
0c1fe6e9 BP |
319 | Frees all memory unused by the array. If the program knows that no new |
320 | items will be added to the array it may call Shrink() to reduce its | |
321 | memory usage. However, if a new item is added to the array, some extra | |
322 | memory will be allocated again. | |
23324ae1 | 323 | */ |
0c1fe6e9 BP |
324 | void Shrink(); |
325 | ||
23324ae1 FM |
326 | //@} |
327 | ||
0c1fe6e9 | 328 | |
23324ae1 | 329 | /** |
0c1fe6e9 | 330 | @name Number of Elements and Simple Item Access |
23324ae1 | 331 | |
0c1fe6e9 BP |
332 | Functions in this section return the total number of array elements and |
333 | allow to retrieve them - possibly using just the C array indexing [] | |
334 | operator which does exactly the same as the Item() method. | |
335 | */ | |
23324ae1 | 336 | //@{ |
0c1fe6e9 | 337 | |
23324ae1 | 338 | /** |
0c1fe6e9 | 339 | Return the number of items in the array. |
23324ae1 | 340 | */ |
0c1fe6e9 | 341 | size_t GetCount() const; |
23324ae1 FM |
342 | |
343 | /** | |
344 | Returns @true if the array is empty, @false otherwise. | |
345 | */ | |
328f5751 | 346 | bool IsEmpty() const; |
23324ae1 FM |
347 | |
348 | /** | |
0c1fe6e9 BP |
349 | Returns the item at the given position in the array. If @a index is out |
350 | of bounds, an assert failure is raised in the debug builds but nothing | |
351 | special is done in the release build. | |
23324ae1 | 352 | |
0c1fe6e9 BP |
353 | The returned value is of type "reference to the array element type" for |
354 | all of the array classes. | |
23324ae1 | 355 | */ |
0c1fe6e9 | 356 | T& Item(size_t index) const; |
23324ae1 FM |
357 | |
358 | /** | |
0c1fe6e9 BP |
359 | Returns the last element in the array, i.e. is the same as calling |
360 | "Item(GetCount() - 1)". An assert failure is raised in the debug mode | |
361 | if the array is empty. | |
3c4f71cc | 362 | |
0c1fe6e9 BP |
363 | The returned value is of type "reference to the array element type" for |
364 | all of the array classes. | |
365 | */ | |
366 | T& Last() const; | |
3c4f71cc | 367 | |
0c1fe6e9 | 368 | //@} |
3c4f71cc | 369 | |
3c4f71cc | 370 | |
0c1fe6e9 BP |
371 | /** |
372 | @name Adding Items | |
373 | */ | |
374 | //@{ | |
3c4f71cc | 375 | |
0c1fe6e9 BP |
376 | /** |
377 | Appends the given number of @a copies of the @a item to the array | |
378 | consisting of the elements of type @c T. | |
3c4f71cc | 379 | |
0c1fe6e9 | 380 | This version is used with wxArray. |
3c4f71cc | 381 | |
0c1fe6e9 BP |
382 | You may also use WX_APPEND_ARRAY() macro to append all elements of one |
383 | array to another one but it is more efficient to use the @a copies | |
384 | parameter and modify the elements in place later if you plan to append | |
385 | a lot of items. | |
386 | */ | |
387 | void Add(T item, size_t copies = 1); | |
388 | /** | |
389 | Appends the @a item to the array consisting of the elements of type | |
390 | @c T. | |
3c4f71cc | 391 | |
0c1fe6e9 BP |
392 | This version is used with wxSortedArray, returning the index where |
393 | @a item is stored. | |
394 | */ | |
395 | size_t Add(T item); | |
396 | /** | |
397 | Appends the @a item to the array consisting of the elements of type | |
398 | @c T. | |
399 | ||
400 | This version is used with wxObjArray. The array will take ownership of | |
401 | the @item, deleting it when the item is deleted from the array. Note | |
402 | that you cannot append more than one pointer as reusing it would lead | |
403 | to deleting it twice (or more) resulting in a crash. | |
404 | ||
405 | You may also use WX_APPEND_ARRAY() macro to append all elements of one | |
406 | array to another one but it is more efficient to use the @a copies | |
407 | parameter and modify the elements in place later if you plan to append | |
408 | a lot of items. | |
409 | */ | |
410 | void Add(T* item); | |
411 | /** | |
412 | Appends the given number of @a copies of the @a item to the array | |
413 | consisting of the elements of type @c T. | |
3c4f71cc | 414 | |
0c1fe6e9 BP |
415 | This version is used with wxObjArray. The array will make a copy of the |
416 | item and will not take ownership of the original item. | |
3c4f71cc | 417 | |
0c1fe6e9 BP |
418 | You may also use WX_APPEND_ARRAY() macro to append all elements of one |
419 | array to another one but it is more efficient to use the @a copies | |
420 | parameter and modify the elements in place later if you plan to append | |
421 | a lot of items. | |
422 | */ | |
423 | void Add(T& item, size_t copies = 1); | |
3c4f71cc | 424 | |
0c1fe6e9 BP |
425 | /** |
426 | Inserts the given @a item into the array in the specified @e index | |
427 | position. | |
3c4f71cc | 428 | |
0c1fe6e9 BP |
429 | Be aware that you will set out the order of the array if you give a |
430 | wrong position. | |
3c4f71cc | 431 | |
0c1fe6e9 BP |
432 | This function is useful in conjunction with IndexForInsert() for a |
433 | common operation of "insert only if not found". | |
434 | */ | |
435 | void AddAt(T item, size_t index); | |
3c4f71cc | 436 | |
0c1fe6e9 BP |
437 | /** |
438 | Insert the given number of @a copies of the @a item into the array | |
439 | before the existing item @a n - thus, @e Insert(something, 0u) will | |
440 | insert an item in such way that it will become the first array element. | |
3c4f71cc | 441 | |
0c1fe6e9 BP |
442 | wxSortedArray doesn't have this function because inserting in wrong |
443 | place would break its sorted condition. | |
3c4f71cc | 444 | |
0c1fe6e9 BP |
445 | Please see Add() for an explanation of the differences between the |
446 | overloaded versions of this function. | |
447 | */ | |
448 | void Insert(T item, size_t n, size_t copies = 1); | |
449 | /** | |
450 | Insert the @a item into the array before the existing item @a n - thus, | |
451 | @e Insert(something, 0u) will insert an item in such way that it will | |
452 | become the first array element. | |
3c4f71cc | 453 | |
0c1fe6e9 BP |
454 | wxSortedArray doesn't have this function because inserting in wrong |
455 | place would break its sorted condition. | |
3c4f71cc | 456 | |
0c1fe6e9 BP |
457 | Please see Add() for an explanation of the differences between the |
458 | overloaded versions of this function. | |
459 | */ | |
460 | void Insert(T* item, size_t n); | |
461 | /** | |
462 | Insert the given number of @a copies of the @a item into the array | |
463 | before the existing item @a n - thus, @e Insert(something, 0u) will | |
464 | insert an item in such way that it will become the first array element. | |
3c4f71cc | 465 | |
0c1fe6e9 BP |
466 | wxSortedArray doesn't have this function because inserting in wrong |
467 | place would break its sorted condition. | |
3c4f71cc | 468 | |
0c1fe6e9 BP |
469 | Please see Add() for an explanation of the differences between the |
470 | overloaded versions of this function. | |
23324ae1 | 471 | */ |
0c1fe6e9 | 472 | void Insert(T& item, size_t n, size_t copies = 1); |
23324ae1 FM |
473 | |
474 | /** | |
0c1fe6e9 BP |
475 | This function ensures that the number of array elements is at least |
476 | @a count. If the array has already @a count or more items, nothing is | |
477 | done. Otherwise, @a count - GetCount() elements are added and | |
478 | initialized to the value @a defval. | |
3c4f71cc | 479 | |
0c1fe6e9 | 480 | @see GetCount() |
23324ae1 | 481 | */ |
0c1fe6e9 | 482 | void SetCount(size_t count, T defval = T(0)); |
23324ae1 | 483 | |
0c1fe6e9 | 484 | //@} |
23324ae1 | 485 | |
3c4f71cc | 486 | |
0c1fe6e9 BP |
487 | /** |
488 | @name Removing Items | |
489 | */ | |
490 | //@{ | |
3c4f71cc | 491 | |
0c1fe6e9 BP |
492 | /** |
493 | This function does the same as Empty() and additionally frees the | |
494 | memory allocated to the array. | |
495 | */ | |
496 | void Clear(); | |
3c4f71cc | 497 | |
0c1fe6e9 BP |
498 | /** |
499 | Removes the element from the array, but unlike Remove(), it doesn't | |
500 | delete it. The function returns the pointer to the removed element. | |
23324ae1 | 501 | */ |
0c1fe6e9 | 502 | T* Detach(size_t index); |
23324ae1 | 503 | |
0c1fe6e9 BP |
504 | /** |
505 | Empties the array. For wxObjArray classes, this destroys all of the | |
506 | array elements. For wxArray and wxSortedArray this does nothing except | |
507 | marking the array of being empty - this function does not free the | |
508 | allocated memory, use Clear() for this. | |
509 | */ | |
510 | void Empty(); | |
23324ae1 FM |
511 | |
512 | /** | |
0c1fe6e9 BP |
513 | Removes an element from the array by value: the first item of the array |
514 | equal to @a item is removed, an assert failure will result from an | |
23324ae1 | 515 | attempt to remove an item which doesn't exist in the array. |
3c4f71cc | 516 | |
0c1fe6e9 BP |
517 | When an element is removed from wxObjArray it is deleted by the array - |
518 | use Detach() if you don't want this to happen. On the other hand, when | |
519 | an object is removed from a wxArray nothing happens - you should delete | |
520 | it manually if required: | |
521 | ||
522 | @code | |
523 | T *item = array[n]; | |
524 | delete item; | |
525 | array.Remove(n); | |
526 | @endcode | |
527 | ||
528 | See also WX_CLEAR_ARRAY() macro which deletes all elements of a wxArray | |
529 | (supposed to contain pointers). | |
23324ae1 | 530 | */ |
7c913512 | 531 | Remove(T item); |
23324ae1 FM |
532 | |
533 | /** | |
4cc4bfaf | 534 | Removes @a count elements starting at @a index from the array. When an |
23324ae1 | 535 | element is removed from wxObjArray it is deleted by the array - use |
0c1fe6e9 BP |
536 | Detach() if you don't want this to happen. On the other hand, when an |
537 | object is removed from a wxArray nothing happens - you should delete it | |
538 | manually if required: | |
539 | ||
540 | @code | |
541 | T *item = array[n]; | |
542 | delete item; | |
543 | array.RemoveAt(n); | |
544 | @endcode | |
545 | ||
546 | See also WX_CLEAR_ARRAY() macro which deletes all elements of a wxArray | |
547 | (supposed to contain pointers). | |
23324ae1 | 548 | */ |
7c913512 | 549 | RemoveAt(size_t index, size_t count = 1); |
23324ae1 | 550 | |
0c1fe6e9 | 551 | //@} |
3c4f71cc | 552 | |
3c4f71cc | 553 | |
0c1fe6e9 BP |
554 | /** |
555 | @name Searching and Sorting | |
556 | */ | |
557 | //@{ | |
3c4f71cc | 558 | |
0c1fe6e9 BP |
559 | /** |
560 | This version of Index() is for wxArray and wxObjArray only. | |
561 | ||
562 | Searches the element in the array, starting from either beginning or | |
563 | the end depending on the value of @a searchFromEnd parameter. | |
564 | @c wxNOT_FOUND is returned if the element is not found, otherwise the | |
565 | index of the element is returned. | |
566 | ||
567 | @note Even for wxObjArray classes, the operator "==" of the elements in | |
568 | the array is @b not used by this function. It searches exactly | |
569 | the given element in the array and so will only succeed if this | |
570 | element had been previously added to the array, but fail even if | |
571 | another, identical, element is in the array. | |
23324ae1 | 572 | */ |
0c1fe6e9 BP |
573 | int Index(T& item, bool searchFromEnd = false) const; |
574 | /** | |
575 | This version of Index() is for wxSortedArray only. | |
23324ae1 | 576 | |
0c1fe6e9 BP |
577 | Searches the element in the array, starting from either beginning or |
578 | the end depending on the value of @a searchFromEnd parameter. | |
579 | @c wxNOT_FOUND is returned if the element is not found, otherwise the | |
580 | index of the element is returned. | |
581 | */ | |
582 | const int Index(T& item) const; | |
23324ae1 FM |
583 | |
584 | /** | |
0c1fe6e9 BP |
585 | Search for a place to insert @a item into the sorted array (binary |
586 | search). The index returned is just before the first existing item that | |
587 | is greater or equal (according to the compare function) to the given | |
588 | @a item. | |
3c4f71cc | 589 | |
0c1fe6e9 BP |
590 | You have to do extra work to know if the @a item already exists in |
591 | array. | |
3c4f71cc | 592 | |
0c1fe6e9 BP |
593 | This function is useful in conjunction with AddAt() for a common |
594 | operation of "insert only if not found". | |
23324ae1 | 595 | */ |
0c1fe6e9 | 596 | size_t IndexForInsert(T item) const; |
23324ae1 FM |
597 | |
598 | /** | |
0c1fe6e9 BP |
599 | The notation @c "CMPFUNCT<T>" should be read as if we had the following |
600 | declaration: | |
3c4f71cc | 601 | |
0c1fe6e9 BP |
602 | @code |
603 | template int CMPFUNC(T *first, T *second); | |
604 | @endcode | |
23324ae1 | 605 | |
0c1fe6e9 BP |
606 | Where @e T is the type of the array elements. I.e. it is a function |
607 | returning @e int which is passed two arguments of type @e T*. | |
23324ae1 | 608 | |
0c1fe6e9 BP |
609 | Sorts the array using the specified compare function: this function |
610 | should return a negative, zero or positive value according to whether | |
611 | the first element passed to it is less than, equal to or greater than | |
612 | the second one. | |
3c4f71cc | 613 | |
23324ae1 FM |
614 | wxSortedArray doesn't have this function because it is always sorted. |
615 | */ | |
616 | void Sort(CMPFUNC<T> compareFunction); | |
617 | ||
23324ae1 FM |
618 | //@} |
619 | }; | |
e54c96f1 | 620 | |
6b4a130c FM |
621 | |
622 | /** | |
623 | This macro may be used to append all elements of the @a other array to the | |
0c1fe6e9 | 624 | @a array. The two arrays must be of the same type. |
6b4a130c | 625 | */ |
0c1fe6e9 | 626 | #define WX_APPEND_ARRAY(wxArray& array, wxArray& other) |
6b4a130c FM |
627 | |
628 | /** | |
0c1fe6e9 BP |
629 | This macro may be used to delete all elements of the array before emptying |
630 | it. It can not be used with wxObjArrays - but they will delete their | |
631 | elements anyway when you call Empty(). | |
6b4a130c | 632 | */ |
0c1fe6e9 | 633 | #define WX_CLEAR_ARRAY(wxArray& array) |
6b4a130c FM |
634 | |
635 | //@{ | |
636 | /** | |
637 | This macro declares a new object array class named @a name and containing | |
0c1fe6e9 BP |
638 | the elements of type @e T. |
639 | ||
640 | An exported array is used when compiling wxWidgets as a DLL under Windows | |
641 | and the array needs to be visible outside the DLL. An user exported array | |
6b4a130c | 642 | needed for exporting an array from a user DLL. |
0c1fe6e9 | 643 | |
6b4a130c FM |
644 | Example: |
645 | ||
0c1fe6e9 BP |
646 | @code |
647 | class MyClass; | |
648 | WX_DECLARE_OBJARRAY(MyClass, wxArrayOfMyClass); // note: not "MyClass *"! | |
649 | @endcode | |
650 | ||
651 | You must use WX_DEFINE_OBJARRAY() macro to define the array class, | |
652 | otherwise you would get link errors. | |
6b4a130c | 653 | */ |
0c1fe6e9 BP |
654 | #define WX_DECLARE_OBJARRAY(T, name) |
655 | #define WX_DECLARE_EXPORTED_OBJARRAY(T, name) | |
656 | #define WX_DECLARE_USER_EXPORTED_OBJARRAY(T, name) | |
6b4a130c FM |
657 | //@} |
658 | ||
659 | //@{ | |
660 | /** | |
661 | This macro defines a new array class named @a name and containing the | |
0c1fe6e9 BP |
662 | elements of type @a T. |
663 | ||
664 | An exported array is used when compiling wxWidgets as a DLL under Windows | |
665 | and the array needs to be visible outside the DLL. An user exported array | |
6b4a130c | 666 | needed for exporting an array from a user DLL. |
0c1fe6e9 | 667 | |
6b4a130c FM |
668 | Example: |
669 | ||
0c1fe6e9 BP |
670 | @code |
671 | WX_DEFINE_ARRAY_INT(int, MyArrayInt); | |
672 | ||
673 | class MyClass; | |
674 | WX_DEFINE_ARRAY(MyClass *, ArrayOfMyClass); | |
675 | @endcode | |
676 | ||
677 | Note that wxWidgets predefines the following standard array classes: | |
678 | @b wxArrayInt, @b wxArrayLong, @b wxArrayShort, @b wxArrayDouble, | |
679 | @b wxArrayPtrVoid. | |
6b4a130c | 680 | */ |
0c1fe6e9 BP |
681 | #define WX_DEFINE_ARRAY(T, name) |
682 | #define WX_DEFINE_EXPORTED_ARRAY(T, name) | |
683 | #define WX_DEFINE_USER_EXPORTED_ARRAY(T, name, exportspec) | |
6b4a130c FM |
684 | //@} |
685 | ||
686 | //@{ | |
687 | /** | |
0c1fe6e9 BP |
688 | This macro defines the methods of the array class @a name not defined by |
689 | the WX_DECLARE_OBJARRAY() macro. You must include the file | |
690 | @<wx/arrimpl.cpp@> before using this macro and you must have the full | |
691 | declaration of the class of array elements in scope! If you forget to do | |
692 | the first, the error will be caught by the compiler, but, unfortunately, | |
693 | many compilers will not give any warnings if you forget to do the second - | |
694 | but the objects of the class will not be copied correctly and their real | |
695 | destructor will not be called. | |
696 | ||
697 | An exported array is used when compiling wxWidgets as a DLL under Windows | |
698 | and the array needs to be visible outside the DLL. An user exported array | |
699 | needed for exporting an array from a user DLL. | |
700 | ||
6b4a130c | 701 | Example of usage: |
0c1fe6e9 BP |
702 | |
703 | @code | |
704 | // first declare the class! | |
705 | class MyClass | |
706 | { | |
707 | public: | |
708 | MyClass(const MyClass&); | |
709 | ||
710 | // ... | |
711 | ||
712 | virtual ~MyClass(); | |
713 | }; | |
714 | ||
715 | #include <wx/arrimpl.cpp> | |
716 | WX_DEFINE_OBJARRAY(wxArrayOfMyClass); | |
717 | @endcode | |
6b4a130c | 718 | */ |
0c1fe6e9 BP |
719 | #define WX_DEFINE_OBJARRAY(name) |
720 | #define WX_DEFINE_EXPORTED_OBJARRAY(name) | |
721 | #define WX_DEFINE_USER_EXPORTED_OBJARRAY(name) | |
6b4a130c FM |
722 | //@} |
723 | ||
724 | //@{ | |
725 | /** | |
726 | This macro defines a new sorted array class named @a name and containing | |
0c1fe6e9 BP |
727 | the elements of type @e T. |
728 | ||
729 | An exported array is used when compiling wxWidgets as a DLL under Windows | |
730 | and the array needs to be visible outside the DLL. An user exported array | |
6b4a130c | 731 | needed for exporting an array from a user DLL. |
0c1fe6e9 | 732 | |
6b4a130c FM |
733 | Example: |
734 | ||
0c1fe6e9 BP |
735 | @code |
736 | WX_DEFINE_SORTED_ARRAY_INT(int, MySortedArrayInt); | |
737 | ||
738 | class MyClass; | |
739 | WX_DEFINE_SORTED_ARRAY(MyClass *, ArrayOfMyClass); | |
740 | @endcode | |
741 | ||
742 | You will have to initialize the objects of this class by passing a | |
743 | comparison function to the array object constructor like this: | |
744 | ||
745 | @code | |
746 | int CompareInts(int n1, int n2) | |
747 | { | |
748 | return n1 - n2; | |
749 | } | |
750 | ||
751 | MySortedArrayInt sorted(CompareInts); | |
752 | ||
753 | int CompareMyClassObjects(MyClass *item1, MyClass *item2) | |
754 | { | |
755 | // sort the items by their address... | |
756 | return Stricmp(item1->GetAddress(), item2->GetAddress()); | |
757 | } | |
758 | ||
759 | ArrayOfMyClass another(CompareMyClassObjects); | |
760 | @endcode | |
6b4a130c | 761 | */ |
0c1fe6e9 BP |
762 | #define WX_DEFINE_SORTED_ARRAY(T, name) |
763 | #define WX_DEFINE_SORTED_EXPORTED_ARRAY(T, name) | |
764 | #define WX_DEFINE_SORTED_USER_EXPORTED_ARRAY(T, name) | |
6b4a130c FM |
765 | //@} |
766 | ||
767 | /** | |
768 | This macro may be used to prepend all elements of the @a other array to the | |
0c1fe6e9 | 769 | @a array. The two arrays must be of the same type. |
6b4a130c | 770 | */ |
0c1fe6e9 BP |
771 | #define WX_PREPEND_ARRAY(wxArray& array, wxArray& other) |
772 |