]>
git.saurik.com Git - wxWidgets.git/blob - include/wx/dynarray.h
1 ///////////////////////////////////////////////////////////////////////////////
3 // Purpose: auto-resizable (i.e. dynamic) array support
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows license
10 ///////////////////////////////////////////////////////////////////////////////
16 #pragma interface "dynarray.h"
22 This header defines the dynamic arrays and object arrays (i.e. arrays which
23 own their elements). Dynamic means that the arrays grow automatically as
26 These macros are ugly (especially if you look in the sources ;-), but they
27 allow us to define "template" classes without actually using templates and so
28 this works with all compilers (and may be also much faster to compile even
29 with a compiler which does support templates). The arrays defined with these
32 Range checking is performed in debug build for both arrays and objarrays but
33 not in release build - so using an invalid index will just lead to a crash
36 Note about memory usage: arrays never shrink automatically (although you may
37 use Shrink() function explicitly), they only grow, so loading 10 millions in
38 an array only to delete them 2 lines below might be a bad idea if the array
39 object is not going to be destroyed soon. However, as it does free memory
40 when destroyed, it is ok if the array is a local variable.
43 // ----------------------------------------------------------------------------
45 // ----------------------------------------------------------------------------
48 The initial size by which an array grows when an element is added default
49 value avoids allocate one or two bytes when the array is created which is
52 #define WX_ARRAY_DEFAULT_INITIAL_SIZE (16)
54 // ----------------------------------------------------------------------------
56 // ----------------------------------------------------------------------------
59 Callback compare function for quick sort.
61 It must return negative value, 0 or positive value if the first item is
62 less than, equal to or greater than the second one.
66 typedef int (wxCMPFUNC_CONV
*CMPFUNC
)(const void* pItem1
, const void* pItem2
);
69 // ----------------------------------------------------------------------------
70 // Base class managing data having size of type 'long' (not used directly)
72 // NB: for efficiency this often used class has no virtual functions (hence no
73 // virtual table), even dtor is *not* virtual. If used as expected it
74 // won't create any problems because ARRAYs from DEFINE_ARRAY have no dtor
75 // at all, so it's not too important if it's not called (this happens when
76 // you cast "SomeArray *" as "BaseArray *" and then delete it)
77 // ----------------------------------------------------------------------------
79 class WXDLLEXPORT wxBaseArray
82 /** @name ctors and dtor */
87 wxBaseArray(const wxBaseArray
& array
);
88 /// assignment operator
89 wxBaseArray
& operator=(const wxBaseArray
& src
);
90 /// not virtual, see above
94 /** @name memory management */
96 /// empties the array, but doesn't release memory
97 void Empty() { m_nCount
= 0; }
98 /// empties the array and releases memory
100 /// preallocates memory for given number of items
101 void Alloc(size_t uiSize
);
102 /// minimizes the memory used by the array (frees unused memory)
106 /** @name simple accessors */
108 /// number of elements in the array
109 size_t GetCount() const { return m_nCount
; }
111 bool IsEmpty() const { return m_nCount
== 0; }
112 /// this version is obsolete, use GetCount()
113 size_t Count() const { return m_nCount
; }
117 // these methods are protected because if they were public one could
118 // mistakenly call one of them instead of DEFINE_ARRAY's or OBJARRAY's
121 /** @name items access */
123 /// get item at position uiIndex (range checking is done in debug version)
124 long& Item(size_t uiIndex
) const
125 { wxASSERT( uiIndex
< m_nCount
); return m_pItems
[uiIndex
]; }
127 long& operator[](size_t uiIndex
) const { return Item(uiIndex
); }
130 /** @name item management */
133 Search the element in the array, starting from the either side
134 @param bFromEnd if TRUE, start from the end
135 @return index of the first item matched or wxNOT_FOUND
138 int Index(long lItem
, bool bFromEnd
= FALSE
) const;
139 /// search for an item using binary search in a sorted array
140 int Index(long lItem
, CMPFUNC fnCompare
) const;
141 /// search for a place to insert the element into a sorted array
142 size_t IndexForInsert(long lItem
, CMPFUNC fnCompare
) const;
143 /// add new element at the end
144 void Add(long lItem
);
145 /// add item assuming the array is sorted with fnCompare function
146 void Add(long lItem
, CMPFUNC fnCompare
);
147 /// add new element at given position (it becomes Item[uiIndex])
148 void Insert(long lItem
, size_t uiIndex
);
149 /// remove first item matching this value
150 void Remove(long lItem
);
151 /// remove item by index
152 void RemoveAt(size_t uiIndex
);
155 /// sort array elements using given compare function
156 void Sort(CMPFUNC fnCompare
);
159 void Grow(); // makes array bigger if needed
161 size_t m_nSize
, // current size of the array
162 m_nCount
; // current number of elements
164 long *m_pItems
; // pointer to data
167 // ============================================================================
168 // The private helper macros containing the core of the array classes
169 // ============================================================================
171 // Implementation notes:
173 // JACS: Salford C++ doesn't like 'var->operator=' syntax, as in:
174 // { ((wxBaseArray *)this)->operator=((const wxBaseArray&)src);
175 // so using a temporary variable instead.
177 // The classes need a (even trivial) ~name() to link under Mac X
179 // _WX_ERROR_REMOVE is needed to resolve the name conflict between the wxT()
180 // macor and T typedef: we can't use wxT() inside WX_DEFINE_ARRAY!
182 #define _WX_ERROR_REMOVE wxT("removing inexisting element in wxArray::Remove")
184 // ----------------------------------------------------------------------------
185 // _WX_DEFINE_ARRAY: array for simple types
186 // ----------------------------------------------------------------------------
188 #define _WX_DEFINE_ARRAY(T, name, classexp) \
189 wxCOMPILE_TIME_ASSERT2(sizeof(T) <= sizeof(long), \
190 TypeIsTooBigToBeStoredInWxArray, \
192 typedef int (CMPFUNC_CONV *CMPFUNC##T)(T *pItem1, T *pItem2); \
193 classexp name : public wxBaseArray \
199 name& operator=(const name& src) \
200 { wxBaseArray* temp = (wxBaseArray*) this; \
201 (*temp) = ((const wxBaseArray&)src); \
204 T& operator[](size_t uiIndex) const \
205 { return (T&)(wxBaseArray::Item(uiIndex)); } \
206 T& Item(size_t uiIndex) const \
207 { return (T&)(wxBaseArray::Item(uiIndex)); } \
209 { return (T&)(wxBaseArray::Item(Count() - 1)); } \
211 int Index(T Item, bool bFromEnd = FALSE) const \
212 { return wxBaseArray::Index((long)Item, bFromEnd); } \
215 { wxBaseArray::Add((long)Item); } \
216 void Insert(T Item, size_t uiIndex) \
217 { wxBaseArray::Insert((long)Item, uiIndex) ; } \
219 void RemoveAt(size_t uiIndex) { wxBaseArray::RemoveAt(uiIndex); } \
220 void Remove(T Item) \
221 { int iIndex = Index(Item); \
222 wxCHECK2_MSG( iIndex != wxNOT_FOUND, return, \
224 wxBaseArray::RemoveAt((size_t)iIndex); } \
226 void Sort(CMPFUNC##T fCmp) { wxBaseArray::Sort((CMPFUNC)fCmp); } \
229 // ----------------------------------------------------------------------------
230 // _WX_DEFINE_SORTED_ARRAY: sorted array for simple data types
231 // ----------------------------------------------------------------------------
233 #define _WX_DEFINE_SORTED_ARRAY(T, name, defcomp, classexp) \
234 wxCOMPILE_TIME_ASSERT2(sizeof(T) <= sizeof(long), \
235 TypeIsTooBigToBeStoredInWxArray, \
237 typedef int (CMPFUNC_CONV *SCMPFUNC##T)(T pItem1, T pItem2); \
238 classexp name : public wxBaseArray \
241 name(SCMPFUNC##T fn defcomp) { m_fnCompare = fn; } \
243 name& operator=(const name& src) \
244 { wxBaseArray* temp = (wxBaseArray*) this; \
245 (*temp) = ((const wxBaseArray&)src); \
246 m_fnCompare = src.m_fnCompare; \
249 T& operator[](size_t uiIndex) const \
250 { return (T&)(wxBaseArray::Item(uiIndex)); } \
251 T& Item(size_t uiIndex) const \
252 { return (T&)(wxBaseArray::Item(uiIndex)); } \
254 { return (T&)(wxBaseArray::Item(Count() - 1)); } \
256 int Index(T Item) const \
257 { return wxBaseArray::Index((long)Item, (CMPFUNC)m_fnCompare); }\
259 size_t IndexForInsert(T Item) const \
260 { return wxBaseArray::IndexForInsert((long)Item, \
261 (CMPFUNC)m_fnCompare); } \
263 void AddAt(T item, size_t index) \
264 { wxBaseArray::Insert((long)item, index); } \
267 { wxBaseArray::Add((long)Item, (CMPFUNC)m_fnCompare); } \
269 void RemoveAt(size_t uiIndex) { wxBaseArray::RemoveAt(uiIndex); } \
270 void Remove(T Item) \
271 { int iIndex = Index(Item); \
272 wxCHECK2_MSG( iIndex != wxNOT_FOUND, return, \
273 _WX_ERROR_REMOVE ); \
274 wxBaseArray::RemoveAt((size_t)iIndex); } \
277 SCMPFUNC##T m_fnCompare; \
280 // ----------------------------------------------------------------------------
281 // _WX_DECLARE_OBJARRAY: an array for pointers to type T with owning semantics
282 // ----------------------------------------------------------------------------
284 #define _WX_DECLARE_OBJARRAY(T, name, classexp) \
285 typedef int (CMPFUNC_CONV *CMPFUNC##T)(T** pItem1, T** pItem2); \
286 classexp name : public wxBaseArray \
290 name(const name& src); \
291 name& operator=(const name& src); \
295 T& operator[](size_t uiIndex) const \
296 { return *(T*)wxBaseArray::Item(uiIndex); } \
297 T& Item(size_t uiIndex) const \
298 { return *(T*)wxBaseArray::Item(uiIndex); } \
300 { return *(T*)(wxBaseArray::Item(Count() - 1)); } \
302 int Index(const T& Item, bool bFromEnd = FALSE) const; \
304 void Add(const T& Item); \
305 void Add(const T* pItem) \
306 { wxBaseArray::Add((long)pItem); } \
308 void Insert(const T& Item, size_t uiIndex); \
309 void Insert(const T* pItem, size_t uiIndex) \
310 { wxBaseArray::Insert((long)pItem, uiIndex); } \
312 void Empty() { DoEmpty(); wxBaseArray::Empty(); } \
313 void Clear() { DoEmpty(); wxBaseArray::Clear(); } \
315 T* Detach(size_t uiIndex) \
316 { T* p = (T*)wxBaseArray::Item(uiIndex); \
317 wxBaseArray::RemoveAt(uiIndex); return p; } \
318 void RemoveAt(size_t uiIndex); \
320 void Sort(CMPFUNC##T fCmp) { wxBaseArray::Sort((CMPFUNC)fCmp); } \
324 void DoCopy(const name& src); \
327 // ============================================================================
328 // The public macros for declaration and definition of the dynamic arrays
329 // ============================================================================
331 // Please note that for each macro WX_FOO_ARRAY we also have
332 // WX_FOO_EXPORTED_ARRAY and WX_FOO_USER_EXPORTED_ARRAY which are exactly the
333 // same except that they use an additional __declspec(dllexport) or equivalent
334 // under Windows if needed.
336 // The first (just EXPORTED) macros do it if wxWindows was compiled as a DLL
337 // and so must be used used inside the library. The second kind (USER_EXPORTED)
338 // allow the user code to do it when it wants. This is needed if you have a dll
339 // that wants to export a wxArray daubed with your own import/export goo.
341 // Finally, you can define the macro below as something special to modify the
342 // arrays defined by a simple WX_FOO_ARRAY as well. By default is is empty.
343 #define wxARRAY_DEFAULT_EXPORT
345 // ----------------------------------------------------------------------------
346 // WX_DEFINE_ARRAY(T, name) define an array class named "name" containing the
347 // elements of simple type T such that sizeof(T) <= sizeof(long)
349 // Note that the class defined has only inline function and doesn't take any
350 // space at all so there is no size penalty for defining multiple array classes
351 // ----------------------------------------------------------------------------
353 #define WX_DEFINE_ARRAY(T, name) \
354 WX_DEFINE_USER_EXPORTED_ARRAY(T, name, wxARRAY_DEFAULT_EXPORT)
356 #define WX_DEFINE_EXPORTED_ARRAY(T, name) \
357 WX_DEFINE_USER_EXPORTED_ARRAY(T, name, WXDLLEXPORT)
359 #define WX_DEFINE_USER_EXPORTED_ARRAY(T, name, expmode) \
360 typedef T _wxArray##name; \
361 _WX_DEFINE_ARRAY(_wxArray##name, name, class expmode)
363 // ----------------------------------------------------------------------------
364 // WX_DEFINE_SORTED_ARRAY: this is the same as the previous macro, but it
365 // defines a sorted array.
368 // 1) it must be given a COMPARE function in ctor which takes 2 items of type
369 // T* and should return -1, 0 or +1 if the first one is less/greater
370 // than/equal to the second one.
371 // 2) the Add() method inserts the item in such was that the array is always
372 // sorted (it uses the COMPARE function)
373 // 3) it has no Sort() method because it's always sorted
374 // 4) Index() method is much faster (the sorted arrays use binary search
375 // instead of linear one), but Add() is slower.
376 // 5) there is no Insert() method because you can't insert an item into the
377 // given position in a sorted array but there is IndexForInsert()/AddAt()
378 // pair which may be used to optimize a common operation of "insert only if
381 // Note that you have to specify the comparison function when creating the
382 // objects of this array type. If, as in 99% of cases, the comparison function
383 // is the same for all objects of a class, WX_DEFINE_SORTED_ARRAY_CMP below is
386 // Summary: use this class when the speed of Index() function is important, use
387 // the normal arrays otherwise.
388 // ----------------------------------------------------------------------------
390 #define wxARRAY_EMPTY_CMP
392 #define WX_DEFINE_SORTED_ARRAY(T, name) \
393 WX_DEFINE_SORTED_USER_EXPORTED_ARRAY(T, name, wxARRAY_DEFAULT_EXPORT)
395 #define WX_DEFINE_SORTED_EXPORTED_ARRAY(T, name) \
396 WX_DEFINE_SORTED_USER_EXPORTED_ARRAY(T, name, WXDLLEXPORT)
398 #define WX_DEFINE_SORTED_USER_EXPORTED_ARRAY(T, name, expmode) \
399 typedef T _wxArray##name; \
400 _WX_DEFINE_SORTED_ARRAY(_wxArray##name, name, wxARRAY_EMPTY_CMP, class expmode)
402 // ----------------------------------------------------------------------------
403 // WX_DEFINE_SORTED_ARRAY_CMP: exactly the same as above but the comparison
404 // function is provided by this macro and the objects of this class have a
405 // default constructor which just uses it.
407 // The arguments are: the element type, the comparison function and the array
410 // NB: this is, of course, how WX_DEFINE_SORTED_ARRAY() should have worked from
411 // the very beginning - unfortunately I didn't think about this earlier :-(
412 // ----------------------------------------------------------------------------
414 #define WX_DEFINE_SORTED_ARRAY_CMP(T, cmpfunc, name) \
415 WX_DEFINE_SORTED_USER_EXPORTED_ARRAY_CMP(T, cmpfunc, name, \
416 wxARRAY_DEFAULT_EXPORT)
418 #define WX_DEFINE_SORTED_EXPORTED_ARRAY_CMP(T, cmpfunc, name) \
419 WX_DEFINE_SORTED_USER_EXPORTED_ARRAY_CMP(T, cmpfunc, name, WXDLLEXPORT)
421 #define WX_DEFINE_SORTED_USER_EXPORTED_ARRAY_CMP(T, cmpfunc, name, expmode) \
422 typedef T _wxArray##name; \
423 _WX_DEFINE_SORTED_ARRAY(_wxArray##name, name, = cmpfunc, class expmode)
425 // ----------------------------------------------------------------------------
426 // WX_DECLARE_OBJARRAY(T, name): this macro generates a new array class
427 // named "name" which owns the objects of type T it contains, i.e. it will
428 // delete them when it is destroyed.
430 // An element is of type T*, but arguments of type T& are taken (see below!)
431 // and T& is returned.
433 // Don't use this for simple types such as "int" or "long"!
434 // You _may_ use it for "double" but it's awfully inefficient.
436 // Note on Add/Insert functions:
437 // 1) function(T*) gives the object to the array, i.e. it will delete the
438 // object when it's removed or in the array's dtor
439 // 2) function(T&) will create a copy of the object and work with it
442 // 1) Remove() will delete the object after removing it from the array
443 // 2) Detach() just removes the object from the array (returning pointer to it)
445 // NB1: Base type T should have an accessible copy ctor if Add(T&) is used
446 // NB2: Never ever cast a array to it's base type: as dtor is not virtual
447 // and so you risk having at least the memory leaks and probably worse
449 // Some functions of this class are not inline, so it takes some space to
450 // define new class from this template even if you don't use it - which is not
451 // the case for the simple (non-object) array classes
454 // To use an objarray class you must
455 // #include "dynarray.h"
456 // WX_DECLARE_OBJARRAY(element_type, list_class_name)
457 // #include "arrimpl.cpp"
458 // WX_DEFINE_OBJARRAY(list_class_name) // name must be the same as above!
460 // This is necessary because at the moment of DEFINE_OBJARRAY class parsing the
461 // element_type must be fully defined (i.e. forward declaration is not
462 // enough), while WX_DECLARE_OBJARRAY may be done anywhere. The separation of
463 // two allows to break cicrcular dependencies with classes which have member
464 // variables of objarray type.
465 // ----------------------------------------------------------------------------
467 #define WX_DECLARE_OBJARRAY(T, name) \
468 WX_DECLARE_USER_EXPORTED_OBJARRAY(T, name, wxARRAY_DEFAULT_EXPORT)
470 #define WX_DECLARE_EXPORTED_OBJARRAY(T, name) \
471 WX_DECLARE_USER_EXPORTED_OBJARRAY(T, name, WXDLLEXPORT)
473 #define WX_DECLARE_USER_EXPORTED_OBJARRAY(T, name, expmode) \
474 typedef T _wxObjArray##name; \
475 _WX_DECLARE_OBJARRAY(_wxObjArray##name, name, class expmode)
477 // WX_DEFINE_OBJARRAY is going to be redefined when arrimpl.cpp is included,
478 // try to provoke a human-understandable error if it used incorrectly.
480 // there is no real need for 3 different macros in the DEFINE case but do it
481 // anyhow for consistency
482 #define WX_DEFINE_OBJARRAY(name) DidYouIncludeArrimplCpp
483 #define WX_DEFINE_EXPORTED_OBJARRAY(name) WX_DEFINE_OBJARRAY(name)
484 #define WX_DEFINE_USER_EXPORTED_OBJARRAY(name) WX_DEFINE_OBJARRAY(name)
486 // ----------------------------------------------------------------------------
487 // Some commonly used predefined arrays
488 // ----------------------------------------------------------------------------
490 WX_DEFINE_EXPORTED_ARRAY(int, wxArrayInt
);
491 WX_DEFINE_EXPORTED_ARRAY(long, wxArrayLong
);
492 WX_DEFINE_EXPORTED_ARRAY(void *, wxArrayPtrVoid
);
494 // -----------------------------------------------------------------------------
495 // convenience macros
496 // -----------------------------------------------------------------------------
498 // append all element of one array to another one
499 #define WX_APPEND_ARRAY(array, other) \
501 size_t count = (other).Count(); \
502 for ( size_t n = 0; n < count; n++ ) \
504 (array).Add((other)[n]); \
508 // delete all array elements
510 // NB: the class declaration of the array elements must be visible from the
511 // place where you use this macro, otherwise the proper destructor may not
512 // be called (a decent compiler should give a warning about it, but don't
514 #define WX_CLEAR_ARRAY(array) \
516 size_t count = (array).Count(); \
517 for ( size_t n = 0; n < count; n++ ) \
525 #endif // _DYNARRAY_H