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