]> git.saurik.com Git - wxWidgets.git/blob - src/common/dynarray.cpp
Reverted accidental commit.
[wxWidgets.git] / src / common / dynarray.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: 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 license
10 ///////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // headers
14 // ============================================================================
15
16 #ifdef __GNUG__
17 #pragma implementation "dynarray.h"
18 #endif
19
20 #include "wx/wxprec.h"
21
22 #ifdef __BORLANDC__
23 #pragma hdrstop
24 #endif
25
26 #include "wx/dynarray.h"
27 #include "wx/intl.h"
28
29 #include <stdlib.h>
30 #include <string.h> // for memmove
31 #ifdef __WINE__
32 #include <search.h>
33 #endif
34
35 #ifndef max
36 #define max(a, b) (((a) > (b)) ? (a) : (b))
37 #endif
38
39 // we cast the value to long from which we cast it to void * in IndexForInsert:
40 // this can't work if the pointers are not big enough
41 wxCOMPILE_TIME_ASSERT( sizeof(long) <= sizeof(void *),
42 wxArraySizeOfPtrLessSizeOfLong ); // < 32 symbols
43
44 // ============================================================================
45 // constants
46 // ============================================================================
47
48 // size increment = max(50% of current size, ARRAY_MAXSIZE_INCREMENT)
49 #define ARRAY_MAXSIZE_INCREMENT 4096
50
51 // ============================================================================
52 // implementation
53 // ============================================================================
54
55 // ----------------------------------------------------------------------------
56 // wxBaseArray - dynamic array of 'T's
57 // ----------------------------------------------------------------------------
58
59 #define _WX_DEFINE_BASEARRAY(T, name) \
60 /* ctor */ \
61 name::name() \
62 { \
63 m_nSize = \
64 m_nCount = 0; \
65 m_pItems = (T *)NULL; \
66 } \
67 \
68 /* copy ctor */ \
69 name::name(const name& src) \
70 { \
71 m_nSize = /* not src.m_nSize to save memory */ \
72 m_nCount = src.m_nCount; \
73 \
74 if ( m_nSize != 0 ) { \
75 m_pItems = new T[m_nSize]; \
76 /* only copy if allocation succeeded */ \
77 if ( m_pItems ) { \
78 memcpy(m_pItems, src.m_pItems, m_nCount*sizeof(T)); \
79 } \
80 else { \
81 m_nSize = 0; \
82 } \
83 } \
84 else \
85 m_pItems = (T *) NULL; \
86 } \
87 \
88 /* assignment operator */ \
89 name& name::operator=(const name& src) \
90 { \
91 wxDELETEA(m_pItems); \
92 \
93 m_nSize = /* not src.m_nSize to save memory */ \
94 m_nCount = src.m_nCount; \
95 \
96 if ( m_nSize != 0 ){ \
97 m_pItems = new T[m_nSize]; \
98 /* only copy if allocation succeeded */ \
99 if ( m_pItems ) { \
100 memcpy(m_pItems, src.m_pItems, m_nCount*sizeof(T)); \
101 } \
102 else { \
103 m_nSize = 0; \
104 } \
105 } \
106 else \
107 m_pItems = (T *) NULL; \
108 \
109 return *this; \
110 } \
111 \
112 /* grow the array */ \
113 void name::Grow(size_t nIncrement) \
114 { \
115 /* only do it if no more place */ \
116 if( (m_nCount == m_nSize) || ((m_nSize - m_nCount) < nIncrement) ) { \
117 if( m_nSize == 0 ) { \
118 /* was empty, determine initial size */ \
119 size_t size = WX_ARRAY_DEFAULT_INITIAL_SIZE; \
120 if (size < nIncrement) size = nIncrement; \
121 /* allocate some memory */ \
122 m_pItems = new T[size]; \
123 /* only grow if allocation succeeded */ \
124 if ( m_pItems ) { \
125 m_nSize = size; \
126 } \
127 } \
128 else \
129 { \
130 /* add at least 50% but not too much */ \
131 size_t ndefIncrement = m_nSize < WX_ARRAY_DEFAULT_INITIAL_SIZE \
132 ? WX_ARRAY_DEFAULT_INITIAL_SIZE : m_nSize >> 1; \
133 if ( ndefIncrement > ARRAY_MAXSIZE_INCREMENT ) \
134 ndefIncrement = ARRAY_MAXSIZE_INCREMENT; \
135 if ( nIncrement < ndefIncrement ) \
136 nIncrement = ndefIncrement; \
137 T *pNew = new T[m_nSize + nIncrement]; \
138 /* only grow if allocation succeeded */ \
139 if ( pNew ) { \
140 m_nSize += nIncrement; \
141 /* copy data to new location */ \
142 memcpy(pNew, m_pItems, m_nCount*sizeof(T)); \
143 delete [] m_pItems; \
144 m_pItems = pNew; \
145 } \
146 } \
147 } \
148 } \
149 \
150 /* dtor */ \
151 name::~name() \
152 { \
153 wxDELETEA(m_pItems); \
154 } \
155 \
156 /* clears the list */ \
157 void name::Clear() \
158 { \
159 m_nSize = \
160 m_nCount = 0; \
161 \
162 wxDELETEA(m_pItems); \
163 } \
164 \
165 /* pre-allocates memory (frees the previous data!) */ \
166 void name::Alloc(size_t nSize) \
167 { \
168 /* only if old buffer was not big enough */ \
169 if ( nSize > m_nSize ) { \
170 wxDELETEA(m_pItems); \
171 m_nSize = 0; \
172 m_pItems = new T[nSize]; \
173 /* only alloc if allocation succeeded */ \
174 if ( m_pItems ) { \
175 m_nSize = nSize; \
176 } \
177 } \
178 \
179 m_nCount = 0; \
180 } \
181 \
182 /* minimizes the memory usage by freeing unused memory */ \
183 void name::Shrink() \
184 { \
185 /* only do it if we have some memory to free */ \
186 if( m_nCount < m_nSize ) { \
187 /* allocates exactly as much memory as we need */ \
188 T *pNew = new T[m_nCount]; \
189 /* only shrink if allocation succeeded */ \
190 if ( pNew ) { \
191 /* copy data to new location */ \
192 memcpy(pNew, m_pItems, m_nCount*sizeof(T)); \
193 delete [] m_pItems; \
194 m_pItems = pNew; \
195 } \
196 } \
197 } \
198 \
199 /* searches the array for an item (forward or backwards) */ \
200 int name::Index(T lItem, bool bFromEnd) const \
201 { \
202 if ( bFromEnd ) { \
203 if ( m_nCount > 0 ) { \
204 size_t n = m_nCount; \
205 do { \
206 if ( m_pItems[--n] == lItem ) \
207 return n; \
208 } \
209 while ( n != 0 ); \
210 } \
211 } \
212 else { \
213 for( size_t n = 0; n < m_nCount; n++ ) { \
214 if( m_pItems[n] == lItem ) \
215 return n; \
216 } \
217 } \
218 \
219 return wxNOT_FOUND; \
220 } \
221 \
222 /* search for a place to insert item into sorted array (binary search) */ \
223 size_t name::IndexForInsert(T lItem, CMPFUNC fnCompare) const \
224 { \
225 size_t i, \
226 lo = 0, \
227 hi = m_nCount; \
228 int res; \
229 \
230 while ( lo < hi ) { \
231 i = (lo + hi)/2; \
232 \
233 res = (*fnCompare)((const void *)(long)lItem, \
234 (const void *)(long)(m_pItems[i])); \
235 if ( res < 0 ) \
236 hi = i; \
237 else if ( res > 0 ) \
238 lo = i + 1; \
239 else { \
240 lo = i; \
241 break; \
242 } \
243 } \
244 \
245 return lo; \
246 } \
247 \
248 /* search for an item in a sorted array (binary search) */ \
249 int name::Index(T lItem, CMPFUNC fnCompare) const \
250 { \
251 size_t n = IndexForInsert(lItem, fnCompare); \
252 \
253 return n < m_nCount && m_pItems[n] == lItem ? (int)n : wxNOT_FOUND; \
254 } \
255 \
256 /* add item at the end */ \
257 void name::Add(T lItem, size_t nInsert) \
258 { \
259 if (nInsert == 0) \
260 return; \
261 Grow(nInsert); \
262 for (size_t i = 0; i < nInsert; i++) \
263 m_pItems[m_nCount++] = lItem; \
264 } \
265 \
266 /* add item assuming the array is sorted with fnCompare function */ \
267 void name::Add(T lItem, CMPFUNC fnCompare) \
268 { \
269 Insert(lItem, IndexForInsert(lItem, fnCompare)); \
270 } \
271 \
272 /* add item at the given position */ \
273 void name::Insert(T lItem, size_t nIndex, size_t nInsert) \
274 { \
275 wxCHECK_RET( nIndex <= m_nCount, wxT("bad index in wxArray::Insert") ); \
276 wxCHECK_RET( m_nCount <= m_nCount + nInsert, \
277 wxT("array size overflow in wxArray::Insert") ); \
278 \
279 if (nInsert == 0) \
280 return; \
281 Grow(nInsert); \
282 \
283 memmove(&m_pItems[nIndex + nInsert], &m_pItems[nIndex], \
284 (m_nCount - nIndex)*sizeof(T)); \
285 for (size_t i = 0; i < nInsert; i++) \
286 m_pItems[nIndex + i] = lItem; \
287 m_nCount += nInsert; \
288 } \
289 \
290 /* removes item from array (by index) */ \
291 void name::RemoveAt(size_t nIndex, size_t nRemove) \
292 { \
293 wxCHECK_RET( nIndex < m_nCount, wxT("bad index in wxArray::RemoveAt") ); \
294 wxCHECK_RET( nIndex + nRemove <= m_nCount, \
295 wxT("removing too many elements in wxArray::RemoveAt") ); \
296 \
297 memmove(&m_pItems[nIndex], &m_pItems[nIndex + nRemove], \
298 (m_nCount - nIndex - nRemove)*sizeof(T)); \
299 m_nCount -= nRemove; \
300 } \
301 \
302 /* removes item from array (by value) */ \
303 void name::Remove(T lItem) \
304 { \
305 int iIndex = Index(lItem); \
306 \
307 wxCHECK_RET( iIndex != wxNOT_FOUND, \
308 wxT("removing inexistent item in wxArray::Remove") ); \
309 \
310 RemoveAt((size_t)iIndex); \
311 } \
312 \
313 /* sort array elements using passed comparaison function */ \
314 void name::Sort(CMPFUNC fCmp) \
315 { \
316 qsort(m_pItems, m_nCount, sizeof(T), fCmp); \
317 }
318
319 _WX_DEFINE_BASEARRAY(const void *, wxBaseArrayPtrVoid)
320 _WX_DEFINE_BASEARRAY(short, wxBaseArrayShort)
321 _WX_DEFINE_BASEARRAY(int, wxBaseArrayInt)
322 _WX_DEFINE_BASEARRAY(long, wxBaseArrayLong)
323 //_WX_DEFINE_BASEARRAY(double, wxBaseArrayDouble)
324