]>
Commit | Line | Data |
---|---|---|
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> | |
55d99c7a | 9 | // Licence: wxWindows licence |
c801d85f KB |
10 | /////////////////////////////////////////////////////////////////////////////// |
11 | ||
12 | // ============================================================================ | |
13 | // headers | |
14 | // ============================================================================ | |
15 | ||
16 | #ifdef __GNUG__ | |
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 | |
38 | wxCOMPILE_TIME_ASSERT( sizeof(long) <= sizeof(void *), | |
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) */ \ | |
58 | int 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 */ \ | |
81 | void name::Add(T lItem, CMPFUNC fnCompare) \ | |
82 | { \ | |
83 | Insert(lItem, IndexForInsert(lItem, fnCompare)); \ | |
84 | } \ | |
85 | \ | |
86 | ||
87 | #if wxUSE_STL | |
88 | ||
89 | #define _WX_DEFINE_BASEARRAY_NOCOMMON(T, name) \ | |
90 | size_t name::IndexForInsert(T lItem, CMPFUNC fnCompare) const \ | |
91 | { \ | |
92 | Predicate p(fnCompare); \ | |
93 | const_iterator it = std::lower_bound(begin(), end(), lItem, p); \ | |
94 | return it - begin(); \ | |
95 | } \ | |
96 | \ | |
97 | int name::Index(T lItem, CMPFUNC fnCompare) const \ | |
98 | { \ | |
99 | size_t n = IndexForInsert(lItem, fnCompare); \ | |
100 | \ | |
101 | return (n >= size() || \ | |
102 | (*fnCompare)(&lItem, &(*this)[n])) ? wxNOT_FOUND : (int)n; \ | |
103 | } \ | |
104 | \ | |
105 | void name::Shrink() \ | |
106 | { \ | |
107 | name tmp(*this); \ | |
108 | swap(tmp); \ | |
109 | } | |
110 | ||
111 | #else // if !wxUSE_STL | |
112 | ||
113 | #define _WX_DEFINE_BASEARRAY_NOCOMMON(T, name) \ | |
5a1cad6e GD |
114 | /* ctor */ \ |
115 | name::name() \ | |
116 | { \ | |
117 | m_nSize = \ | |
118 | m_nCount = 0; \ | |
119 | m_pItems = (T *)NULL; \ | |
120 | } \ | |
121 | \ | |
122 | /* copy ctor */ \ | |
123 | name::name(const name& src) \ | |
124 | { \ | |
125 | m_nSize = /* not src.m_nSize to save memory */ \ | |
126 | m_nCount = src.m_nCount; \ | |
127 | \ | |
128 | if ( m_nSize != 0 ) { \ | |
129 | m_pItems = new T[m_nSize]; \ | |
130 | /* only copy if allocation succeeded */ \ | |
131 | if ( m_pItems ) { \ | |
132 | memcpy(m_pItems, src.m_pItems, m_nCount*sizeof(T)); \ | |
133 | } \ | |
134 | else { \ | |
135 | m_nSize = 0; \ | |
136 | } \ | |
137 | } \ | |
138 | else \ | |
139 | m_pItems = (T *) NULL; \ | |
140 | } \ | |
141 | \ | |
142 | /* assignment operator */ \ | |
143 | name& name::operator=(const name& src) \ | |
144 | { \ | |
145 | wxDELETEA(m_pItems); \ | |
146 | \ | |
147 | m_nSize = /* not src.m_nSize to save memory */ \ | |
148 | m_nCount = src.m_nCount; \ | |
149 | \ | |
150 | if ( m_nSize != 0 ){ \ | |
151 | m_pItems = new T[m_nSize]; \ | |
152 | /* only copy if allocation succeeded */ \ | |
153 | if ( m_pItems ) { \ | |
154 | memcpy(m_pItems, src.m_pItems, m_nCount*sizeof(T)); \ | |
155 | } \ | |
156 | else { \ | |
157 | m_nSize = 0; \ | |
158 | } \ | |
159 | } \ | |
160 | else \ | |
161 | m_pItems = (T *) NULL; \ | |
162 | \ | |
163 | return *this; \ | |
164 | } \ | |
165 | \ | |
2abb9d2f VZ |
166 | /* allocate new buffer of the given size and move our data to it */ \ |
167 | bool name::Realloc(size_t nSize) \ | |
168 | { \ | |
169 | T *pNew = new T[nSize]; \ | |
170 | /* only grow if allocation succeeded */ \ | |
171 | if ( !pNew ) \ | |
172 | return false; \ | |
173 | \ | |
174 | m_nSize = nSize; \ | |
175 | /* copy data to new location */ \ | |
176 | memcpy(pNew, m_pItems, m_nCount*sizeof(T)); \ | |
177 | delete [] m_pItems; \ | |
178 | m_pItems = pNew; \ | |
179 | \ | |
180 | return true; \ | |
181 | } \ | |
182 | \ | |
5a1cad6e | 183 | /* grow the array */ \ |
3b0b5f13 | 184 | void name::Grow(size_t nIncrement) \ |
5a1cad6e GD |
185 | { \ |
186 | /* only do it if no more place */ \ | |
2b5f62a0 | 187 | if( (m_nCount == m_nSize) || ((m_nSize - m_nCount) < nIncrement) ) { \ |
5a1cad6e | 188 | if( m_nSize == 0 ) { \ |
a1db4ad4 SN |
189 | /* was empty, determine initial size */ \ |
190 | size_t size = WX_ARRAY_DEFAULT_INITIAL_SIZE; \ | |
191 | if (size < nIncrement) size = nIncrement; \ | |
192 | /* allocate some memory */ \ | |
193 | m_pItems = new T[size]; \ | |
5a1cad6e GD |
194 | /* only grow if allocation succeeded */ \ |
195 | if ( m_pItems ) { \ | |
a1db4ad4 | 196 | m_nSize = size; \ |
5a1cad6e GD |
197 | } \ |
198 | } \ | |
199 | else \ | |
200 | { \ | |
3b0b5f13 VZ |
201 | /* add at least 50% but not too much */ \ |
202 | size_t ndefIncrement = m_nSize < WX_ARRAY_DEFAULT_INITIAL_SIZE \ | |
203 | ? WX_ARRAY_DEFAULT_INITIAL_SIZE : m_nSize >> 1; \ | |
204 | if ( ndefIncrement > ARRAY_MAXSIZE_INCREMENT ) \ | |
205 | ndefIncrement = ARRAY_MAXSIZE_INCREMENT; \ | |
206 | if ( nIncrement < ndefIncrement ) \ | |
207 | nIncrement = ndefIncrement; \ | |
2abb9d2f | 208 | Realloc(m_nSize + nIncrement); \ |
5a1cad6e GD |
209 | } \ |
210 | } \ | |
211 | } \ | |
212 | \ | |
2abb9d2f VZ |
213 | /* make sure that the array has at least count elements */ \ |
214 | void name::SetCount(size_t count, T defval) \ | |
215 | { \ | |
216 | if ( m_nSize < count ) \ | |
217 | { \ | |
218 | /* need to realloc memory: don't overallocate it here as if */ \ | |
219 | /* SetCount() is called, it probably means that the caller */ \ | |
220 | /* knows in advance how many elements there will be in the */ \ | |
221 | /* array and so it won't be necessary to realloc it later */ \ | |
222 | if ( !Realloc(count) ) \ | |
223 | { \ | |
224 | /* out of memory -- what can we do? */ \ | |
225 | return; \ | |
226 | } \ | |
227 | } \ | |
228 | \ | |
229 | /* add new elements if we extend the array */ \ | |
230 | while ( m_nCount < count ) \ | |
231 | { \ | |
232 | m_pItems[m_nCount++] = defval; \ | |
233 | } \ | |
234 | } \ | |
235 | \ | |
5a1cad6e GD |
236 | /* dtor */ \ |
237 | name::~name() \ | |
238 | { \ | |
239 | wxDELETEA(m_pItems); \ | |
240 | } \ | |
241 | \ | |
242 | /* clears the list */ \ | |
243 | void name::Clear() \ | |
244 | { \ | |
245 | m_nSize = \ | |
246 | m_nCount = 0; \ | |
247 | \ | |
248 | wxDELETEA(m_pItems); \ | |
249 | } \ | |
250 | \ | |
251 | /* pre-allocates memory (frees the previous data!) */ \ | |
252 | void name::Alloc(size_t nSize) \ | |
253 | { \ | |
254 | /* only if old buffer was not big enough */ \ | |
255 | if ( nSize > m_nSize ) { \ | |
256 | wxDELETEA(m_pItems); \ | |
257 | m_nSize = 0; \ | |
258 | m_pItems = new T[nSize]; \ | |
259 | /* only alloc if allocation succeeded */ \ | |
260 | if ( m_pItems ) { \ | |
261 | m_nSize = nSize; \ | |
262 | } \ | |
263 | } \ | |
264 | \ | |
265 | m_nCount = 0; \ | |
266 | } \ | |
267 | \ | |
268 | /* minimizes the memory usage by freeing unused memory */ \ | |
269 | void name::Shrink() \ | |
270 | { \ | |
271 | /* only do it if we have some memory to free */ \ | |
272 | if( m_nCount < m_nSize ) { \ | |
273 | /* allocates exactly as much memory as we need */ \ | |
274 | T *pNew = new T[m_nCount]; \ | |
275 | /* only shrink if allocation succeeded */ \ | |
276 | if ( pNew ) { \ | |
277 | /* copy data to new location */ \ | |
278 | memcpy(pNew, m_pItems, m_nCount*sizeof(T)); \ | |
279 | delete [] m_pItems; \ | |
280 | m_pItems = pNew; \ | |
79e929e7 VZ |
281 | \ |
282 | /* update the size of the new block */ \ | |
283 | m_nSize = m_nCount; \ | |
5a1cad6e | 284 | } \ |
79e929e7 | 285 | /* else: don't do anything, better keep old memory block! */ \ |
5a1cad6e GD |
286 | } \ |
287 | } \ | |
288 | \ | |
df5168c4 MB |
289 | /* add item at the end */ \ |
290 | void name::Add(T lItem, size_t nInsert) \ | |
5a1cad6e | 291 | { \ |
df5168c4 MB |
292 | if (nInsert == 0) \ |
293 | return; \ | |
294 | Grow(nInsert); \ | |
295 | for (size_t i = 0; i < nInsert; i++) \ | |
296 | m_pItems[m_nCount++] = lItem; \ | |
297 | } \ | |
5a1cad6e | 298 | \ |
df5168c4 MB |
299 | /* add item at the given position */ \ |
300 | void name::Insert(T lItem, size_t nIndex, size_t nInsert) \ | |
301 | { \ | |
302 | wxCHECK_RET( nIndex <= m_nCount, wxT("bad index in wxArray::Insert") ); \ | |
303 | wxCHECK_RET( m_nCount <= m_nCount + nInsert, \ | |
304 | wxT("array size overflow in wxArray::Insert") ); \ | |
305 | \ | |
306 | if (nInsert == 0) \ | |
307 | return; \ | |
308 | Grow(nInsert); \ | |
309 | \ | |
310 | memmove(&m_pItems[nIndex + nInsert], &m_pItems[nIndex], \ | |
311 | (m_nCount - nIndex)*sizeof(T)); \ | |
312 | for (size_t i = 0; i < nInsert; i++) \ | |
313 | m_pItems[nIndex + i] = lItem; \ | |
314 | m_nCount += nInsert; \ | |
5a1cad6e GD |
315 | } \ |
316 | \ | |
317 | /* search for a place to insert item into sorted array (binary search) */ \ | |
318 | size_t name::IndexForInsert(T lItem, CMPFUNC fnCompare) const \ | |
319 | { \ | |
320 | size_t i, \ | |
321 | lo = 0, \ | |
322 | hi = m_nCount; \ | |
323 | int res; \ | |
324 | \ | |
325 | while ( lo < hi ) { \ | |
326 | i = (lo + hi)/2; \ | |
327 | \ | |
acf88ae6 VZ |
328 | res = (*fnCompare)((const void *)(long)lItem, \ |
329 | (const void *)(long)(m_pItems[i])); \ | |
5a1cad6e GD |
330 | if ( res < 0 ) \ |
331 | hi = i; \ | |
332 | else if ( res > 0 ) \ | |
333 | lo = i + 1; \ | |
334 | else { \ | |
335 | lo = i; \ | |
336 | break; \ | |
337 | } \ | |
338 | } \ | |
339 | \ | |
340 | return lo; \ | |
341 | } \ | |
342 | \ | |
343 | /* search for an item in a sorted array (binary search) */ \ | |
344 | int name::Index(T lItem, CMPFUNC fnCompare) const \ | |
345 | { \ | |
346 | size_t n = IndexForInsert(lItem, fnCompare); \ | |
347 | \ | |
bb793b0c | 348 | return (n >= m_nCount || \ |
4365da58 | 349 | (*fnCompare)((const void *)(long)lItem, \ |
bb793b0c VZ |
350 | ((const void *)(long)m_pItems[n]))) ? wxNOT_FOUND \ |
351 | : (int)n; \ | |
5a1cad6e GD |
352 | } \ |
353 | \ | |
5a1cad6e | 354 | /* removes item from array (by index) */ \ |
3b0b5f13 | 355 | void name::RemoveAt(size_t nIndex, size_t nRemove) \ |
5a1cad6e | 356 | { \ |
3b0b5f13 VZ |
357 | wxCHECK_RET( nIndex < m_nCount, wxT("bad index in wxArray::RemoveAt") ); \ |
358 | wxCHECK_RET( nIndex + nRemove <= m_nCount, \ | |
359 | wxT("removing too many elements in wxArray::RemoveAt") ); \ | |
5a1cad6e | 360 | \ |
3b0b5f13 VZ |
361 | memmove(&m_pItems[nIndex], &m_pItems[nIndex + nRemove], \ |
362 | (m_nCount - nIndex - nRemove)*sizeof(T)); \ | |
363 | m_nCount -= nRemove; \ | |
5a1cad6e GD |
364 | } \ |
365 | \ | |
366 | /* removes item from array (by value) */ \ | |
367 | void name::Remove(T lItem) \ | |
368 | { \ | |
369 | int iIndex = Index(lItem); \ | |
370 | \ | |
371 | wxCHECK_RET( iIndex != wxNOT_FOUND, \ | |
372 | wxT("removing inexistent item in wxArray::Remove") ); \ | |
373 | \ | |
374 | RemoveAt((size_t)iIndex); \ | |
375 | } \ | |
376 | \ | |
377 | /* sort array elements using passed comparaison function */ \ | |
378 | void name::Sort(CMPFUNC fCmp) \ | |
379 | { \ | |
380 | qsort(m_pItems, m_nCount, sizeof(T), fCmp); \ | |
c801d85f KB |
381 | } |
382 | ||
df5168c4 MB |
383 | #endif |
384 | ||
385 | #define _WX_DEFINE_BASEARRAY(T, name) \ | |
386 | _WX_DEFINE_BASEARRAY_COMMON(T, name) \ | |
387 | _WX_DEFINE_BASEARRAY_NOCOMMON(T, name) | |
388 | ||
f1322419 VZ |
389 | _WX_DEFINE_BASEARRAY(const void *, wxBaseArrayPtrVoid) |
390 | _WX_DEFINE_BASEARRAY(short, wxBaseArrayShort) | |
391 | _WX_DEFINE_BASEARRAY(int, wxBaseArrayInt) | |
392 | _WX_DEFINE_BASEARRAY(long, wxBaseArrayLong) | |
393 | //_WX_DEFINE_BASEARRAY(double, wxBaseArrayDouble) | |
c801d85f | 394 | |
df5168c4 MB |
395 | #if wxUSE_STL |
396 | #include "wx/arrstr.h" | |
397 | ||
398 | _WX_DEFINE_BASEARRAY(wxString, wxBaseArrayStringBase) | |
399 | ||
400 | #endif |