]>
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 lists
23 @memo Arrays which grow on demand and do range checking (only in debug)
27 // ----------------------------------------------------------------------------
29 // ----------------------------------------------------------------------------
32 the initial size by which an array/list 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 -1, 0 or +1 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 and list classes
66 // ----------------------------------------------------------------------------
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 list, but doesn't release memory
89 void Empty() { m_uiCount
= 0; }
90 /// empties the list and releases memory
92 /// preallocates memory for given number of items
93 void Alloc(uint uiSize
);
96 /** @name simple accessors */
98 /// number of elements in the array
99 uint
Count() const { return m_uiCount
; }
101 bool IsEmpty() const { return m_uiCount
== 0; }
105 // these methods are protected because if they were public one could
106 // mistakenly call one of them instead of DEFINE_ARRAY's or LIST's
109 /** @name items access */
111 /// get item at position uiIndex (range checking is done in debug version)
112 long& Item(uint uiIndex
) const
113 { wxASSERT( uiIndex
< m_uiCount
); return m_pItems
[uiIndex
]; }
115 long& operator[](uint uiIndex
) const { return Item(uiIndex
); }
118 /** @name item management */
121 Search the element in the array, starting from the either side
122 @param bFromEnd if TRUE, start from the end
123 @return index of the first item matched or NOT_FOUND
126 int Index (long lItem
, bool bFromEnd
= FALSE
) const;
127 /// add new element at the end
128 void Add (long lItem
);
129 /// add new element at given position
130 void Insert(long lItem
, uint uiIndex
);
131 /// remove first item matching this value
132 void Remove(long lItem
);
133 /// remove item by index
134 void Remove(uint uiIndex
);
137 /// sort array elements using given compare function
138 void Sort(CMPFUNC fCmp
);
141 void Grow(); // makes array bigger if needed
143 uint m_uiSize
, // current size of the array
144 m_uiCount
; // current number of elements
146 long *m_pItems
; // pointer to data
149 // ============================================================================
151 // ============================================================================
153 // ----------------------------------------------------------------------------
154 // This macro generates a new array class. It is intended for storage of simple
155 // types of sizeof()<=sizeof(long) or pointers if sizeof(pointer)<=sizeof(long)
157 // NB: it has only inline functions => takes no space at all
158 // ----------------------------------------------------------------------------
159 #define _WX_DEFINE_ARRAY(T, name) \
160 typedef int (CMPFUNC_CONV *CMPFUNC##T)(T *pItem1, T *pItem2); \
161 class name : public wxBaseArray \
165 { wxASSERT( sizeof(T) <= sizeof(long) ); } \
167 name& operator=(const name& src) \
168 { ((wxBaseArray *)this)->operator=((const wxBaseArray&)src); \
171 T& operator[](uint uiIndex) const \
172 { return (T&)(wxBaseArray::Item(uiIndex)); } \
173 T& Item(uint uiIndex) const \
174 { return (T&)(wxBaseArray::Item(uiIndex)); } \
176 int Index(T Item, bool bFromEnd = FALSE) const \
177 { return wxBaseArray::Index((long)Item, bFromEnd); } \
180 { wxBaseArray::Add((long)Item); } \
181 void Insert(T Item, uint uiIndex) \
182 { wxBaseArray::Insert((long)Item, uiIndex) ; } \
184 void Remove(uint uiIndex) { wxBaseArray::Remove(uiIndex); } \
185 void Remove(T Item) \
186 { int iIndex = Index(Item); \
187 wxCHECK2_MSG( iIndex != NOT_FOUND, return, \
188 "removing inexisting element in wxArray::Remove" ); \
189 wxBaseArray::Remove((uint)iIndex); } \
191 void Sort(CMPFUNC##T fCmp) { wxBaseArray::Sort((CMPFUNC)fCmp); } \
194 // ----------------------------------------------------------------------------
195 // see WX_DECLARE_LIST and WX_DEFINE_LIST
196 // ----------------------------------------------------------------------------
197 #define _WX_DECLARE_LIST(T, name) \
198 typedef int (CMPFUNC_CONV *CMPFUNC##T)(T** pItem1, T** pItem2); \
199 class name : public wxBaseArray \
203 name(const name& src); \
204 name& operator=(const name& src); \
208 T& operator[](uint uiIndex) const \
209 { return *(T*)wxBaseArray::Item(uiIndex); } \
210 T& Item(uint uiIndex) const \
211 { return *(T*)wxBaseArray::Item(uiIndex); } \
213 int Index(const T& Item, bool bFromEnd = FALSE) const; \
215 void Add(const T& Item); \
216 void Add(const T* pItem) \
217 { wxBaseArray::Add((long)pItem); } \
219 void Insert(const T& Item, uint uiIndex); \
220 void Insert(const T* pItem, uint uiIndex) \
221 { wxBaseArray::Insert((long)pItem, uiIndex); } \
225 T* Detach(uint uiIndex) \
226 { T* p = (T*)wxBaseArray::Item(uiIndex); \
227 wxBaseArray::Remove(uiIndex); return p; } \
228 void Remove(uint uiIndex); \
230 void Sort(CMPFUNC##T fCmp) { wxBaseArray::Sort((CMPFUNC)fCmp); } \
233 void DoCopy(const name& src); \
236 // ----------------------------------------------------------------------------
237 /** @name Macros for definition of dynamic arrays and lists
239 These macros are ugly (especially if you look in the sources ;-), but they
240 allow us to define 'template' classes without actually using templates.
243 Range checking is performed in debug build for both arrays and lists. Type
244 checking is done at compile-time. Warning: arrays <I>never</I> shrink, they
245 only grow, so loading 10 millions in an array only to delete them 2 lines
246 below is <I>not</I> recommended. However, it does free memory when it's
247 destroyed, so if you destroy array also, it's ok.
249 // ----------------------------------------------------------------------------
253 This macro generates a new array class. It is intended for storage of simple
254 types of sizeof()<=sizeof(long) or pointers if sizeof(pointer)<=sizeof(long)
256 NB: it has only inline functions => takes no space at all
259 @memo declare and define array class 'name' containing elements of type 'T'
261 #define WX_DEFINE_ARRAY(T, name) typedef T _A##name; \
262 _WX_DEFINE_ARRAY(_A##name, name)
264 This macro generates a new list class which owns the objects it contains,
265 i.e. it will delete them when it is destroyed. An element is of type T*,
266 but arguments of type T& are taken (see below!) and T& is returned.
268 Don't use this for simple types such as "int" or "long"!
269 You _may_ use it for "double" but it's awfully inefficient.
272 Note on Add/Insert functions:
274 1) function(T*) gives the object to the list, i.e. it will delete the
275 object when it's removed or in the list's dtor
277 2) function(T&) will create a copy of the object and work with it
282 1) Remove() will delete the object after removing it from the list
284 2) Detach() just removes the object from the list (returning pointer to it)
287 NB1: Base type T should have an accessible copy ctor if Add(T&) is used,
289 NB2: Never ever cast a list to it's base type: as dtor is <B>not</B> virtual
290 it will provoke memory leaks
293 some functions of this class are not inline, so it takes some space to
294 define new class from this template.
296 @memo declare list class 'name' containing elements of type 'T'
298 #define WX_DECLARE_LIST(T, name) typedef T _L##name; \
299 _WX_DECLARE_LIST(_L##name, name)
301 To use a list class you must
303 <li>#include "dynarray.h"
304 <li>DECLARE_LIST(element_type, list_class_name)
305 <li>#include "listimpl.cpp"
306 <li>DEFINE_LIST(list_class_name) // same as above!
309 This is necessary because at the moment of DEFINE_LIST class element_type
310 must be fully defined (i.e. forward declaration is not enough), while
311 DECLARE_LIST may be done anywhere. The separation of two allows to break
312 cicrcular dependencies with classes which have member variables of list
315 @memo define (must include listimpl.cpp!) list class 'name'
317 #define WX_DEFINE_LIST(name) "don't forget to include listimpl.cpp!"
320 // ----------------------------------------------------------------------------
321 /** @name Some commonly used predefined arrays */
322 // # overhead if not used?
323 // ----------------------------------------------------------------------------
326 /** @name ArrayInt */
327 WX_DEFINE_ARRAY(int, wxArrayInt
);
328 /** @name ArrayLong */
329 WX_DEFINE_ARRAY(long, wxArrayLong
);
330 /** @name ArrayPtrVoid */
331 WX_DEFINE_ARRAY(void *, wxArrayPtrVoid
);
336 #endif // _DYNARRAY_H