]> git.saurik.com Git - wxWidgets.git/blame - src/common/dynarray.cpp
Applied #15375 to stop event-sending in generic wxSpinCtrl ctor (eco)
[wxWidgets.git] / src / common / dynarray.cpp
CommitLineData
c801d85f 1///////////////////////////////////////////////////////////////////////////////
ad9835c9 2// Name: src/common/dynarray.cpp
c801d85f
KB
3// Purpose: implementation of wxBaseArray class
4// Author: Vadim Zeitlin
3bfa4402 5// Modified by:
c801d85f 6// Created: 12.09.97
c801d85f 7// Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
65571936 8// Licence: wxWindows licence
c801d85f
KB
9///////////////////////////////////////////////////////////////////////////////
10
11// ============================================================================
12// headers
13// ============================================================================
14
ad9835c9 15// For compilers that support precompilation, includes "wx.h".
3096bd2f 16#include "wx/wxprec.h"
c801d85f
KB
17
18#ifdef __BORLANDC__
ad9835c9 19 #pragma hdrstop
c801d85f
KB
20#endif
21
ad9835c9
WS
22#ifndef WX_PRECOMP
23 #include "wx/dynarray.h"
88a7a4e1 24 #include "wx/intl.h"
ad9835c9
WS
25#endif //WX_PRECOMP
26
c801d85f 27#include <stdlib.h>
3bfa4402 28#include <string.h> // for memmove
c801d85f 29
01871bf6 30#if !wxUSE_STD_CONTAINERS
35d5da67 31
acf88ae6
VZ
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
af5b273c 34wxCOMPILE_TIME_ASSERT( sizeof(wxUIntPtr) <= sizeof(void *),
acf88ae6
VZ
35 wxArraySizeOfPtrLessSizeOfLong ); // < 32 symbols
36
c801d85f
KB
37// ============================================================================
38// constants
39// ============================================================================
40
41// size increment = max(50% of current size, ARRAY_MAXSIZE_INCREMENT)
42#define ARRAY_MAXSIZE_INCREMENT 4096
43
44// ============================================================================
45// implementation
46// ============================================================================
47
48// ----------------------------------------------------------------------------
5a1cad6e 49// wxBaseArray - dynamic array of 'T's
c801d85f
KB
50// ----------------------------------------------------------------------------
51
35d5da67 52#define _WX_DEFINE_BASEARRAY(T, name) \
df5168c4
MB
53/* searches the array for an item (forward or backwards) */ \
54int name::Index(T lItem, bool bFromEnd) const \
55{ \
56 if ( bFromEnd ) { \
57 if ( size() > 0 ) { \
58 size_t n = size(); \
59 do { \
60 if ( (*this)[--n] == lItem ) \
61 return n; \
62 } \
63 while ( n != 0 ); \
64 } \
65 } \
66 else { \
67 for( size_t n = 0; n < size(); n++ ) { \
68 if( (*this)[n] == lItem ) \
69 return n; \
70 } \
71 } \
72 \
73 return wxNOT_FOUND; \
74} \
75 \
76/* add item assuming the array is sorted with fnCompare function */ \
6992d326 77size_t name::Add(T lItem, CMPFUNC fnCompare) \
df5168c4 78{ \
6992d326
MB
79 size_t idx = IndexForInsert(lItem, fnCompare); \
80 Insert(lItem, idx); \
81 return idx; \
df5168c4
MB
82} \
83 \
5a1cad6e
GD
84/* ctor */ \
85name::name() \
86{ \
87 m_nSize = \
88 m_nCount = 0; \
d3b9f782 89 m_pItems = NULL; \
5a1cad6e
GD
90} \
91 \
92/* copy ctor */ \
93name::name(const name& src) \
94{ \
95 m_nSize = /* not src.m_nSize to save memory */ \
96 m_nCount = src.m_nCount; \
97 \
98 if ( m_nSize != 0 ) { \
99 m_pItems = new T[m_nSize]; \
100 /* only copy if allocation succeeded */ \
101 if ( m_pItems ) { \
102 memcpy(m_pItems, src.m_pItems, m_nCount*sizeof(T)); \
103 } \
104 else { \
105 m_nSize = 0; \
106 } \
107 } \
108 else \
d3b9f782 109 m_pItems = NULL; \
5a1cad6e
GD
110} \
111 \
112/* assignment operator */ \
113name& name::operator=(const name& src) \
114{ \
115 wxDELETEA(m_pItems); \
116 \
117 m_nSize = /* not src.m_nSize to save memory */ \
118 m_nCount = src.m_nCount; \
119 \
120 if ( m_nSize != 0 ){ \
121 m_pItems = new T[m_nSize]; \
122 /* only copy if allocation succeeded */ \
123 if ( m_pItems ) { \
124 memcpy(m_pItems, src.m_pItems, m_nCount*sizeof(T)); \
125 } \
126 else { \
127 m_nSize = 0; \
128 } \
129 } \
130 else \
d3b9f782 131 m_pItems = NULL; \
5a1cad6e
GD
132 \
133 return *this; \
134} \
135 \
2abb9d2f
VZ
136/* allocate new buffer of the given size and move our data to it */ \
137bool name::Realloc(size_t nSize) \
138{ \
139 T *pNew = new T[nSize]; \
140 /* only grow if allocation succeeded */ \
141 if ( !pNew ) \
142 return false; \
143 \
144 m_nSize = nSize; \
145 /* copy data to new location */ \
146 memcpy(pNew, m_pItems, m_nCount*sizeof(T)); \
147 delete [] m_pItems; \
148 m_pItems = pNew; \
149 \
150 return true; \
151} \
152 \
5a1cad6e 153/* grow the array */ \
3b0b5f13 154void name::Grow(size_t nIncrement) \
5a1cad6e
GD
155{ \
156 /* only do it if no more place */ \
2b5f62a0 157 if( (m_nCount == m_nSize) || ((m_nSize - m_nCount) < nIncrement) ) { \
5a1cad6e 158 if( m_nSize == 0 ) { \
a1db4ad4 159 /* was empty, determine initial size */ \
7d1214cd
PC
160 size_t sz = WX_ARRAY_DEFAULT_INITIAL_SIZE; \
161 if (sz < nIncrement) sz = nIncrement; \
a1db4ad4 162 /* allocate some memory */ \
7d1214cd 163 m_pItems = new T[sz]; \
5a1cad6e
GD
164 /* only grow if allocation succeeded */ \
165 if ( m_pItems ) { \
7d1214cd 166 m_nSize = sz; \
5a1cad6e
GD
167 } \
168 } \
169 else \
170 { \
3b0b5f13
VZ
171 /* add at least 50% but not too much */ \
172 size_t ndefIncrement = m_nSize < WX_ARRAY_DEFAULT_INITIAL_SIZE \
173 ? WX_ARRAY_DEFAULT_INITIAL_SIZE : m_nSize >> 1; \
174 if ( ndefIncrement > ARRAY_MAXSIZE_INCREMENT ) \
175 ndefIncrement = ARRAY_MAXSIZE_INCREMENT; \
176 if ( nIncrement < ndefIncrement ) \
177 nIncrement = ndefIncrement; \
2abb9d2f 178 Realloc(m_nSize + nIncrement); \
5a1cad6e
GD
179 } \
180 } \
181} \
182 \
2abb9d2f
VZ
183/* make sure that the array has at least count elements */ \
184void name::SetCount(size_t count, T defval) \
185{ \
186 if ( m_nSize < count ) \
187 { \
188 /* need to realloc memory: don't overallocate it here as if */ \
189 /* SetCount() is called, it probably means that the caller */ \
190 /* knows in advance how many elements there will be in the */ \
191 /* array and so it won't be necessary to realloc it later */ \
192 if ( !Realloc(count) ) \
193 { \
194 /* out of memory -- what can we do? */ \
195 return; \
196 } \
197 } \
198 \
199 /* add new elements if we extend the array */ \
200 while ( m_nCount < count ) \
201 { \
202 m_pItems[m_nCount++] = defval; \
203 } \
204} \
205 \
5a1cad6e
GD
206/* dtor */ \
207name::~name() \
208{ \
209 wxDELETEA(m_pItems); \
210} \
211 \
212/* clears the list */ \
213void name::Clear() \
214{ \
215 m_nSize = \
216 m_nCount = 0; \
217 \
218 wxDELETEA(m_pItems); \
219} \
220 \
5a1cad6e
GD
221/* minimizes the memory usage by freeing unused memory */ \
222void name::Shrink() \
223{ \
224 /* only do it if we have some memory to free */ \
225 if( m_nCount < m_nSize ) { \
226 /* allocates exactly as much memory as we need */ \
227 T *pNew = new T[m_nCount]; \
228 /* only shrink if allocation succeeded */ \
229 if ( pNew ) { \
230 /* copy data to new location */ \
231 memcpy(pNew, m_pItems, m_nCount*sizeof(T)); \
232 delete [] m_pItems; \
233 m_pItems = pNew; \
79e929e7
VZ
234 \
235 /* update the size of the new block */ \
236 m_nSize = m_nCount; \
5a1cad6e 237 } \
79e929e7 238 /* else: don't do anything, better keep old memory block! */ \
5a1cad6e
GD
239 } \
240} \
241 \
df5168c4
MB
242/* add item at the end */ \
243void name::Add(T lItem, size_t nInsert) \
5a1cad6e 244{ \
df5168c4
MB
245 if (nInsert == 0) \
246 return; \
247 Grow(nInsert); \
248 for (size_t i = 0; i < nInsert; i++) \
249 m_pItems[m_nCount++] = lItem; \
250} \
5a1cad6e 251 \
df5168c4
MB
252/* add item at the given position */ \
253void name::Insert(T lItem, size_t nIndex, size_t nInsert) \
254{ \
255 wxCHECK_RET( nIndex <= m_nCount, wxT("bad index in wxArray::Insert") ); \
256 wxCHECK_RET( m_nCount <= m_nCount + nInsert, \
257 wxT("array size overflow in wxArray::Insert") ); \
258 \
259 if (nInsert == 0) \
260 return; \
261 Grow(nInsert); \
262 \
263 memmove(&m_pItems[nIndex + nInsert], &m_pItems[nIndex], \
264 (m_nCount - nIndex)*sizeof(T)); \
265 for (size_t i = 0; i < nInsert; i++) \
266 m_pItems[nIndex + i] = lItem; \
267 m_nCount += nInsert; \
5a1cad6e
GD
268} \
269 \
270/* search for a place to insert item into sorted array (binary search) */ \
271size_t name::IndexForInsert(T lItem, CMPFUNC fnCompare) const \
272{ \
273 size_t i, \
274 lo = 0, \
275 hi = m_nCount; \
276 int res; \
277 \
278 while ( lo < hi ) { \
279 i = (lo + hi)/2; \
280 \
af5b273c
VZ
281 res = (*fnCompare)((const void *)(wxUIntPtr)lItem, \
282 (const void *)(wxUIntPtr)(m_pItems[i])); \
5a1cad6e
GD
283 if ( res < 0 ) \
284 hi = i; \
285 else if ( res > 0 ) \
286 lo = i + 1; \
287 else { \
288 lo = i; \
289 break; \
290 } \
291 } \
292 \
293 return lo; \
294} \
295 \
296/* search for an item in a sorted array (binary search) */ \
297int name::Index(T lItem, CMPFUNC fnCompare) const \
298{ \
299 size_t n = IndexForInsert(lItem, fnCompare); \
300 \
bb793b0c 301 return (n >= m_nCount || \
af5b273c
VZ
302 (*fnCompare)((const void *)(wxUIntPtr)lItem, \
303 ((const void *)(wxUIntPtr)m_pItems[n]))) \
304 ? wxNOT_FOUND \
305 : (int)n; \
5a1cad6e
GD
306} \
307 \
5a1cad6e 308/* removes item from array (by index) */ \
3b0b5f13 309void name::RemoveAt(size_t nIndex, size_t nRemove) \
5a1cad6e 310{ \
3b0b5f13
VZ
311 wxCHECK_RET( nIndex < m_nCount, wxT("bad index in wxArray::RemoveAt") ); \
312 wxCHECK_RET( nIndex + nRemove <= m_nCount, \
313 wxT("removing too many elements in wxArray::RemoveAt") ); \
5a1cad6e 314 \
3b0b5f13
VZ
315 memmove(&m_pItems[nIndex], &m_pItems[nIndex + nRemove], \
316 (m_nCount - nIndex - nRemove)*sizeof(T)); \
317 m_nCount -= nRemove; \
5a1cad6e
GD
318} \
319 \
320/* removes item from array (by value) */ \
321void name::Remove(T lItem) \
322{ \
323 int iIndex = Index(lItem); \
324 \
325 wxCHECK_RET( iIndex != wxNOT_FOUND, \
326 wxT("removing inexistent item in wxArray::Remove") ); \
327 \
328 RemoveAt((size_t)iIndex); \
329} \
330 \
331/* sort array elements using passed comparaison function */ \
332void name::Sort(CMPFUNC fCmp) \
333{ \
334 qsort(m_pItems, m_nCount, sizeof(T), fCmp); \
360b63dd
MB
335} \
336 \
337void name::assign(const_iterator first, const_iterator last) \
338{ \
339 clear(); \
340 reserve(last - first); \
341 for(; first != last; ++first) \
342 push_back(*first); \
343} \
344 \
345void name::assign(size_type n, const_reference v) \
346{ \
347 clear(); \
348 reserve(n); \
349 for( size_type i = 0; i < n; ++i ) \
350 push_back(v); \
351} \
352 \
353void name::insert(iterator it, const_iterator first, const_iterator last) \
354{ \
355 size_t nInsert = last - first, nIndex = it - begin(); \
356 if (nInsert == 0) \
357 return; \
358 Grow(nInsert); \
359 \
9c5c5c9c 360 /* old iterator could have been invalidated by Grow(). */ \
bd5754f2 361 it = begin() + nIndex; \
9c5c5c9c 362 \
360b63dd
MB
363 memmove(&m_pItems[nIndex + nInsert], &m_pItems[nIndex], \
364 (m_nCount - nIndex)*sizeof(T)); \
365 for (size_t i = 0; i < nInsert; ++i, ++it, ++first) \
366 *it = *first; \
367 m_nCount += nInsert; \
c801d85f
KB
368}
369
17a1ebd1
VZ
370#ifdef __INTELC__
371 #pragma warning(push)
372 #pragma warning(disable: 1684)
373 #pragma warning(disable: 1572)
374#endif
375
f1322419 376_WX_DEFINE_BASEARRAY(const void *, wxBaseArrayPtrVoid)
1ffc8d7a 377_WX_DEFINE_BASEARRAY(char, wxBaseArrayChar)
f1322419
VZ
378_WX_DEFINE_BASEARRAY(short, wxBaseArrayShort)
379_WX_DEFINE_BASEARRAY(int, wxBaseArrayInt)
380_WX_DEFINE_BASEARRAY(long, wxBaseArrayLong)
e55e9486 381_WX_DEFINE_BASEARRAY(size_t, wxBaseArraySizeT)
360b63dd 382_WX_DEFINE_BASEARRAY(double, wxBaseArrayDouble)
c801d85f 383
17a1ebd1
VZ
384#ifdef __INTELC__
385 #pragma warning(pop)
386#endif
387
01871bf6 388#else // wxUSE_STD_CONTAINERS
35d5da67 389
df5168c4
MB
390#include "wx/arrstr.h"
391
2da2f941
MB
392#include "wx/beforestd.h"
393#include <functional>
394#include "wx/afterstd.h"
395
48e4ba4f
VZ
396// some compilers (Sun CC being the only known example) distinguish between
397// extern "C" functions and the functions with C++ linkage and ptr_fun and
398// wxStringCompareLess can't take wxStrcmp/wxStricmp directly as arguments in
399// this case, we need the wrappers below to make this work
86501081 400struct wxStringCmp
48e4ba4f 401{
86501081
VS
402 typedef wxString first_argument_type;
403 typedef wxString second_argument_type;
404 typedef int result_type;
405
406 int operator()(const wxString& s1, const wxString& s2) const
407 {
408 return s1.compare(s2);
409 }
410};
48e4ba4f 411
86501081 412struct wxStringCmpNoCase
48e4ba4f 413{
86501081
VS
414 typedef wxString first_argument_type;
415 typedef wxString second_argument_type;
416 typedef int result_type;
48e4ba4f 417
86501081
VS
418 int operator()(const wxString& s1, const wxString& s2) const
419 {
420 return s1.CmpNoCase(s2);
421 }
422};
423
424int wxArrayString::Index(const wxString& str, bool bCase, bool WXUNUSED(bFromEnd)) const
2da2f941
MB
425{
426 wxArrayString::const_iterator it;
427
428 if (bCase)
48e4ba4f 429 {
2da2f941 430 it = std::find_if(begin(), end(),
48e4ba4f
VZ
431 std::not1(
432 std::bind2nd(
86501081 433 wxStringCmp(), str)));
48e4ba4f
VZ
434 }
435 else // !bCase
436 {
2da2f941 437 it = std::find_if(begin(), end(),
48e4ba4f
VZ
438 std::not1(
439 std::bind2nd(
86501081 440 wxStringCmpNoCase(), str)));
48e4ba4f 441 }
2da2f941
MB
442
443 return it == end() ? wxNOT_FOUND : it - begin();
444}
445
4e75b65f 446template<class F>
2da2f941
MB
447class wxStringCompareLess
448{
449public:
4e75b65f 450 wxStringCompareLess(F f) : m_f(f) { }
4e75b65f
MB
451 bool operator()(const wxString& s1, const wxString& s2)
452 { return m_f(s1, s2) < 0; }
2da2f941 453private:
4e75b65f 454 F m_f;
2da2f941
MB
455};
456
4e75b65f
MB
457template<class F>
458wxStringCompareLess<F> wxStringCompare(F f)
459{
460 return wxStringCompareLess<F>(f);
461}
462
463void wxArrayString::Sort(CompareFunction function)
464{
465 std::sort(begin(), end(), wxStringCompare(function));
466}
467
468void wxArrayString::Sort(bool reverseOrder)
469{
470 if (reverseOrder)
471 {
472 std::sort(begin(), end(), std::greater<wxString>());
473 }
474 else
475 {
476 std::sort(begin(), end());
477 }
478}
479
dc9934d4
VZ
480int wxSortedArrayString::Index(const wxString& str,
481 bool WXUNUSED_UNLESS_DEBUG(bCase),
482 bool WXUNUSED_UNLESS_DEBUG(bFromEnd)) const
2da2f941 483{
dc9934d4
VZ
484 wxASSERT_MSG( bCase && !bFromEnd,
485 "search parameters ignored for sorted array" );
2da2f941 486
dc9934d4
VZ
487 wxSortedArrayString::const_iterator
488 it = std::lower_bound(begin(), end(), str, wxStringCompare(wxStringCmp()));
2da2f941 489
dc9934d4 490 if ( it == end() || str.Cmp(*it) != 0 )
2da2f941 491 return wxNOT_FOUND;
e11d2abe 492
2da2f941
MB
493 return it - begin();
494}
df5168c4 495
01871bf6 496#endif // !wxUSE_STD_CONTAINERS/wxUSE_STD_CONTAINERS