]>
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"
21 /** @name Dynamic arrays and object arrays (array which own their elements)
22 @memo Arrays which grow on demand and do range checking (only in debug)
26 // ----------------------------------------------------------------------------
28 // ----------------------------------------------------------------------------
31 the initial size by which an array grows when an element is added
32 default value avoids allocate one or two bytes when the array is created
33 which is rather inefficient
35 #define WX_ARRAY_DEFAULT_INITIAL_SIZE (16)
37 // ----------------------------------------------------------------------------
39 // ----------------------------------------------------------------------------
42 callback compare function for quick sort
43 must return negative value, 0 or positive value if pItem1 <, = or > pItem2
47 typedef int (wxCMPFUNC_CONV
*CMPFUNC
)(const void* pItem1
, const void* pItem2
);
50 // ----------------------------------------------------------------------------
52 base class managing data having size of type 'long' (not used directly)
54 NB: for efficiency this often used class has no virtual functions (hence no
55 VTBL), even dtor is <B>not</B> virtual. If used as expected it won't
56 create any problems because ARRAYs from DEFINE_ARRAY have no dtor at all,
57 so it's not too important if it's not called (this happens when you cast
58 "SomeArray *" as "BaseArray *" and then delete it)
60 @memo Base class for template array classes
62 // ----------------------------------------------------------------------------
63 class WXDLLEXPORT wxBaseArray
66 /** @name ctors and dtor */
71 wxBaseArray(const wxBaseArray
& array
);
72 /// assignment operator
73 wxBaseArray
& operator=(const wxBaseArray
& src
);
74 /// not virtual, see above
78 /** @name memory management */
80 /// empties the array, but doesn't release memory
81 void Empty() { m_nCount
= 0; }
82 /// empties the array and releases memory
84 /// preallocates memory for given number of items
85 void Alloc(size_t uiSize
);
86 /// minimizes the memory used by the array (frees unused memory)
90 /** @name simple accessors */
92 /// number of elements in the array
93 size_t Count() const { return m_nCount
; }
94 size_t GetCount() const { return m_nCount
; }
96 bool IsEmpty() const { return m_nCount
== 0; }
100 // these methods are protected because if they were public one could
101 // mistakenly call one of them instead of DEFINE_ARRAY's or OBJARRAY's
104 /** @name items access */
106 /// get item at position uiIndex (range checking is done in debug version)
107 long& Item(size_t uiIndex
) const
108 { wxASSERT( uiIndex
< m_nCount
); return m_pItems
[uiIndex
]; }
110 long& operator[](size_t uiIndex
) const { return Item(uiIndex
); }
113 /** @name item management */
116 Search the element in the array, starting from the either side
117 @param bFromEnd if TRUE, start from the end
118 @return index of the first item matched or wxNOT_FOUND
121 int Index(long lItem
, bool bFromEnd
= FALSE
) const;
122 /// search for an item using binary search in a sorted array
123 int Index(long lItem
, CMPFUNC fnCompare
) const;
124 /// search for a place to insert the element into a sorted array
125 size_t IndexForInsert(long lItem
, CMPFUNC fnCompare
) const;
126 /// add new element at the end
127 void Add(long lItem
);
128 /// add item assuming the array is sorted with fnCompare function
129 void Add(long lItem
, CMPFUNC fnCompare
);
130 /// add new element at given position (it becomes Item[uiIndex])
131 void Insert(long lItem
, size_t uiIndex
);
132 /// remove first item matching this value
133 void Remove(long lItem
);
134 /// remove item by index
135 void RemoveAt(size_t uiIndex
);
138 /// sort array elements using given compare function
139 void Sort(CMPFUNC fnCompare
);
142 void Grow(); // makes array bigger if needed
144 size_t m_nSize
, // current size of the array
145 m_nCount
; // current number of elements
147 long *m_pItems
; // pointer to data
150 // ============================================================================
152 // ============================================================================
154 // resolves the name conflict between the wxT() macor and T typedef: we can't
155 // use wxT() inside WX_DEFINE_ARRAY!
156 #define _WX_ERROR_SIZEOF wxT("illegal use of DEFINE_ARRAY")
157 #define _WX_ERROR_REMOVE wxT("removing inexisting element in wxArray::Remove")
159 // ----------------------------------------------------------------------------
160 // This macro generates a new array class. It is intended for storage of simple
161 // types of sizeof()<=sizeof(long) or pointers if sizeof(pointer)<=sizeof(long)
163 // NB: it has only inline functions => takes no space at all
164 // Mod by JACS: Salford C++ doesn't like 'var->operator=' syntax, as in:
165 // { ((wxBaseArray *)this)->operator=((const wxBaseArray&)src);
166 // so using a temporary variable instead.
167 // ----------------------------------------------------------------------------
168 // __MAC_X__ added min ~name() below for compiling Mac X
169 #define _WX_DEFINE_ARRAY(T, name, classexp) \
170 typedef int (CMPFUNC_CONV *CMPFUNC##T)(T *pItem1, T *pItem2); \
171 classexp name : public wxBaseArray \
176 size_t type = sizeof(T); \
177 size_t sizelong = sizeof(long); \
178 if ( type > sizelong ) \
179 { wxFAIL_MSG( _WX_ERROR_SIZEOF ); } \
183 name& operator=(const name& src) \
184 { wxBaseArray* temp = (wxBaseArray*) this; \
185 (*temp) = ((const wxBaseArray&)src); \
188 T& operator[](size_t uiIndex) const \
189 { return (T&)(wxBaseArray::Item(uiIndex)); } \
190 T& Item(size_t uiIndex) const \
191 { return (T&)(wxBaseArray::Item(uiIndex)); } \
193 { return (T&)(wxBaseArray::Item(Count() - 1)); } \
195 int Index(T Item, bool bFromEnd = FALSE) const \
196 { return wxBaseArray::Index((long)Item, bFromEnd); } \
199 { wxBaseArray::Add((long)Item); } \
200 void Insert(T Item, size_t uiIndex) \
201 { wxBaseArray::Insert((long)Item, uiIndex) ; } \
203 void RemoveAt(size_t uiIndex) { wxBaseArray::RemoveAt(uiIndex); } \
204 void Remove(T Item) \
205 { int iIndex = Index(Item); \
206 wxCHECK2_MSG( iIndex != wxNOT_FOUND, return, \
208 wxBaseArray::RemoveAt((size_t)iIndex); } \
210 void Sort(CMPFUNC##T fCmp) { wxBaseArray::Sort((CMPFUNC)fCmp); } \
213 // ----------------------------------------------------------------------------
214 // This is the same as the previous macro, but it defines a sorted array.
216 // 1) it must be given a COMPARE function in ctor which takes 2 items of type
217 // T* and should return -1, 0 or +1 if the first one is less/greater
218 // than/equal to the second one.
219 // 2) the Add() method inserts the item in such was that the array is always
220 // sorted (it uses the COMPARE function)
221 // 3) it has no Sort() method because it's always sorted
222 // 4) Index() method is much faster (the sorted arrays use binary search
223 // instead of linear one), but Add() is slower.
224 // 5) there is no Insert() method because you can't insert an item into the
225 // given position in a sorted array but there is IndexForInsert()/AddAt()
226 // pair which may be used to optimize a common operation of "insert only if
229 // Summary: use this class when the speed of Index() function is important, use
230 // the normal arrays otherwise.
232 // NB: it has only inline functions => takes no space at all
233 // Mod by JACS: Salford C++ doesn't like 'var->operator=' syntax, as in:
234 // { ((wxBaseArray *)this)->operator=((const wxBaseArray&)src);
235 // so using a temporary variable instead.
236 // ----------------------------------------------------------------------------
237 #define _WX_DEFINE_SORTED_ARRAY(T, name, classexp) \
238 typedef int (CMPFUNC_CONV *SCMPFUNC##T)(T pItem1, T pItem2); \
239 classexp name : public wxBaseArray \
242 name(SCMPFUNC##T fn) \
243 { size_t type = sizeof(T); \
244 size_t sizelong = sizeof(long); \
245 if ( type > sizelong ) \
246 { wxFAIL_MSG( _WX_ERROR_SIZEOF ); } \
250 name& operator=(const name& src) \
251 { wxBaseArray* temp = (wxBaseArray*) this; \
252 (*temp) = ((const wxBaseArray&)src); \
253 m_fnCompare = src.m_fnCompare; \
256 T& operator[](size_t uiIndex) const \
257 { return (T&)(wxBaseArray::Item(uiIndex)); } \
258 T& Item(size_t uiIndex) const \
259 { return (T&)(wxBaseArray::Item(uiIndex)); } \
261 { return (T&)(wxBaseArray::Item(Count() - 1)); } \
263 int Index(T Item) const \
264 { return wxBaseArray::Index((long)Item, (CMPFUNC)m_fnCompare); }\
266 size_t IndexForInsert(T Item) const \
267 { return wxBaseArray::IndexForInsert((long)Item, \
268 (CMPFUNC)m_fnCompare); } \
270 void AddAt(T item, size_t index) \
271 { wxBaseArray::Insert((long)item, index); } \
274 { wxBaseArray::Add((long)Item, (CMPFUNC)m_fnCompare); } \
276 void RemoveAt(size_t uiIndex) { wxBaseArray::RemoveAt(uiIndex); } \
277 void Remove(T Item) \
278 { int iIndex = Index(Item); \
279 wxCHECK2_MSG( iIndex != wxNOT_FOUND, return, \
280 _WX_ERROR_REMOVE ); \
281 wxBaseArray::RemoveAt((size_t)iIndex); } \
284 SCMPFUNC##T m_fnCompare; \
287 // ----------------------------------------------------------------------------
288 // see WX_DECLARE_OBJARRAY and WX_DEFINE_OBJARRAY
289 // ----------------------------------------------------------------------------
290 #define _WX_DECLARE_OBJARRAY(T, name, classexp) \
291 typedef int (CMPFUNC_CONV *CMPFUNC##T)(T** pItem1, T** pItem2); \
292 classexp name : public wxBaseArray \
296 name(const name& src); \
297 name& operator=(const name& src); \
301 T& operator[](size_t uiIndex) const \
302 { return *(T*)wxBaseArray::Item(uiIndex); } \
303 T& Item(size_t uiIndex) const \
304 { return *(T*)wxBaseArray::Item(uiIndex); } \
306 { return *(T*)(wxBaseArray::Item(Count() - 1)); } \
308 int Index(const T& Item, bool bFromEnd = FALSE) const; \
310 void Add(const T& Item); \
311 void Add(const T* pItem) \
312 { wxBaseArray::Add((long)pItem); } \
314 void Insert(const T& Item, size_t uiIndex); \
315 void Insert(const T* pItem, size_t uiIndex) \
316 { wxBaseArray::Insert((long)pItem, uiIndex); } \
318 void Empty() { DoEmpty(); wxBaseArray::Empty(); } \
319 void Clear() { DoEmpty(); wxBaseArray::Clear(); } \
321 T* Detach(size_t uiIndex) \
322 { T* p = (T*)wxBaseArray::Item(uiIndex); \
323 wxBaseArray::RemoveAt(uiIndex); return p; } \
324 void RemoveAt(size_t uiIndex); \
326 void Sort(CMPFUNC##T fCmp) { wxBaseArray::Sort((CMPFUNC)fCmp); } \
330 void DoCopy(const name& src); \
333 // ----------------------------------------------------------------------------
334 /** @name Macros for definition of dynamic arrays and objarrays
336 These macros are ugly (especially if you look in the sources ;-), but they
337 allow us to define 'template' classes without actually using templates.
340 Range checking is performed in debug build for both arrays and objarrays.
341 Type checking is done at compile-time. Warning: arrays <I>never</I> shrink,
342 they only grow, so loading 10 millions in an array only to delete them 2
343 lines below is <I>not</I> recommended. However, it does free memory when
344 it's destroyed, so if you destroy array also, it's ok.
346 // ----------------------------------------------------------------------------
350 This macro generates a new array class. It is intended for storage of simple
351 types of sizeof()<=sizeof(long) or pointers if sizeof(pointer)<=sizeof(long)
353 NB: it has only inline functions => takes no space at all
356 @memo declare and define array class 'name' containing elements of type 'T'
358 #define WX_DEFINE_ARRAY(T, name) \
359 typedef T _A##name; \
360 _WX_DEFINE_ARRAY(_A##name, name, class)
363 This macro does the same as WX_DEFINE_ARRAY except that the array will be
364 sorted with the specified compare function.
366 #define WX_DEFINE_SORTED_ARRAY(T, name) \
367 typedef T _A##name; \
368 _WX_DEFINE_SORTED_ARRAY(_A##name, name, class)
371 This macro generates a new objarrays class which owns the objects it
372 contains, i.e. it will delete them when it is destroyed. An element is of
373 type T*, but arguments of type T& are taken (see below!) and T& is
375 Don't use this for simple types such as "int" or "long"!
376 You _may_ use it for "double" but it's awfully inefficient.
379 Note on Add/Insert functions:
381 1) function(T*) gives the object to the array, i.e. it will delete the
382 object when it's removed or in the array's dtor
384 2) function(T&) will create a copy of the object and work with it
389 1) Remove() will delete the object after removing it from the array
391 2) Detach() just removes the object from the array (returning pointer to it)
394 NB1: Base type T should have an accessible copy ctor if Add(T&) is used,
396 NB2: Never ever cast a array to it's base type: as dtor is <B>not</B> virtual
397 it will provoke memory leaks
400 some functions of this class are not inline, so it takes some space to
401 define new class from this template.
403 @memo declare objarray class 'name' containing elements of type 'T'
405 #define WX_DECLARE_OBJARRAY(T, name) \
406 typedef T _L##name; \
407 _WX_DECLARE_OBJARRAY(_L##name, name, class)
410 To use an objarray class you must
412 <li>#include "dynarray.h"
413 <li>WX_DECLARE_OBJARRAY(element_type, list_class_name)
414 <li>#include "arrimpl.cpp"
415 <li>WX_DEFINE_OBJARRAY(list_class_name) // same as above!
418 This is necessary because at the moment of DEFINE_OBJARRAY class
419 element_type must be fully defined (i.e. forward declaration is not
420 enough), while WX_DECLARE_OBJARRAY may be done anywhere. The separation of
421 two allows to break cicrcular dependencies with classes which have member
422 variables of objarray type.
424 @memo define (must include arrimpl.cpp!) objarray class 'name'
426 #define WX_DEFINE_OBJARRAY(name) "don't forget to include arrimpl.cpp!"
429 // these macros do the same thing as the WX_XXX ones above, but should be used
430 // inside the library for user visible classes because otherwise they wouldn't
431 // be visible from outside (when using wxWindows as DLL under Windows)
432 #define WX_DEFINE_EXPORTED_ARRAY(T, name) \
433 typedef T _A##name; \
434 _WX_DEFINE_ARRAY(_A##name, name, class WXDLLEXPORT)
436 #define WX_DEFINE_SORTED_EXPORTED_ARRAY(T, name) \
437 typedef T _A##name; \
438 _WX_DEFINE_SORTED_ARRAY(_A##name, name, class WXDLLEXPORT)
440 #define WX_DEFINE_EXPORTED_OBJARRAY(name) WX_DEFINE_OBJARRAY(name)
441 #define WX_DECLARE_EXPORTED_OBJARRAY(T, name) \
442 typedef T _L##name; \
443 _WX_DECLARE_OBJARRAY(_L##name, name, class WXDLLEXPORT)
445 // ..and likewise these macros do very same thing as the ones above them too,
446 // but allow the user to specify the export spec. Needed if you have a dll
447 // that wants to export a wxArray daubed with your own import/export goo.
448 #define WX_DEFINE_USER_EXPORTED_ARRAY(T, name, usergoo) \
449 typedef T _A##name; \
450 _WX_DEFINE_ARRAY(_A##name, name, class usergoo)
452 #define WX_DEFINE_SORTED_USER_EXPORTED_ARRAY(T, name, usergoo) \
453 typedef T _A##name; \
454 _WX_DEFINE_SORTED_ARRAY(_A##name, name, class usergoo)
456 #define WX_DEFINE_USER_EXPORTED_OBJARRAY(name) WX_DEFINE_OBJARRAY(name)
457 #define WX_DECLARE_USER_EXPORTED_OBJARRAY(T, name, usergoo) \
458 typedef T _L##name; \
459 _WX_DECLARE_OBJARRAY(_L##name, name, class usergoo)
461 // ----------------------------------------------------------------------------
462 /** @name Some commonly used predefined arrays */
463 // ----------------------------------------------------------------------------
466 /** @name ArrayInt */
467 WX_DEFINE_EXPORTED_ARRAY(int, wxArrayInt
);
468 /** @name ArrayLong */
469 WX_DEFINE_EXPORTED_ARRAY(long, wxArrayLong
);
470 /** @name ArrayPtrVoid */
471 WX_DEFINE_EXPORTED_ARRAY(void *, wxArrayPtrVoid
);
476 // -----------------------------------------------------------------------------
477 // convenience macros
478 // -----------------------------------------------------------------------------
480 // append all element of one array to another one
481 #define WX_APPEND_ARRAY(array, other) \
483 size_t count = (other).Count(); \
484 for ( size_t n = 0; n < count; n++ ) \
486 (array).Add((other)[n]); \
490 // delete all array elements
492 // NB: the class declaration of the array elements must be visible from the
493 // place where you use this macro, otherwise the proper destructor may not
494 // be called (a decent compiler should give a warning about it, but don't
496 #define WX_CLEAR_ARRAY(array) \
498 size_t count = (array).Count(); \
499 for ( size_t n = 0; n < count; n++ ) \
507 #endif // _DYNARRAY_H