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