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(wxUIntPtr
) <= 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() && !p(lItem, *it)) ? \
103 (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 *)(wxUIntPtr)lItem, \
330 (const void *)(wxUIntPtr)(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 *)(wxUIntPtr)lItem, \
351 ((const void *)(wxUIntPtr)m_pItems[n]))) \
356 /* removes item from array (by index) */ \
357 void name::RemoveAt(size_t nIndex, size_t nRemove) \
359 wxCHECK_RET( nIndex < m_nCount, wxT("bad index in wxArray::RemoveAt") ); \
360 wxCHECK_RET( nIndex + nRemove <= m_nCount, \
361 wxT("removing too many elements in wxArray::RemoveAt") ); \
363 memmove(&m_pItems[nIndex], &m_pItems[nIndex + nRemove], \
364 (m_nCount - nIndex - nRemove)*sizeof(T)); \
365 m_nCount -= nRemove; \
368 /* removes item from array (by value) */ \
369 void name::Remove(T lItem) \
371 int iIndex = Index(lItem); \
373 wxCHECK_RET( iIndex != wxNOT_FOUND, \
374 wxT("removing inexistent item in wxArray::Remove") ); \
376 RemoveAt((size_t)iIndex); \
379 /* sort array elements using passed comparaison function */ \
380 void name::Sort(CMPFUNC fCmp) \
382 qsort(m_pItems, m_nCount, sizeof(T), fCmp); \
385 void name::assign(const_iterator first, const_iterator last) \
388 reserve(last - first); \
389 for(; first != last; ++first) \
393 void name::assign(size_type n, const_reference v) \
397 for( size_type i = 0; i < n; ++i ) \
401 void name::insert(iterator it, const_iterator first, const_iterator last) \
403 size_t nInsert = last - first, nIndex = it - begin(); \
408 memmove(&m_pItems[nIndex + nInsert], &m_pItems[nIndex], \
409 (m_nCount - nIndex)*sizeof(T)); \
410 for (size_t i = 0; i < nInsert; ++i, ++it, ++first) \
412 m_nCount += nInsert; \
417 #define _WX_DEFINE_BASEARRAY(T, name) \
418 _WX_DEFINE_BASEARRAY_COMMON(T, name) \
419 _WX_DEFINE_BASEARRAY_NOCOMMON(T, name)
421 _WX_DEFINE_BASEARRAY(const void *, wxBaseArrayPtrVoid
)
422 _WX_DEFINE_BASEARRAY(short, wxBaseArrayShort
)
423 _WX_DEFINE_BASEARRAY(int, wxBaseArrayInt
)
424 _WX_DEFINE_BASEARRAY(long, wxBaseArrayLong
)
425 _WX_DEFINE_BASEARRAY(size_t, wxBaseArraySizeT
)
426 _WX_DEFINE_BASEARRAY(double, wxBaseArrayDouble
)
429 #include "wx/arrstr.h"
431 #include "wx/beforestd.h"
432 #include <functional>
433 #include "wx/afterstd.h"
435 _WX_DEFINE_BASEARRAY(wxString
, wxBaseArrayStringBase
);
437 // some compilers (Sun CC being the only known example) distinguish between
438 // extern "C" functions and the functions with C++ linkage and ptr_fun and
439 // wxStringCompareLess can't take wxStrcmp/wxStricmp directly as arguments in
440 // this case, we need the wrappers below to make this work
441 inline int wxStrcmpCppWrapper(const wxChar
*p
, const wxChar
*q
)
443 return wxStrcmp(p
, q
);
446 inline int wxStricmpCppWrapper(const wxChar
*p
, const wxChar
*q
)
448 return wxStricmp(p
, q
);
451 int wxArrayString::Index(const wxChar
* sz
, bool bCase
, bool WXUNUSED(bFromEnd
)) const
453 wxArrayString::const_iterator it
;
457 it
= std::find_if(begin(), end(),
460 std::ptr_fun(wxStrcmpCppWrapper
), sz
)));
464 it
= std::find_if(begin(), end(),
467 std::ptr_fun(wxStricmpCppWrapper
), sz
)));
470 return it
== end() ? wxNOT_FOUND
: it
- begin();
474 class wxStringCompareLess
477 wxStringCompareLess(F f
) : m_f(f
) { }
478 bool operator()(const wxChar
* s1
, const wxChar
* s2
)
479 { return m_f(s1
, s2
) < 0; }
480 bool operator()(const wxString
& s1
, const wxString
& s2
)
481 { return m_f(s1
, s2
) < 0; }
487 wxStringCompareLess
<F
> wxStringCompare(F f
)
489 return wxStringCompareLess
<F
>(f
);
492 void wxArrayString::Sort(CompareFunction function
)
494 std::sort(begin(), end(), wxStringCompare(function
));
497 void wxArrayString::Sort(bool reverseOrder
)
501 std::sort(begin(), end(), std::greater
<wxString
>());
505 std::sort(begin(), end());
509 int wxSortedArrayString::Index(const wxChar
* sz
, bool bCase
, bool WXUNUSED(bFromEnd
)) const
511 wxSortedArrayString::const_iterator it
;
515 it
= std::lower_bound(begin(), end(), s
,
516 wxStringCompare(wxStrcmpCppWrapper
));
518 it
= std::lower_bound(begin(), end(), s
,
519 wxStringCompare(wxStricmpCppWrapper
));
526 if (wxStrcmp(it
->c_str(), sz
) != 0)
531 if (wxStricmp(it
->c_str(), sz
) != 0)