]>
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
48 #define CMPFUNC_CONV _cdecl
52 typedef int (CMPFUNC_CONV
*CMPFUNC
)(const void* pItem1
, const void* pItem2
);
54 // ----------------------------------------------------------------------------
56 base class managing data having size of type 'long' (not used directly)
58 NB: for efficiency this often used class has no virtual functions (hence no
59 VTBL), even dtor is <B>not</B> virtual. If used as expected it won't
60 create any problems because ARRAYs from DEFINE_ARRAY have no dtor at all,
61 so it's not too important if it's not called (this happens when you cast
62 "SomeArray *" as "BaseArray *" and then delete it)
64 @memo Base class for template array classes
66 // ----------------------------------------------------------------------------
67 class WXDLLEXPORT wxBaseArray
70 /** @name ctors and dtor */
75 wxBaseArray(const wxBaseArray
& array
);
76 /// assignment operator
77 wxBaseArray
& operator=(const wxBaseArray
& src
);
78 /// not virtual, see above
79 /// EXCEPT for Gnu compiler to reduce warnings...
86 /** @name memory management */
88 /// empties the array, but doesn't release memory
89 void Empty() { m_nCount
= 0; }
90 /// empties the array and releases memory
92 /// preallocates memory for given number of items
93 void Alloc(size_t uiSize
);
94 /// minimizes the memory used by the array (frees unused memory)
98 /** @name simple accessors */
100 /// number of elements in the array
101 size_t Count() const { return m_nCount
; }
102 size_t GetCount() const { return m_nCount
; }
104 bool IsEmpty() const { return m_nCount
== 0; }
108 // these methods are protected because if they were public one could
109 // mistakenly call one of them instead of DEFINE_ARRAY's or OBJARRAY's
112 /** @name items access */
114 /// get item at position uiIndex (range checking is done in debug version)
115 long& Item(size_t uiIndex
) const
116 { wxASSERT( uiIndex
< m_nCount
); return m_pItems
[uiIndex
]; }
118 long& operator[](size_t uiIndex
) const { return Item(uiIndex
); }
121 /** @name item management */
124 Search the element in the array, starting from the either side
125 @param bFromEnd if TRUE, start from the end
126 @return index of the first item matched or NOT_FOUND
129 int Index(long lItem
, bool bFromEnd
= FALSE
) const;
130 /// search for an item using binary search in a sorted array
131 int Index(long lItem
, CMPFUNC fnCompare
) const;
132 /// add new element at the end
133 void Add(long lItem
);
134 /// add item assuming the array is sorted with fnCompare function
135 void Add(long lItem
, CMPFUNC fnCompare
);
136 /// add new element at given position (it becomes Item[uiIndex])
137 void Insert(long lItem
, size_t uiIndex
);
138 /// remove first item matching this value
139 void Remove(long lItem
);
140 /// remove item by index
141 void Remove(size_t uiIndex
);
144 /// sort array elements using given compare function
145 void Sort(CMPFUNC fnCompare
);
148 void Grow(); // makes array bigger if needed
150 size_t m_nSize
, // current size of the array
151 m_nCount
; // current number of elements
153 long *m_pItems
; // pointer to data
156 // ============================================================================
158 // ============================================================================
160 // ----------------------------------------------------------------------------
161 // This macro generates a new array class. It is intended for storage of simple
162 // types of sizeof()<=sizeof(long) or pointers if sizeof(pointer)<=sizeof(long)
164 // NB: it has only inline functions => takes no space at all
165 // ----------------------------------------------------------------------------
166 #define _WX_DEFINE_ARRAY(T, name) \
167 typedef int (CMPFUNC_CONV *CMPFUNC##T)(T *pItem1, T *pItem2); \
168 class WXDLLEXPORTLOCAL name : public wxBaseArray \
172 { wxASSERT( sizeof(T) <= sizeof(long) ); } \
174 name& operator=(const name& src) \
175 { ((wxBaseArray *)this)->operator=((const wxBaseArray&)src); \
178 T& operator[](size_t uiIndex) const \
179 { return (T&)(wxBaseArray::Item(uiIndex)); } \
180 T& Item(size_t uiIndex) const \
181 { return (T&)(wxBaseArray::Item(uiIndex)); } \
183 { return (T&)(wxBaseArray::Item(Count() - 1)); } \
185 int Index(T Item, bool bFromEnd = FALSE) const \
186 { return wxBaseArray::Index((long)Item, bFromEnd); } \
189 { wxBaseArray::Add((long)Item); } \
190 void Insert(T Item, size_t uiIndex) \
191 { wxBaseArray::Insert((long)Item, uiIndex) ; } \
193 void Remove(size_t uiIndex) { wxBaseArray::Remove(uiIndex); } \
194 void Remove(T Item) \
195 { int iIndex = Index(Item); \
196 wxCHECK2_MSG( iIndex != NOT_FOUND, return, \
197 "removing inexisting element in wxArray::Remove" ); \
198 wxBaseArray::Remove((size_t)iIndex); } \
200 void Sort(CMPFUNC##T fCmp) { wxBaseArray::Sort((CMPFUNC)fCmp); } \
203 // ----------------------------------------------------------------------------
204 // This is the same as the previous macro, but it defines a sorted array.
206 // 1) it must be given a COMPARE function in ctor which takes 2 items of type
207 // T* and should return -1, 0 or +1 if the first one is less/greater
208 // than/equal to the second one.
209 // 2) the Add() method inserts the item in such was that the array is always
210 // sorted (it uses the COMPARE function)
211 // 3) it has no Sort() method because it's always sorted
212 // 4) Index() method is much faster (the sorted arrays use binary search
213 // instead of linear one), but Add() is slower.
215 // Summary: use this class when the speed of Index() function is important, use
216 // the normal arrays otherwise.
218 // NB: it has only inline functions => takes no space at all
219 // ----------------------------------------------------------------------------
220 #define _WX_DEFINE_SORTED_ARRAY(T, name) \
221 typedef int (CMPFUNC_CONV *SCMPFUNC##T)(T pItem1, T pItem2); \
222 class WXDLLEXPORTLOCAL name : public wxBaseArray \
225 name(SCMPFUNC##T fn) \
226 { wxASSERT( sizeof(T) <= sizeof(long) ); m_fnCompare = fn; } \
228 name& operator=(const name& src) \
229 { ((wxBaseArray *)this)->operator=((const wxBaseArray&)src); \
230 m_fnCompare = src.m_fnCompare; \
233 T& operator[](size_t uiIndex) const \
234 { return (T&)(wxBaseArray::Item(uiIndex)); } \
235 T& Item(size_t uiIndex) const \
236 { return (T&)(wxBaseArray::Item(uiIndex)); } \
238 { return (T&)(wxBaseArray::Item(Count() - 1)); } \
240 int Index(T Item) const \
241 { return wxBaseArray::Index((long)Item, (CMPFUNC)m_fnCompare); }\
244 { wxBaseArray::Add((long)Item, (CMPFUNC)m_fnCompare); } \
246 void Remove(size_t uiIndex) { wxBaseArray::Remove(uiIndex); } \
247 void Remove(T Item) \
248 { int iIndex = Index(Item); \
249 wxCHECK2_MSG( iIndex != NOT_FOUND, return, \
250 "removing inexisting element in wxArray::Remove" ); \
251 wxBaseArray::Remove((size_t)iIndex); } \
254 SCMPFUNC##T m_fnCompare; \
257 // ----------------------------------------------------------------------------
258 // see WX_DECLARE_OBJARRAY and WX_DEFINE_OBJARRAY
259 // ----------------------------------------------------------------------------
260 #define _WX_DECLARE_OBJARRAY(T, name) \
261 typedef int (CMPFUNC_CONV *CMPFUNC##T)(T** pItem1, T** pItem2); \
262 class WXDLLEXPORTLOCAL name : public wxBaseArray \
266 name(const name& src); \
267 name& operator=(const name& src); \
271 T& operator[](size_t uiIndex) const \
272 { return *(T*)wxBaseArray::Item(uiIndex); } \
273 T& Item(size_t uiIndex) const \
274 { return *(T*)wxBaseArray::Item(uiIndex); } \
276 { return *(T*)(wxBaseArray::Item(Count() - 1)); } \
278 int Index(const T& Item, bool bFromEnd = FALSE) const; \
280 void Add(const T& Item); \
281 void Add(const T* pItem) \
282 { wxBaseArray::Add((long)pItem); } \
284 void Insert(const T& Item, size_t uiIndex); \
285 void Insert(const T* pItem, size_t uiIndex) \
286 { wxBaseArray::Insert((long)pItem, uiIndex); } \
290 T* Detach(size_t uiIndex) \
291 { T* p = (T*)wxBaseArray::Item(uiIndex); \
292 wxBaseArray::Remove(uiIndex); return p; } \
293 void Remove(size_t uiIndex); \
295 void Sort(CMPFUNC##T fCmp) { wxBaseArray::Sort((CMPFUNC)fCmp); } \
298 void DoCopy(const name& src); \
301 // ----------------------------------------------------------------------------
302 /** @name Macros for definition of dynamic arrays and objarrays
304 These macros are ugly (especially if you look in the sources ;-), but they
305 allow us to define 'template' classes without actually using templates.
308 Range checking is performed in debug build for both arrays and objarrays.
309 Type checking is done at compile-time. Warning: arrays <I>never</I> shrink,
310 they only grow, so loading 10 millions in an array only to delete them 2
311 lines below is <I>not</I> recommended. However, it does free memory when
312 it's destroyed, so if you destroy array also, it's ok.
314 // ----------------------------------------------------------------------------
318 This macro generates a new array class. It is intended for storage of simple
319 types of sizeof()<=sizeof(long) or pointers if sizeof(pointer)<=sizeof(long)
321 NB: it has only inline functions => takes no space at all
324 @memo declare and define array class 'name' containing elements of type 'T'
326 #define WX_DEFINE_ARRAY(T, name) typedef T _A##name; \
327 _WX_DEFINE_ARRAY(_A##name, name)
330 This macro does the same as WX_DEFINE_ARRAY except that the array will be
331 sorted with the specified compare function.
333 #define WX_DEFINE_SORTED_ARRAY(T, name) typedef T _A##name; \
334 _WX_DEFINE_SORTED_ARRAY(_A##name, name)
337 This macro generates a new objarrays class which owns the objects it
338 contains, i.e. it will delete them when it is destroyed. An element is of
339 type T*, but arguments of type T& are taken (see below!) and T& is
341 Don't use this for simple types such as "int" or "long"!
342 You _may_ use it for "double" but it's awfully inefficient.
345 Note on Add/Insert functions:
347 1) function(T*) gives the object to the array, i.e. it will delete the
348 object when it's removed or in the array's dtor
350 2) function(T&) will create a copy of the object and work with it
355 1) Remove() will delete the object after removing it from the array
357 2) Detach() just removes the object from the array (returning pointer to it)
360 NB1: Base type T should have an accessible copy ctor if Add(T&) is used,
362 NB2: Never ever cast a array to it's base type: as dtor is <B>not</B> virtual
363 it will provoke memory leaks
366 some functions of this class are not inline, so it takes some space to
367 define new class from this template.
369 @memo declare objarray class 'name' containing elements of type 'T'
371 #define WX_DECLARE_OBJARRAY(T, name) typedef T _L##name; \
372 _WX_DECLARE_OBJARRAY(_L##name, name)
374 To use an objarray class you must
376 <li>#include "dynarray.h"
377 <li>WX_DECLARE_OBJARRAY(element_type, list_class_name)
378 <li>#include "arrimpl.cpp"
379 <li>WX_DEFINE_OBJARRAY(list_class_name) // same as above!
382 This is necessary because at the moment of DEFINE_OBJARRAY class
383 element_type must be fully defined (i.e. forward declaration is not
384 enough), while WX_DECLARE_OBJARRAY may be done anywhere. The separation of
385 two allows to break cicrcular dependencies with classes which have member
386 variables of objarray type.
388 @memo define (must include arrimpl.cpp!) objarray class 'name'
390 #define WX_DEFINE_OBJARRAY(name) "don't forget to include arrimpl.cpp!"
393 // ----------------------------------------------------------------------------
394 /** @name Some commonly used predefined arrays */
395 // # overhead if not used?
396 // ----------------------------------------------------------------------------
398 #define WXDLLEXPORTLOCAL WXDLLEXPORT
401 /** @name ArrayInt */
402 WX_DEFINE_ARRAY(int, wxArrayInt
);
403 /** @name ArrayLong */
404 WX_DEFINE_ARRAY(long, wxArrayLong
);
405 /** @name ArrayPtrVoid */
406 WX_DEFINE_ARRAY(void *, wxArrayPtrVoid
);
411 #undef WXDLLEXPORTLOCAL
412 #define WXDLLEXPORTLOCAL
414 // -----------------------------------------------------------------------------
415 // convinience macros
416 // -----------------------------------------------------------------------------
418 // delete all array elements
420 // NB: the class declaration of the array elements must be visible from the
421 // place where you use this macro, otherwise the proper destructor may not
422 // be called (a decent compiler should give a warning about it, but don't
424 #define WX_CLEAR_ARRAY(array) \
426 size_t count = array.Count(); \
427 for ( size_t n = 0; n < count; n++ ) \
432 #endif // _DYNARRAY_H