]>
Commit | Line | Data |
---|---|---|
1 | /////////////////////////////////////////////////////////////////////////////// | |
2 | // Name: wx/vector.h | |
3 | // Purpose: STL vector clone | |
4 | // Author: Lindsay Mathieson | |
5 | // Modified by: Vaclav Slavik - make it a template | |
6 | // Created: 30.07.2001 | |
7 | // Copyright: (c) 2001 Lindsay Mathieson <lindsay@mathieson.org>, | |
8 | // 2007 Vaclav Slavik <vslavik@fastmail.fm> | |
9 | // Licence: wxWindows licence | |
10 | /////////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | #ifndef _WX_VECTOR_H_ | |
13 | #define _WX_VECTOR_H_ | |
14 | ||
15 | #include "wx/defs.h" | |
16 | ||
17 | #if wxUSE_STL | |
18 | ||
19 | #include <vector> | |
20 | #define wxVector std::vector | |
21 | ||
22 | #else // !wxUSE_STL | |
23 | ||
24 | #include "wx/utils.h" | |
25 | #include "wx/scopeguard.h" | |
26 | #include "wx/meta/movable.h" | |
27 | #include "wx/meta/if.h" | |
28 | ||
29 | #include "wx/beforestd.h" | |
30 | #include <new> // for placement new | |
31 | #include "wx/afterstd.h" | |
32 | ||
33 | namespace wxPrivate | |
34 | { | |
35 | ||
36 | // These templates encapsulate memory operations for use by wxVector; there are | |
37 | // two implementations, both in generic way for any C++ types and as an | |
38 | // optimized version for "movable" types that uses realloc() and memmove(). | |
39 | ||
40 | // version for movable types: | |
41 | template<typename T> | |
42 | struct wxVectorMemOpsMovable | |
43 | { | |
44 | static void Free(T* array) | |
45 | { free(array); } | |
46 | ||
47 | static T* Realloc(T* old, size_t newCapacity, size_t WXUNUSED(occupiedSize)) | |
48 | { return (T*)realloc(old, newCapacity * sizeof(T)); } | |
49 | ||
50 | static void MemmoveBackward(T* dest, T* source, size_t count) | |
51 | { memmove(dest, source, count * sizeof(T)); } | |
52 | ||
53 | static void MemmoveForward(T* dest, T* source, size_t count) | |
54 | { memmove(dest, source, count * sizeof(T)); } | |
55 | }; | |
56 | ||
57 | // generic version for non-movable types: | |
58 | template<typename T> | |
59 | struct wxVectorMemOpsGeneric | |
60 | { | |
61 | static void Free(T* array) | |
62 | { ::operator delete(array); } | |
63 | ||
64 | static T* Realloc(T* old, size_t newCapacity, size_t occupiedSize) | |
65 | { | |
66 | T *mem = (T*)::operator new(newCapacity * sizeof(T)); | |
67 | for ( size_t i = 0; i < occupiedSize; i++ ) | |
68 | { | |
69 | ::new(mem + i) T(old[i]); | |
70 | old[i].~T(); | |
71 | } | |
72 | ::operator delete(old); | |
73 | return mem; | |
74 | } | |
75 | ||
76 | static void MemmoveBackward(T* dest, T* source, size_t count) | |
77 | { | |
78 | wxASSERT( dest < source ); | |
79 | T* destptr = dest; | |
80 | T* sourceptr = source; | |
81 | for ( size_t i = count; i > 0; --i, ++destptr, ++sourceptr ) | |
82 | { | |
83 | ::new(destptr) T(*sourceptr); | |
84 | sourceptr->~T(); | |
85 | } | |
86 | } | |
87 | ||
88 | static void MemmoveForward(T* dest, T* source, size_t count) | |
89 | { | |
90 | wxASSERT( dest > source ); | |
91 | T* destptr = dest + count - 1; | |
92 | T* sourceptr = source + count - 1; | |
93 | for ( size_t i = count; i > 0; --i, --destptr, --sourceptr ) | |
94 | { | |
95 | ::new(destptr) T(*sourceptr); | |
96 | sourceptr->~T(); | |
97 | } | |
98 | } | |
99 | }; | |
100 | ||
101 | ||
102 | } // namespace wxPrivate | |
103 | ||
104 | template<typename T> | |
105 | class wxVector | |
106 | { | |
107 | private: | |
108 | // This cryptic expression means "typedef Ops to wxVectorMemOpsMovable if | |
109 | // type T is movable type, otherwise to wxVectorMemOpsGeneric". | |
110 | // | |
111 | // Note that we use typedef instead of privately deriving from this (which | |
112 | // would allowed us to omit "Ops::" prefixes below) to keep VC6 happy, | |
113 | // it can't compile code that derives from wxIf<...>::value. | |
114 | typedef typename wxIf< wxIsMovable<T>::value, | |
115 | wxPrivate::wxVectorMemOpsMovable<T>, | |
116 | wxPrivate::wxVectorMemOpsGeneric<T> >::value | |
117 | Ops; | |
118 | ||
119 | public: | |
120 | typedef size_t size_type; | |
121 | typedef size_t difference_type; | |
122 | typedef T value_type; | |
123 | typedef value_type* pointer; | |
124 | typedef value_type* iterator; | |
125 | typedef const value_type* const_iterator; | |
126 | typedef value_type& reference; | |
127 | ||
128 | class reverse_iterator | |
129 | { | |
130 | public: | |
131 | reverse_iterator() : m_ptr(NULL) { } | |
132 | wxEXPLICIT reverse_iterator(iterator it) : m_ptr(it) { } | |
133 | reverse_iterator(const reverse_iterator& it) : m_ptr(it.m_ptr) { } | |
134 | ||
135 | reference operator*() const { return *m_ptr; } | |
136 | pointer operator->() const { return m_ptr; } | |
137 | ||
138 | iterator base() const { return m_ptr; } | |
139 | ||
140 | reverse_iterator& operator++() | |
141 | { --m_ptr; return *this; } | |
142 | reverse_iterator operator++(int) | |
143 | { reverse_iterator tmp = *this; --m_ptr; return tmp; } | |
144 | reverse_iterator& operator--() | |
145 | { ++m_ptr; return *this; } | |
146 | reverse_iterator operator--(int) | |
147 | { reverse_iterator tmp = *this; ++m_ptr; return tmp; } | |
148 | ||
149 | reverse_iterator operator+(difference_type n) const | |
150 | { return reverse_iterator(m_ptr - n); } | |
151 | reverse_iterator& operator+=(difference_type n) | |
152 | { m_ptr -= n; return *this; } | |
153 | reverse_iterator operator-(difference_type n) const | |
154 | { return reverse_iterator(m_ptr + n); } | |
155 | reverse_iterator& operator-=(difference_type n) | |
156 | { m_ptr += n; return *this; } | |
157 | ||
158 | reference operator[](difference_type n) const | |
159 | { return *(*this + n); } | |
160 | ||
161 | bool operator ==(const reverse_iterator& it) const | |
162 | { return m_ptr == it.m_ptr; } | |
163 | bool operator !=(const reverse_iterator& it) const | |
164 | { return m_ptr != it.m_ptr; } | |
165 | ||
166 | private: | |
167 | value_type *m_ptr; | |
168 | }; | |
169 | ||
170 | wxVector() : m_size(0), m_capacity(0), m_values(NULL) {} | |
171 | ||
172 | wxVector(size_type size) | |
173 | : m_size(0), m_capacity(0), m_values(NULL) | |
174 | { | |
175 | reserve(size); | |
176 | for ( size_t n = 0; n < size; n++ ) | |
177 | push_back(value_type()); | |
178 | } | |
179 | ||
180 | wxVector(size_type size, const value_type& v) | |
181 | : m_size(0), m_capacity(0), m_values(NULL) | |
182 | { | |
183 | reserve(size); | |
184 | for ( size_t n = 0; n < size; n++ ) | |
185 | push_back(v); | |
186 | } | |
187 | ||
188 | wxVector(const wxVector& c) : m_size(0), m_capacity(0), m_values(NULL) | |
189 | { | |
190 | Copy(c); | |
191 | } | |
192 | ||
193 | ~wxVector() | |
194 | { | |
195 | clear(); | |
196 | } | |
197 | ||
198 | void clear() | |
199 | { | |
200 | // call destructors of stored objects: | |
201 | for ( size_type i = 0; i < m_size; i++ ) | |
202 | { | |
203 | m_values[i].~T(); | |
204 | } | |
205 | ||
206 | Ops::Free(m_values); | |
207 | m_values = NULL; | |
208 | m_size = | |
209 | m_capacity = 0; | |
210 | } | |
211 | ||
212 | void reserve(size_type n) | |
213 | { | |
214 | if ( n <= m_capacity ) | |
215 | return; | |
216 | ||
217 | // increase the size twice, unless we're already too big or unless | |
218 | // more is requested | |
219 | // | |
220 | // NB: casts to size_type are needed to suppress mingw32 warnings about | |
221 | // mixing enums and ints in the same expression | |
222 | const size_type increment = m_size > 0 | |
223 | ? wxMin(m_size, (size_type)ALLOC_MAX_SIZE) | |
224 | : (size_type)ALLOC_INITIAL_SIZE; | |
225 | if ( m_capacity + increment > n ) | |
226 | n = m_capacity + increment; | |
227 | ||
228 | m_values = Ops::Realloc(m_values, n * sizeof(value_type), m_size); | |
229 | m_capacity = n; | |
230 | } | |
231 | ||
232 | size_type size() const | |
233 | { | |
234 | return m_size; | |
235 | } | |
236 | ||
237 | size_type capacity() const | |
238 | { | |
239 | return m_capacity; | |
240 | } | |
241 | ||
242 | bool empty() const | |
243 | { | |
244 | return size() == 0; | |
245 | } | |
246 | ||
247 | wxVector& operator=(const wxVector& vb) | |
248 | { | |
249 | if (this != &vb) | |
250 | { | |
251 | clear(); | |
252 | Copy(vb); | |
253 | } | |
254 | return *this; | |
255 | } | |
256 | ||
257 | void push_back(const value_type& v) | |
258 | { | |
259 | reserve(size() + 1); | |
260 | ||
261 | // use placement new to initialize new object in preallocated place in | |
262 | // m_values and store 'v' in it: | |
263 | void* const place = m_values + m_size; | |
264 | ::new(place) value_type(v); | |
265 | ||
266 | // only increase m_size if the ctor didn't throw an exception; notice | |
267 | // that if it _did_ throw, everything is OK, because we only increased | |
268 | // vector's capacity so far and possibly written some data to | |
269 | // uninitialized memory at the end of m_values | |
270 | m_size++; | |
271 | } | |
272 | ||
273 | void pop_back() | |
274 | { | |
275 | erase(end() - 1); | |
276 | } | |
277 | ||
278 | const value_type& at(size_type idx) const | |
279 | { | |
280 | wxASSERT(idx < m_size); | |
281 | return m_values[idx]; | |
282 | } | |
283 | ||
284 | value_type& at(size_type idx) | |
285 | { | |
286 | wxASSERT(idx < m_size); | |
287 | return m_values[idx]; | |
288 | } | |
289 | ||
290 | const value_type& operator[](size_type idx) const { return at(idx); } | |
291 | value_type& operator[](size_type idx) { return at(idx); } | |
292 | const value_type& front() const { return at(0); } | |
293 | value_type& front() { return at(0); } | |
294 | const value_type& back() const { return at(size() - 1); } | |
295 | value_type& back() { return at(size() - 1); } | |
296 | ||
297 | const_iterator begin() const { return m_values; } | |
298 | iterator begin() { return m_values; } | |
299 | const_iterator end() const { return m_values + size(); } | |
300 | iterator end() { return m_values + size(); } | |
301 | ||
302 | reverse_iterator rbegin() { return reverse_iterator(end() - 1); } | |
303 | reverse_iterator rend() { return reverse_iterator(begin() - 1); } | |
304 | ||
305 | iterator insert(iterator it, const value_type& v = value_type()) | |
306 | { | |
307 | // NB: this must be done before reserve(), because reserve() | |
308 | // invalidates iterators! | |
309 | const size_t idx = it - begin(); | |
310 | const size_t after = end() - it; | |
311 | ||
312 | reserve(size() + 1); | |
313 | ||
314 | // the place where the new element is going to be inserted | |
315 | value_type * const place = m_values + idx; | |
316 | ||
317 | // unless we're inserting at the end, move following elements out of | |
318 | // the way: | |
319 | if ( after > 0 ) | |
320 | Ops::MemmoveForward(place + 1, place, after); | |
321 | ||
322 | // if the ctor called below throws an exception, we need to move all | |
323 | // the elements back to their original positions in m_values | |
324 | wxScopeGuard moveBack = wxMakeGuard( | |
325 | Ops::MemmoveBackward, place, place + 1, after); | |
326 | if ( !after ) | |
327 | moveBack.Dismiss(); | |
328 | ||
329 | // use placement new to initialize new object in preallocated place in | |
330 | // m_values and store 'v' in it: | |
331 | ::new(place) value_type(v); | |
332 | ||
333 | // now that we did successfully add the new element, increment the size | |
334 | // and disable moving the items back | |
335 | moveBack.Dismiss(); | |
336 | m_size++; | |
337 | ||
338 | return begin() + idx; | |
339 | } | |
340 | ||
341 | iterator erase(iterator it) | |
342 | { | |
343 | return erase(it, it + 1); | |
344 | } | |
345 | ||
346 | iterator erase(iterator first, iterator last) | |
347 | { | |
348 | if ( first == last ) | |
349 | return first; | |
350 | wxASSERT( first < end() && last <= end() ); | |
351 | ||
352 | const size_type idx = first - begin(); | |
353 | const size_type count = last - first; | |
354 | const size_type after = end() - last; | |
355 | ||
356 | // erase elements by calling their destructors: | |
357 | for ( iterator i = first; i < last; ++i ) | |
358 | i->~T(); | |
359 | ||
360 | // once that's done, move following elements over to the freed space: | |
361 | if ( after > 0 ) | |
362 | { | |
363 | Ops::MemmoveBackward(m_values + idx, m_values + idx + count, after); | |
364 | } | |
365 | ||
366 | m_size -= count; | |
367 | ||
368 | return begin() + idx; | |
369 | } | |
370 | ||
371 | #if WXWIN_COMPATIBILITY_2_8 | |
372 | wxDEPRECATED( size_type erase(size_type n) ); | |
373 | #endif // WXWIN_COMPATIBILITY_2_8 | |
374 | ||
375 | private: | |
376 | // VC6 can't compile static const int members | |
377 | enum { ALLOC_INITIAL_SIZE = 16 }; | |
378 | enum { ALLOC_MAX_SIZE = 4096 }; | |
379 | ||
380 | void Copy(const wxVector& vb) | |
381 | { | |
382 | reserve(vb.size()); | |
383 | ||
384 | for ( const_iterator i = vb.begin(); i != vb.end(); ++i ) | |
385 | push_back(*i); | |
386 | } | |
387 | ||
388 | private: | |
389 | size_type m_size, | |
390 | m_capacity; | |
391 | value_type *m_values; | |
392 | }; | |
393 | ||
394 | #if WXWIN_COMPATIBILITY_2_8 | |
395 | template<typename T> | |
396 | inline typename wxVector<T>::size_type wxVector<T>::erase(size_type n) | |
397 | { | |
398 | erase(begin() + n); | |
399 | return n; | |
400 | } | |
401 | #endif // WXWIN_COMPATIBILITY_2_8 | |
402 | ||
403 | #endif // wxUSE_STL/!wxUSE_STL | |
404 | ||
405 | #if WXWIN_COMPATIBILITY_2_8 | |
406 | #define WX_DECLARE_VECTORBASE(obj, cls) typedef wxVector<obj> cls | |
407 | #define _WX_DECLARE_VECTOR(obj, cls, exp) WX_DECLARE_VECTORBASE(obj, cls) | |
408 | #define WX_DECLARE_VECTOR(obj, cls) WX_DECLARE_VECTORBASE(obj, cls) | |
409 | #endif // WXWIN_COMPATIBILITY_2_8 | |
410 | ||
411 | #endif // _WX_VECTOR_H_ |