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