1 ///////////////////////////////////////////////////////////////////////////////
3 // Purpose: implementation of wxBaseArray class
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
17 #pragma implementation "dynarray.h"
20 #include "wx/wxprec.h"
26 #include "wx/dynarray.h"
30 #include <string.h> // for memmove
33 #define max(a, b) (((a) > (b)) ? (a) : (b))
36 // we cast the value to long from which we cast it to void * in IndexForInsert:
37 // this can't work if the pointers are not big enough
38 wxCOMPILE_TIME_ASSERT( sizeof(long) <= sizeof(void *),
39 wxArraySizeOfPtrLessSizeOfLong
); // < 32 symbols
41 // ============================================================================
43 // ============================================================================
45 // size increment = max(50% of current size, ARRAY_MAXSIZE_INCREMENT)
46 #define ARRAY_MAXSIZE_INCREMENT 4096
48 // ============================================================================
50 // ============================================================================
52 // ----------------------------------------------------------------------------
53 // wxBaseArray - dynamic array of 'T's
54 // ----------------------------------------------------------------------------
56 #define _WX_DEFINE_BASEARRAY(T, name) \
62 m_pItems = (T *)NULL; \
66 name::name(const name& src) \
68 m_nSize = /* not src.m_nSize to save memory */ \
69 m_nCount = src.m_nCount; \
71 if ( m_nSize != 0 ) { \
72 m_pItems = new T[m_nSize]; \
73 /* only copy if allocation succeeded */ \
75 memcpy(m_pItems, src.m_pItems, m_nCount*sizeof(T)); \
82 m_pItems = (T *) NULL; \
85 /* assignment operator */ \
86 name& name::operator=(const name& src) \
88 wxDELETEA(m_pItems); \
90 m_nSize = /* not src.m_nSize to save memory */ \
91 m_nCount = src.m_nCount; \
93 if ( m_nSize != 0 ){ \
94 m_pItems = new T[m_nSize]; \
95 /* only copy if allocation succeeded */ \
97 memcpy(m_pItems, src.m_pItems, m_nCount*sizeof(T)); \
104 m_pItems = (T *) NULL; \
109 /* allocate new buffer of the given size and move our data to it */ \
110 bool name::Realloc(size_t nSize) \
112 T *pNew = new T[nSize]; \
113 /* only grow if allocation succeeded */ \
118 /* copy data to new location */ \
119 memcpy(pNew, m_pItems, m_nCount*sizeof(T)); \
120 delete [] m_pItems; \
126 /* grow the array */ \
127 void name::Grow(size_t nIncrement) \
129 /* only do it if no more place */ \
130 if( (m_nCount == m_nSize) || ((m_nSize - m_nCount) < nIncrement) ) { \
131 if( m_nSize == 0 ) { \
132 /* was empty, determine initial size */ \
133 size_t size = WX_ARRAY_DEFAULT_INITIAL_SIZE; \
134 if (size < nIncrement) size = nIncrement; \
135 /* allocate some memory */ \
136 m_pItems = new T[size]; \
137 /* only grow if allocation succeeded */ \
144 /* add at least 50% but not too much */ \
145 size_t ndefIncrement = m_nSize < WX_ARRAY_DEFAULT_INITIAL_SIZE \
146 ? WX_ARRAY_DEFAULT_INITIAL_SIZE : m_nSize >> 1; \
147 if ( ndefIncrement > ARRAY_MAXSIZE_INCREMENT ) \
148 ndefIncrement = ARRAY_MAXSIZE_INCREMENT; \
149 if ( nIncrement < ndefIncrement ) \
150 nIncrement = ndefIncrement; \
151 Realloc(m_nSize + nIncrement); \
156 /* make sure that the array has at least count elements */ \
157 void name::SetCount(size_t count, T defval) \
159 if ( m_nSize < count ) \
161 /* need to realloc memory: don't overallocate it here as if */ \
162 /* SetCount() is called, it probably means that the caller */ \
163 /* knows in advance how many elements there will be in the */ \
164 /* array and so it won't be necessary to realloc it later */ \
165 if ( !Realloc(count) ) \
167 /* out of memory -- what can we do? */ \
172 /* add new elements if we extend the array */ \
173 while ( m_nCount < count ) \
175 m_pItems[m_nCount++] = defval; \
182 wxDELETEA(m_pItems); \
185 /* clears the list */ \
191 wxDELETEA(m_pItems); \
194 /* pre-allocates memory (frees the previous data!) */ \
195 void name::Alloc(size_t nSize) \
197 /* only if old buffer was not big enough */ \
198 if ( nSize > m_nSize ) { \
199 wxDELETEA(m_pItems); \
201 m_pItems = new T[nSize]; \
202 /* only alloc if allocation succeeded */ \
211 /* minimizes the memory usage by freeing unused memory */ \
212 void name::Shrink() \
214 /* only do it if we have some memory to free */ \
215 if( m_nCount < m_nSize ) { \
216 /* allocates exactly as much memory as we need */ \
217 T *pNew = new T[m_nCount]; \
218 /* only shrink if allocation succeeded */ \
220 /* copy data to new location */ \
221 memcpy(pNew, m_pItems, m_nCount*sizeof(T)); \
222 delete [] m_pItems; \
225 /* update the size of the new block */ \
226 m_nSize = m_nCount; \
228 /* else: don't do anything, better keep old memory block! */ \
232 /* searches the array for an item (forward or backwards) */ \
233 int name::Index(T lItem, bool bFromEnd) const \
236 if ( m_nCount > 0 ) { \
237 size_t n = m_nCount; \
239 if ( m_pItems[--n] == lItem ) \
246 for( size_t n = 0; n < m_nCount; n++ ) { \
247 if( m_pItems[n] == lItem ) \
252 return wxNOT_FOUND; \
255 /* search for a place to insert item into sorted array (binary search) */ \
256 size_t name::IndexForInsert(T lItem, CMPFUNC fnCompare) const \
263 while ( lo < hi ) { \
266 res = (*fnCompare)((const void *)(long)lItem, \
267 (const void *)(long)(m_pItems[i])); \
270 else if ( res > 0 ) \
281 /* search for an item in a sorted array (binary search) */ \
282 int name::Index(T lItem, CMPFUNC fnCompare) const \
284 size_t n = IndexForInsert(lItem, fnCompare); \
286 return n < m_nCount && \
287 (*fnCompare)((const void *)(long)lItem, \
288 ((const void *)(long)m_pItems[n])) ? (int)n \
292 /* add item at the end */ \
293 void name::Add(T lItem, size_t nInsert) \
298 for (size_t i = 0; i < nInsert; i++) \
299 m_pItems[m_nCount++] = lItem; \
302 /* add item assuming the array is sorted with fnCompare function */ \
303 void name::Add(T lItem, CMPFUNC fnCompare) \
305 Insert(lItem, IndexForInsert(lItem, fnCompare)); \
308 /* add item at the given position */ \
309 void name::Insert(T lItem, size_t nIndex, size_t nInsert) \
311 wxCHECK_RET( nIndex <= m_nCount, wxT("bad index in wxArray::Insert") ); \
312 wxCHECK_RET( m_nCount <= m_nCount + nInsert, \
313 wxT("array size overflow in wxArray::Insert") ); \
319 memmove(&m_pItems[nIndex + nInsert], &m_pItems[nIndex], \
320 (m_nCount - nIndex)*sizeof(T)); \
321 for (size_t i = 0; i < nInsert; i++) \
322 m_pItems[nIndex + i] = lItem; \
323 m_nCount += nInsert; \
326 /* removes item from array (by index) */ \
327 void name::RemoveAt(size_t nIndex, size_t nRemove) \
329 wxCHECK_RET( nIndex < m_nCount, wxT("bad index in wxArray::RemoveAt") ); \
330 wxCHECK_RET( nIndex + nRemove <= m_nCount, \
331 wxT("removing too many elements in wxArray::RemoveAt") ); \
333 memmove(&m_pItems[nIndex], &m_pItems[nIndex + nRemove], \
334 (m_nCount - nIndex - nRemove)*sizeof(T)); \
335 m_nCount -= nRemove; \
338 /* removes item from array (by value) */ \
339 void name::Remove(T lItem) \
341 int iIndex = Index(lItem); \
343 wxCHECK_RET( iIndex != wxNOT_FOUND, \
344 wxT("removing inexistent item in wxArray::Remove") ); \
346 RemoveAt((size_t)iIndex); \
349 /* sort array elements using passed comparaison function */ \
350 void name::Sort(CMPFUNC fCmp) \
352 qsort(m_pItems, m_nCount, sizeof(T), fCmp); \
355 _WX_DEFINE_BASEARRAY(const void *, wxBaseArrayPtrVoid
)
356 _WX_DEFINE_BASEARRAY(short, wxBaseArrayShort
)
357 _WX_DEFINE_BASEARRAY(int, wxBaseArrayInt
)
358 _WX_DEFINE_BASEARRAY(long, wxBaseArrayLong
)
359 //_WX_DEFINE_BASEARRAY(double, wxBaseArrayDouble)