]> git.saurik.com Git - wxWidgets.git/blame - src/common/dynarray.cpp
reenable WS_TABSTOP (patch 1045547)
[wxWidgets.git] / src / common / dynarray.cpp
CommitLineData
c801d85f
KB
1///////////////////////////////////////////////////////////////////////////////
2// Name: dynarray.cpp
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
14f355c2 16#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
c801d85f
KB
17#pragma implementation "dynarray.h"
18#endif
19
3096bd2f 20#include "wx/wxprec.h"
c801d85f
KB
21
22#ifdef __BORLANDC__
23 #pragma hdrstop
24#endif
25
26#include "wx/dynarray.h"
3096bd2f 27#include "wx/intl.h"
c801d85f
KB
28
29#include <stdlib.h>
3bfa4402 30#include <string.h> // for memmove
c801d85f
KB
31
32#ifndef max
33 #define max(a, b) (((a) > (b)) ? (a) : (b))
34#endif
35
acf88ae6
VZ
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
af5b273c 38wxCOMPILE_TIME_ASSERT( sizeof(wxUIntPtr) <= sizeof(void *),
acf88ae6
VZ
39 wxArraySizeOfPtrLessSizeOfLong ); // < 32 symbols
40
c801d85f
KB
41// ============================================================================
42// constants
43// ============================================================================
44
45// size increment = max(50% of current size, ARRAY_MAXSIZE_INCREMENT)
46#define ARRAY_MAXSIZE_INCREMENT 4096
47
48// ============================================================================
49// implementation
50// ============================================================================
51
52// ----------------------------------------------------------------------------
5a1cad6e 53// wxBaseArray - dynamic array of 'T's
c801d85f
KB
54// ----------------------------------------------------------------------------
55
df5168c4
MB
56#define _WX_DEFINE_BASEARRAY_COMMON(T, name) \
57/* searches the array for an item (forward or backwards) */ \
58int name::Index(T lItem, bool bFromEnd) const \
59{ \
60 if ( bFromEnd ) { \
61 if ( size() > 0 ) { \
62 size_t n = size(); \
63 do { \
64 if ( (*this)[--n] == lItem ) \
65 return n; \
66 } \
67 while ( n != 0 ); \
68 } \
69 } \
70 else { \
71 for( size_t n = 0; n < size(); n++ ) { \
72 if( (*this)[n] == lItem ) \
73 return n; \
74 } \
75 } \
76 \
77 return wxNOT_FOUND; \
78} \
79 \
80/* add item assuming the array is sorted with fnCompare function */ \
6992d326 81size_t name::Add(T lItem, CMPFUNC fnCompare) \
df5168c4 82{ \
6992d326
MB
83 size_t idx = IndexForInsert(lItem, fnCompare); \
84 Insert(lItem, idx); \
85 return idx; \
86}
df5168c4
MB
87
88#if wxUSE_STL
89
90#define _WX_DEFINE_BASEARRAY_NOCOMMON(T, name) \
91size_t name::IndexForInsert(T lItem, CMPFUNC fnCompare) const \
92{ \
f700f98c 93 Predicate p((SCMPFUNC)fnCompare); \
df5168c4
MB
94 const_iterator it = std::lower_bound(begin(), end(), lItem, p); \
95 return it - begin(); \
96} \
97 \
98int name::Index(T lItem, CMPFUNC fnCompare) const \
99{ \
f700f98c
MB
100 Predicate p((SCMPFUNC)fnCompare); \
101 const_iterator it = std::lower_bound(begin(), end(), lItem, p); \
10dcbe63
MB
102 return (it != end() && !p(lItem, *it)) ? \
103 (int)(it - begin()) : wxNOT_FOUND; \
df5168c4
MB
104} \
105 \
106void name::Shrink() \
107{ \
108 name tmp(*this); \
109 swap(tmp); \
110}
111
112#else // if !wxUSE_STL
113
114#define _WX_DEFINE_BASEARRAY_NOCOMMON(T, name) \
5a1cad6e
GD
115/* ctor */ \
116name::name() \
117{ \
118 m_nSize = \
119 m_nCount = 0; \
120 m_pItems = (T *)NULL; \
121} \
122 \
123/* copy ctor */ \
124name::name(const name& src) \
125{ \
126 m_nSize = /* not src.m_nSize to save memory */ \
127 m_nCount = src.m_nCount; \
128 \
129 if ( m_nSize != 0 ) { \
130 m_pItems = new T[m_nSize]; \
131 /* only copy if allocation succeeded */ \
132 if ( m_pItems ) { \
133 memcpy(m_pItems, src.m_pItems, m_nCount*sizeof(T)); \
134 } \
135 else { \
136 m_nSize = 0; \
137 } \
138 } \
139 else \
140 m_pItems = (T *) NULL; \
141} \
142 \
143/* assignment operator */ \
144name& name::operator=(const name& src) \
145{ \
146 wxDELETEA(m_pItems); \
147 \
148 m_nSize = /* not src.m_nSize to save memory */ \
149 m_nCount = src.m_nCount; \
150 \
151 if ( m_nSize != 0 ){ \
152 m_pItems = new T[m_nSize]; \
153 /* only copy if allocation succeeded */ \
154 if ( m_pItems ) { \
155 memcpy(m_pItems, src.m_pItems, m_nCount*sizeof(T)); \
156 } \
157 else { \
158 m_nSize = 0; \
159 } \
160 } \
161 else \
162 m_pItems = (T *) NULL; \
163 \
164 return *this; \
165} \
166 \
2abb9d2f
VZ
167/* allocate new buffer of the given size and move our data to it */ \
168bool name::Realloc(size_t nSize) \
169{ \
170 T *pNew = new T[nSize]; \
171 /* only grow if allocation succeeded */ \
172 if ( !pNew ) \
173 return false; \
174 \
175 m_nSize = nSize; \
176 /* copy data to new location */ \
177 memcpy(pNew, m_pItems, m_nCount*sizeof(T)); \
178 delete [] m_pItems; \
179 m_pItems = pNew; \
180 \
181 return true; \
182} \
183 \
5a1cad6e 184/* grow the array */ \
3b0b5f13 185void name::Grow(size_t nIncrement) \
5a1cad6e
GD
186{ \
187 /* only do it if no more place */ \
2b5f62a0 188 if( (m_nCount == m_nSize) || ((m_nSize - m_nCount) < nIncrement) ) { \
5a1cad6e 189 if( m_nSize == 0 ) { \
a1db4ad4
SN
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]; \
5a1cad6e
GD
195 /* only grow if allocation succeeded */ \
196 if ( m_pItems ) { \
a1db4ad4 197 m_nSize = size; \
5a1cad6e
GD
198 } \
199 } \
200 else \
201 { \
3b0b5f13
VZ
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; \
2abb9d2f 209 Realloc(m_nSize + nIncrement); \
5a1cad6e
GD
210 } \
211 } \
212} \
213 \
2abb9d2f
VZ
214/* make sure that the array has at least count elements */ \
215void name::SetCount(size_t count, T defval) \
216{ \
217 if ( m_nSize < count ) \
218 { \
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) ) \
224 { \
225 /* out of memory -- what can we do? */ \
226 return; \
227 } \
228 } \
229 \
230 /* add new elements if we extend the array */ \
231 while ( m_nCount < count ) \
232 { \
233 m_pItems[m_nCount++] = defval; \
234 } \
235} \
236 \
5a1cad6e
GD
237/* dtor */ \
238name::~name() \
239{ \
240 wxDELETEA(m_pItems); \
241} \
242 \
243/* clears the list */ \
244void name::Clear() \
245{ \
246 m_nSize = \
247 m_nCount = 0; \
248 \
249 wxDELETEA(m_pItems); \
250} \
251 \
252/* pre-allocates memory (frees the previous data!) */ \
253void name::Alloc(size_t nSize) \
254{ \
255 /* only if old buffer was not big enough */ \
256 if ( nSize > m_nSize ) { \
257 wxDELETEA(m_pItems); \
258 m_nSize = 0; \
259 m_pItems = new T[nSize]; \
260 /* only alloc if allocation succeeded */ \
261 if ( m_pItems ) { \
262 m_nSize = nSize; \
263 } \
264 } \
265 \
266 m_nCount = 0; \
267} \
268 \
269/* minimizes the memory usage by freeing unused memory */ \
270void name::Shrink() \
271{ \
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 */ \
277 if ( pNew ) { \
278 /* copy data to new location */ \
279 memcpy(pNew, m_pItems, m_nCount*sizeof(T)); \
280 delete [] m_pItems; \
281 m_pItems = pNew; \
79e929e7
VZ
282 \
283 /* update the size of the new block */ \
284 m_nSize = m_nCount; \
5a1cad6e 285 } \
79e929e7 286 /* else: don't do anything, better keep old memory block! */ \
5a1cad6e
GD
287 } \
288} \
289 \
df5168c4
MB
290/* add item at the end */ \
291void name::Add(T lItem, size_t nInsert) \
5a1cad6e 292{ \
df5168c4
MB
293 if (nInsert == 0) \
294 return; \
295 Grow(nInsert); \
296 for (size_t i = 0; i < nInsert; i++) \
297 m_pItems[m_nCount++] = lItem; \
298} \
5a1cad6e 299 \
df5168c4
MB
300/* add item at the given position */ \
301void name::Insert(T lItem, size_t nIndex, size_t nInsert) \
302{ \
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") ); \
306 \
307 if (nInsert == 0) \
308 return; \
309 Grow(nInsert); \
310 \
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; \
5a1cad6e
GD
316} \
317 \
318/* search for a place to insert item into sorted array (binary search) */ \
319size_t name::IndexForInsert(T lItem, CMPFUNC fnCompare) const \
320{ \
321 size_t i, \
322 lo = 0, \
323 hi = m_nCount; \
324 int res; \
325 \
326 while ( lo < hi ) { \
327 i = (lo + hi)/2; \
328 \
af5b273c
VZ
329 res = (*fnCompare)((const void *)(wxUIntPtr)lItem, \
330 (const void *)(wxUIntPtr)(m_pItems[i])); \
5a1cad6e
GD
331 if ( res < 0 ) \
332 hi = i; \
333 else if ( res > 0 ) \
334 lo = i + 1; \
335 else { \
336 lo = i; \
337 break; \
338 } \
339 } \
340 \
341 return lo; \
342} \
343 \
344/* search for an item in a sorted array (binary search) */ \
345int name::Index(T lItem, CMPFUNC fnCompare) const \
346{ \
347 size_t n = IndexForInsert(lItem, fnCompare); \
348 \
bb793b0c 349 return (n >= m_nCount || \
af5b273c
VZ
350 (*fnCompare)((const void *)(wxUIntPtr)lItem, \
351 ((const void *)(wxUIntPtr)m_pItems[n]))) \
352 ? wxNOT_FOUND \
353 : (int)n; \
5a1cad6e
GD
354} \
355 \
5a1cad6e 356/* removes item from array (by index) */ \
3b0b5f13 357void name::RemoveAt(size_t nIndex, size_t nRemove) \
5a1cad6e 358{ \
3b0b5f13
VZ
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") ); \
5a1cad6e 362 \
3b0b5f13
VZ
363 memmove(&m_pItems[nIndex], &m_pItems[nIndex + nRemove], \
364 (m_nCount - nIndex - nRemove)*sizeof(T)); \
365 m_nCount -= nRemove; \
5a1cad6e
GD
366} \
367 \
368/* removes item from array (by value) */ \
369void name::Remove(T lItem) \
370{ \
371 int iIndex = Index(lItem); \
372 \
373 wxCHECK_RET( iIndex != wxNOT_FOUND, \
374 wxT("removing inexistent item in wxArray::Remove") ); \
375 \
376 RemoveAt((size_t)iIndex); \
377} \
378 \
379/* sort array elements using passed comparaison function */ \
380void name::Sort(CMPFUNC fCmp) \
381{ \
382 qsort(m_pItems, m_nCount, sizeof(T), fCmp); \
360b63dd
MB
383} \
384 \
385void name::assign(const_iterator first, const_iterator last) \
386{ \
387 clear(); \
388 reserve(last - first); \
389 for(; first != last; ++first) \
390 push_back(*first); \
391} \
392 \
393void name::assign(size_type n, const_reference v) \
394{ \
395 clear(); \
396 reserve(n); \
397 for( size_type i = 0; i < n; ++i ) \
398 push_back(v); \
399} \
400 \
401void name::insert(iterator it, const_iterator first, const_iterator last) \
402{ \
403 size_t nInsert = last - first, nIndex = it - begin(); \
404 if (nInsert == 0) \
405 return; \
406 Grow(nInsert); \
407 \
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) \
411 *it = *first; \
412 m_nCount += nInsert; \
c801d85f
KB
413}
414
df5168c4
MB
415#endif
416
417#define _WX_DEFINE_BASEARRAY(T, name) \
418 _WX_DEFINE_BASEARRAY_COMMON(T, name) \
419 _WX_DEFINE_BASEARRAY_NOCOMMON(T, name)
420
f1322419
VZ
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)
e55e9486 425_WX_DEFINE_BASEARRAY(size_t, wxBaseArraySizeT)
360b63dd 426_WX_DEFINE_BASEARRAY(double, wxBaseArrayDouble)
c801d85f 427
df5168c4
MB
428#if wxUSE_STL
429#include "wx/arrstr.h"
430
2da2f941
MB
431#include "wx/beforestd.h"
432#include <functional>
433#include "wx/afterstd.h"
434
435_WX_DEFINE_BASEARRAY(wxString, wxBaseArrayStringBase);
436
48e4ba4f
VZ
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
441inline int wxStrcmpCppWrapper(const wxChar *p, const wxChar *q)
442{
443 return wxStrcmp(p, q);
444}
445
446inline int wxStricmpCppWrapper(const wxChar *p, const wxChar *q)
447{
448 return wxStricmp(p, q);
449}
450
a1d48124 451int wxArrayString::Index(const wxChar* sz, bool bCase, bool WXUNUSED(bFromEnd)) const
2da2f941
MB
452{
453 wxArrayString::const_iterator it;
454
455 if (bCase)
48e4ba4f 456 {
2da2f941 457 it = std::find_if(begin(), end(),
48e4ba4f
VZ
458 std::not1(
459 std::bind2nd(
460 std::ptr_fun(wxStrcmpCppWrapper), sz)));
461 }
462 else // !bCase
463 {
2da2f941 464 it = std::find_if(begin(), end(),
48e4ba4f
VZ
465 std::not1(
466 std::bind2nd(
467 std::ptr_fun(wxStricmpCppWrapper), sz)));
468 }
2da2f941
MB
469
470 return it == end() ? wxNOT_FOUND : it - begin();
471}
472
4e75b65f 473template<class F>
2da2f941
MB
474class wxStringCompareLess
475{
476public:
4e75b65f 477 wxStringCompareLess(F f) : m_f(f) { }
2da2f941
MB
478 bool operator()(const wxChar* s1, const wxChar* s2)
479 { return m_f(s1, s2) < 0; }
4e75b65f
MB
480 bool operator()(const wxString& s1, const wxString& s2)
481 { return m_f(s1, s2) < 0; }
2da2f941 482private:
4e75b65f 483 F m_f;
2da2f941
MB
484};
485
4e75b65f
MB
486template<class F>
487wxStringCompareLess<F> wxStringCompare(F f)
488{
489 return wxStringCompareLess<F>(f);
490}
491
492void wxArrayString::Sort(CompareFunction function)
493{
494 std::sort(begin(), end(), wxStringCompare(function));
495}
496
497void wxArrayString::Sort(bool reverseOrder)
498{
499 if (reverseOrder)
500 {
501 std::sort(begin(), end(), std::greater<wxString>());
502 }
503 else
504 {
505 std::sort(begin(), end());
506 }
507}
508
a1d48124 509int wxSortedArrayString::Index(const wxChar* sz, bool bCase, bool WXUNUSED(bFromEnd)) const
2da2f941
MB
510{
511 wxSortedArrayString::const_iterator it;
4e75b65f 512 wxString s(sz);
2da2f941
MB
513
514 if (bCase)
4e75b65f
MB
515 it = std::lower_bound(begin(), end(), s,
516 wxStringCompare(wxStrcmpCppWrapper));
2da2f941 517 else
4e75b65f
MB
518 it = std::lower_bound(begin(), end(), s,
519 wxStringCompare(wxStricmpCppWrapper));
2da2f941 520
e11d2abe 521 if (it == end())
2da2f941 522 return wxNOT_FOUND;
e11d2abe
WS
523
524 if (bCase)
525 {
526 if (wxStrcmp(it->c_str(), sz) != 0)
527 return wxNOT_FOUND;
528 }
529 else
530 {
531 if (wxStricmp(it->c_str(), sz) != 0)
532 return wxNOT_FOUND;
533 }
534
2da2f941
MB
535 return it - begin();
536}
df5168c4
MB
537
538#endif