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
36 #define max(a, b) (((a) > (b)) ? (a) : (b))
39 // we cast the value to long from which we cast it to void * in IndexForInsert:
40 // this can't work if the pointers are not big enough
41 wxCOMPILE_TIME_ASSERT( sizeof(long) <= sizeof(void *),
42 wxArraySizeOfPtrLessSizeOfLong
); // < 32 symbols
44 // ============================================================================
46 // ============================================================================
48 // size increment = max(50% of current size, ARRAY_MAXSIZE_INCREMENT)
49 #define ARRAY_MAXSIZE_INCREMENT 4096
51 // ============================================================================
53 // ============================================================================
55 // ----------------------------------------------------------------------------
56 // wxBaseArray - dynamic array of 'T's
57 // ----------------------------------------------------------------------------
59 #define _WX_DEFINE_BASEARRAY(T, name) \
65 m_pItems = (T *)NULL; \
69 name::name(const name& src) \
71 m_nSize = /* not src.m_nSize to save memory */ \
72 m_nCount = src.m_nCount; \
74 if ( m_nSize != 0 ) { \
75 m_pItems = new T[m_nSize]; \
76 /* only copy if allocation succeeded */ \
78 memcpy(m_pItems, src.m_pItems, m_nCount*sizeof(T)); \
85 m_pItems = (T *) NULL; \
88 /* assignment operator */ \
89 name& name::operator=(const name& src) \
91 wxDELETEA(m_pItems); \
93 m_nSize = /* not src.m_nSize to save memory */ \
94 m_nCount = src.m_nCount; \
96 if ( m_nSize != 0 ){ \
97 m_pItems = new T[m_nSize]; \
98 /* only copy if allocation succeeded */ \
100 memcpy(m_pItems, src.m_pItems, m_nCount*sizeof(T)); \
107 m_pItems = (T *) NULL; \
112 /* grow the array */ \
113 void name::Grow(size_t nIncrement) \
115 /* only do it if no more place */ \
116 if( (m_nCount == m_nSize) || ((m_nSize - m_nCount) < nIncrement) ) { \
117 if( m_nSize == 0 ) { \
118 /* was empty, determine initial size */ \
119 size_t size = WX_ARRAY_DEFAULT_INITIAL_SIZE; \
120 if (size < nIncrement) size = nIncrement; \
121 /* allocate some memory */ \
122 m_pItems = new T[size]; \
123 /* only grow if allocation succeeded */ \
130 /* add at least 50% but not too much */ \
131 size_t ndefIncrement = m_nSize < WX_ARRAY_DEFAULT_INITIAL_SIZE \
132 ? WX_ARRAY_DEFAULT_INITIAL_SIZE : m_nSize >> 1; \
133 if ( ndefIncrement > ARRAY_MAXSIZE_INCREMENT ) \
134 ndefIncrement = ARRAY_MAXSIZE_INCREMENT; \
135 if ( nIncrement < ndefIncrement ) \
136 nIncrement = ndefIncrement; \
137 T *pNew = new T[m_nSize + nIncrement]; \
138 /* only grow if allocation succeeded */ \
140 m_nSize += nIncrement; \
141 /* copy data to new location */ \
142 memcpy(pNew, m_pItems, m_nCount*sizeof(T)); \
143 delete [] m_pItems; \
153 wxDELETEA(m_pItems); \
156 /* clears the list */ \
162 wxDELETEA(m_pItems); \
165 /* pre-allocates memory (frees the previous data!) */ \
166 void name::Alloc(size_t nSize) \
168 /* only if old buffer was not big enough */ \
169 if ( nSize > m_nSize ) { \
170 wxDELETEA(m_pItems); \
172 m_pItems = new T[nSize]; \
173 /* only alloc if allocation succeeded */ \
182 /* minimizes the memory usage by freeing unused memory */ \
183 void name::Shrink() \
185 /* only do it if we have some memory to free */ \
186 if( m_nCount < m_nSize ) { \
187 /* allocates exactly as much memory as we need */ \
188 T *pNew = new T[m_nCount]; \
189 /* only shrink if allocation succeeded */ \
191 /* copy data to new location */ \
192 memcpy(pNew, m_pItems, m_nCount*sizeof(T)); \
193 delete [] m_pItems; \
199 /* searches the array for an item (forward or backwards) */ \
200 int name::Index(T lItem, bool bFromEnd) const \
203 if ( m_nCount > 0 ) { \
204 size_t n = m_nCount; \
206 if ( m_pItems[--n] == lItem ) \
213 for( size_t n = 0; n < m_nCount; n++ ) { \
214 if( m_pItems[n] == lItem ) \
219 return wxNOT_FOUND; \
222 /* search for a place to insert item into sorted array (binary search) */ \
223 size_t name::IndexForInsert(T lItem, CMPFUNC fnCompare) const \
230 while ( lo < hi ) { \
233 res = (*fnCompare)((const void *)(long)lItem, \
234 (const void *)(long)(m_pItems[i])); \
237 else if ( res > 0 ) \
248 /* search for an item in a sorted array (binary search) */ \
249 int name::Index(T lItem, CMPFUNC fnCompare) const \
251 size_t n = IndexForInsert(lItem, fnCompare); \
253 return n < m_nCount && m_pItems[n] == lItem ? (int)n : wxNOT_FOUND; \
256 /* add item at the end */ \
257 void name::Add(T lItem, size_t nInsert) \
262 for (size_t i = 0; i < nInsert; i++) \
263 m_pItems[m_nCount++] = lItem; \
266 /* add item assuming the array is sorted with fnCompare function */ \
267 void name::Add(T lItem, CMPFUNC fnCompare) \
269 Insert(lItem, IndexForInsert(lItem, fnCompare)); \
272 /* add item at the given position */ \
273 void name::Insert(T lItem, size_t nIndex, size_t nInsert) \
275 wxCHECK_RET( nIndex <= m_nCount, wxT("bad index in wxArray::Insert") ); \
276 wxCHECK_RET( m_nCount <= m_nCount + nInsert, \
277 wxT("array size overflow in wxArray::Insert") ); \
283 memmove(&m_pItems[nIndex + nInsert], &m_pItems[nIndex], \
284 (m_nCount - nIndex)*sizeof(T)); \
285 for (size_t i = 0; i < nInsert; i++) \
286 m_pItems[nIndex + i] = lItem; \
287 m_nCount += nInsert; \
290 /* removes item from array (by index) */ \
291 void name::RemoveAt(size_t nIndex, size_t nRemove) \
293 wxCHECK_RET( nIndex < m_nCount, wxT("bad index in wxArray::RemoveAt") ); \
294 wxCHECK_RET( nIndex + nRemove <= m_nCount, \
295 wxT("removing too many elements in wxArray::RemoveAt") ); \
297 memmove(&m_pItems[nIndex], &m_pItems[nIndex + nRemove], \
298 (m_nCount - nIndex - nRemove)*sizeof(T)); \
299 m_nCount -= nRemove; \
302 /* removes item from array (by value) */ \
303 void name::Remove(T lItem) \
305 int iIndex = Index(lItem); \
307 wxCHECK_RET( iIndex != wxNOT_FOUND, \
308 wxT("removing inexistent item in wxArray::Remove") ); \
310 RemoveAt((size_t)iIndex); \
313 /* sort array elements using passed comparaison function */ \
314 void name::Sort(CMPFUNC fCmp) \
316 qsort(m_pItems, m_nCount, sizeof(T), fCmp); \
319 _WX_DEFINE_BASEARRAY(const void *, wxBaseArrayPtrVoid
)
320 _WX_DEFINE_BASEARRAY(short, wxBaseArrayShort
)
321 _WX_DEFINE_BASEARRAY(int, wxBaseArrayInt
)
322 _WX_DEFINE_BASEARRAY(long, wxBaseArrayLong
)
323 //_WX_DEFINE_BASEARRAY(double, wxBaseArrayDouble)