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