]>
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 /** @name Dynamic arrays and object arrays (array which own their elements)
23 @memo Arrays which grow on demand and do range checking (only in debug)
27 // ----------------------------------------------------------------------------
29 // ----------------------------------------------------------------------------
32 the initial size by which an array grows when an element is added
33 default value avoids allocate one or two bytes when the array is created
34 which is rather inefficient
36 #define WX_ARRAY_DEFAULT_INITIAL_SIZE (16)
38 // ----------------------------------------------------------------------------
40 // ----------------------------------------------------------------------------
43 callback compare function for quick sort
44 must return negative value, 0 or positive value if pItem1 <, = or > pItem2
46 typedef int (wxCMPFUNC_CONV
*CMPFUNC
)(const void* pItem1
, const void* pItem2
);
48 // ----------------------------------------------------------------------------
50 base class managing data having size of type 'long' (not used directly)
52 NB: for efficiency this often used class has no virtual functions (hence no
53 VTBL), even dtor is <B>not</B> virtual. If used as expected it won't
54 create any problems because ARRAYs from DEFINE_ARRAY have no dtor at all,
55 so it's not too important if it's not called (this happens when you cast
56 "SomeArray *" as "BaseArray *" and then delete it)
58 @memo Base class for template array classes
60 // ----------------------------------------------------------------------------
61 class WXDLLEXPORT wxBaseArray
64 /** @name ctors and dtor */
69 wxBaseArray(const wxBaseArray
& array
);
70 /// assignment operator
71 wxBaseArray
& operator=(const wxBaseArray
& src
);
72 /// not virtual, see above
73 /// EXCEPT for Gnu compiler to reduce warnings...
80 /** @name memory management */
82 /// empties the array, but doesn't release memory
83 void Empty() { m_nCount
= 0; }
84 /// empties the array and releases memory
86 /// preallocates memory for given number of items
87 void Alloc(size_t uiSize
);
88 /// minimizes the memory used by the array (frees unused memory)
92 /** @name simple accessors */
94 /// number of elements in the array
95 size_t Count() const { return m_nCount
; }
96 size_t GetCount() const { return m_nCount
; }
98 bool IsEmpty() const { return m_nCount
== 0; }
102 // these methods are protected because if they were public one could
103 // mistakenly call one of them instead of DEFINE_ARRAY's or OBJARRAY's
106 /** @name items access */
108 /// get item at position uiIndex (range checking is done in debug version)
109 long& Item(size_t uiIndex
) const
110 { wxASSERT( uiIndex
< m_nCount
); return m_pItems
[uiIndex
]; }
112 long& operator[](size_t uiIndex
) const { return Item(uiIndex
); }
115 /** @name item management */
118 Search the element in the array, starting from the either side
119 @param bFromEnd if TRUE, start from the end
120 @return index of the first item matched or wxNOT_FOUND
123 int Index(long lItem
, bool bFromEnd
= FALSE
) const;
124 /// search for an item using binary search in a sorted array
125 int Index(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 Remove(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 #define _WX_DEFINE_ARRAY(T, name, classexp) \
169 typedef int (CMPFUNC_CONV *CMPFUNC##T)(T *pItem1, T *pItem2); \
170 classexp name : public wxBaseArray \
175 size_t type = sizeof(T); \
176 size_t sizelong = sizeof(long); \
177 if ( type > sizelong ) \
178 { wxFAIL_MSG( _WX_ERROR_SIZEOF ); } \
181 name& operator=(const name& src) \
182 { wxBaseArray* temp = (wxBaseArray*) this; \
183 (*temp) = ((const wxBaseArray&)src); \
186 T& operator[](size_t uiIndex) const \
187 { return (T&)(wxBaseArray::Item(uiIndex)); } \
188 T& Item(size_t uiIndex) const \
189 { return (T&)(wxBaseArray::Item(uiIndex)); } \
191 { return (T&)(wxBaseArray::Item(Count() - 1)); } \
193 int Index(T Item, bool bFromEnd = FALSE) const \
194 { return wxBaseArray::Index((long)Item, bFromEnd); } \
197 { wxBaseArray::Add((long)Item); } \
198 void Insert(T Item, size_t uiIndex) \
199 { wxBaseArray::Insert((long)Item, uiIndex) ; } \
201 void Remove(size_t uiIndex) { wxBaseArray::Remove(uiIndex); } \
202 void Remove(T Item) \
203 { int iIndex = Index(Item); \
204 wxCHECK2_MSG( iIndex != wxNOT_FOUND, return, \
206 wxBaseArray::Remove((size_t)iIndex); } \
208 void Sort(CMPFUNC##T fCmp) { wxBaseArray::Sort((CMPFUNC)fCmp); } \
211 // ----------------------------------------------------------------------------
212 // This is the same as the previous macro, but it defines a sorted array.
214 // 1) it must be given a COMPARE function in ctor which takes 2 items of type
215 // T* and should return -1, 0 or +1 if the first one is less/greater
216 // than/equal to the second one.
217 // 2) the Add() method inserts the item in such was that the array is always
218 // sorted (it uses the COMPARE function)
219 // 3) it has no Sort() method because it's always sorted
220 // 4) Index() method is much faster (the sorted arrays use binary search
221 // instead of linear one), but Add() is slower.
223 // Summary: use this class when the speed of Index() function is important, use
224 // the normal arrays otherwise.
226 // NB: it has only inline functions => takes no space at all
227 // Mod by JACS: Salford C++ doesn't like 'var->operator=' syntax, as in:
228 // { ((wxBaseArray *)this)->operator=((const wxBaseArray&)src);
229 // so using a temporary variable instead.
230 // ----------------------------------------------------------------------------
231 #define _WX_DEFINE_SORTED_ARRAY(T, name, classexp) \
232 typedef int (CMPFUNC_CONV *SCMPFUNC##T)(T pItem1, T pItem2); \
233 classexp name : public wxBaseArray \
236 name(SCMPFUNC##T fn) \
237 { size_t type = sizeof(T); \
238 size_t sizelong = sizeof(long); \
239 if ( type > sizelong ) \
240 { wxFAIL_MSG( _WX_ERROR_SIZEOF ); } \
244 name& operator=(const name& src) \
245 { wxBaseArray* temp = (wxBaseArray*) this; \
246 (*temp) = ((const wxBaseArray&)src); \
247 m_fnCompare = src.m_fnCompare; \
250 T& operator[](size_t uiIndex) const \
251 { return (T&)(wxBaseArray::Item(uiIndex)); } \
252 T& Item(size_t uiIndex) const \
253 { return (T&)(wxBaseArray::Item(uiIndex)); } \
255 { return (T&)(wxBaseArray::Item(Count() - 1)); } \
257 int Index(T Item) const \
258 { return wxBaseArray::Index((long)Item, (CMPFUNC)m_fnCompare); }\
261 { wxBaseArray::Add((long)Item, (CMPFUNC)m_fnCompare); } \
263 void Remove(size_t uiIndex) { wxBaseArray::Remove(uiIndex); } \
264 void Remove(T Item) \
265 { int iIndex = Index(Item); \
266 wxCHECK2_MSG( iIndex != wxNOT_FOUND, return, \
267 _WX_ERROR_REMOVE ); \
268 wxBaseArray::Remove((size_t)iIndex); } \
271 SCMPFUNC##T m_fnCompare; \
274 // ----------------------------------------------------------------------------
275 // see WX_DECLARE_OBJARRAY and WX_DEFINE_OBJARRAY
276 // ----------------------------------------------------------------------------
277 #define _WX_DECLARE_OBJARRAY(T, name, classexp) \
278 typedef int (CMPFUNC_CONV *CMPFUNC##T)(T** pItem1, T** pItem2); \
279 classexp name : public wxBaseArray \
283 name(const name& src); \
284 name& operator=(const name& src); \
288 T& operator[](size_t uiIndex) const \
289 { return *(T*)wxBaseArray::Item(uiIndex); } \
290 T& Item(size_t uiIndex) const \
291 { return *(T*)wxBaseArray::Item(uiIndex); } \
293 { return *(T*)(wxBaseArray::Item(Count() - 1)); } \
295 int Index(const T& Item, bool bFromEnd = FALSE) const; \
297 void Add(const T& Item); \
298 void Add(const T* pItem) \
299 { wxBaseArray::Add((long)pItem); } \
301 void Insert(const T& Item, size_t uiIndex); \
302 void Insert(const T* pItem, size_t uiIndex) \
303 { wxBaseArray::Insert((long)pItem, uiIndex); } \
307 T* Detach(size_t uiIndex) \
308 { T* p = (T*)wxBaseArray::Item(uiIndex); \
309 wxBaseArray::Remove(uiIndex); return p; } \
310 void Remove(size_t uiIndex); \
312 void Sort(CMPFUNC##T fCmp) { wxBaseArray::Sort((CMPFUNC)fCmp); } \
315 void DoCopy(const name& src); \
318 // ----------------------------------------------------------------------------
319 /** @name Macros for definition of dynamic arrays and objarrays
321 These macros are ugly (especially if you look in the sources ;-), but they
322 allow us to define 'template' classes without actually using templates.
325 Range checking is performed in debug build for both arrays and objarrays.
326 Type checking is done at compile-time. Warning: arrays <I>never</I> shrink,
327 they only grow, so loading 10 millions in an array only to delete them 2
328 lines below is <I>not</I> recommended. However, it does free memory when
329 it's destroyed, so if you destroy array also, it's ok.
331 // ----------------------------------------------------------------------------
335 This macro generates a new array class. It is intended for storage of simple
336 types of sizeof()<=sizeof(long) or pointers if sizeof(pointer)<=sizeof(long)
338 NB: it has only inline functions => takes no space at all
341 @memo declare and define array class 'name' containing elements of type 'T'
343 #define WX_DEFINE_ARRAY(T, name) \
344 typedef T _A##name; \
345 _WX_DEFINE_ARRAY(_A##name, name, class)
348 This macro does the same as WX_DEFINE_ARRAY except that the array will be
349 sorted with the specified compare function.
351 #define WX_DEFINE_SORTED_ARRAY(T, name) \
352 typedef T _A##name; \
353 _WX_DEFINE_SORTED_ARRAY(_A##name, name, class)
356 This macro generates a new objarrays class which owns the objects it
357 contains, i.e. it will delete them when it is destroyed. An element is of
358 type T*, but arguments of type T& are taken (see below!) and T& is
360 Don't use this for simple types such as "int" or "long"!
361 You _may_ use it for "double" but it's awfully inefficient.
364 Note on Add/Insert functions:
366 1) function(T*) gives the object to the array, i.e. it will delete the
367 object when it's removed or in the array's dtor
369 2) function(T&) will create a copy of the object and work with it
374 1) Remove() will delete the object after removing it from the array
376 2) Detach() just removes the object from the array (returning pointer to it)
379 NB1: Base type T should have an accessible copy ctor if Add(T&) is used,
381 NB2: Never ever cast a array to it's base type: as dtor is <B>not</B> virtual
382 it will provoke memory leaks
385 some functions of this class are not inline, so it takes some space to
386 define new class from this template.
388 @memo declare objarray class 'name' containing elements of type 'T'
390 #define WX_DECLARE_OBJARRAY(T, name) \
391 typedef T _L##name; \
392 _WX_DECLARE_OBJARRAY(_L##name, name, class)
395 To use an objarray class you must
397 <li>#include "dynarray.h"
398 <li>WX_DECLARE_OBJARRAY(element_type, list_class_name)
399 <li>#include "arrimpl.cpp"
400 <li>WX_DEFINE_OBJARRAY(list_class_name) // same as above!
403 This is necessary because at the moment of DEFINE_OBJARRAY class
404 element_type must be fully defined (i.e. forward declaration is not
405 enough), while WX_DECLARE_OBJARRAY may be done anywhere. The separation of
406 two allows to break cicrcular dependencies with classes which have member
407 variables of objarray type.
409 @memo define (must include arrimpl.cpp!) objarray class 'name'
411 #define WX_DEFINE_OBJARRAY(name) "don't forget to include arrimpl.cpp!"
414 // these macros do the same thing as the WX_XXX ones above, but should be used
415 // inside the library for user visible classes because otherwise they wouldn't
416 // be visible from outside (when using wxWindows as DLL under Windows)
417 #define WX_DEFINE_EXPORTED_ARRAY(T, name) \
418 typedef T _A##name; \
419 _WX_DEFINE_ARRAY(_A##name, name, class WXDLLEXPORT)
421 #define WX_DEFINE_SORTED_EXPORTED_ARRAY(T, name) \
422 typedef T _A##name; \
423 _WX_DEFINE_SORTED_ARRAY(_A##name, name, class WXDLLEXPORT)
425 #define WX_DECLARE_EXPORTED_OBJARRAY(T, name) \
426 typedef T _L##name; \
427 _WX_DECLARE_OBJARRAY(_L##name, name, class WXDLLEXPORT)
429 // ----------------------------------------------------------------------------
430 /** @name Some commonly used predefined arrays */
431 // ----------------------------------------------------------------------------
434 /** @name ArrayInt */
435 WX_DEFINE_EXPORTED_ARRAY(int, wxArrayInt
);
436 /** @name ArrayLong */
437 WX_DEFINE_EXPORTED_ARRAY(long, wxArrayLong
);
438 /** @name ArrayPtrVoid */
439 WX_DEFINE_EXPORTED_ARRAY(void *, wxArrayPtrVoid
);
444 // -----------------------------------------------------------------------------
445 // convinience macros
446 // -----------------------------------------------------------------------------
448 // delete all array elements
450 // NB: the class declaration of the array elements must be visible from the
451 // place where you use this macro, otherwise the proper destructor may not
452 // be called (a decent compiler should give a warning about it, but don't
454 #define WX_CLEAR_ARRAY(array) \
456 size_t count = array.Count(); \
457 for ( size_t n = 0; n < count; n++ ) \
465 #endif // _DYNARRAY_H