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
32 // we cast the value to long from which we cast it to void * in IndexForInsert:
33 // this can't work if the pointers are not big enough
34 wxCOMPILE_TIME_ASSERT( sizeof(wxUIntPtr
) <= sizeof(void *),
35 wxArraySizeOfPtrLessSizeOfLong
); // < 32 symbols
37 // ============================================================================
39 // ============================================================================
41 // size increment = max(50% of current size, ARRAY_MAXSIZE_INCREMENT)
42 #define ARRAY_MAXSIZE_INCREMENT 4096
44 // ============================================================================
46 // ============================================================================
48 // ----------------------------------------------------------------------------
49 // wxBaseArray - dynamic array of 'T's
50 // ----------------------------------------------------------------------------
52 #define _WX_DEFINE_BASEARRAY_COMMON(T, name) \
53 /* searches the array for an item (forward or backwards) */ \
54 int name::Index(T lItem, bool bFromEnd) const \
60 if ( (*this)[--n] == lItem ) \
67 for( size_t n = 0; n < size(); n++ ) { \
68 if( (*this)[n] == lItem ) \
76 /* add item assuming the array is sorted with fnCompare function */ \
77 size_t name::Add(T lItem, CMPFUNC fnCompare) \
79 size_t idx = IndexForInsert(lItem, fnCompare); \
86 #define _WX_DEFINE_BASEARRAY_NOCOMMON(T, name) \
87 size_t name::IndexForInsert(T lItem, CMPFUNC fnCompare) const \
89 Predicate p((SCMPFUNC)fnCompare); \
90 const_iterator it = std::lower_bound(begin(), end(), lItem, p); \
91 return it - begin(); \
94 int name::Index(T lItem, CMPFUNC fnCompare) const \
96 Predicate p((SCMPFUNC)fnCompare); \
97 const_iterator it = std::lower_bound(begin(), end(), lItem, p); \
98 return (it != end() && !p(lItem, *it)) ? \
99 (int)(it - begin()) : wxNOT_FOUND; \
102 void name::Shrink() \
108 #else // if !wxUSE_STL
110 #define _WX_DEFINE_BASEARRAY_NOCOMMON(T, name) \
116 m_pItems = (T *)NULL; \
120 name::name(const name& src) \
122 m_nSize = /* not src.m_nSize to save memory */ \
123 m_nCount = src.m_nCount; \
125 if ( m_nSize != 0 ) { \
126 m_pItems = new T[m_nSize]; \
127 /* only copy if allocation succeeded */ \
129 memcpy(m_pItems, src.m_pItems, m_nCount*sizeof(T)); \
136 m_pItems = (T *) NULL; \
139 /* assignment operator */ \
140 name& name::operator=(const name& src) \
142 wxDELETEA(m_pItems); \
144 m_nSize = /* not src.m_nSize to save memory */ \
145 m_nCount = src.m_nCount; \
147 if ( m_nSize != 0 ){ \
148 m_pItems = new T[m_nSize]; \
149 /* only copy if allocation succeeded */ \
151 memcpy(m_pItems, src.m_pItems, m_nCount*sizeof(T)); \
158 m_pItems = (T *) NULL; \
163 /* allocate new buffer of the given size and move our data to it */ \
164 bool name::Realloc(size_t nSize) \
166 T *pNew = new T[nSize]; \
167 /* only grow if allocation succeeded */ \
172 /* copy data to new location */ \
173 memcpy(pNew, m_pItems, m_nCount*sizeof(T)); \
174 delete [] m_pItems; \
180 /* grow the array */ \
181 void name::Grow(size_t nIncrement) \
183 /* only do it if no more place */ \
184 if( (m_nCount == m_nSize) || ((m_nSize - m_nCount) < nIncrement) ) { \
185 if( m_nSize == 0 ) { \
186 /* was empty, determine initial size */ \
187 size_t size = WX_ARRAY_DEFAULT_INITIAL_SIZE; \
188 if (size < nIncrement) size = nIncrement; \
189 /* allocate some memory */ \
190 m_pItems = new T[size]; \
191 /* only grow if allocation succeeded */ \
198 /* add at least 50% but not too much */ \
199 size_t ndefIncrement = m_nSize < WX_ARRAY_DEFAULT_INITIAL_SIZE \
200 ? WX_ARRAY_DEFAULT_INITIAL_SIZE : m_nSize >> 1; \
201 if ( ndefIncrement > ARRAY_MAXSIZE_INCREMENT ) \
202 ndefIncrement = ARRAY_MAXSIZE_INCREMENT; \
203 if ( nIncrement < ndefIncrement ) \
204 nIncrement = ndefIncrement; \
205 Realloc(m_nSize + nIncrement); \
210 /* make sure that the array has at least count elements */ \
211 void name::SetCount(size_t count, T defval) \
213 if ( m_nSize < count ) \
215 /* need to realloc memory: don't overallocate it here as if */ \
216 /* SetCount() is called, it probably means that the caller */ \
217 /* knows in advance how many elements there will be in the */ \
218 /* array and so it won't be necessary to realloc it later */ \
219 if ( !Realloc(count) ) \
221 /* out of memory -- what can we do? */ \
226 /* add new elements if we extend the array */ \
227 while ( m_nCount < count ) \
229 m_pItems[m_nCount++] = defval; \
236 wxDELETEA(m_pItems); \
239 /* clears the list */ \
245 wxDELETEA(m_pItems); \
248 /* pre-allocates memory (frees the previous data!) */ \
249 void name::Alloc(size_t nSize) \
251 /* only if old buffer was not big enough */ \
252 if ( nSize > m_nSize ) { \
253 wxDELETEA(m_pItems); \
255 m_pItems = new T[nSize]; \
256 /* only alloc if allocation succeeded */ \
265 /* minimizes the memory usage by freeing unused memory */ \
266 void name::Shrink() \
268 /* only do it if we have some memory to free */ \
269 if( m_nCount < m_nSize ) { \
270 /* allocates exactly as much memory as we need */ \
271 T *pNew = new T[m_nCount]; \
272 /* only shrink if allocation succeeded */ \
274 /* copy data to new location */ \
275 memcpy(pNew, m_pItems, m_nCount*sizeof(T)); \
276 delete [] m_pItems; \
279 /* update the size of the new block */ \
280 m_nSize = m_nCount; \
282 /* else: don't do anything, better keep old memory block! */ \
286 /* add item at the end */ \
287 void name::Add(T lItem, size_t nInsert) \
292 for (size_t i = 0; i < nInsert; i++) \
293 m_pItems[m_nCount++] = lItem; \
296 /* add item at the given position */ \
297 void name::Insert(T lItem, size_t nIndex, size_t nInsert) \
299 wxCHECK_RET( nIndex <= m_nCount, wxT("bad index in wxArray::Insert") ); \
300 wxCHECK_RET( m_nCount <= m_nCount + nInsert, \
301 wxT("array size overflow in wxArray::Insert") ); \
307 memmove(&m_pItems[nIndex + nInsert], &m_pItems[nIndex], \
308 (m_nCount - nIndex)*sizeof(T)); \
309 for (size_t i = 0; i < nInsert; i++) \
310 m_pItems[nIndex + i] = lItem; \
311 m_nCount += nInsert; \
314 /* search for a place to insert item into sorted array (binary search) */ \
315 size_t name::IndexForInsert(T lItem, CMPFUNC fnCompare) const \
322 while ( lo < hi ) { \
325 res = (*fnCompare)((const void *)(wxUIntPtr)lItem, \
326 (const void *)(wxUIntPtr)(m_pItems[i])); \
329 else if ( res > 0 ) \
340 /* search for an item in a sorted array (binary search) */ \
341 int name::Index(T lItem, CMPFUNC fnCompare) const \
343 size_t n = IndexForInsert(lItem, fnCompare); \
345 return (n >= m_nCount || \
346 (*fnCompare)((const void *)(wxUIntPtr)lItem, \
347 ((const void *)(wxUIntPtr)m_pItems[n]))) \
352 /* removes item from array (by index) */ \
353 void name::RemoveAt(size_t nIndex, size_t nRemove) \
355 wxCHECK_RET( nIndex < m_nCount, wxT("bad index in wxArray::RemoveAt") ); \
356 wxCHECK_RET( nIndex + nRemove <= m_nCount, \
357 wxT("removing too many elements in wxArray::RemoveAt") ); \
359 memmove(&m_pItems[nIndex], &m_pItems[nIndex + nRemove], \
360 (m_nCount - nIndex - nRemove)*sizeof(T)); \
361 m_nCount -= nRemove; \
364 /* removes item from array (by value) */ \
365 void name::Remove(T lItem) \
367 int iIndex = Index(lItem); \
369 wxCHECK_RET( iIndex != wxNOT_FOUND, \
370 wxT("removing inexistent item in wxArray::Remove") ); \
372 RemoveAt((size_t)iIndex); \
375 /* sort array elements using passed comparaison function */ \
376 void name::Sort(CMPFUNC fCmp) \
378 qsort(m_pItems, m_nCount, sizeof(T), fCmp); \
381 void name::assign(const_iterator first, const_iterator last) \
384 reserve(last - first); \
385 for(; first != last; ++first) \
389 void name::assign(size_type n, const_reference v) \
393 for( size_type i = 0; i < n; ++i ) \
397 void name::insert(iterator it, const_iterator first, const_iterator last) \
399 size_t nInsert = last - first, nIndex = it - begin(); \
404 memmove(&m_pItems[nIndex + nInsert], &m_pItems[nIndex], \
405 (m_nCount - nIndex)*sizeof(T)); \
406 for (size_t i = 0; i < nInsert; ++i, ++it, ++first) \
408 m_nCount += nInsert; \
413 #define _WX_DEFINE_BASEARRAY(T, name) \
414 _WX_DEFINE_BASEARRAY_COMMON(T, name) \
415 _WX_DEFINE_BASEARRAY_NOCOMMON(T, name)
417 _WX_DEFINE_BASEARRAY(const void *, wxBaseArrayPtrVoid
)
418 _WX_DEFINE_BASEARRAY(short, wxBaseArrayShort
)
419 _WX_DEFINE_BASEARRAY(int, wxBaseArrayInt
)
420 _WX_DEFINE_BASEARRAY(long, wxBaseArrayLong
)
421 _WX_DEFINE_BASEARRAY(size_t, wxBaseArraySizeT
)
422 _WX_DEFINE_BASEARRAY(double, wxBaseArrayDouble
)
425 #include "wx/arrstr.h"
427 #include "wx/beforestd.h"
428 #include <functional>
429 #include "wx/afterstd.h"
431 _WX_DEFINE_BASEARRAY(wxString
, wxBaseArrayStringBase
);
433 // some compilers (Sun CC being the only known example) distinguish between
434 // extern "C" functions and the functions with C++ linkage and ptr_fun and
435 // wxStringCompareLess can't take wxStrcmp/wxStricmp directly as arguments in
436 // this case, we need the wrappers below to make this work
437 inline int wxStrcmpCppWrapper(const wxChar
*p
, const wxChar
*q
)
439 return wxStrcmp(p
, q
);
442 inline int wxStricmpCppWrapper(const wxChar
*p
, const wxChar
*q
)
444 return wxStricmp(p
, q
);
447 int wxArrayString::Index(const wxChar
* sz
, bool bCase
, bool WXUNUSED(bFromEnd
)) const
449 wxArrayString::const_iterator it
;
453 it
= std::find_if(begin(), end(),
456 std::ptr_fun(wxStrcmpCppWrapper
), sz
)));
460 it
= std::find_if(begin(), end(),
463 std::ptr_fun(wxStricmpCppWrapper
), sz
)));
466 return it
== end() ? wxNOT_FOUND
: it
- begin();
470 class wxStringCompareLess
473 wxStringCompareLess(F f
) : m_f(f
) { }
474 bool operator()(const wxChar
* s1
, const wxChar
* s2
)
475 { return m_f(s1
, s2
) < 0; }
476 bool operator()(const wxString
& s1
, const wxString
& s2
)
477 { return m_f(s1
, s2
) < 0; }
483 wxStringCompareLess
<F
> wxStringCompare(F f
)
485 return wxStringCompareLess
<F
>(f
);
488 void wxArrayString::Sort(CompareFunction function
)
490 std::sort(begin(), end(), wxStringCompare(function
));
493 void wxArrayString::Sort(bool reverseOrder
)
497 std::sort(begin(), end(), std::greater
<wxString
>());
501 std::sort(begin(), end());
505 int wxSortedArrayString::Index(const wxChar
* sz
, bool bCase
, bool WXUNUSED(bFromEnd
)) const
507 wxSortedArrayString::const_iterator it
;
511 it
= std::lower_bound(begin(), end(), s
,
512 wxStringCompare(wxStrcmpCppWrapper
));
514 it
= std::lower_bound(begin(), end(), s
,
515 wxStringCompare(wxStricmpCppWrapper
));
522 if (wxStrcmp(it
->c_str(), sz
) != 0)
527 if (wxStricmp(it
->c_str(), sz
) != 0)