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