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