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