]>
git.saurik.com Git - wxWidgets.git/blob - src/common/dynarray.cpp
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 // ============================================================================
38 // ============================================================================
40 // size increment = max(50% of current size, ARRAY_MAXSIZE_INCREMENT)
41 #define ARRAY_MAXSIZE_INCREMENT 4096
43 // ============================================================================
45 // ============================================================================
47 // ----------------------------------------------------------------------------
48 // wxBaseArray - dynamic array of 'T's
49 // ----------------------------------------------------------------------------
51 #define _WX_DEFINE_BASEARRAY(T, name) \
57 m_pItems = (T *)NULL; \
61 name::name(const name& src) \
63 m_nSize = /* not src.m_nSize to save memory */ \
64 m_nCount = src.m_nCount; \
66 if ( m_nSize != 0 ) { \
67 m_pItems = new T[m_nSize]; \
68 /* only copy if allocation succeeded */ \
70 memcpy(m_pItems, src.m_pItems, m_nCount*sizeof(T)); \
77 m_pItems = (T *) NULL; \
80 /* assignment operator */ \
81 name& name::operator=(const name& src) \
83 wxDELETEA(m_pItems); \
85 m_nSize = /* not src.m_nSize to save memory */ \
86 m_nCount = src.m_nCount; \
88 if ( m_nSize != 0 ){ \
89 m_pItems = new T[m_nSize]; \
90 /* only copy if allocation succeeded */ \
92 memcpy(m_pItems, src.m_pItems, m_nCount*sizeof(T)); \
99 m_pItems = (T *) NULL; \
104 /* grow the array */ \
107 /* only do it if no more place */ \
108 if( m_nCount == m_nSize ) { \
109 if( m_nSize == 0 ) { \
110 /* was empty, alloc some memory */ \
111 m_pItems = new T[WX_ARRAY_DEFAULT_INITIAL_SIZE]; \
112 /* only grow if allocation succeeded */ \
114 m_nSize = WX_ARRAY_DEFAULT_INITIAL_SIZE; \
119 /* add 50% but not too much */ \
120 size_t nIncrement = m_nSize < WX_ARRAY_DEFAULT_INITIAL_SIZE \
121 ? WX_ARRAY_DEFAULT_INITIAL_SIZE : m_nSize >> 1; \
122 if ( nIncrement > ARRAY_MAXSIZE_INCREMENT ) \
123 nIncrement = ARRAY_MAXSIZE_INCREMENT; \
124 T *pNew = new T[m_nSize + nIncrement]; \
125 /* only grow if allocation succeeded */ \
127 m_nSize += nIncrement; \
128 /* copy data to new location */ \
129 memcpy(pNew, m_pItems, m_nCount*sizeof(T)); \
130 delete [] m_pItems; \
140 wxDELETEA(m_pItems); \
143 /* clears the list */ \
149 wxDELETEA(m_pItems); \
152 /* pre-allocates memory (frees the previous data!) */ \
153 void name::Alloc(size_t nSize) \
155 /* only if old buffer was not big enough */ \
156 if ( nSize > m_nSize ) { \
157 wxDELETEA(m_pItems); \
159 m_pItems = new T[nSize]; \
160 /* only alloc if allocation succeeded */ \
169 /* minimizes the memory usage by freeing unused memory */ \
170 void name::Shrink() \
172 /* only do it if we have some memory to free */ \
173 if( m_nCount < m_nSize ) { \
174 /* allocates exactly as much memory as we need */ \
175 T *pNew = new T[m_nCount]; \
176 /* only shrink if allocation succeeded */ \
178 /* copy data to new location */ \
179 memcpy(pNew, m_pItems, m_nCount*sizeof(T)); \
180 delete [] m_pItems; \
186 /* searches the array for an item (forward or backwards) */ \
187 int name::Index(T lItem, bool bFromEnd) const \
190 if ( m_nCount > 0 ) { \
191 size_t n = m_nCount; \
193 if ( m_pItems[--n] == lItem ) \
200 for( size_t n = 0; n < m_nCount; n++ ) { \
201 if( m_pItems[n] == lItem ) \
206 return wxNOT_FOUND; \
209 /* search for a place to insert item into sorted array (binary search) */ \
210 size_t name::IndexForInsert(T lItem, CMPFUNC fnCompare) const \
217 while ( lo < hi ) { \
220 res = (*fnCompare)((const void *)lItem, (const void *)(m_pItems[i])); \
223 else if ( res > 0 ) \
234 /* search for an item in a sorted array (binary search) */ \
235 int name::Index(T lItem, CMPFUNC fnCompare) const \
237 size_t n = IndexForInsert(lItem, fnCompare); \
239 return n < m_nCount && m_pItems[n] == lItem ? (int)n : wxNOT_FOUND; \
242 /* add item at the end */ \
243 void name::Add(T lItem) \
246 m_pItems[m_nCount++] = lItem; \
249 /* add item assuming the array is sorted with fnCompare function */ \
250 void name::Add(T lItem, CMPFUNC fnCompare) \
252 Insert(lItem, IndexForInsert(lItem, fnCompare)); \
255 /* add item at the given position */ \
256 void name::Insert(T lItem, size_t nIndex) \
258 wxCHECK_RET( nIndex <= m_nCount, wxT("bad index in wxArray::Insert") ); \
262 memmove(&m_pItems[nIndex + 1], &m_pItems[nIndex], \
263 (m_nCount - nIndex)*sizeof(T)); \
264 m_pItems[nIndex] = lItem; \
268 /* removes item from array (by index) */ \
269 void name::RemoveAt(size_t nIndex) \
271 wxCHECK_RET( nIndex <= m_nCount, wxT("bad index in wxArray::RemoveAt") ); \
273 memmove(&m_pItems[nIndex], &m_pItems[nIndex + 1], \
274 (m_nCount - nIndex - 1)*sizeof(T)); \
278 /* removes item from array (by value) */ \
279 void name::Remove(T lItem) \
281 int iIndex = Index(lItem); \
283 wxCHECK_RET( iIndex != wxNOT_FOUND, \
284 wxT("removing inexistent item in wxArray::Remove") ); \
286 RemoveAt((size_t)iIndex); \
289 /* sort array elements using passed comparaison function */ \
290 void name::Sort(CMPFUNC fCmp) \
292 qsort(m_pItems, m_nCount, sizeof(T), fCmp); \
295 _WX_DEFINE_BASEARRAY(const void *, wxBaseArrayPtrVoid
)
296 _WX_DEFINE_BASEARRAY(short, wxBaseArrayShort
)
297 _WX_DEFINE_BASEARRAY(int, wxBaseArrayInt
)
298 _WX_DEFINE_BASEARRAY(long, wxBaseArrayLong
)
299 //_WX_DEFINE_BASEARRAY(double, wxBaseArrayDouble)