]> git.saurik.com Git - wxWidgets.git/blame - include/wx/dynarray.h
Generic dirctrl corrections
[wxWidgets.git] / include / wx / dynarray.h
CommitLineData
c801d85f
KB
1///////////////////////////////////////////////////////////////////////////////
2// Name: dynarray.h
3// Purpose: auto-resizable (i.e. dynamic) array support
4// Author: Vadim Zeitlin
3bfa4402 5// Modified by:
c801d85f
KB
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"
c801d85f 20
2600d8ee 21/** @name Dynamic arrays and object arrays (array which own their elements)
c801d85f
KB
22 @memo Arrays which grow on demand and do range checking (only in debug)
23 */
24//@{
25
26// ----------------------------------------------------------------------------
27// constants
28// ----------------------------------------------------------------------------
29
30/**
2600d8ee 31 the initial size by which an array grows when an element is added
3bfa4402 32 default value avoids allocate one or two bytes when the array is created
c801d85f
KB
33 which is rather inefficient
34*/
35#define WX_ARRAY_DEFAULT_INITIAL_SIZE (16)
36
37// ----------------------------------------------------------------------------
38// types
39// ----------------------------------------------------------------------------
40
41/**
42 callback compare function for quick sort
3bfa4402 43 must return negative value, 0 or positive value if pItem1 <, = or > pItem2
c801d85f 44 */
0661ec39 45typedef int (wxCMPFUNC_CONV *CMPFUNC)(const void* pItem1, const void* pItem2);
c801d85f
KB
46
47// ----------------------------------------------------------------------------
48/**
49 base class managing data having size of type 'long' (not used directly)
50
51 NB: for efficiency this often used class has no virtual functions (hence no
3bfa4402
VZ
52 VTBL), even dtor is <B>not</B> virtual. If used as expected it won't
53 create any problems because ARRAYs from DEFINE_ARRAY have no dtor at all,
54 so it's not too important if it's not called (this happens when you cast
c801d85f
KB
55 "SomeArray *" as "BaseArray *" and then delete it)
56
2600d8ee 57 @memo Base class for template array classes
c801d85f
KB
58*/
59// ----------------------------------------------------------------------------
fbcb4166 60class WXDLLEXPORT wxBaseArray
c801d85f
KB
61{
62public:
63 /** @name ctors and dtor */
64 //@{
65 /// default ctor
66 wxBaseArray();
67 /// copy ctor
68 wxBaseArray(const wxBaseArray& array);
69 /// assignment operator
70 wxBaseArray& operator=(const wxBaseArray& src);
71 /// not virtual, see above
72 /// EXCEPT for Gnu compiler to reduce warnings...
73#ifdef __GNUG__
74 virtual
75#endif
76 ~wxBaseArray();
77 //@}
78
79 /** @name memory management */
80 //@{
2600d8ee 81 /// empties the array, but doesn't release memory
3093cef8 82 void Empty() { m_nCount = 0; }
2600d8ee 83 /// empties the array and releases memory
c801d85f
KB
84 void Clear();
85 /// preallocates memory for given number of items
c86f1403 86 void Alloc(size_t uiSize);
3093cef8
VZ
87 /// minimizes the memory used by the array (frees unused memory)
88 void Shrink();
c801d85f
KB
89 //@}
90
91 /** @name simple accessors */
92 //@{
93 /// number of elements in the array
3093cef8
VZ
94 size_t Count() const { return m_nCount; }
95 size_t GetCount() const { return m_nCount; }
c801d85f 96 /// is it empty?
3093cef8 97 bool IsEmpty() const { return m_nCount == 0; }
c801d85f
KB
98 //@}
99
100protected:
3bfa4402 101 // these methods are protected because if they were public one could
2600d8ee 102 // mistakenly call one of them instead of DEFINE_ARRAY's or OBJARRAY's
c801d85f
KB
103 // type safe methods
104
105 /** @name items access */
106 //@{
107 /// get item at position uiIndex (range checking is done in debug version)
c86f1403 108 long& Item(size_t uiIndex) const
3093cef8 109 { wxASSERT( uiIndex < m_nCount ); return m_pItems[uiIndex]; }
c801d85f 110 /// same as Item()
c86f1403 111 long& operator[](size_t uiIndex) const { return Item(uiIndex); }
c801d85f
KB
112 //@}
113
114 /** @name item management */
115 //@{
116 /**
117 Search the element in the array, starting from the either side
118 @param bFromEnd if TRUE, start from the end
3c67202d
VZ
119 @return index of the first item matched or wxNOT_FOUND
120 @see wxNOT_FOUND
c801d85f 121 */
3bfa4402
VZ
122 int Index(long lItem, bool bFromEnd = FALSE) const;
123 /// search for an item using binary search in a sorted array
e99c3048 124 int Index(long lItem, CMPFUNC fnCompare) const;
c801d85f 125 /// add new element at the end
3bfa4402
VZ
126 void Add(long lItem);
127 /// add item assuming the array is sorted with fnCompare function
128 void Add(long lItem, CMPFUNC fnCompare);
129 /// add new element at given position (it becomes Item[uiIndex])
c86f1403 130 void Insert(long lItem, size_t uiIndex);
c801d85f
KB
131 /// remove first item matching this value
132 void Remove(long lItem);
133 /// remove item by index
8a729bb8 134 void RemoveAt(size_t uiIndex);
c801d85f
KB
135 //@}
136
137 /// sort array elements using given compare function
3bfa4402 138 void Sort(CMPFUNC fnCompare);
c801d85f
KB
139
140private:
141 void Grow(); // makes array bigger if needed
142
3093cef8
VZ
143 size_t m_nSize, // current size of the array
144 m_nCount; // current number of elements
c801d85f
KB
145
146 long *m_pItems; // pointer to data
147};
148
149// ============================================================================
150// template classes
151// ============================================================================
152
223d09f6
KB
153// resolves the name conflict between the wxT() macor and T typedef: we can't
154// use wxT() inside WX_DEFINE_ARRAY!
155#define _WX_ERROR_SIZEOF wxT("illegal use of DEFINE_ARRAY")
156#define _WX_ERROR_REMOVE wxT("removing inexisting element in wxArray::Remove")
e90c1d2a 157
c801d85f
KB
158// ----------------------------------------------------------------------------
159// This macro generates a new array class. It is intended for storage of simple
160// types of sizeof()<=sizeof(long) or pointers if sizeof(pointer)<=sizeof(long)
3bfa4402 161//
c801d85f 162// NB: it has only inline functions => takes no space at all
a3ef5bf5
JS
163// Mod by JACS: Salford C++ doesn't like 'var->operator=' syntax, as in:
164// { ((wxBaseArray *)this)->operator=((const wxBaseArray&)src);
165// so using a temporary variable instead.
c801d85f 166// ----------------------------------------------------------------------------
a497618a 167#define _WX_DEFINE_ARRAY(T, name, classexp) \
c801d85f 168typedef int (CMPFUNC_CONV *CMPFUNC##T)(T *pItem1, T *pItem2); \
a497618a 169classexp name : public wxBaseArray \
c801d85f
KB
170{ \
171public: \
172 name() \
e70f5e13
RR
173 { \
174 size_t type = sizeof(T); \
54da4255 175 size_t sizelong = sizeof(long); \
d422d01e 176 if ( type > sizelong ) \
e90c1d2a 177 { wxFAIL_MSG( _WX_ERROR_SIZEOF ); } \
54da4255 178 } \
c801d85f
KB
179 \
180 name& operator=(const name& src) \
a3ef5bf5
JS
181 { wxBaseArray* temp = (wxBaseArray*) this; \
182 (*temp) = ((const wxBaseArray&)src); \
c801d85f
KB
183 return *this; } \
184 \
3093cef8 185 T& operator[](size_t uiIndex) const \
c801d85f 186 { return (T&)(wxBaseArray::Item(uiIndex)); } \
3093cef8 187 T& Item(size_t uiIndex) const \
c801d85f 188 { return (T&)(wxBaseArray::Item(uiIndex)); } \
3bfaaefe
VZ
189 T& Last() const \
190 { return (T&)(wxBaseArray::Item(Count() - 1)); } \
c801d85f 191 \
bc7665d4 192 int Index(T Item, bool bFromEnd = FALSE) const \
c801d85f
KB
193 { return wxBaseArray::Index((long)Item, bFromEnd); } \
194 \
195 void Add(T Item) \
196 { wxBaseArray::Add((long)Item); } \
3093cef8 197 void Insert(T Item, size_t uiIndex) \
c801d85f
KB
198 { wxBaseArray::Insert((long)Item, uiIndex) ; } \
199 \
8a729bb8
VZ
200 void Remove(size_t uiIndex) { RemoveAt(uiIndex); } \
201 void RemoveAt(size_t uiIndex) { wxBaseArray::RemoveAt(uiIndex); } \
c801d85f
KB
202 void Remove(T Item) \
203 { int iIndex = Index(Item); \
9d2f3c71 204 wxCHECK2_MSG( iIndex != wxNOT_FOUND, return, \
e90c1d2a 205 _WX_ERROR_REMOVE); \
f373f197 206 wxBaseArray::RemoveAt((size_t)iIndex); } \
c801d85f
KB
207 \
208 void Sort(CMPFUNC##T fCmp) { wxBaseArray::Sort((CMPFUNC)fCmp); } \
209}
210
3bfa4402
VZ
211// ----------------------------------------------------------------------------
212// This is the same as the previous macro, but it defines a sorted array.
213// Differences:
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.
222//
223// Summary: use this class when the speed of Index() function is important, use
224// the normal arrays otherwise.
225//
226// NB: it has only inline functions => takes no space at all
a3ef5bf5
JS
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.
3bfa4402 230// ----------------------------------------------------------------------------
a497618a 231#define _WX_DEFINE_SORTED_ARRAY(T, name, classexp) \
3bfa4402 232typedef int (CMPFUNC_CONV *SCMPFUNC##T)(T pItem1, T pItem2); \
a497618a 233classexp name : public wxBaseArray \
3bfa4402
VZ
234{ \
235public: \
236 name(SCMPFUNC##T fn) \
54da4255
DW
237 { size_t type = sizeof(T); \
238 size_t sizelong = sizeof(long); \
d422d01e 239 if ( type > sizelong ) \
e90c1d2a 240 { wxFAIL_MSG( _WX_ERROR_SIZEOF ); } \
54da4255
DW
241 m_fnCompare = fn; \
242 } \
3bfa4402
VZ
243 \
244 name& operator=(const name& src) \
a3ef5bf5
JS
245 { wxBaseArray* temp = (wxBaseArray*) this; \
246 (*temp) = ((const wxBaseArray&)src); \
3bfa4402
VZ
247 m_fnCompare = src.m_fnCompare; \
248 return *this; } \
249 \
3093cef8 250 T& operator[](size_t uiIndex) const \
3bfa4402 251 { return (T&)(wxBaseArray::Item(uiIndex)); } \
3093cef8 252 T& Item(size_t uiIndex) const \
3bfa4402
VZ
253 { return (T&)(wxBaseArray::Item(uiIndex)); } \
254 T& Last() const \
255 { return (T&)(wxBaseArray::Item(Count() - 1)); } \
256 \
257 int Index(T Item) const \
258 { return wxBaseArray::Index((long)Item, (CMPFUNC)m_fnCompare); }\
259 \
260 void Add(T Item) \
261 { wxBaseArray::Add((long)Item, (CMPFUNC)m_fnCompare); } \
262 \
8a729bb8
VZ
263 void Remove(size_t uiIndex) { RemoveAt(uiIndex); } \
264 void RemoveAt(size_t uiIndex) { wxBaseArray::RemoveAt(uiIndex); } \
3bfa4402
VZ
265 void Remove(T Item) \
266 { int iIndex = Index(Item); \
9d2f3c71 267 wxCHECK2_MSG( iIndex != wxNOT_FOUND, return, \
e90c1d2a 268 _WX_ERROR_REMOVE ); \
f373f197 269 wxBaseArray::RemoveAt((size_t)iIndex); } \
3bfa4402
VZ
270 \
271private: \
272 SCMPFUNC##T m_fnCompare; \
273}
274
c801d85f 275// ----------------------------------------------------------------------------
2600d8ee 276// see WX_DECLARE_OBJARRAY and WX_DEFINE_OBJARRAY
c801d85f 277// ----------------------------------------------------------------------------
a497618a 278#define _WX_DECLARE_OBJARRAY(T, name, classexp) \
c801d85f 279typedef int (CMPFUNC_CONV *CMPFUNC##T)(T** pItem1, T** pItem2); \
a497618a 280classexp name : public wxBaseArray \
c801d85f
KB
281{ \
282public: \
283 name() { } \
284 name(const name& src); \
285 name& operator=(const name& src); \
286 \
287 ~name(); \
288 \
3093cef8 289 T& operator[](size_t uiIndex) const \
c801d85f 290 { return *(T*)wxBaseArray::Item(uiIndex); } \
3093cef8 291 T& Item(size_t uiIndex) const \
c801d85f 292 { return *(T*)wxBaseArray::Item(uiIndex); } \
3bfaaefe
VZ
293 T& Last() const \
294 { return *(T*)(wxBaseArray::Item(Count() - 1)); } \
c801d85f 295 \
bc7665d4 296 int Index(const T& Item, bool bFromEnd = FALSE) const; \
c801d85f
KB
297 \
298 void Add(const T& Item); \
299 void Add(const T* pItem) \
300 { wxBaseArray::Add((long)pItem); } \
301 \
3093cef8
VZ
302 void Insert(const T& Item, size_t uiIndex); \
303 void Insert(const T* pItem, size_t uiIndex) \
c801d85f
KB
304 { wxBaseArray::Insert((long)pItem, uiIndex); } \
305 \
f6bcfd97
BP
306 void Empty() { DoEmpty(); wxBaseArray::Empty(); } \
307 void Clear() { DoEmpty(); wxBaseArray::Clear(); } \
c801d85f 308 \
3093cef8 309 T* Detach(size_t uiIndex) \
c801d85f 310 { T* p = (T*)wxBaseArray::Item(uiIndex); \
32193feb 311 wxBaseArray::RemoveAt(uiIndex); return p; } \
8a729bb8
VZ
312 void Remove(size_t uiIndex) { RemoveAt(uiIndex); } \
313 void RemoveAt(size_t uiIndex); \
c801d85f
KB
314 \
315 void Sort(CMPFUNC##T fCmp) { wxBaseArray::Sort((CMPFUNC)fCmp); } \
316 \
317private: \
f6bcfd97 318 void DoEmpty(); \
c801d85f
KB
319 void DoCopy(const name& src); \
320}
321
322// ----------------------------------------------------------------------------
2600d8ee 323/** @name Macros for definition of dynamic arrays and objarrays
c801d85f
KB
324
325 These macros are ugly (especially if you look in the sources ;-), but they
326 allow us to define 'template' classes without actually using templates.
327 <BR>
328 <BR>
2600d8ee
VZ
329 Range checking is performed in debug build for both arrays and objarrays.
330 Type checking is done at compile-time. Warning: arrays <I>never</I> shrink,
331 they only grow, so loading 10 millions in an array only to delete them 2
332 lines below is <I>not</I> recommended. However, it does free memory when
333 it's destroyed, so if you destroy array also, it's ok.
c801d85f
KB
334 */
335// ----------------------------------------------------------------------------
336
337//@{
338 /**
339 This macro generates a new array class. It is intended for storage of simple
340 types of sizeof()<=sizeof(long) or pointers if sizeof(pointer)<=sizeof(long)
341 <BR>
342 NB: it has only inline functions => takes no space at all
343 <BR>
344
345 @memo declare and define array class 'name' containing elements of type 'T'
346 */
a497618a
VZ
347#define WX_DEFINE_ARRAY(T, name) \
348 typedef T _A##name; \
349 _WX_DEFINE_ARRAY(_A##name, name, class)
3bfa4402
VZ
350
351 /**
352 This macro does the same as WX_DEFINE_ARRAY except that the array will be
353 sorted with the specified compare function.
354 */
a497618a
VZ
355#define WX_DEFINE_SORTED_ARRAY(T, name) \
356 typedef T _A##name; \
357 _WX_DEFINE_SORTED_ARRAY(_A##name, name, class)
3bfa4402 358
c801d85f 359 /**
2600d8ee
VZ
360 This macro generates a new objarrays class which owns the objects it
361 contains, i.e. it will delete them when it is destroyed. An element is of
362 type T*, but arguments of type T& are taken (see below!) and T& is
363 returned. <BR>
c801d85f
KB
364 Don't use this for simple types such as "int" or "long"!
365 You _may_ use it for "double" but it's awfully inefficient.
366 <BR>
367 <BR>
368 Note on Add/Insert functions:
369 <BR>
2600d8ee
VZ
370 1) function(T*) gives the object to the array, i.e. it will delete the
371 object when it's removed or in the array's dtor
c801d85f
KB
372 <BR>
373 2) function(T&) will create a copy of the object and work with it
374 <BR>
375 <BR>
376 Also:
377 <BR>
2600d8ee 378 1) Remove() will delete the object after removing it from the array
c801d85f 379 <BR>
2600d8ee 380 2) Detach() just removes the object from the array (returning pointer to it)
c801d85f
KB
381 <BR>
382 <BR>
383 NB1: Base type T should have an accessible copy ctor if Add(T&) is used,
384 <BR>
2600d8ee 385 NB2: Never ever cast a array to it's base type: as dtor is <B>not</B> virtual
c801d85f
KB
386 it will provoke memory leaks
387 <BR>
388 <BR>
3bfa4402 389 some functions of this class are not inline, so it takes some space to
c801d85f
KB
390 define new class from this template.
391
2600d8ee 392 @memo declare objarray class 'name' containing elements of type 'T'
c801d85f 393 */
a497618a
VZ
394#define WX_DECLARE_OBJARRAY(T, name) \
395 typedef T _L##name; \
396 _WX_DECLARE_OBJARRAY(_L##name, name, class)
397
c801d85f 398 /**
2600d8ee 399 To use an objarray class you must
c801d85f
KB
400 <ll>
401 <li>#include "dynarray.h"
2600d8ee
VZ
402 <li>WX_DECLARE_OBJARRAY(element_type, list_class_name)
403 <li>#include "arrimpl.cpp"
404 <li>WX_DEFINE_OBJARRAY(list_class_name) // same as above!
c801d85f
KB
405 </ll>
406 <BR><BR>
2600d8ee
VZ
407 This is necessary because at the moment of DEFINE_OBJARRAY class
408 element_type must be fully defined (i.e. forward declaration is not
409 enough), while WX_DECLARE_OBJARRAY may be done anywhere. The separation of
410 two allows to break cicrcular dependencies with classes which have member
411 variables of objarray type.
c801d85f 412
2600d8ee 413 @memo define (must include arrimpl.cpp!) objarray class 'name'
c801d85f 414 */
2600d8ee 415#define WX_DEFINE_OBJARRAY(name) "don't forget to include arrimpl.cpp!"
c801d85f
KB
416//@}
417
a497618a
VZ
418// these macros do the same thing as the WX_XXX ones above, but should be used
419// inside the library for user visible classes because otherwise they wouldn't
420// be visible from outside (when using wxWindows as DLL under Windows)
421#define WX_DEFINE_EXPORTED_ARRAY(T, name) \
422 typedef T _A##name; \
423 _WX_DEFINE_ARRAY(_A##name, name, class WXDLLEXPORT)
424
425#define WX_DEFINE_SORTED_EXPORTED_ARRAY(T, name) \
426 typedef T _A##name; \
427 _WX_DEFINE_SORTED_ARRAY(_A##name, name, class WXDLLEXPORT)
428
429#define WX_DECLARE_EXPORTED_OBJARRAY(T, name) \
430 typedef T _L##name; \
431 _WX_DECLARE_OBJARRAY(_L##name, name, class WXDLLEXPORT)
432
c801d85f
KB
433// ----------------------------------------------------------------------------
434/** @name Some commonly used predefined arrays */
c801d85f
KB
435// ----------------------------------------------------------------------------
436
437//@{
438 /** @name ArrayInt */
a497618a 439WX_DEFINE_EXPORTED_ARRAY(int, wxArrayInt);
c801d85f 440 /** @name ArrayLong */
a497618a 441WX_DEFINE_EXPORTED_ARRAY(long, wxArrayLong);
c801d85f 442 /** @name ArrayPtrVoid */
a497618a 443WX_DEFINE_EXPORTED_ARRAY(void *, wxArrayPtrVoid);
c801d85f
KB
444//@}
445
446//@}
447
2b9bd418
VZ
448// -----------------------------------------------------------------------------
449// convinience macros
450// -----------------------------------------------------------------------------
451
4f6aed9c
VZ
452// append all element of one array to another one
453#define WX_APPEND_ARRAY(array, other) \
454 { \
455 size_t count = other.Count(); \
456 for ( size_t n = 0; n < count; n++ ) \
457 { \
458 array.Add(other[n]); \
459 } \
460 }
461
2b9bd418
VZ
462// delete all array elements
463//
464// NB: the class declaration of the array elements must be visible from the
465// place where you use this macro, otherwise the proper destructor may not
466// be called (a decent compiler should give a warning about it, but don't
467// count on it)!
468#define WX_CLEAR_ARRAY(array) \
469 { \
470 size_t count = array.Count(); \
471 for ( size_t n = 0; n < count; n++ ) \
472 { \
473 delete array[n]; \
474 } \
2c356747
VZ
475 \
476 array.Empty(); \
2b9bd418 477 }
a497618a 478
c801d85f
KB
479#endif // _DYNARRAY_H
480