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