]> git.saurik.com Git - wxWidgets.git/blob - include/wx/dynarray.h
wxCALLBACK added, wxCOMPFUNC_CONV moved to defs.h
[wxWidgets.git] / include / wx / dynarray.h
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: dynarray.h
3 // Purpose: auto-resizable (i.e. dynamic) array support
4 // Author: Vadim Zeitlin
5 // Modified by:
6 // Created: 12.09.97
7 // RCS-ID: $Id$
8 // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows license
10 ///////////////////////////////////////////////////////////////////////////////
11
12 #ifndef _DYNARRAY_H
13 #define _DYNARRAY_H
14
15 #ifdef __GNUG__
16 #pragma interface "dynarray.h"
17 #endif
18
19 #include "wx/defs.h"
20 #include "wx/debug.h"
21
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)
24 */
25 //@{
26
27 // ----------------------------------------------------------------------------
28 // constants
29 // ----------------------------------------------------------------------------
30
31 /**
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
35 */
36 #define WX_ARRAY_DEFAULT_INITIAL_SIZE (16)
37
38 // ----------------------------------------------------------------------------
39 // types
40 // ----------------------------------------------------------------------------
41
42 /**
43 callback compare function for quick sort
44 must return negative value, 0 or positive value if pItem1 <, = or > pItem2
45 */
46 typedef int (wxCMPFUNC_CONV *CMPFUNC)(const void* pItem1, const void* pItem2);
47
48 // ----------------------------------------------------------------------------
49 /**
50 base class managing data having size of type 'long' (not used directly)
51
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)
57
58 @memo Base class for template array classes
59 */
60 // ----------------------------------------------------------------------------
61 class WXDLLEXPORT wxBaseArray
62 {
63 public:
64 /** @name ctors and dtor */
65 //@{
66 /// default ctor
67 wxBaseArray();
68 /// copy ctor
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...
74 #ifdef __GNUG__
75 virtual
76 #endif
77 ~wxBaseArray();
78 //@}
79
80 /** @name memory management */
81 //@{
82 /// empties the array, but doesn't release memory
83 void Empty() { m_nCount = 0; }
84 /// empties the array and releases memory
85 void Clear();
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)
89 void Shrink();
90 //@}
91
92 /** @name simple accessors */
93 //@{
94 /// number of elements in the array
95 size_t Count() const { return m_nCount; }
96 size_t GetCount() const { return m_nCount; }
97 /// is it empty?
98 bool IsEmpty() const { return m_nCount == 0; }
99 //@}
100
101 protected:
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
104 // type safe methods
105
106 /** @name items access */
107 //@{
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]; }
111 /// same as Item()
112 long& operator[](size_t uiIndex) const { return Item(uiIndex); }
113 //@}
114
115 /** @name item management */
116 //@{
117 /**
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
121 @see wxNOT_FOUND
122 */
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);
136 //@}
137
138 /// sort array elements using given compare function
139 void Sort(CMPFUNC fnCompare);
140
141 private:
142 void Grow(); // makes array bigger if needed
143
144 size_t m_nSize, // current size of the array
145 m_nCount; // current number of elements
146
147 long *m_pItems; // pointer to data
148 };
149
150 // ============================================================================
151 // template classes
152 // ============================================================================
153
154 // ----------------------------------------------------------------------------
155 // This macro generates a new array class. It is intended for storage of simple
156 // types of sizeof()<=sizeof(long) or pointers if sizeof(pointer)<=sizeof(long)
157 //
158 // NB: it has only inline functions => takes no space at all
159 // Mod by JACS: Salford C++ doesn't like 'var->operator=' syntax, as in:
160 // { ((wxBaseArray *)this)->operator=((const wxBaseArray&)src);
161 // so using a temporary variable instead.
162 // ----------------------------------------------------------------------------
163 #define _WX_DEFINE_ARRAY(T, name) \
164 typedef int (CMPFUNC_CONV *CMPFUNC##T)(T *pItem1, T *pItem2); \
165 class WXDLLEXPORT name : public wxBaseArray \
166 { \
167 public: \
168 name() \
169 { \
170 size_t type = sizeof(T); \
171 size_t sizelong = sizeof(long); \
172 if ( type > sizelong ) \
173 { wxFAIL_MSG( _T("illegal use of DEFINE_ARRAY") ); } \
174 } \
175 \
176 name& operator=(const name& src) \
177 { wxBaseArray* temp = (wxBaseArray*) this; \
178 (*temp) = ((const wxBaseArray&)src); \
179 return *this; } \
180 \
181 T& operator[](size_t uiIndex) const \
182 { return (T&)(wxBaseArray::Item(uiIndex)); } \
183 T& Item(size_t uiIndex) const \
184 { return (T&)(wxBaseArray::Item(uiIndex)); } \
185 T& Last() const \
186 { return (T&)(wxBaseArray::Item(Count() - 1)); } \
187 \
188 int Index(T Item, bool bFromEnd = FALSE) const \
189 { return wxBaseArray::Index((long)Item, bFromEnd); } \
190 \
191 void Add(T Item) \
192 { wxBaseArray::Add((long)Item); } \
193 void Insert(T Item, size_t uiIndex) \
194 { wxBaseArray::Insert((long)Item, uiIndex) ; } \
195 \
196 void Remove(size_t uiIndex) { wxBaseArray::Remove(uiIndex); } \
197 void Remove(T Item) \
198 { int iIndex = Index(Item); \
199 wxCHECK2_MSG( iIndex != wxNOT_FOUND, return, \
200 _T("removing inexisting element in wxArray::Remove") ); \
201 wxBaseArray::Remove((size_t)iIndex); } \
202 \
203 void Sort(CMPFUNC##T fCmp) { wxBaseArray::Sort((CMPFUNC)fCmp); } \
204 }
205
206 // ----------------------------------------------------------------------------
207 // This is the same as the previous macro, but it defines a sorted array.
208 // Differences:
209 // 1) it must be given a COMPARE function in ctor which takes 2 items of type
210 // T* and should return -1, 0 or +1 if the first one is less/greater
211 // than/equal to the second one.
212 // 2) the Add() method inserts the item in such was that the array is always
213 // sorted (it uses the COMPARE function)
214 // 3) it has no Sort() method because it's always sorted
215 // 4) Index() method is much faster (the sorted arrays use binary search
216 // instead of linear one), but Add() is slower.
217 //
218 // Summary: use this class when the speed of Index() function is important, use
219 // the normal arrays otherwise.
220 //
221 // NB: it has only inline functions => takes no space at all
222 // Mod by JACS: Salford C++ doesn't like 'var->operator=' syntax, as in:
223 // { ((wxBaseArray *)this)->operator=((const wxBaseArray&)src);
224 // so using a temporary variable instead.
225 // ----------------------------------------------------------------------------
226 #define _WX_DEFINE_SORTED_ARRAY(T, name) \
227 typedef int (CMPFUNC_CONV *SCMPFUNC##T)(T pItem1, T pItem2); \
228 class WXDLLEXPORT name : public wxBaseArray \
229 { \
230 public: \
231 name(SCMPFUNC##T fn) \
232 { size_t type = sizeof(T); \
233 size_t sizelong = sizeof(long); \
234 if ( type > sizelong ) \
235 { wxFAIL_MSG( _T("illegal use of DEFINE_ARRAY") ); } \
236 m_fnCompare = fn; \
237 } \
238 \
239 name& operator=(const name& src) \
240 { wxBaseArray* temp = (wxBaseArray*) this; \
241 (*temp) = ((const wxBaseArray&)src); \
242 m_fnCompare = src.m_fnCompare; \
243 return *this; } \
244 \
245 T& operator[](size_t uiIndex) const \
246 { return (T&)(wxBaseArray::Item(uiIndex)); } \
247 T& Item(size_t uiIndex) const \
248 { return (T&)(wxBaseArray::Item(uiIndex)); } \
249 T& Last() const \
250 { return (T&)(wxBaseArray::Item(Count() - 1)); } \
251 \
252 int Index(T Item) const \
253 { return wxBaseArray::Index((long)Item, (CMPFUNC)m_fnCompare); }\
254 \
255 void Add(T Item) \
256 { wxBaseArray::Add((long)Item, (CMPFUNC)m_fnCompare); } \
257 \
258 void Remove(size_t uiIndex) { wxBaseArray::Remove(uiIndex); } \
259 void Remove(T Item) \
260 { int iIndex = Index(Item); \
261 wxCHECK2_MSG( iIndex != wxNOT_FOUND, return, \
262 _T("removing inexisting element in wxArray::Remove") ); \
263 wxBaseArray::Remove((size_t)iIndex); } \
264 \
265 private: \
266 SCMPFUNC##T m_fnCompare; \
267 }
268
269 // ----------------------------------------------------------------------------
270 // see WX_DECLARE_OBJARRAY and WX_DEFINE_OBJARRAY
271 // ----------------------------------------------------------------------------
272 #define _WX_DECLARE_OBJARRAY(T, name) \
273 typedef int (CMPFUNC_CONV *CMPFUNC##T)(T** pItem1, T** pItem2); \
274 class WXDLLEXPORT name : public wxBaseArray \
275 { \
276 public: \
277 name() { } \
278 name(const name& src); \
279 name& operator=(const name& src); \
280 \
281 ~name(); \
282 \
283 T& operator[](size_t uiIndex) const \
284 { return *(T*)wxBaseArray::Item(uiIndex); } \
285 T& Item(size_t uiIndex) const \
286 { return *(T*)wxBaseArray::Item(uiIndex); } \
287 T& Last() const \
288 { return *(T*)(wxBaseArray::Item(Count() - 1)); } \
289 \
290 int Index(const T& Item, bool bFromEnd = FALSE) const; \
291 \
292 void Add(const T& Item); \
293 void Add(const T* pItem) \
294 { wxBaseArray::Add((long)pItem); } \
295 \
296 void Insert(const T& Item, size_t uiIndex); \
297 void Insert(const T* pItem, size_t uiIndex) \
298 { wxBaseArray::Insert((long)pItem, uiIndex); } \
299 \
300 void Empty(); \
301 \
302 T* Detach(size_t uiIndex) \
303 { T* p = (T*)wxBaseArray::Item(uiIndex); \
304 wxBaseArray::Remove(uiIndex); return p; } \
305 void Remove(size_t uiIndex); \
306 \
307 void Sort(CMPFUNC##T fCmp) { wxBaseArray::Sort((CMPFUNC)fCmp); } \
308 \
309 private: \
310 void DoCopy(const name& src); \
311 }
312
313 // ----------------------------------------------------------------------------
314 /** @name Macros for definition of dynamic arrays and objarrays
315
316 These macros are ugly (especially if you look in the sources ;-), but they
317 allow us to define 'template' classes without actually using templates.
318 <BR>
319 <BR>
320 Range checking is performed in debug build for both arrays and objarrays.
321 Type checking is done at compile-time. Warning: arrays <I>never</I> shrink,
322 they only grow, so loading 10 millions in an array only to delete them 2
323 lines below is <I>not</I> recommended. However, it does free memory when
324 it's destroyed, so if you destroy array also, it's ok.
325 */
326 // ----------------------------------------------------------------------------
327
328 //@{
329 /**
330 This macro generates a new array class. It is intended for storage of simple
331 types of sizeof()<=sizeof(long) or pointers if sizeof(pointer)<=sizeof(long)
332 <BR>
333 NB: it has only inline functions => takes no space at all
334 <BR>
335
336 @memo declare and define array class 'name' containing elements of type 'T'
337 */
338 #define WX_DEFINE_ARRAY(T, name) typedef T _A##name; \
339 _WX_DEFINE_ARRAY(_A##name, name)
340
341 /**
342 This macro does the same as WX_DEFINE_ARRAY except that the array will be
343 sorted with the specified compare function.
344 */
345 #define WX_DEFINE_SORTED_ARRAY(T, name) typedef T _A##name; \
346 _WX_DEFINE_SORTED_ARRAY(_A##name, name)
347
348 /**
349 This macro generates a new objarrays class which owns the objects it
350 contains, i.e. it will delete them when it is destroyed. An element is of
351 type T*, but arguments of type T& are taken (see below!) and T& is
352 returned. <BR>
353 Don't use this for simple types such as "int" or "long"!
354 You _may_ use it for "double" but it's awfully inefficient.
355 <BR>
356 <BR>
357 Note on Add/Insert functions:
358 <BR>
359 1) function(T*) gives the object to the array, i.e. it will delete the
360 object when it's removed or in the array's dtor
361 <BR>
362 2) function(T&) will create a copy of the object and work with it
363 <BR>
364 <BR>
365 Also:
366 <BR>
367 1) Remove() will delete the object after removing it from the array
368 <BR>
369 2) Detach() just removes the object from the array (returning pointer to it)
370 <BR>
371 <BR>
372 NB1: Base type T should have an accessible copy ctor if Add(T&) is used,
373 <BR>
374 NB2: Never ever cast a array to it's base type: as dtor is <B>not</B> virtual
375 it will provoke memory leaks
376 <BR>
377 <BR>
378 some functions of this class are not inline, so it takes some space to
379 define new class from this template.
380
381 @memo declare objarray class 'name' containing elements of type 'T'
382 */
383 #define WX_DECLARE_OBJARRAY(T, name) typedef T _L##name; \
384 _WX_DECLARE_OBJARRAY(_L##name, name)
385 /**
386 To use an objarray class you must
387 <ll>
388 <li>#include "dynarray.h"
389 <li>WX_DECLARE_OBJARRAY(element_type, list_class_name)
390 <li>#include "arrimpl.cpp"
391 <li>WX_DEFINE_OBJARRAY(list_class_name) // same as above!
392 </ll>
393 <BR><BR>
394 This is necessary because at the moment of DEFINE_OBJARRAY class
395 element_type must be fully defined (i.e. forward declaration is not
396 enough), while WX_DECLARE_OBJARRAY may be done anywhere. The separation of
397 two allows to break cicrcular dependencies with classes which have member
398 variables of objarray type.
399
400 @memo define (must include arrimpl.cpp!) objarray class 'name'
401 */
402 #define WX_DEFINE_OBJARRAY(name) "don't forget to include arrimpl.cpp!"
403 //@}
404
405 // ----------------------------------------------------------------------------
406 /** @name Some commonly used predefined arrays */
407 // # overhead if not used?
408 // ----------------------------------------------------------------------------
409
410 //@{
411 /** @name ArrayInt */
412 WX_DEFINE_ARRAY(int, wxArrayInt);
413 /** @name ArrayLong */
414 WX_DEFINE_ARRAY(long, wxArrayLong);
415 /** @name ArrayPtrVoid */
416 WX_DEFINE_ARRAY(void *, wxArrayPtrVoid);
417 //@}
418
419 //@}
420
421 // -----------------------------------------------------------------------------
422 // convinience macros
423 // -----------------------------------------------------------------------------
424
425 // delete all array elements
426 //
427 // NB: the class declaration of the array elements must be visible from the
428 // place where you use this macro, otherwise the proper destructor may not
429 // be called (a decent compiler should give a warning about it, but don't
430 // count on it)!
431 #define WX_CLEAR_ARRAY(array) \
432 { \
433 size_t count = array.Count(); \
434 for ( size_t n = 0; n < count; n++ ) \
435 { \
436 delete array[n]; \
437 } \
438 \
439 array.Empty(); \
440 }
441 #endif // _DYNARRAY_H
442