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