]> git.saurik.com Git - wxWidgets.git/blame - src/common/dynarray.cpp
added wxStaticText::Wrap()
[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 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
df5168c4
MB
52#define _WX_DEFINE_BASEARRAY_COMMON(T, name) \
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; \
82}
df5168c4
MB
83
84#if wxUSE_STL
85
86#define _WX_DEFINE_BASEARRAY_NOCOMMON(T, name) \
87size_t name::IndexForInsert(T lItem, CMPFUNC fnCompare) const \
88{ \
f700f98c 89 Predicate p((SCMPFUNC)fnCompare); \
df5168c4
MB
90 const_iterator it = std::lower_bound(begin(), end(), lItem, p); \
91 return it - begin(); \
92} \
93 \
94int name::Index(T lItem, CMPFUNC fnCompare) const \
95{ \
f700f98c
MB
96 Predicate p((SCMPFUNC)fnCompare); \
97 const_iterator it = std::lower_bound(begin(), end(), lItem, p); \
10dcbe63
MB
98 return (it != end() && !p(lItem, *it)) ? \
99 (int)(it - begin()) : wxNOT_FOUND; \
df5168c4
MB
100} \
101 \
102void name::Shrink() \
103{ \
104 name tmp(*this); \
105 swap(tmp); \
106}
107
108#else // if !wxUSE_STL
109
110#define _WX_DEFINE_BASEARRAY_NOCOMMON(T, name) \
5a1cad6e
GD
111/* ctor */ \
112name::name() \
113{ \
114 m_nSize = \
115 m_nCount = 0; \
116 m_pItems = (T *)NULL; \
117} \
118 \
119/* copy ctor */ \
120name::name(const name& src) \
121{ \
122 m_nSize = /* not src.m_nSize to save memory */ \
123 m_nCount = src.m_nCount; \
124 \
125 if ( m_nSize != 0 ) { \
126 m_pItems = new T[m_nSize]; \
127 /* only copy if allocation succeeded */ \
128 if ( m_pItems ) { \
129 memcpy(m_pItems, src.m_pItems, m_nCount*sizeof(T)); \
130 } \
131 else { \
132 m_nSize = 0; \
133 } \
134 } \
135 else \
136 m_pItems = (T *) NULL; \
137} \
138 \
139/* assignment operator */ \
140name& name::operator=(const name& src) \
141{ \
142 wxDELETEA(m_pItems); \
143 \
144 m_nSize = /* not src.m_nSize to save memory */ \
145 m_nCount = src.m_nCount; \
146 \
147 if ( m_nSize != 0 ){ \
148 m_pItems = new T[m_nSize]; \
149 /* only copy if allocation succeeded */ \
150 if ( m_pItems ) { \
151 memcpy(m_pItems, src.m_pItems, m_nCount*sizeof(T)); \
152 } \
153 else { \
154 m_nSize = 0; \
155 } \
156 } \
157 else \
158 m_pItems = (T *) NULL; \
159 \
160 return *this; \
161} \
162 \
2abb9d2f
VZ
163/* allocate new buffer of the given size and move our data to it */ \
164bool name::Realloc(size_t nSize) \
165{ \
166 T *pNew = new T[nSize]; \
167 /* only grow if allocation succeeded */ \
168 if ( !pNew ) \
169 return false; \
170 \
171 m_nSize = nSize; \
172 /* copy data to new location */ \
173 memcpy(pNew, m_pItems, m_nCount*sizeof(T)); \
174 delete [] m_pItems; \
175 m_pItems = pNew; \
176 \
177 return true; \
178} \
179 \
5a1cad6e 180/* grow the array */ \
3b0b5f13 181void name::Grow(size_t nIncrement) \
5a1cad6e
GD
182{ \
183 /* only do it if no more place */ \
2b5f62a0 184 if( (m_nCount == m_nSize) || ((m_nSize - m_nCount) < nIncrement) ) { \
5a1cad6e 185 if( m_nSize == 0 ) { \
a1db4ad4
SN
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]; \
5a1cad6e
GD
191 /* only grow if allocation succeeded */ \
192 if ( m_pItems ) { \
a1db4ad4 193 m_nSize = size; \
5a1cad6e
GD
194 } \
195 } \
196 else \
197 { \
3b0b5f13
VZ
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; \
2abb9d2f 205 Realloc(m_nSize + nIncrement); \
5a1cad6e
GD
206 } \
207 } \
208} \
209 \
2abb9d2f
VZ
210/* make sure that the array has at least count elements */ \
211void name::SetCount(size_t count, T defval) \
212{ \
213 if ( m_nSize < count ) \
214 { \
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) ) \
220 { \
221 /* out of memory -- what can we do? */ \
222 return; \
223 } \
224 } \
225 \
226 /* add new elements if we extend the array */ \
227 while ( m_nCount < count ) \
228 { \
229 m_pItems[m_nCount++] = defval; \
230 } \
231} \
232 \
5a1cad6e
GD
233/* dtor */ \
234name::~name() \
235{ \
236 wxDELETEA(m_pItems); \
237} \
238 \
239/* clears the list */ \
240void name::Clear() \
241{ \
242 m_nSize = \
243 m_nCount = 0; \
244 \
245 wxDELETEA(m_pItems); \
246} \
247 \
248/* pre-allocates memory (frees the previous data!) */ \
249void name::Alloc(size_t nSize) \
250{ \
251 /* only if old buffer was not big enough */ \
252 if ( nSize > m_nSize ) { \
253 wxDELETEA(m_pItems); \
254 m_nSize = 0; \
255 m_pItems = new T[nSize]; \
256 /* only alloc if allocation succeeded */ \
257 if ( m_pItems ) { \
258 m_nSize = nSize; \
259 } \
260 } \
261 \
262 m_nCount = 0; \
263} \
264 \
265/* minimizes the memory usage by freeing unused memory */ \
266void name::Shrink() \
267{ \
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 */ \
273 if ( pNew ) { \
274 /* copy data to new location */ \
275 memcpy(pNew, m_pItems, m_nCount*sizeof(T)); \
276 delete [] m_pItems; \
277 m_pItems = pNew; \
79e929e7
VZ
278 \
279 /* update the size of the new block */ \
280 m_nSize = m_nCount; \
5a1cad6e 281 } \
79e929e7 282 /* else: don't do anything, better keep old memory block! */ \
5a1cad6e
GD
283 } \
284} \
285 \
df5168c4
MB
286/* add item at the end */ \
287void name::Add(T lItem, size_t nInsert) \
5a1cad6e 288{ \
df5168c4
MB
289 if (nInsert == 0) \
290 return; \
291 Grow(nInsert); \
292 for (size_t i = 0; i < nInsert; i++) \
293 m_pItems[m_nCount++] = lItem; \
294} \
5a1cad6e 295 \
df5168c4
MB
296/* add item at the given position */ \
297void name::Insert(T lItem, size_t nIndex, size_t nInsert) \
298{ \
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") ); \
302 \
303 if (nInsert == 0) \
304 return; \
305 Grow(nInsert); \
306 \
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; \
5a1cad6e
GD
312} \
313 \
314/* search for a place to insert item into sorted array (binary search) */ \
315size_t name::IndexForInsert(T lItem, CMPFUNC fnCompare) const \
316{ \
317 size_t i, \
318 lo = 0, \
319 hi = m_nCount; \
320 int res; \
321 \
322 while ( lo < hi ) { \
323 i = (lo + hi)/2; \
324 \
af5b273c
VZ
325 res = (*fnCompare)((const void *)(wxUIntPtr)lItem, \
326 (const void *)(wxUIntPtr)(m_pItems[i])); \
5a1cad6e
GD
327 if ( res < 0 ) \
328 hi = i; \
329 else if ( res > 0 ) \
330 lo = i + 1; \
331 else { \
332 lo = i; \
333 break; \
334 } \
335 } \
336 \
337 return lo; \
338} \
339 \
340/* search for an item in a sorted array (binary search) */ \
341int name::Index(T lItem, CMPFUNC fnCompare) const \
342{ \
343 size_t n = IndexForInsert(lItem, fnCompare); \
344 \
bb793b0c 345 return (n >= m_nCount || \
af5b273c
VZ
346 (*fnCompare)((const void *)(wxUIntPtr)lItem, \
347 ((const void *)(wxUIntPtr)m_pItems[n]))) \
348 ? wxNOT_FOUND \
349 : (int)n; \
5a1cad6e
GD
350} \
351 \
5a1cad6e 352/* removes item from array (by index) */ \
3b0b5f13 353void name::RemoveAt(size_t nIndex, size_t nRemove) \
5a1cad6e 354{ \
3b0b5f13
VZ
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") ); \
5a1cad6e 358 \
3b0b5f13
VZ
359 memmove(&m_pItems[nIndex], &m_pItems[nIndex + nRemove], \
360 (m_nCount - nIndex - nRemove)*sizeof(T)); \
361 m_nCount -= nRemove; \
5a1cad6e
GD
362} \
363 \
364/* removes item from array (by value) */ \
365void name::Remove(T lItem) \
366{ \
367 int iIndex = Index(lItem); \
368 \
369 wxCHECK_RET( iIndex != wxNOT_FOUND, \
370 wxT("removing inexistent item in wxArray::Remove") ); \
371 \
372 RemoveAt((size_t)iIndex); \
373} \
374 \
375/* sort array elements using passed comparaison function */ \
376void name::Sort(CMPFUNC fCmp) \
377{ \
378 qsort(m_pItems, m_nCount, sizeof(T), fCmp); \
360b63dd
MB
379} \
380 \
381void name::assign(const_iterator first, const_iterator last) \
382{ \
383 clear(); \
384 reserve(last - first); \
385 for(; first != last; ++first) \
386 push_back(*first); \
387} \
388 \
389void name::assign(size_type n, const_reference v) \
390{ \
391 clear(); \
392 reserve(n); \
393 for( size_type i = 0; i < n; ++i ) \
394 push_back(v); \
395} \
396 \
397void name::insert(iterator it, const_iterator first, const_iterator last) \
398{ \
399 size_t nInsert = last - first, nIndex = it - begin(); \
400 if (nInsert == 0) \
401 return; \
402 Grow(nInsert); \
403 \
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) \
407 *it = *first; \
408 m_nCount += nInsert; \
c801d85f
KB
409}
410
df5168c4
MB
411#endif
412
413#define _WX_DEFINE_BASEARRAY(T, name) \
414 _WX_DEFINE_BASEARRAY_COMMON(T, name) \
415 _WX_DEFINE_BASEARRAY_NOCOMMON(T, name)
416
f1322419
VZ
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)
e55e9486 421_WX_DEFINE_BASEARRAY(size_t, wxBaseArraySizeT)
360b63dd 422_WX_DEFINE_BASEARRAY(double, wxBaseArrayDouble)
c801d85f 423
df5168c4
MB
424#if wxUSE_STL
425#include "wx/arrstr.h"
426
2da2f941
MB
427#include "wx/beforestd.h"
428#include <functional>
429#include "wx/afterstd.h"
430
431_WX_DEFINE_BASEARRAY(wxString, wxBaseArrayStringBase);
432
48e4ba4f
VZ
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
437inline int wxStrcmpCppWrapper(const wxChar *p, const wxChar *q)
438{
439 return wxStrcmp(p, q);
440}
441
442inline int wxStricmpCppWrapper(const wxChar *p, const wxChar *q)
443{
444 return wxStricmp(p, q);
445}
446
a1d48124 447int wxArrayString::Index(const wxChar* sz, bool bCase, bool WXUNUSED(bFromEnd)) const
2da2f941
MB
448{
449 wxArrayString::const_iterator it;
450
451 if (bCase)
48e4ba4f 452 {
2da2f941 453 it = std::find_if(begin(), end(),
48e4ba4f
VZ
454 std::not1(
455 std::bind2nd(
456 std::ptr_fun(wxStrcmpCppWrapper), sz)));
457 }
458 else // !bCase
459 {
2da2f941 460 it = std::find_if(begin(), end(),
48e4ba4f
VZ
461 std::not1(
462 std::bind2nd(
463 std::ptr_fun(wxStricmpCppWrapper), sz)));
464 }
2da2f941
MB
465
466 return it == end() ? wxNOT_FOUND : it - begin();
467}
468
4e75b65f 469template<class F>
2da2f941
MB
470class wxStringCompareLess
471{
472public:
4e75b65f 473 wxStringCompareLess(F f) : m_f(f) { }
2da2f941
MB
474 bool operator()(const wxChar* s1, const wxChar* s2)
475 { return m_f(s1, s2) < 0; }
4e75b65f
MB
476 bool operator()(const wxString& s1, const wxString& s2)
477 { return m_f(s1, s2) < 0; }
2da2f941 478private:
4e75b65f 479 F m_f;
2da2f941
MB
480};
481
4e75b65f
MB
482template<class F>
483wxStringCompareLess<F> wxStringCompare(F f)
484{
485 return wxStringCompareLess<F>(f);
486}
487
488void wxArrayString::Sort(CompareFunction function)
489{
490 std::sort(begin(), end(), wxStringCompare(function));
491}
492
493void wxArrayString::Sort(bool reverseOrder)
494{
495 if (reverseOrder)
496 {
497 std::sort(begin(), end(), std::greater<wxString>());
498 }
499 else
500 {
501 std::sort(begin(), end());
502 }
503}
504
a1d48124 505int wxSortedArrayString::Index(const wxChar* sz, bool bCase, bool WXUNUSED(bFromEnd)) const
2da2f941
MB
506{
507 wxSortedArrayString::const_iterator it;
4e75b65f 508 wxString s(sz);
2da2f941
MB
509
510 if (bCase)
4e75b65f
MB
511 it = std::lower_bound(begin(), end(), s,
512 wxStringCompare(wxStrcmpCppWrapper));
2da2f941 513 else
4e75b65f
MB
514 it = std::lower_bound(begin(), end(), s,
515 wxStringCompare(wxStricmpCppWrapper));
2da2f941 516
e11d2abe 517 if (it == end())
2da2f941 518 return wxNOT_FOUND;
e11d2abe
WS
519
520 if (bCase)
521 {
522 if (wxStrcmp(it->c_str(), sz) != 0)
523 return wxNOT_FOUND;
524 }
525 else
526 {
527 if (wxStricmp(it->c_str(), sz) != 0)
528 return wxNOT_FOUND;
529 }
530
2da2f941
MB
531 return it - begin();
532}
df5168c4
MB
533
534#endif