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 // ============================================================================
16 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
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 size_t name::Add(T lItem, CMPFUNC fnCompare) \
83 size_t idx = IndexForInsert(lItem, fnCompare); \
90 #define _WX_DEFINE_BASEARRAY_NOCOMMON(T, name) \
91 size_t name::IndexForInsert(T lItem, CMPFUNC fnCompare) const \
93 Predicate p((SCMPFUNC)fnCompare); \
94 const_iterator it = std::lower_bound(begin(), end(), lItem, p); \
95 return it - begin(); \
98 int name::Index(T lItem, CMPFUNC fnCompare) const \
100 Predicate p((SCMPFUNC)fnCompare); \
101 const_iterator it = std::lower_bound(begin(), end(), lItem, p); \
102 return (it != end() && \
103 p(lItem, *it)) ? (int)(it - begin()) : wxNOT_FOUND; \
106 void name::Shrink() \
112 #else // if !wxUSE_STL
114 #define _WX_DEFINE_BASEARRAY_NOCOMMON(T, name) \
120 m_pItems = (T *)NULL; \
124 name::name(const name& src) \
126 m_nSize = /* not src.m_nSize to save memory */ \
127 m_nCount = src.m_nCount; \
129 if ( m_nSize != 0 ) { \
130 m_pItems = new T[m_nSize]; \
131 /* only copy if allocation succeeded */ \
133 memcpy(m_pItems, src.m_pItems, m_nCount*sizeof(T)); \
140 m_pItems = (T *) NULL; \
143 /* assignment operator */ \
144 name& name::operator=(const name& src) \
146 wxDELETEA(m_pItems); \
148 m_nSize = /* not src.m_nSize to save memory */ \
149 m_nCount = src.m_nCount; \
151 if ( m_nSize != 0 ){ \
152 m_pItems = new T[m_nSize]; \
153 /* only copy if allocation succeeded */ \
155 memcpy(m_pItems, src.m_pItems, m_nCount*sizeof(T)); \
162 m_pItems = (T *) NULL; \
167 /* allocate new buffer of the given size and move our data to it */ \
168 bool name::Realloc(size_t nSize) \
170 T *pNew = new T[nSize]; \
171 /* only grow if allocation succeeded */ \
176 /* copy data to new location */ \
177 memcpy(pNew, m_pItems, m_nCount*sizeof(T)); \
178 delete [] m_pItems; \
184 /* grow the array */ \
185 void name::Grow(size_t nIncrement) \
187 /* only do it if no more place */ \
188 if( (m_nCount == m_nSize) || ((m_nSize - m_nCount) < nIncrement) ) { \
189 if( m_nSize == 0 ) { \
190 /* was empty, determine initial size */ \
191 size_t size = WX_ARRAY_DEFAULT_INITIAL_SIZE; \
192 if (size < nIncrement) size = nIncrement; \
193 /* allocate some memory */ \
194 m_pItems = new T[size]; \
195 /* only grow if allocation succeeded */ \
202 /* add at least 50% but not too much */ \
203 size_t ndefIncrement = m_nSize < WX_ARRAY_DEFAULT_INITIAL_SIZE \
204 ? WX_ARRAY_DEFAULT_INITIAL_SIZE : m_nSize >> 1; \
205 if ( ndefIncrement > ARRAY_MAXSIZE_INCREMENT ) \
206 ndefIncrement = ARRAY_MAXSIZE_INCREMENT; \
207 if ( nIncrement < ndefIncrement ) \
208 nIncrement = ndefIncrement; \
209 Realloc(m_nSize + nIncrement); \
214 /* make sure that the array has at least count elements */ \
215 void name::SetCount(size_t count, T defval) \
217 if ( m_nSize < count ) \
219 /* need to realloc memory: don't overallocate it here as if */ \
220 /* SetCount() is called, it probably means that the caller */ \
221 /* knows in advance how many elements there will be in the */ \
222 /* array and so it won't be necessary to realloc it later */ \
223 if ( !Realloc(count) ) \
225 /* out of memory -- what can we do? */ \
230 /* add new elements if we extend the array */ \
231 while ( m_nCount < count ) \
233 m_pItems[m_nCount++] = defval; \
240 wxDELETEA(m_pItems); \
243 /* clears the list */ \
249 wxDELETEA(m_pItems); \
252 /* pre-allocates memory (frees the previous data!) */ \
253 void name::Alloc(size_t nSize) \
255 /* only if old buffer was not big enough */ \
256 if ( nSize > m_nSize ) { \
257 wxDELETEA(m_pItems); \
259 m_pItems = new T[nSize]; \
260 /* only alloc if allocation succeeded */ \
269 /* minimizes the memory usage by freeing unused memory */ \
270 void name::Shrink() \
272 /* only do it if we have some memory to free */ \
273 if( m_nCount < m_nSize ) { \
274 /* allocates exactly as much memory as we need */ \
275 T *pNew = new T[m_nCount]; \
276 /* only shrink if allocation succeeded */ \
278 /* copy data to new location */ \
279 memcpy(pNew, m_pItems, m_nCount*sizeof(T)); \
280 delete [] m_pItems; \
283 /* update the size of the new block */ \
284 m_nSize = m_nCount; \
286 /* else: don't do anything, better keep old memory block! */ \
290 /* add item at the end */ \
291 void name::Add(T lItem, size_t nInsert) \
296 for (size_t i = 0; i < nInsert; i++) \
297 m_pItems[m_nCount++] = lItem; \
300 /* add item at the given position */ \
301 void name::Insert(T lItem, size_t nIndex, size_t nInsert) \
303 wxCHECK_RET( nIndex <= m_nCount, wxT("bad index in wxArray::Insert") ); \
304 wxCHECK_RET( m_nCount <= m_nCount + nInsert, \
305 wxT("array size overflow in wxArray::Insert") ); \
311 memmove(&m_pItems[nIndex + nInsert], &m_pItems[nIndex], \
312 (m_nCount - nIndex)*sizeof(T)); \
313 for (size_t i = 0; i < nInsert; i++) \
314 m_pItems[nIndex + i] = lItem; \
315 m_nCount += nInsert; \
318 /* search for a place to insert item into sorted array (binary search) */ \
319 size_t name::IndexForInsert(T lItem, CMPFUNC fnCompare) const \
326 while ( lo < hi ) { \
329 res = (*fnCompare)((const void *)(long)lItem, \
330 (const void *)(long)(m_pItems[i])); \
333 else if ( res > 0 ) \
344 /* search for an item in a sorted array (binary search) */ \
345 int name::Index(T lItem, CMPFUNC fnCompare) const \
347 size_t n = IndexForInsert(lItem, fnCompare); \
349 return (n >= m_nCount || \
350 (*fnCompare)((const void *)(long)lItem, \
351 ((const void *)(long)m_pItems[n]))) ? wxNOT_FOUND \
355 /* removes item from array (by index) */ \
356 void name::RemoveAt(size_t nIndex, size_t nRemove) \
358 wxCHECK_RET( nIndex < m_nCount, wxT("bad index in wxArray::RemoveAt") ); \
359 wxCHECK_RET( nIndex + nRemove <= m_nCount, \
360 wxT("removing too many elements in wxArray::RemoveAt") ); \
362 memmove(&m_pItems[nIndex], &m_pItems[nIndex + nRemove], \
363 (m_nCount - nIndex - nRemove)*sizeof(T)); \
364 m_nCount -= nRemove; \
367 /* removes item from array (by value) */ \
368 void name::Remove(T lItem) \
370 int iIndex = Index(lItem); \
372 wxCHECK_RET( iIndex != wxNOT_FOUND, \
373 wxT("removing inexistent item in wxArray::Remove") ); \
375 RemoveAt((size_t)iIndex); \
378 /* sort array elements using passed comparaison function */ \
379 void name::Sort(CMPFUNC fCmp) \
381 qsort(m_pItems, m_nCount, sizeof(T), fCmp); \
384 void name::assign(const_iterator first, const_iterator last) \
387 reserve(last - first); \
388 for(; first != last; ++first) \
392 void name::assign(size_type n, const_reference v) \
396 for( size_type i = 0; i < n; ++i ) \
400 void name::insert(iterator it, const_iterator first, const_iterator last) \
402 size_t nInsert = last - first, nIndex = it - begin(); \
407 memmove(&m_pItems[nIndex + nInsert], &m_pItems[nIndex], \
408 (m_nCount - nIndex)*sizeof(T)); \
409 for (size_t i = 0; i < nInsert; ++i, ++it, ++first) \
411 m_nCount += nInsert; \
416 #define _WX_DEFINE_BASEARRAY(T, name) \
417 _WX_DEFINE_BASEARRAY_COMMON(T, name) \
418 _WX_DEFINE_BASEARRAY_NOCOMMON(T, name)
420 _WX_DEFINE_BASEARRAY(const void *, wxBaseArrayPtrVoid
)
421 _WX_DEFINE_BASEARRAY(short, wxBaseArrayShort
)
422 _WX_DEFINE_BASEARRAY(int, wxBaseArrayInt
)
423 _WX_DEFINE_BASEARRAY(long, wxBaseArrayLong
)
424 _WX_DEFINE_BASEARRAY(double, wxBaseArrayDouble
)
427 #include "wx/arrstr.h"
429 #include "wx/beforestd.h"
430 #include <functional>
431 #include "wx/afterstd.h"
433 _WX_DEFINE_BASEARRAY(wxString
, wxBaseArrayStringBase
);
435 int wxArrayString::Index(const wxChar
* sz
, bool bCase
, bool WXUNUSED(bFromEnd
)) const
437 wxArrayString::const_iterator it
;
440 it
= std::find_if(begin(), end(),
441 std::not1(std::bind2nd(std::ptr_fun(wxStrcmp
), sz
)));
443 it
= std::find_if(begin(), end(),
444 std::not1(std::bind2nd(std::ptr_fun(wxStricmp
), sz
)));
446 return it
== end() ? wxNOT_FOUND
: it
- begin();
449 class wxStringCompareLess
452 typedef int (wxCMPFUNC_CONV
* fnc
)(const wxChar
*, const wxChar
*);
454 wxStringCompareLess(fnc f
) : m_f(f
) { }
455 bool operator()(const wxChar
* s1
, const wxChar
* s2
)
456 { return m_f(s1
, s2
) < 0; }
461 int wxSortedArrayString::Index(const wxChar
* sz
, bool bCase
, bool WXUNUSED(bFromEnd
)) const
463 wxSortedArrayString::const_iterator it
;
466 it
= std::lower_bound(begin(), end(), sz
,
467 wxStringCompareLess(wxStrcmp
));
469 it
= std::lower_bound(begin(), end(), sz
,
470 wxStringCompareLess(wxStricmp
));
472 if (it
== end() || (bCase
? wxStrcmp
: wxStricmp
)(it
->c_str(), sz
) != 0)