]> git.saurik.com Git - wxWidgets.git/blame - include/wx/dynarray.h
Fixed Get...String functions.
[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"
3bfa4402 20#include "wx/debug.h"
c801d85f 21
3bfa4402 22/** @name Dynamic arrays and lists
c801d85f
KB
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/list grows when an element is added
3bfa4402 33 default value avoids allocate one or two bytes when the array is created
c801d85f
KB
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
3bfa4402 44 must return negative value, 0 or positive value if pItem1 <, = or > pItem2
c801d85f
KB
45 */
46
47#ifdef __VISUALC__
48 #define CMPFUNC_CONV _cdecl
49#else // !Visual C++
50 #define CMPFUNC_CONV
51#endif // compiler
52typedef int (CMPFUNC_CONV *CMPFUNC)(const void* pItem1, const void* pItem2);
53
54// ----------------------------------------------------------------------------
55/**
56 base class managing data having size of type 'long' (not used directly)
57
58 NB: for efficiency this often used class has no virtual functions (hence no
3bfa4402
VZ
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
c801d85f
KB
62 "SomeArray *" as "BaseArray *" and then delete it)
63
64 @memo Base class for template array and list classes
65*/
66// ----------------------------------------------------------------------------
67class wxBaseArray
68{
69public:
70 /** @name ctors and dtor */
71 //@{
72 /// default ctor
73 wxBaseArray();
74 /// copy ctor
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...
80#ifdef __GNUG__
81 virtual
82#endif
83 ~wxBaseArray();
84 //@}
85
86 /** @name memory management */
87 //@{
88 /// empties the list, but doesn't release memory
89 void Empty() { m_uiCount = 0; }
90 /// empties the list and releases memory
91 void Clear();
92 /// preallocates memory for given number of items
c86f1403 93 void Alloc(size_t uiSize);
c801d85f
KB
94 //@}
95
96 /** @name simple accessors */
97 //@{
98 /// number of elements in the array
c86f1403 99 size_t Count() const { return m_uiCount; }
c801d85f 100 /// is it empty?
bc7665d4 101 bool IsEmpty() const { return m_uiCount == 0; }
c801d85f
KB
102 //@}
103
104protected:
3bfa4402 105 // these methods are protected because if they were public one could
c801d85f
KB
106 // mistakenly call one of them instead of DEFINE_ARRAY's or LIST's
107 // type safe methods
108
109 /** @name items access */
110 //@{
111 /// get item at position uiIndex (range checking is done in debug version)
c86f1403 112 long& Item(size_t uiIndex) const
c801d85f
KB
113 { wxASSERT( uiIndex < m_uiCount ); return m_pItems[uiIndex]; }
114 /// same as Item()
c86f1403 115 long& operator[](size_t uiIndex) const { return Item(uiIndex); }
c801d85f
KB
116 //@}
117
118 /** @name item management */
119 //@{
120 /**
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
124 @see NOT_FOUND
125 */
3bfa4402
VZ
126 int Index(long lItem, bool bFromEnd = FALSE) const;
127 /// search for an item using binary search in a sorted array
e99c3048 128 int Index(long lItem, CMPFUNC fnCompare) const;
c801d85f 129 /// add new element at the end
3bfa4402
VZ
130 void Add(long lItem);
131 /// add item assuming the array is sorted with fnCompare function
132 void Add(long lItem, CMPFUNC fnCompare);
133 /// add new element at given position (it becomes Item[uiIndex])
c86f1403 134 void Insert(long lItem, size_t uiIndex);
c801d85f
KB
135 /// remove first item matching this value
136 void Remove(long lItem);
137 /// remove item by index
c86f1403 138 void Remove(size_t uiIndex);
c801d85f
KB
139 //@}
140
141 /// sort array elements using given compare function
3bfa4402 142 void Sort(CMPFUNC fnCompare);
c801d85f
KB
143
144private:
145 void Grow(); // makes array bigger if needed
146
c86f1403 147 size_t m_uiSize, // current size of the array
c801d85f
KB
148 m_uiCount; // current number of elements
149
150 long *m_pItems; // pointer to data
151};
152
153// ============================================================================
154// template classes
155// ============================================================================
156
157// ----------------------------------------------------------------------------
158// This macro generates a new array class. It is intended for storage of simple
159// types of sizeof()<=sizeof(long) or pointers if sizeof(pointer)<=sizeof(long)
3bfa4402 160//
c801d85f
KB
161// NB: it has only inline functions => takes no space at all
162// ----------------------------------------------------------------------------
163#define _WX_DEFINE_ARRAY(T, name) \
164typedef int (CMPFUNC_CONV *CMPFUNC##T)(T *pItem1, T *pItem2); \
165class name : public wxBaseArray \
166{ \
167public: \
168 name() \
169 { wxASSERT( sizeof(T) <= sizeof(long) ); } \
170 \
171 name& operator=(const name& src) \
172 { ((wxBaseArray *)this)->operator=((const wxBaseArray&)src); \
173 return *this; } \
174 \
c86f1403 175 T& operator[](size_t uiIndex) const \
c801d85f 176 { return (T&)(wxBaseArray::Item(uiIndex)); } \
c86f1403 177 T& Item(size_t uiIndex) const \
c801d85f 178 { return (T&)(wxBaseArray::Item(uiIndex)); } \
3bfaaefe
VZ
179 T& Last() const \
180 { return (T&)(wxBaseArray::Item(Count() - 1)); } \
c801d85f 181 \
bc7665d4 182 int Index(T Item, bool bFromEnd = FALSE) const \
c801d85f
KB
183 { return wxBaseArray::Index((long)Item, bFromEnd); } \
184 \
185 void Add(T Item) \
186 { wxBaseArray::Add((long)Item); } \
c86f1403 187 void Insert(T Item, size_t uiIndex) \
c801d85f
KB
188 { wxBaseArray::Insert((long)Item, uiIndex) ; } \
189 \
c86f1403 190 void Remove(size_t uiIndex) { wxBaseArray::Remove(uiIndex); } \
c801d85f
KB
191 void Remove(T Item) \
192 { int iIndex = Index(Item); \
1311c7a9
VZ
193 wxCHECK2_MSG( iIndex != NOT_FOUND, return, \
194 "removing inexisting element in wxArray::Remove" ); \
c86f1403 195 wxBaseArray::Remove((size_t)iIndex); } \
c801d85f
KB
196 \
197 void Sort(CMPFUNC##T fCmp) { wxBaseArray::Sort((CMPFUNC)fCmp); } \
198}
199
3bfa4402
VZ
200// ----------------------------------------------------------------------------
201// This is the same as the previous macro, but it defines a sorted array.
202// Differences:
203// 1) it must be given a COMPARE function in ctor which takes 2 items of type
204// T* and should return -1, 0 or +1 if the first one is less/greater
205// than/equal to the second one.
206// 2) the Add() method inserts the item in such was that the array is always
207// sorted (it uses the COMPARE function)
208// 3) it has no Sort() method because it's always sorted
209// 4) Index() method is much faster (the sorted arrays use binary search
210// instead of linear one), but Add() is slower.
211//
212// Summary: use this class when the speed of Index() function is important, use
213// the normal arrays otherwise.
214//
215// NB: it has only inline functions => takes no space at all
216// ----------------------------------------------------------------------------
217#define _WX_DEFINE_SORTED_ARRAY(T, name) \
218typedef int (CMPFUNC_CONV *SCMPFUNC##T)(T pItem1, T pItem2); \
219class name : public wxBaseArray \
220{ \
221public: \
222 name(SCMPFUNC##T fn) \
223 { wxASSERT( sizeof(T) <= sizeof(long) ); m_fnCompare = fn; } \
224 \
225 name& operator=(const name& src) \
226 { ((wxBaseArray *)this)->operator=((const wxBaseArray&)src); \
227 m_fnCompare = src.m_fnCompare; \
228 return *this; } \
229 \
c86f1403 230 T& operator[](size_t uiIndex) const \
3bfa4402 231 { return (T&)(wxBaseArray::Item(uiIndex)); } \
c86f1403 232 T& Item(size_t uiIndex) const \
3bfa4402
VZ
233 { return (T&)(wxBaseArray::Item(uiIndex)); } \
234 T& Last() const \
235 { return (T&)(wxBaseArray::Item(Count() - 1)); } \
236 \
237 int Index(T Item) const \
238 { return wxBaseArray::Index((long)Item, (CMPFUNC)m_fnCompare); }\
239 \
240 void Add(T Item) \
241 { wxBaseArray::Add((long)Item, (CMPFUNC)m_fnCompare); } \
242 \
c86f1403 243 void Remove(size_t uiIndex) { wxBaseArray::Remove(uiIndex); } \
3bfa4402
VZ
244 void Remove(T Item) \
245 { int iIndex = Index(Item); \
246 wxCHECK2_MSG( iIndex != NOT_FOUND, return, \
247 "removing inexisting element in wxArray::Remove" ); \
c86f1403 248 wxBaseArray::Remove((size_t)iIndex); } \
3bfa4402
VZ
249 \
250private: \
251 SCMPFUNC##T m_fnCompare; \
252}
253
c801d85f
KB
254// ----------------------------------------------------------------------------
255// see WX_DECLARE_LIST and WX_DEFINE_LIST
256// ----------------------------------------------------------------------------
257#define _WX_DECLARE_LIST(T, name) \
258typedef int (CMPFUNC_CONV *CMPFUNC##T)(T** pItem1, T** pItem2); \
259class name : public wxBaseArray \
260{ \
261public: \
262 name() { } \
263 name(const name& src); \
264 name& operator=(const name& src); \
265 \
266 ~name(); \
267 \
c86f1403 268 T& operator[](size_t uiIndex) const \
c801d85f 269 { return *(T*)wxBaseArray::Item(uiIndex); } \
c86f1403 270 T& Item(size_t uiIndex) const \
c801d85f 271 { return *(T*)wxBaseArray::Item(uiIndex); } \
3bfaaefe
VZ
272 T& Last() const \
273 { return *(T*)(wxBaseArray::Item(Count() - 1)); } \
c801d85f 274 \
bc7665d4 275 int Index(const T& Item, bool bFromEnd = FALSE) const; \
c801d85f
KB
276 \
277 void Add(const T& Item); \
278 void Add(const T* pItem) \
279 { wxBaseArray::Add((long)pItem); } \
280 \
c86f1403
VZ
281 void Insert(const T& Item, size_t uiIndex); \
282 void Insert(const T* pItem, size_t uiIndex) \
c801d85f
KB
283 { wxBaseArray::Insert((long)pItem, uiIndex); } \
284 \
285 void Empty(); \
286 \
c86f1403 287 T* Detach(size_t uiIndex) \
c801d85f
KB
288 { T* p = (T*)wxBaseArray::Item(uiIndex); \
289 wxBaseArray::Remove(uiIndex); return p; } \
c86f1403 290 void Remove(size_t uiIndex); \
c801d85f
KB
291 \
292 void Sort(CMPFUNC##T fCmp) { wxBaseArray::Sort((CMPFUNC)fCmp); } \
293 \
294private: \
295 void DoCopy(const name& src); \
296}
297
298// ----------------------------------------------------------------------------
3bfa4402 299/** @name Macros for definition of dynamic arrays and lists
c801d85f
KB
300
301 These macros are ugly (especially if you look in the sources ;-), but they
302 allow us to define 'template' classes without actually using templates.
303 <BR>
304 <BR>
305 Range checking is performed in debug build for both arrays and lists. Type
306 checking is done at compile-time. Warning: arrays <I>never</I> shrink, they
307 only grow, so loading 10 millions in an array only to delete them 2 lines
3bfa4402 308 below is <I>not</I> recommended. However, it does free memory when it's
c801d85f
KB
309 destroyed, so if you destroy array also, it's ok.
310 */
311// ----------------------------------------------------------------------------
312
313//@{
314 /**
315 This macro generates a new array class. It is intended for storage of simple
316 types of sizeof()<=sizeof(long) or pointers if sizeof(pointer)<=sizeof(long)
317 <BR>
318 NB: it has only inline functions => takes no space at all
319 <BR>
320
321 @memo declare and define array class 'name' containing elements of type 'T'
322 */
323#define WX_DEFINE_ARRAY(T, name) typedef T _A##name; \
324 _WX_DEFINE_ARRAY(_A##name, name)
3bfa4402
VZ
325
326 /**
327 This macro does the same as WX_DEFINE_ARRAY except that the array will be
328 sorted with the specified compare function.
329 */
330#define WX_DEFINE_SORTED_ARRAY(T, name) typedef T _A##name; \
331 _WX_DEFINE_SORTED_ARRAY(_A##name, name)
332
c801d85f
KB
333 /**
334 This macro generates a new list class which owns the objects it contains,
335 i.e. it will delete them when it is destroyed. An element is of type T*,
336 but arguments of type T& are taken (see below!) and T& is returned.
337 <BR>
338 Don't use this for simple types such as "int" or "long"!
339 You _may_ use it for "double" but it's awfully inefficient.
340 <BR>
341 <BR>
342 Note on Add/Insert functions:
343 <BR>
344 1) function(T*) gives the object to the list, i.e. it will delete the
345 object when it's removed or in the list's dtor
346 <BR>
347 2) function(T&) will create a copy of the object and work with it
348 <BR>
349 <BR>
350 Also:
351 <BR>
352 1) Remove() will delete the object after removing it from the list
353 <BR>
354 2) Detach() just removes the object from the list (returning pointer to it)
355 <BR>
356 <BR>
357 NB1: Base type T should have an accessible copy ctor if Add(T&) is used,
358 <BR>
3bfa4402 359 NB2: Never ever cast a list to it's base type: as dtor is <B>not</B> virtual
c801d85f
KB
360 it will provoke memory leaks
361 <BR>
362 <BR>
3bfa4402 363 some functions of this class are not inline, so it takes some space to
c801d85f
KB
364 define new class from this template.
365
366 @memo declare list class 'name' containing elements of type 'T'
367 */
368#define WX_DECLARE_LIST(T, name) typedef T _L##name; \
369 _WX_DECLARE_LIST(_L##name, name)
370 /**
371 To use a list class you must
372 <ll>
373 <li>#include "dynarray.h"
374 <li>DECLARE_LIST(element_type, list_class_name)
375 <li>#include "listimpl.cpp"
376 <li>DEFINE_LIST(list_class_name) // same as above!
377 </ll>
378 <BR><BR>
379 This is necessary because at the moment of DEFINE_LIST class element_type
380 must be fully defined (i.e. forward declaration is not enough), while
381 DECLARE_LIST may be done anywhere. The separation of two allows to break
3bfa4402 382 cicrcular dependencies with classes which have member variables of list
c801d85f
KB
383 type.
384
385 @memo define (must include listimpl.cpp!) list class 'name'
386 */
387#define WX_DEFINE_LIST(name) "don't forget to include listimpl.cpp!"
388//@}
389
390// ----------------------------------------------------------------------------
391/** @name Some commonly used predefined arrays */
392// # overhead if not used?
393// ----------------------------------------------------------------------------
394
395//@{
396 /** @name ArrayInt */
397WX_DEFINE_ARRAY(int, wxArrayInt);
398 /** @name ArrayLong */
399WX_DEFINE_ARRAY(long, wxArrayLong);
400 /** @name ArrayPtrVoid */
401WX_DEFINE_ARRAY(void *, wxArrayPtrVoid);
402//@}
403
404//@}
405
406#endif // _DYNARRAY_H
407