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 license
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 /* grow the array */ \
110 void name::Grow(size_t nIncrement) \
112 /* only do it if no more place */ \
113 if( m_nCount == m_nSize ) { \
114 if( m_nSize == 0 ) { \
115 /* was empty, alloc some memory */ \
116 m_pItems = new T[WX_ARRAY_DEFAULT_INITIAL_SIZE]; \
117 /* only grow if allocation succeeded */ \
119 m_nSize = WX_ARRAY_DEFAULT_INITIAL_SIZE; \
124 /* add at least 50% but not too much */ \
125 size_t ndefIncrement = m_nSize < WX_ARRAY_DEFAULT_INITIAL_SIZE \
126 ? WX_ARRAY_DEFAULT_INITIAL_SIZE : m_nSize >> 1; \
127 if ( ndefIncrement > ARRAY_MAXSIZE_INCREMENT ) \
128 ndefIncrement = ARRAY_MAXSIZE_INCREMENT; \
129 if ( nIncrement < ndefIncrement ) \
130 nIncrement = ndefIncrement; \
131 T *pNew = new T[m_nSize + nIncrement]; \
132 /* only grow if allocation succeeded */ \
134 m_nSize += nIncrement; \
135 /* copy data to new location */ \
136 memcpy(pNew, m_pItems, m_nCount*sizeof(T)); \
137 delete [] m_pItems; \
147 wxDELETEA(m_pItems); \
150 /* clears the list */ \
156 wxDELETEA(m_pItems); \
159 /* pre-allocates memory (frees the previous data!) */ \
160 void name::Alloc(size_t nSize) \
162 /* only if old buffer was not big enough */ \
163 if ( nSize > m_nSize ) { \
164 wxDELETEA(m_pItems); \
166 m_pItems = new T[nSize]; \
167 /* only alloc if allocation succeeded */ \
176 /* minimizes the memory usage by freeing unused memory */ \
177 void name::Shrink() \
179 /* only do it if we have some memory to free */ \
180 if( m_nCount < m_nSize ) { \
181 /* allocates exactly as much memory as we need */ \
182 T *pNew = new T[m_nCount]; \
183 /* only shrink if allocation succeeded */ \
185 /* copy data to new location */ \
186 memcpy(pNew, m_pItems, m_nCount*sizeof(T)); \
187 delete [] m_pItems; \
193 /* searches the array for an item (forward or backwards) */ \
194 int name::Index(T lItem, bool bFromEnd) const \
197 if ( m_nCount > 0 ) { \
198 size_t n = m_nCount; \
200 if ( m_pItems[--n] == lItem ) \
207 for( size_t n = 0; n < m_nCount; n++ ) { \
208 if( m_pItems[n] == lItem ) \
213 return wxNOT_FOUND; \
216 /* search for a place to insert item into sorted array (binary search) */ \
217 size_t name::IndexForInsert(T lItem, CMPFUNC fnCompare) const \
224 while ( lo < hi ) { \
227 res = (*fnCompare)((const void *)(long)lItem, \
228 (const void *)(long)(m_pItems[i])); \
231 else if ( res > 0 ) \
242 /* search for an item in a sorted array (binary search) */ \
243 int name::Index(T lItem, CMPFUNC fnCompare) const \
245 size_t n = IndexForInsert(lItem, fnCompare); \
247 return n < m_nCount && m_pItems[n] == lItem ? (int)n : wxNOT_FOUND; \
250 /* add item at the end */ \
251 void name::Add(T lItem, size_t nInsert) \
254 for (size_t i = 0; i < nInsert; i++) \
255 m_pItems[m_nCount++] = lItem; \
258 /* add item assuming the array is sorted with fnCompare function */ \
259 void name::Add(T lItem, CMPFUNC fnCompare) \
261 Insert(lItem, IndexForInsert(lItem, fnCompare)); \
264 /* add item at the given position */ \
265 void name::Insert(T lItem, size_t nIndex, size_t nInsert) \
267 wxCHECK_RET( nIndex <= m_nCount, wxT("bad index in wxArray::Insert") ); \
268 wxCHECK_RET( m_nCount <= m_nCount + nInsert, \
269 wxT("array size overflow in wxArray::Insert") ); \
273 memmove(&m_pItems[nIndex + nInsert], &m_pItems[nIndex], \
274 (m_nCount - nIndex)*sizeof(T)); \
275 for (size_t i = 0; i < nInsert; i++) \
276 m_pItems[nIndex + i] = lItem; \
277 m_nCount += nInsert; \
280 /* removes item from array (by index) */ \
281 void name::RemoveAt(size_t nIndex, size_t nRemove) \
283 wxCHECK_RET( nIndex < m_nCount, wxT("bad index in wxArray::RemoveAt") ); \
284 wxCHECK_RET( nIndex + nRemove <= m_nCount, \
285 wxT("removing too many elements in wxArray::RemoveAt") ); \
287 memmove(&m_pItems[nIndex], &m_pItems[nIndex + nRemove], \
288 (m_nCount - nIndex - nRemove)*sizeof(T)); \
289 m_nCount -= nRemove; \
292 /* removes item from array (by value) */ \
293 void name::Remove(T lItem) \
295 int iIndex = Index(lItem); \
297 wxCHECK_RET( iIndex != wxNOT_FOUND, \
298 wxT("removing inexistent item in wxArray::Remove") ); \
300 RemoveAt((size_t)iIndex); \
303 /* sort array elements using passed comparaison function */ \
304 void name::Sort(CMPFUNC fCmp) \
306 qsort(m_pItems, m_nCount, sizeof(T), fCmp); \
309 _WX_DEFINE_BASEARRAY(const void *, wxBaseArrayPtrVoid
)
310 _WX_DEFINE_BASEARRAY(short, wxBaseArrayShort
)
311 _WX_DEFINE_BASEARRAY(int, wxBaseArrayInt
)
312 _WX_DEFINE_BASEARRAY(long, wxBaseArrayLong
)
313 //_WX_DEFINE_BASEARRAY(double, wxBaseArrayDouble)