Include wx/dynarray.h according to precompiled headers of wx/wx.h (with other minor...
[wxWidgets.git] / src / common / dynarray.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/dynarray.cpp
3 // Purpose: implementation of wxBaseArray class
4 // Author: Vadim Zeitlin
5 // Modified by:
6 // Created: 12.09.97
7 // RCS-ID: $Id$
8 // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // headers
14 // ============================================================================
15
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
18
19 #ifdef __BORLANDC__
20 #pragma hdrstop
21 #endif
22
23 #ifndef WX_PRECOMP
24 #include "wx/dynarray.h"
25 #endif //WX_PRECOMP
26
27 #include "wx/intl.h"
28
29 #include <stdlib.h>
30 #include <string.h> // for memmove
31
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
34 wxCOMPILE_TIME_ASSERT( sizeof(wxUIntPtr) <= sizeof(void *),
35 wxArraySizeOfPtrLessSizeOfLong ); // < 32 symbols
36
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 // ----------------------------------------------------------------------------
49 // wxBaseArray - dynamic array of 'T's
50 // ----------------------------------------------------------------------------
51
52 #define _WX_DEFINE_BASEARRAY_COMMON(T, name) \
53 /* searches the array for an item (forward or backwards) */ \
54 int 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 */ \
77 size_t name::Add(T lItem, CMPFUNC fnCompare) \
78 { \
79 size_t idx = IndexForInsert(lItem, fnCompare); \
80 Insert(lItem, idx); \
81 return idx; \
82 }
83
84 #if wxUSE_STL
85
86 #define _WX_DEFINE_BASEARRAY_NOCOMMON(T, name) \
87 size_t name::IndexForInsert(T lItem, CMPFUNC fnCompare) const \
88 { \
89 Predicate p((SCMPFUNC)fnCompare); \
90 const_iterator it = std::lower_bound(begin(), end(), lItem, p); \
91 return it - begin(); \
92 } \
93 \
94 int name::Index(T lItem, CMPFUNC fnCompare) const \
95 { \
96 Predicate p((SCMPFUNC)fnCompare); \
97 const_iterator it = std::lower_bound(begin(), end(), lItem, p); \
98 return (it != end() && !p(lItem, *it)) ? \
99 (int)(it - begin()) : wxNOT_FOUND; \
100 } \
101 \
102 void 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) \
111 /* ctor */ \
112 name::name() \
113 { \
114 m_nSize = \
115 m_nCount = 0; \
116 m_pItems = (T *)NULL; \
117 } \
118 \
119 /* copy ctor */ \
120 name::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 */ \
140 name& 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 \
163 /* allocate new buffer of the given size and move our data to it */ \
164 bool 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 \
180 /* grow the array */ \
181 void name::Grow(size_t nIncrement) \
182 { \
183 /* only do it if no more place */ \
184 if( (m_nCount == m_nSize) || ((m_nSize - m_nCount) < nIncrement) ) { \
185 if( m_nSize == 0 ) { \
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]; \
191 /* only grow if allocation succeeded */ \
192 if ( m_pItems ) { \
193 m_nSize = size; \
194 } \
195 } \
196 else \
197 { \
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; \
205 Realloc(m_nSize + nIncrement); \
206 } \
207 } \
208 } \
209 \
210 /* make sure that the array has at least count elements */ \
211 void 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 \
233 /* dtor */ \
234 name::~name() \
235 { \
236 wxDELETEA(m_pItems); \
237 } \
238 \
239 /* clears the list */ \
240 void 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!) */ \
249 void 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 */ \
266 void 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; \
278 \
279 /* update the size of the new block */ \
280 m_nSize = m_nCount; \
281 } \
282 /* else: don't do anything, better keep old memory block! */ \
283 } \
284 } \
285 \
286 /* add item at the end */ \
287 void name::Add(T lItem, size_t nInsert) \
288 { \
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 } \
295 \
296 /* add item at the given position */ \
297 void 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; \
312 } \
313 \
314 /* search for a place to insert item into sorted array (binary search) */ \
315 size_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 \
325 res = (*fnCompare)((const void *)(wxUIntPtr)lItem, \
326 (const void *)(wxUIntPtr)(m_pItems[i])); \
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) */ \
341 int name::Index(T lItem, CMPFUNC fnCompare) const \
342 { \
343 size_t n = IndexForInsert(lItem, fnCompare); \
344 \
345 return (n >= m_nCount || \
346 (*fnCompare)((const void *)(wxUIntPtr)lItem, \
347 ((const void *)(wxUIntPtr)m_pItems[n]))) \
348 ? wxNOT_FOUND \
349 : (int)n; \
350 } \
351 \
352 /* removes item from array (by index) */ \
353 void name::RemoveAt(size_t nIndex, size_t nRemove) \
354 { \
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") ); \
358 \
359 memmove(&m_pItems[nIndex], &m_pItems[nIndex + nRemove], \
360 (m_nCount - nIndex - nRemove)*sizeof(T)); \
361 m_nCount -= nRemove; \
362 } \
363 \
364 /* removes item from array (by value) */ \
365 void 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 */ \
376 void name::Sort(CMPFUNC fCmp) \
377 { \
378 qsort(m_pItems, m_nCount, sizeof(T), fCmp); \
379 } \
380 \
381 void 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 \
389 void 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 \
397 void 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; \
409 }
410
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
417 #ifdef __INTELC__
418 #pragma warning(push)
419 #pragma warning(disable: 1684)
420 #pragma warning(disable: 1572)
421 #endif
422
423 _WX_DEFINE_BASEARRAY(const void *, wxBaseArrayPtrVoid)
424 _WX_DEFINE_BASEARRAY(short, wxBaseArrayShort)
425 _WX_DEFINE_BASEARRAY(int, wxBaseArrayInt)
426 _WX_DEFINE_BASEARRAY(long, wxBaseArrayLong)
427 _WX_DEFINE_BASEARRAY(size_t, wxBaseArraySizeT)
428 _WX_DEFINE_BASEARRAY(double, wxBaseArrayDouble)
429
430 #ifdef __INTELC__
431 #pragma warning(pop)
432 #endif
433
434 #if wxUSE_STL
435 #include "wx/arrstr.h"
436
437 #include "wx/beforestd.h"
438 #include <functional>
439 #include "wx/afterstd.h"
440
441 _WX_DEFINE_BASEARRAY(wxString, wxBaseArrayStringBase)
442
443 // some compilers (Sun CC being the only known example) distinguish between
444 // extern "C" functions and the functions with C++ linkage and ptr_fun and
445 // wxStringCompareLess can't take wxStrcmp/wxStricmp directly as arguments in
446 // this case, we need the wrappers below to make this work
447 inline int wxStrcmpCppWrapper(const wxChar *p, const wxChar *q)
448 {
449 return wxStrcmp(p, q);
450 }
451
452 inline int wxStricmpCppWrapper(const wxChar *p, const wxChar *q)
453 {
454 return wxStricmp(p, q);
455 }
456
457 int wxArrayString::Index(const wxChar* sz, bool bCase, bool WXUNUSED(bFromEnd)) const
458 {
459 wxArrayString::const_iterator it;
460
461 if (bCase)
462 {
463 it = std::find_if(begin(), end(),
464 std::not1(
465 std::bind2nd(
466 std::ptr_fun(wxStrcmpCppWrapper), sz)));
467 }
468 else // !bCase
469 {
470 it = std::find_if(begin(), end(),
471 std::not1(
472 std::bind2nd(
473 std::ptr_fun(wxStricmpCppWrapper), sz)));
474 }
475
476 return it == end() ? wxNOT_FOUND : it - begin();
477 }
478
479 template<class F>
480 class wxStringCompareLess
481 {
482 public:
483 wxStringCompareLess(F f) : m_f(f) { }
484 bool operator()(const wxChar* s1, const wxChar* s2)
485 { return m_f(s1, s2) < 0; }
486 bool operator()(const wxString& s1, const wxString& s2)
487 { return m_f(s1, s2) < 0; }
488 private:
489 F m_f;
490 };
491
492 template<class F>
493 wxStringCompareLess<F> wxStringCompare(F f)
494 {
495 return wxStringCompareLess<F>(f);
496 }
497
498 void wxArrayString::Sort(CompareFunction function)
499 {
500 std::sort(begin(), end(), wxStringCompare(function));
501 }
502
503 void wxArrayString::Sort(bool reverseOrder)
504 {
505 if (reverseOrder)
506 {
507 std::sort(begin(), end(), std::greater<wxString>());
508 }
509 else
510 {
511 std::sort(begin(), end());
512 }
513 }
514
515 int wxSortedArrayString::Index(const wxChar* sz, bool bCase, bool WXUNUSED(bFromEnd)) const
516 {
517 wxSortedArrayString::const_iterator it;
518 wxString s(sz);
519
520 if (bCase)
521 it = std::lower_bound(begin(), end(), s,
522 wxStringCompare(wxStrcmpCppWrapper));
523 else
524 it = std::lower_bound(begin(), end(), s,
525 wxStringCompare(wxStricmpCppWrapper));
526
527 if (it == end())
528 return wxNOT_FOUND;
529
530 if (bCase)
531 {
532 if (wxStrcmp(it->c_str(), sz) != 0)
533 return wxNOT_FOUND;
534 }
535 else
536 {
537 if (wxStricmp(it->c_str(), sz) != 0)
538 return wxNOT_FOUND;
539 }
540
541 return it - begin();
542 }
543
544 #endif