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_COMMON(T, name) \
57 /* searches the array for an item (forward or backwards) */ \
58 int name::Index(T lItem, bool bFromEnd) const \
64 if ( (*this)[--n] == lItem ) \
71 for( size_t n = 0; n < size(); n++ ) { \
72 if( (*this)[n] == lItem ) \
80 /* add item assuming the array is sorted with fnCompare function */ \
81 void name::Add(T lItem, CMPFUNC fnCompare) \
83 Insert(lItem, IndexForInsert(lItem, fnCompare)); \
89 #define _WX_DEFINE_BASEARRAY_NOCOMMON(T, name) \
90 size_t name::IndexForInsert(T lItem, CMPFUNC fnCompare) const \
92 Predicate p(fnCompare); \
93 const_iterator it = std::lower_bound(begin(), end(), lItem, p); \
94 return it - begin(); \
97 int name::Index(T lItem, CMPFUNC fnCompare) const \
99 size_t n = IndexForInsert(lItem, fnCompare); \
101 return (n >= size() || \
102 (*fnCompare)(&lItem, &(*this)[n])) ? wxNOT_FOUND : (int)n; \
105 void name::Shrink() \
111 #else // if !wxUSE_STL
113 #define _WX_DEFINE_BASEARRAY_NOCOMMON(T, name) \
119 m_pItems = (T *)NULL; \
123 name::name(const name& src) \
125 m_nSize = /* not src.m_nSize to save memory */ \
126 m_nCount = src.m_nCount; \
128 if ( m_nSize != 0 ) { \
129 m_pItems = new T[m_nSize]; \
130 /* only copy if allocation succeeded */ \
132 memcpy(m_pItems, src.m_pItems, m_nCount*sizeof(T)); \
139 m_pItems = (T *) NULL; \
142 /* assignment operator */ \
143 name& name::operator=(const name& src) \
145 wxDELETEA(m_pItems); \
147 m_nSize = /* not src.m_nSize to save memory */ \
148 m_nCount = src.m_nCount; \
150 if ( m_nSize != 0 ){ \
151 m_pItems = new T[m_nSize]; \
152 /* only copy if allocation succeeded */ \
154 memcpy(m_pItems, src.m_pItems, m_nCount*sizeof(T)); \
161 m_pItems = (T *) NULL; \
166 /* allocate new buffer of the given size and move our data to it */ \
167 bool name::Realloc(size_t nSize) \
169 T *pNew = new T[nSize]; \
170 /* only grow if allocation succeeded */ \
175 /* copy data to new location */ \
176 memcpy(pNew, m_pItems, m_nCount*sizeof(T)); \
177 delete [] m_pItems; \
183 /* grow the array */ \
184 void name::Grow(size_t nIncrement) \
186 /* only do it if no more place */ \
187 if( (m_nCount == m_nSize) || ((m_nSize - m_nCount) < nIncrement) ) { \
188 if( m_nSize == 0 ) { \
189 /* was empty, determine initial size */ \
190 size_t size = WX_ARRAY_DEFAULT_INITIAL_SIZE; \
191 if (size < nIncrement) size = nIncrement; \
192 /* allocate some memory */ \
193 m_pItems = new T[size]; \
194 /* only grow if allocation succeeded */ \
201 /* add at least 50% but not too much */ \
202 size_t ndefIncrement = m_nSize < WX_ARRAY_DEFAULT_INITIAL_SIZE \
203 ? WX_ARRAY_DEFAULT_INITIAL_SIZE : m_nSize >> 1; \
204 if ( ndefIncrement > ARRAY_MAXSIZE_INCREMENT ) \
205 ndefIncrement = ARRAY_MAXSIZE_INCREMENT; \
206 if ( nIncrement < ndefIncrement ) \
207 nIncrement = ndefIncrement; \
208 Realloc(m_nSize + nIncrement); \
213 /* make sure that the array has at least count elements */ \
214 void name::SetCount(size_t count, T defval) \
216 if ( m_nSize < count ) \
218 /* need to realloc memory: don't overallocate it here as if */ \
219 /* SetCount() is called, it probably means that the caller */ \
220 /* knows in advance how many elements there will be in the */ \
221 /* array and so it won't be necessary to realloc it later */ \
222 if ( !Realloc(count) ) \
224 /* out of memory -- what can we do? */ \
229 /* add new elements if we extend the array */ \
230 while ( m_nCount < count ) \
232 m_pItems[m_nCount++] = defval; \
239 wxDELETEA(m_pItems); \
242 /* clears the list */ \
248 wxDELETEA(m_pItems); \
251 /* pre-allocates memory (frees the previous data!) */ \
252 void name::Alloc(size_t nSize) \
254 /* only if old buffer was not big enough */ \
255 if ( nSize > m_nSize ) { \
256 wxDELETEA(m_pItems); \
258 m_pItems = new T[nSize]; \
259 /* only alloc if allocation succeeded */ \
268 /* minimizes the memory usage by freeing unused memory */ \
269 void name::Shrink() \
271 /* only do it if we have some memory to free */ \
272 if( m_nCount < m_nSize ) { \
273 /* allocates exactly as much memory as we need */ \
274 T *pNew = new T[m_nCount]; \
275 /* only shrink if allocation succeeded */ \
277 /* copy data to new location */ \
278 memcpy(pNew, m_pItems, m_nCount*sizeof(T)); \
279 delete [] m_pItems; \
282 /* update the size of the new block */ \
283 m_nSize = m_nCount; \
285 /* else: don't do anything, better keep old memory block! */ \
289 /* add item at the end */ \
290 void name::Add(T lItem, size_t nInsert) \
295 for (size_t i = 0; i < nInsert; i++) \
296 m_pItems[m_nCount++] = lItem; \
299 /* add item at the given position */ \
300 void name::Insert(T lItem, size_t nIndex, size_t nInsert) \
302 wxCHECK_RET( nIndex <= m_nCount, wxT("bad index in wxArray::Insert") ); \
303 wxCHECK_RET( m_nCount <= m_nCount + nInsert, \
304 wxT("array size overflow in wxArray::Insert") ); \
310 memmove(&m_pItems[nIndex + nInsert], &m_pItems[nIndex], \
311 (m_nCount - nIndex)*sizeof(T)); \
312 for (size_t i = 0; i < nInsert; i++) \
313 m_pItems[nIndex + i] = lItem; \
314 m_nCount += nInsert; \
317 /* search for a place to insert item into sorted array (binary search) */ \
318 size_t name::IndexForInsert(T lItem, CMPFUNC fnCompare) const \
325 while ( lo < hi ) { \
328 res = (*fnCompare)((const void *)(long)lItem, \
329 (const void *)(long)(m_pItems[i])); \
332 else if ( res > 0 ) \
343 /* search for an item in a sorted array (binary search) */ \
344 int name::Index(T lItem, CMPFUNC fnCompare) const \
346 size_t n = IndexForInsert(lItem, fnCompare); \
348 return (n >= m_nCount || \
349 (*fnCompare)((const void *)(long)lItem, \
350 ((const void *)(long)m_pItems[n]))) ? wxNOT_FOUND \
354 /* removes item from array (by index) */ \
355 void name::RemoveAt(size_t nIndex, size_t nRemove) \
357 wxCHECK_RET( nIndex < m_nCount, wxT("bad index in wxArray::RemoveAt") ); \
358 wxCHECK_RET( nIndex + nRemove <= m_nCount, \
359 wxT("removing too many elements in wxArray::RemoveAt") ); \
361 memmove(&m_pItems[nIndex], &m_pItems[nIndex + nRemove], \
362 (m_nCount - nIndex - nRemove)*sizeof(T)); \
363 m_nCount -= nRemove; \
366 /* removes item from array (by value) */ \
367 void name::Remove(T lItem) \
369 int iIndex = Index(lItem); \
371 wxCHECK_RET( iIndex != wxNOT_FOUND, \
372 wxT("removing inexistent item in wxArray::Remove") ); \
374 RemoveAt((size_t)iIndex); \
377 /* sort array elements using passed comparaison function */ \
378 void name::Sort(CMPFUNC fCmp) \
380 qsort(m_pItems, m_nCount, sizeof(T), fCmp); \
383 void name::assign(const_iterator first, const_iterator last) \
386 reserve(last - first); \
387 for(; first != last; ++first) \
391 void name::assign(size_type n, const_reference v) \
395 for( size_type i = 0; i < n; ++i ) \
399 void name::insert(iterator it, const_iterator first, const_iterator last) \
401 size_t nInsert = last - first, nIndex = it - begin(); \
406 memmove(&m_pItems[nIndex + nInsert], &m_pItems[nIndex], \
407 (m_nCount - nIndex)*sizeof(T)); \
408 for (size_t i = 0; i < nInsert; ++i, ++it, ++first) \
410 m_nCount += nInsert; \
415 #define _WX_DEFINE_BASEARRAY(T, name) \
416 _WX_DEFINE_BASEARRAY_COMMON(T, name) \
417 _WX_DEFINE_BASEARRAY_NOCOMMON(T, name)
419 _WX_DEFINE_BASEARRAY(const void *, wxBaseArrayPtrVoid
)
420 _WX_DEFINE_BASEARRAY(short, wxBaseArrayShort
)
421 _WX_DEFINE_BASEARRAY(int, wxBaseArrayInt
)
422 _WX_DEFINE_BASEARRAY(long, wxBaseArrayLong
)
423 _WX_DEFINE_BASEARRAY(double, wxBaseArrayDouble
)
426 #include "wx/arrstr.h"
428 _WX_DEFINE_BASEARRAY(wxString
, wxBaseArrayStringBase
)