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