]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: dynarray.h | |
3 | // Purpose: interface of wxArray<T> | |
4 | // Author: wxWidgets team | |
5 | // RCS-ID: $Id$ | |
6 | // Licence: wxWindows licence | |
7 | ///////////////////////////////////////////////////////////////////////////// | |
8 | ||
9 | /** | |
10 | ||
11 | This section describes the so called @e "dynamic arrays". This is a C | |
12 | array-like type safe data structure i.e. the member access time is constant | |
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. | |
33 | ||
34 | wxWidgets has three different kinds of array. All of them derive from | |
35 | wxBaseArray class which works with untyped data and cannot 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: | |
84 | ||
85 | @code | |
86 | #include <wx/dynarray.h> | |
87 | ||
88 | // We must forward declare the array because it is used | |
89 | // inside the class declaration. | |
90 | class MyDirectory; | |
91 | class MyFile; | |
92 | ||
93 | // This defines two new types: ArrayOfDirectories and ArrayOfFiles which | |
94 | // can be now used as shown below. | |
95 | WX_DECLARE_OBJARRAY(MyDirectory, ArrayOfDirectories); | |
96 | WX_DECLARE_OBJARRAY(MyFile, ArrayOfFiles); | |
97 | ||
98 | class MyDirectory | |
99 | { | |
100 | // ... | |
101 | ArrayOfDirectories m_subdirectories; // All subdirectories | |
102 | ArrayOfFiles m_files; // All files in this directory | |
103 | }; | |
104 | ||
105 | // ... | |
106 | ||
107 | // Now that we have MyDirectory declaration in scope we may finish the | |
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) | |
111 | #include <wx/arrimpl.cpp> // This is a magic incantation which must be done! | |
112 | WX_DEFINE_OBJARRAY(ArrayOfDirectories); | |
113 | ||
114 | // that's all! | |
115 | @endcode | |
116 | ||
117 | It is not as elegant as writing this: | |
118 | ||
119 | @code | |
120 | typedef std::vector<MyDirectory> ArrayOfDirectories; | |
121 | @endcode | |
122 | ||
123 | But is not that complicated and allows the code to be compiled with any, | |
124 | however dumb, C++ compiler in the world. | |
125 | ||
126 | Remember to include @<wx/arrimpl.cpp@> just before each | |
127 | WX_DEFINE_OBJARRAY() occurrence in your code, even if you have several in | |
128 | the same file. | |
129 | ||
130 | Things are much simpler for wxArray and wxSortedArray however: it is enough | |
131 | just to write: | |
132 | ||
133 | @code | |
134 | WX_DEFINE_ARRAY_INT(int, ArrayOfInts); | |
135 | WX_DEFINE_SORTED_ARRAY_INT(int, ArrayOfSortedInts); | |
136 | @endcode | |
137 | ||
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 | |
140 | @c WX_DEFINE_ARRAY_CHAR/SHORT/INT/SIZE_T/LONG/DOUBLE should be used | |
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 | ||
199 | ||
200 | @section array_predef Predefined array types | |
201 | ||
202 | wxWidgets defines the following dynamic array types: | |
203 | - ::wxArrayShort | |
204 | - ::wxArrayInt | |
205 | - ::wxArrayDouble | |
206 | - ::wxArrayLong | |
207 | - ::wxArrayPtrVoid | |
208 | ||
209 | To use them you don't need any macro; you just need to include @c dynarray.h. | |
210 | ||
211 | ||
212 | @library{wxbase} | |
213 | @category{containers} | |
214 | ||
215 | @see @ref overview_container, wxList<T>, wxVector<T> | |
216 | */ | |
217 | template <typename T> | |
218 | class wxArray<T> | |
219 | { | |
220 | public: | |
221 | /** | |
222 | @name Constructors and Destructors | |
223 | ||
224 | Array classes are 100% C++ objects and as such they have the | |
225 | appropriate copy constructors and assignment operators. Copying wxArray | |
226 | just copies the elements but copying wxObjArray copies the arrays | |
227 | items. However, for memory-efficiency sake, neither of these classes | |
228 | has virtual destructor. It is not very important for wxArray which has | |
229 | trivial destructor anyhow, but it does mean that you should avoid | |
230 | deleting wxObjArray through a wxBaseArray pointer (as you would never | |
231 | use wxBaseArray anyhow it shouldn't be a problem) and that you should | |
232 | not derive your own classes from the array classes. | |
233 | */ | |
234 | //@{ | |
235 | ||
236 | /** | |
237 | Default constructor. | |
238 | */ | |
239 | wxArray(); | |
240 | ||
241 | /** | |
242 | Default constructor initializes an empty array object. | |
243 | */ | |
244 | wxObjArray(); | |
245 | ||
246 | /** | |
247 | There is no default constructor for wxSortedArray classes - you must | |
248 | initialize it with a function to use for item comparison. It is a | |
249 | function which is passed two arguments of type @c T where @c T is the | |
250 | array element type and which should return a negative, zero or positive | |
251 | value according to whether the first element passed to it is less than, | |
252 | equal to or greater than the second one. | |
253 | */ | |
254 | wxSortedArray(int (*)(T first, T second)compareFunction); | |
255 | ||
256 | /** | |
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). | |
259 | */ | |
260 | wxArray(const wxArray& array); | |
261 | ||
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(const wxSortedArray& array); | |
267 | ||
268 | /** | |
269 | Performs a deep copy (i.e.\ the array element are copied too). | |
270 | */ | |
271 | wxObjArray(const wxObjArray& array); | |
272 | ||
273 | /** | |
274 | Performs a shallow array copy (i.e.\ doesn't copy the objects pointed to | |
275 | even if the source array contains the items of pointer type). | |
276 | */ | |
277 | wxArray& operator=(const wxArray& array); | |
278 | ||
279 | /** | |
280 | Performs a shallow array copy (i.e.\ doesn't copy the objects pointed to | |
281 | even if the source array contains the items of pointer type). | |
282 | */ | |
283 | wxSortedArray& operator=(const wxSortedArray& array); | |
284 | ||
285 | /** | |
286 | Performs a deep copy (i.e.\ the array element are copied too). | |
287 | */ | |
288 | wxObjArray& operator=(const wxObjArray& array); | |
289 | ||
290 | /** | |
291 | This destructor does not delete all the items owned by the array, you | |
292 | may use the WX_CLEAR_ARRAY() macro for this. | |
293 | */ | |
294 | ~wxArray(); | |
295 | ||
296 | /** | |
297 | This destructor does not delete all the items owned by the array, you | |
298 | may use the WX_CLEAR_ARRAY() macro for this. | |
299 | */ | |
300 | ~wxSortedArray(); | |
301 | ||
302 | /** | |
303 | This destructor deletes all the items owned by the array. | |
304 | */ | |
305 | ~wxObjArray(); | |
306 | ||
307 | //@} | |
308 | ||
309 | ||
310 | /** | |
311 | @name Memory Management | |
312 | ||
313 | Automatic array memory management is quite trivial: the array starts by | |
314 | preallocating some minimal amount of memory (defined by | |
315 | @c WX_ARRAY_DEFAULT_INITIAL_SIZE) and when further new items exhaust | |
316 | already allocated memory it reallocates it adding 50% of the currently | |
317 | allocated amount, but no more than some maximal number which is defined | |
318 | by the @c ARRAY_MAXSIZE_INCREMENT constant. Of course, this may lead to | |
319 | some memory being wasted (@c ARRAY_MAXSIZE_INCREMENT in the worst case, | |
320 | i.e. 4Kb in the current implementation), so the Shrink() function is | |
321 | provided to deallocate the extra memory. The Alloc() function can also | |
322 | be quite useful if you know in advance how many items you are going to | |
323 | put in the array and will prevent the array code from reallocating the | |
324 | memory more times than needed. | |
325 | */ | |
326 | //@{ | |
327 | ||
328 | /** | |
329 | Preallocates memory for a given number of array elements. It is worth | |
330 | calling when the number of items which are going to be added to the | |
331 | array is known in advance because it will save unneeded memory | |
332 | reallocation. If the array already has enough memory for the given | |
333 | number of items, nothing happens. In any case, the existing contents of | |
334 | the array is not modified. | |
335 | */ | |
336 | void Alloc(size_t count); | |
337 | ||
338 | /** | |
339 | Frees all memory unused by the array. If the program knows that no new | |
340 | items will be added to the array it may call Shrink() to reduce its | |
341 | memory usage. However, if a new item is added to the array, some extra | |
342 | memory will be allocated again. | |
343 | */ | |
344 | void Shrink(); | |
345 | ||
346 | //@} | |
347 | ||
348 | ||
349 | /** | |
350 | @name Number of Elements and Simple Item Access | |
351 | ||
352 | Functions in this section return the total number of array elements and | |
353 | allow to retrieve them - possibly using just the C array indexing [] | |
354 | operator which does exactly the same as the Item() method. | |
355 | */ | |
356 | //@{ | |
357 | ||
358 | /** | |
359 | Return the number of items in the array. | |
360 | */ | |
361 | size_t GetCount() const; | |
362 | ||
363 | /** | |
364 | Returns @true if the array is empty, @false otherwise. | |
365 | */ | |
366 | bool IsEmpty() const; | |
367 | ||
368 | /** | |
369 | Returns the item at the given position in the array. If @a index is out | |
370 | of bounds, an assert failure is raised in the debug builds but nothing | |
371 | special is done in the release build. | |
372 | ||
373 | The returned value is of type "reference to the array element type" for | |
374 | all of the array classes. | |
375 | */ | |
376 | T& Item(size_t index) const; | |
377 | ||
378 | /** | |
379 | Returns the last element in the array, i.e.\ is the same as calling | |
380 | "Item(GetCount() - 1)". An assert failure is raised in the debug mode | |
381 | if the array is empty. | |
382 | ||
383 | The returned value is of type "reference to the array element type" for | |
384 | all of the array classes. | |
385 | */ | |
386 | T& Last() const; | |
387 | ||
388 | //@} | |
389 | ||
390 | ||
391 | /** | |
392 | @name Adding Items | |
393 | */ | |
394 | //@{ | |
395 | ||
396 | /** | |
397 | Appends the given number of @a copies of the @a item to the array | |
398 | consisting of the elements of type @c T. | |
399 | ||
400 | This version is used with wxArray. | |
401 | ||
402 | You may also use WX_APPEND_ARRAY() macro to append all elements of one | |
403 | array to another one but it is more efficient to use the @a copies | |
404 | parameter and modify the elements in place later if you plan to append | |
405 | a lot of items. | |
406 | */ | |
407 | void Add(T item, size_t copies = 1); | |
408 | ||
409 | /** | |
410 | Appends the @a item to the array consisting of the elements of type | |
411 | @c T. | |
412 | ||
413 | This version is used with wxSortedArray, returning the index where | |
414 | @a item is stored. | |
415 | */ | |
416 | size_t Add(T item); | |
417 | ||
418 | /** | |
419 | Appends the @a item to the array consisting of the elements of type | |
420 | @c T. | |
421 | ||
422 | This version is used with wxObjArray. The array will take ownership of | |
423 | the @a item, deleting it when the item is deleted from the array. Note | |
424 | that you cannot append more than one pointer as reusing it would lead | |
425 | to deleting it twice (or more) resulting in a crash. | |
426 | ||
427 | You may also use WX_APPEND_ARRAY() macro to append all elements of one | |
428 | array to another one but it is more efficient to use the @a copies | |
429 | parameter and modify the elements in place later if you plan to append | |
430 | a lot of items. | |
431 | */ | |
432 | void Add(T* item); | |
433 | ||
434 | /** | |
435 | Appends the given number of @a copies of the @a item to the array | |
436 | consisting of the elements of type @c T. | |
437 | ||
438 | This version is used with wxObjArray. The array will make a copy of the | |
439 | item and will not take ownership of the original item. | |
440 | ||
441 | You may also use WX_APPEND_ARRAY() macro to append all elements of one | |
442 | array to another one but it is more efficient to use the @a copies | |
443 | parameter and modify the elements in place later if you plan to append | |
444 | a lot of items. | |
445 | */ | |
446 | void Add(T& item, size_t copies = 1); | |
447 | ||
448 | /** | |
449 | Inserts the given @a item into the array in the specified @e index | |
450 | position. | |
451 | ||
452 | Be aware that you will set out the order of the array if you give a | |
453 | wrong position. | |
454 | ||
455 | This function is useful in conjunction with IndexForInsert() for a | |
456 | common operation of "insert only if not found". | |
457 | */ | |
458 | void AddAt(T item, size_t index); | |
459 | ||
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. | |
464 | ||
465 | wxSortedArray doesn't have this function because inserting in wrong | |
466 | place would break its sorted condition. | |
467 | ||
468 | Please see Add() for an explanation of the differences between the | |
469 | overloaded versions of this function. | |
470 | */ | |
471 | void Insert(T item, size_t n, size_t copies = 1); | |
472 | ||
473 | /** | |
474 | Insert the @a item into the array before the existing item @a n - thus, | |
475 | @e Insert(something, 0u) will insert an item in such way that it will | |
476 | become the first array element. | |
477 | ||
478 | wxSortedArray doesn't have this function because inserting in wrong | |
479 | place would break its sorted condition. | |
480 | ||
481 | Please see Add() for an explanation of the differences between the | |
482 | overloaded versions of this function. | |
483 | */ | |
484 | void Insert(T* item, size_t n); | |
485 | ||
486 | /** | |
487 | Insert the given number of @a copies of the @a item into the array | |
488 | before the existing item @a n - thus, @e Insert(something, 0u) will | |
489 | insert an item in such way that it will become the first array element. | |
490 | ||
491 | wxSortedArray doesn't have this function because inserting in wrong | |
492 | place would break its sorted condition. | |
493 | ||
494 | Please see Add() for an explanation of the differences between the | |
495 | overloaded versions of this function. | |
496 | */ | |
497 | void Insert(T& item, size_t n, size_t copies = 1); | |
498 | ||
499 | /** | |
500 | This function ensures that the number of array elements is at least | |
501 | @a count. If the array has already @a count or more items, nothing is | |
502 | done. Otherwise, @a count - GetCount() elements are added and | |
503 | initialized to the value @a defval. | |
504 | ||
505 | @see GetCount() | |
506 | */ | |
507 | void SetCount(size_t count, T defval = T(0)); | |
508 | ||
509 | //@} | |
510 | ||
511 | ||
512 | /** | |
513 | @name Removing Items | |
514 | */ | |
515 | //@{ | |
516 | ||
517 | /** | |
518 | This function does the same as Empty() and additionally frees the | |
519 | memory allocated to the array. | |
520 | */ | |
521 | void Clear(); | |
522 | ||
523 | /** | |
524 | Removes the element from the array, but unlike Remove(), it doesn't | |
525 | delete it. The function returns the pointer to the removed element. | |
526 | */ | |
527 | T* Detach(size_t index); | |
528 | ||
529 | /** | |
530 | Empties the array. For wxObjArray classes, this destroys all of the | |
531 | array elements. For wxArray and wxSortedArray this does nothing except | |
532 | marking the array of being empty - this function does not free the | |
533 | allocated memory, use Clear() for this. | |
534 | */ | |
535 | void Empty(); | |
536 | ||
537 | /** | |
538 | Removes an element from the array by value: the first item of the array | |
539 | equal to @a item is removed, an assert failure will result from an | |
540 | attempt to remove an item which doesn't exist in the array. | |
541 | ||
542 | When an element is removed from wxObjArray it is deleted by the array - | |
543 | use Detach() if you don't want this to happen. On the other hand, when | |
544 | an object is removed from a wxArray nothing happens - you should delete | |
545 | it manually if required: | |
546 | ||
547 | @code | |
548 | T *item = array[n]; | |
549 | array.Remove(item); | |
550 | delete item; | |
551 | @endcode | |
552 | ||
553 | See also WX_CLEAR_ARRAY() macro which deletes all elements of a wxArray | |
554 | (supposed to contain pointers). | |
555 | ||
556 | Notice that for sorted arrays this method uses binary search to find | |
557 | the item so it doesn't necessarily remove the first matching item, but | |
558 | the first one found by the binary search. | |
559 | ||
560 | @see RemoveAt() | |
561 | */ | |
562 | void Remove(T item); | |
563 | ||
564 | /** | |
565 | Removes @a count elements starting at @a index from the array. When an | |
566 | element is removed from wxObjArray it is deleted by the array - use | |
567 | Detach() if you don't want this to happen. On the other hand, when an | |
568 | object is removed from a wxArray nothing happens - you should delete it | |
569 | manually if required: | |
570 | ||
571 | @code | |
572 | T *item = array[n]; | |
573 | delete item; | |
574 | array.RemoveAt(n); | |
575 | @endcode | |
576 | ||
577 | See also WX_CLEAR_ARRAY() macro which deletes all elements of a wxArray | |
578 | (supposed to contain pointers). | |
579 | */ | |
580 | void RemoveAt(size_t index, size_t count = 1); | |
581 | ||
582 | //@} | |
583 | ||
584 | ||
585 | /** | |
586 | @name Searching and Sorting | |
587 | */ | |
588 | //@{ | |
589 | ||
590 | /** | |
591 | This version of Index() is for wxArray and wxObjArray only. | |
592 | ||
593 | Searches the element in the array, starting from either beginning or | |
594 | the end depending on the value of @a searchFromEnd parameter. | |
595 | @c wxNOT_FOUND is returned if the element is not found, otherwise the | |
596 | index of the element is returned. | |
597 | ||
598 | @note Even for wxObjArray classes, the operator "==" of the elements in | |
599 | the array is @b not used by this function. It searches exactly | |
600 | the given element in the array and so will only succeed if this | |
601 | element had been previously added to the array, but fail even if | |
602 | another, identical, element is in the array. | |
603 | */ | |
604 | int Index(T& item, bool searchFromEnd = false) const; | |
605 | ||
606 | /** | |
607 | This version of Index() is for wxSortedArray only. | |
608 | ||
609 | Searches for the element in the array, using binary search. | |
610 | ||
611 | @c wxNOT_FOUND is returned if the element is not found, otherwise the | |
612 | index of the element is returned. | |
613 | */ | |
614 | int Index(T& item) const; | |
615 | ||
616 | /** | |
617 | Search for a place to insert @a item into the sorted array (binary | |
618 | search). The index returned is just before the first existing item that | |
619 | is greater or equal (according to the compare function) to the given | |
620 | @a item. | |
621 | ||
622 | You have to do extra work to know if the @a item already exists in | |
623 | array. | |
624 | ||
625 | This function is useful in conjunction with AddAt() for a common | |
626 | operation of "insert only if not found". | |
627 | */ | |
628 | size_t IndexForInsert(T item) const; | |
629 | ||
630 | /** | |
631 | The notation @c "CMPFUNCT<T>" should be read as if we had the following | |
632 | declaration: | |
633 | ||
634 | @code | |
635 | template int CMPFUNC(T *first, T *second); | |
636 | @endcode | |
637 | ||
638 | Where @e T is the type of the array elements. I.e. it is a function | |
639 | returning @e int which is passed two arguments of type @e T*. | |
640 | ||
641 | Sorts the array using the specified compare function: this function | |
642 | should return a negative, zero or positive value according to whether | |
643 | the first element passed to it is less than, equal to or greater than | |
644 | the second one. | |
645 | ||
646 | wxSortedArray doesn't have this function because it is always sorted. | |
647 | */ | |
648 | void Sort(CMPFUNC<T> compareFunction); | |
649 | ||
650 | //@} | |
651 | }; | |
652 | ||
653 | ||
654 | /** | |
655 | This macro may be used to append all elements of the @a wxArray_arrayToBeAppended | |
656 | array to the @a wxArray_arrayToModify. The two arrays must be of the same type. | |
657 | */ | |
658 | #define WX_APPEND_ARRAY(wxArray_arrayToModify, wxArray_arrayToBeAppended) | |
659 | ||
660 | /** | |
661 | This macro may be used to delete all elements of the array before emptying | |
662 | it. It cannot be used with wxObjArrays - but they will delete their | |
663 | elements anyway when you call Empty(). | |
664 | */ | |
665 | #define WX_CLEAR_ARRAY(wxArray_arrayToBeCleared) | |
666 | ||
667 | //@{ | |
668 | /** | |
669 | This macro declares a new object array class named @a name and containing | |
670 | the elements of type @e T. | |
671 | ||
672 | An exported array is used when compiling wxWidgets as a DLL under Windows | |
673 | and the array needs to be visible outside the DLL. An user exported array | |
674 | needed for exporting an array from a user DLL. | |
675 | ||
676 | Example: | |
677 | ||
678 | @code | |
679 | class MyClass; | |
680 | WX_DECLARE_OBJARRAY(MyClass, wxArrayOfMyClass); // note: not "MyClass *"! | |
681 | @endcode | |
682 | ||
683 | You must use WX_DEFINE_OBJARRAY() macro to define the array class, | |
684 | otherwise you would get link errors. | |
685 | */ | |
686 | #define WX_DECLARE_OBJARRAY(T, name) | |
687 | #define WX_DECLARE_EXPORTED_OBJARRAY(T, name) | |
688 | #define WX_DECLARE_USER_EXPORTED_OBJARRAY(T, name) | |
689 | //@} | |
690 | ||
691 | //@{ | |
692 | /** | |
693 | This macro defines a new array class named @a name and containing the | |
694 | elements of type @a T. | |
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 | ||
700 | Example: | |
701 | ||
702 | @code | |
703 | WX_DEFINE_ARRAY_INT(int, MyArrayInt); | |
704 | ||
705 | class MyClass; | |
706 | WX_DEFINE_ARRAY(MyClass *, ArrayOfMyClass); | |
707 | @endcode | |
708 | ||
709 | Note that wxWidgets predefines the following standard array classes: | |
710 | @b wxArrayInt, @b wxArrayLong, @b wxArrayShort, @b wxArrayDouble, | |
711 | @b wxArrayPtrVoid. | |
712 | */ | |
713 | #define WX_DEFINE_ARRAY(T, name) | |
714 | #define WX_DEFINE_EXPORTED_ARRAY(T, name) | |
715 | #define WX_DEFINE_USER_EXPORTED_ARRAY(T, name, exportspec) | |
716 | //@} | |
717 | ||
718 | //@{ | |
719 | /** | |
720 | This macro defines the methods of the array class @a name not defined by | |
721 | the WX_DECLARE_OBJARRAY() macro. You must include the file | |
722 | @<wx/arrimpl.cpp@> before using this macro and you must have the full | |
723 | declaration of the class of array elements in scope! If you forget to do | |
724 | the first, the error will be caught by the compiler, but, unfortunately, | |
725 | many compilers will not give any warnings if you forget to do the second - | |
726 | but the objects of the class will not be copied correctly and their real | |
727 | destructor will not be called. | |
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 | |
731 | needed for exporting an array from a user DLL. | |
732 | ||
733 | Example of usage: | |
734 | ||
735 | @code | |
736 | // first declare the class! | |
737 | class MyClass | |
738 | { | |
739 | public: | |
740 | MyClass(const MyClass&); | |
741 | ||
742 | // ... | |
743 | ||
744 | virtual ~MyClass(); | |
745 | }; | |
746 | ||
747 | #include <wx/arrimpl.cpp> | |
748 | WX_DEFINE_OBJARRAY(wxArrayOfMyClass); | |
749 | @endcode | |
750 | */ | |
751 | #define WX_DEFINE_OBJARRAY(name) | |
752 | #define WX_DEFINE_EXPORTED_OBJARRAY(name) | |
753 | #define WX_DEFINE_USER_EXPORTED_OBJARRAY(name) | |
754 | //@} | |
755 | ||
756 | //@{ | |
757 | /** | |
758 | This macro defines a new sorted array class named @a name and containing | |
759 | the elements of type @e T. | |
760 | ||
761 | An exported array is used when compiling wxWidgets as a DLL under Windows | |
762 | and the array needs to be visible outside the DLL. An user exported array | |
763 | needed for exporting an array from a user DLL. | |
764 | ||
765 | Example: | |
766 | ||
767 | @code | |
768 | WX_DEFINE_SORTED_ARRAY_INT(int, MySortedArrayInt); | |
769 | ||
770 | class MyClass; | |
771 | WX_DEFINE_SORTED_ARRAY(MyClass *, ArrayOfMyClass); | |
772 | @endcode | |
773 | ||
774 | You will have to initialize the objects of this class by passing a | |
775 | comparison function to the array object constructor like this: | |
776 | ||
777 | @code | |
778 | int CompareInts(int n1, int n2) | |
779 | { | |
780 | return n1 - n2; | |
781 | } | |
782 | ||
783 | MySortedArrayInt sorted(CompareInts); | |
784 | ||
785 | int CompareMyClassObjects(MyClass *item1, MyClass *item2) | |
786 | { | |
787 | // sort the items by their address... | |
788 | return Stricmp(item1->GetAddress(), item2->GetAddress()); | |
789 | } | |
790 | ||
791 | ArrayOfMyClass another(CompareMyClassObjects); | |
792 | @endcode | |
793 | */ | |
794 | #define WX_DEFINE_SORTED_ARRAY(T, name) | |
795 | #define WX_DEFINE_SORTED_EXPORTED_ARRAY(T, name) | |
796 | #define WX_DEFINE_SORTED_USER_EXPORTED_ARRAY(T, name) | |
797 | //@} | |
798 | ||
799 | /** | |
800 | This macro may be used to prepend all elements of the @a wxArray_arrayToBePrepended | |
801 | array to the @a wxArray_arrayToModify. The two arrays must be of the same type. | |
802 | */ | |
803 | #define WX_PREPEND_ARRAY(wxArray_arrayToModify, wxArray_arrayToBePrepended) | |
804 | ||
805 | //@{ | |
806 | /** | |
807 | Predefined specialization of wxArray<T> for standard types. | |
808 | */ | |
809 | typedef wxArray<int> wxArrayInt; | |
810 | typedef wxArray<long> wxArrayLong; | |
811 | typedef wxArray<short> wxArrayShort; | |
812 | typedef wxArray<double> wxArrayDouble; | |
813 | typedef wxArray<void*> wxArrayPtrVoid; | |
814 | //@} |