]>
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 */ \
105 void name::Grow(size_t nIncrement) \
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 at least 50% but not too much */ \
120 size_t ndefIncrement = m_nSize < WX_ARRAY_DEFAULT_INITIAL_SIZE \
121 ? WX_ARRAY_DEFAULT_INITIAL_SIZE : m_nSize >> 1; \
122 if ( ndefIncrement > ARRAY_MAXSIZE_INCREMENT ) \
123 ndefIncrement = ARRAY_MAXSIZE_INCREMENT; \
124 if ( nIncrement < ndefIncrement ) \
125 nIncrement = ndefIncrement; \
126 T *pNew = new T[m_nSize + nIncrement]; \
127 /* only grow if allocation succeeded */ \
129 m_nSize += nIncrement; \
130 /* copy data to new location */ \
131 memcpy(pNew, m_pItems, m_nCount*sizeof(T)); \
132 delete [] m_pItems; \
142 wxDELETEA(m_pItems); \
145 /* clears the list */ \
151 wxDELETEA(m_pItems); \
154 /* pre-allocates memory (frees the previous data!) */ \
155 void name::Alloc(size_t nSize) \
157 /* only if old buffer was not big enough */ \
158 if ( nSize > m_nSize ) { \
159 wxDELETEA(m_pItems); \
161 m_pItems = new T[nSize]; \
162 /* only alloc if allocation succeeded */ \
171 /* minimizes the memory usage by freeing unused memory */ \
172 void name::Shrink() \
174 /* only do it if we have some memory to free */ \
175 if( m_nCount < m_nSize ) { \
176 /* allocates exactly as much memory as we need */ \
177 T *pNew = new T[m_nCount]; \
178 /* only shrink if allocation succeeded */ \
180 /* copy data to new location */ \
181 memcpy(pNew, m_pItems, m_nCount*sizeof(T)); \
182 delete [] m_pItems; \
188 /* searches the array for an item (forward or backwards) */ \
189 int name::Index(T lItem, bool bFromEnd) const \
192 if ( m_nCount > 0 ) { \
193 size_t n = m_nCount; \
195 if ( m_pItems[--n] == lItem ) \
202 for( size_t n = 0; n < m_nCount; n++ ) { \
203 if( m_pItems[n] == lItem ) \
208 return wxNOT_FOUND; \
211 /* search for a place to insert item into sorted array (binary search) */ \
212 size_t name::IndexForInsert(T lItem, CMPFUNC fnCompare) const \
219 while ( lo < hi ) { \
222 res = (*fnCompare)((const void *)lItem, (const void *)(m_pItems[i])); \
225 else if ( res > 0 ) \
236 /* search for an item in a sorted array (binary search) */ \
237 int name::Index(T lItem, CMPFUNC fnCompare) const \
239 size_t n = IndexForInsert(lItem, fnCompare); \
241 return n < m_nCount && m_pItems[n] == lItem ? (int)n : wxNOT_FOUND; \
244 /* add item at the end */ \
245 void name::Add(T lItem, size_t nInsert) \
248 for (size_t i = 0; i < nInsert; i++) \
249 m_pItems[m_nCount++] = lItem; \
252 /* add item assuming the array is sorted with fnCompare function */ \
253 void name::Add(T lItem, CMPFUNC fnCompare) \
255 Insert(lItem, IndexForInsert(lItem, fnCompare)); \
258 /* add item at the given position */ \
259 void name::Insert(T lItem, size_t nIndex, size_t nInsert) \
261 wxCHECK_RET( nIndex <= m_nCount, wxT("bad index in wxArray::Insert") ); \
262 wxCHECK_RET( m_nCount <= m_nCount + nInsert, \
263 wxT("array size overflow in wxArray::Insert") ); \
267 memmove(&m_pItems[nIndex + nInsert], &m_pItems[nIndex], \
268 (m_nCount - nIndex)*sizeof(T)); \
269 for (size_t i = 0; i < nInsert; i++) \
270 m_pItems[nIndex + i] = lItem; \
271 m_nCount += nInsert; \
274 /* removes item from array (by index) */ \
275 void name::RemoveAt(size_t nIndex, size_t nRemove) \
277 wxCHECK_RET( nIndex < m_nCount, wxT("bad index in wxArray::RemoveAt") ); \
278 wxCHECK_RET( nIndex + nRemove <= m_nCount, \
279 wxT("removing too many elements in wxArray::RemoveAt") ); \
281 memmove(&m_pItems[nIndex], &m_pItems[nIndex + nRemove], \
282 (m_nCount - nIndex - nRemove)*sizeof(T)); \
283 m_nCount -= nRemove; \
286 /* removes item from array (by value) */ \
287 void name::Remove(T lItem) \
289 int iIndex = Index(lItem); \
291 wxCHECK_RET( iIndex != wxNOT_FOUND, \
292 wxT("removing inexistent item in wxArray::Remove") ); \
294 RemoveAt((size_t)iIndex); \
297 /* sort array elements using passed comparaison function */ \
298 void name::Sort(CMPFUNC fCmp) \
300 qsort(m_pItems, m_nCount, sizeof(T), fCmp); \
303 _WX_DEFINE_BASEARRAY(const void *, wxBaseArrayPtrVoid
)
304 _WX_DEFINE_BASEARRAY(short, wxBaseArrayShort
)
305 _WX_DEFINE_BASEARRAY(int, wxBaseArrayInt
)
306 _WX_DEFINE_BASEARRAY(long, wxBaseArrayLong
)
307 //_WX_DEFINE_BASEARRAY(double, wxBaseArrayDouble)