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