]>
Commit | Line | Data |
---|---|---|
3c648a82 VZ |
1 | /////////////////////////////////////////////////////////////////////////////// |
2 | // Name: wx/vector.h | |
3 | // Purpose: STL vector clone | |
4 | // Author: Lindsay Mathieson | |
e966f815 | 5 | // Modified by: Vaclav Slavik - make it a template |
3c648a82 | 6 | // Created: 30.07.2001 |
e966f815 VS |
7 | // Copyright: (c) 2001 Lindsay Mathieson <lindsay@mathieson.org>, |
8 | // 2007 Vaclav Slavik <vslavik@fastmail.fm> | |
65571936 | 9 | // Licence: wxWindows licence |
3c648a82 VZ |
10 | /////////////////////////////////////////////////////////////////////////////// |
11 | ||
12 | #ifndef _WX_VECTOR_H_ | |
13 | #define _WX_VECTOR_H_ | |
14 | ||
15 | #include "wx/defs.h" | |
16 | ||
df4aed1c | 17 | #if wxUSE_STL |
e966f815 | 18 | |
e966f815 VS |
19 | #include <vector> |
20 | #define wxVector std::vector | |
21 | ||
22 | #else // !wxUSE_STL | |
23 | ||
7df6b37a | 24 | #include "wx/utils.h" |
fc31181d | 25 | #include "wx/scopeguard.h" |
6712283c VS |
26 | #include "wx/meta/movable.h" |
27 | #include "wx/meta/if.h" | |
7df6b37a | 28 | |
c157b27e VS |
29 | #include "wx/beforestd.h" |
30 | #include <new> // for placement new | |
31 | #include "wx/afterstd.h" | |
32 | ||
6712283c VS |
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 | { | |
54742e34 | 69 | ::new(mem + i) T(old[i]); |
6712283c VS |
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 | { | |
0e82d270 | 83 | ::new(destptr) T(*sourceptr); |
6712283c VS |
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 | { | |
0e82d270 | 95 | ::new(destptr) T(*sourceptr); |
6712283c VS |
96 | sourceptr->~T(); |
97 | } | |
98 | } | |
99 | }; | |
100 | ||
101 | ||
102 | } // namespace wxPrivate | |
103 | ||
e966f815 VS |
104 | template<typename T> |
105 | class wxVector | |
3c648a82 | 106 | { |
c9faa9e9 | 107 | private: |
0c73d133 | 108 | // This cryptic expression means "typedef Ops to wxVectorMemOpsMovable if |
c9faa9e9 VS |
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 | ||
2d6c58d6 | 119 | public: |
5fd588d2 | 120 | typedef size_t size_type; |
946954d3 | 121 | typedef size_t difference_type; |
e966f815 | 122 | typedef T value_type; |
946954d3 | 123 | typedef value_type* pointer; |
df4aed1c | 124 | typedef value_type* iterator; |
0516de2c | 125 | typedef const value_type* const_iterator; |
df4aed1c | 126 | typedef value_type& reference; |
e966f815 | 127 | |
946954d3 RR |
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) { } | |
deb0c402 | 134 | |
946954d3 RR |
135 | reference operator*() const { return *m_ptr; } |
136 | pointer operator->() const { return m_ptr; } | |
deb0c402 | 137 | |
946954d3 | 138 | iterator base() const { return m_ptr; } |
deb0c402 VZ |
139 | |
140 | reverse_iterator& operator++() | |
141 | { --m_ptr; return *this; } | |
946954d3 | 142 | reverse_iterator operator++(int) |
deb0c402 VZ |
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 | ||
946954d3 | 149 | reverse_iterator operator+(difference_type n) const |
deb0c402 | 150 | { return reverse_iterator(m_ptr - n); } |
946954d3 | 151 | reverse_iterator& operator+=(difference_type n) |
deb0c402 | 152 | { m_ptr -= n; return *this; } |
946954d3 | 153 | reverse_iterator operator-(difference_type n) const |
deb0c402 | 154 | { return reverse_iterator(m_ptr + n); } |
946954d3 | 155 | reverse_iterator& operator-=(difference_type n) |
deb0c402 VZ |
156 | { m_ptr += n; return *this; } |
157 | ||
946954d3 | 158 | reference operator[](difference_type n) const |
deb0c402 VZ |
159 | { return *(*this + n); } |
160 | ||
161 | bool operator ==(const reverse_iterator& it) const | |
162 | { return m_ptr == it.m_ptr; } | |
946954d3 | 163 | bool operator !=(const reverse_iterator& it) const |
deb0c402 VZ |
164 | { return m_ptr != it.m_ptr; } |
165 | ||
946954d3 RR |
166 | private: |
167 | value_type *m_ptr; | |
168 | }; | |
169 | ||
0516de2c | 170 | wxVector() : m_size(0), m_capacity(0), m_values(NULL) {} |
e966f815 | 171 | |
f6363458 | 172 | wxVector(size_type p_size) |
e068310a | 173 | : m_size(0), m_capacity(0), m_values(NULL) |
664e5ff9 | 174 | { |
f6363458 VZ |
175 | reserve(p_size); |
176 | for ( size_t n = 0; n < p_size; n++ ) | |
664e5ff9 VZ |
177 | push_back(value_type()); |
178 | } | |
179 | ||
f6363458 | 180 | wxVector(size_type p_size, const value_type& v) |
e068310a | 181 | : m_size(0), m_capacity(0), m_values(NULL) |
664e5ff9 | 182 | { |
f6363458 VZ |
183 | reserve(p_size); |
184 | for ( size_t n = 0; n < p_size; n++ ) | |
664e5ff9 VZ |
185 | push_back(v); |
186 | } | |
187 | ||
a470957a | 188 | wxVector(const wxVector& c) : m_size(0), m_capacity(0), m_values(NULL) |
e966f815 | 189 | { |
0516de2c | 190 | Copy(c); |
e966f815 VS |
191 | } |
192 | ||
193 | ~wxVector() | |
194 | { | |
195 | clear(); | |
196 | } | |
197 | ||
198 | void clear() | |
199 | { | |
c157b27e VS |
200 | // call destructors of stored objects: |
201 | for ( size_type i = 0; i < m_size; i++ ) | |
202 | { | |
82b6efd2 | 203 | m_values[i].~T(); |
c157b27e VS |
204 | } |
205 | ||
c9faa9e9 | 206 | Ops::Free(m_values); |
0516de2c | 207 | m_values = NULL; |
a470957a VZ |
208 | m_size = |
209 | m_capacity = 0; | |
e966f815 VS |
210 | } |
211 | ||
212 | void reserve(size_type n) | |
213 | { | |
0516de2c VS |
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 | |
f5851311 | 219 | // |
c157b27e | 220 | // NB: casts to size_type are needed to suppress mingw32 warnings about |
f5851311 VZ |
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; | |
0516de2c VS |
225 | if ( m_capacity + increment > n ) |
226 | n = m_capacity + increment; | |
227 | ||
c9faa9e9 | 228 | m_values = Ops::Realloc(m_values, n * sizeof(value_type), m_size); |
0516de2c | 229 | m_capacity = n; |
e966f815 VS |
230 | } |
231 | ||
e068310a VZ |
232 | void resize(size_type n) |
233 | { | |
234 | if ( n < m_size ) | |
235 | Shrink(n); | |
236 | else if ( n > m_size ) | |
237 | Extend(n, value_type()); | |
238 | } | |
239 | ||
240 | void resize(size_type n, const value_type& v) | |
241 | { | |
242 | if ( n < m_size ) | |
243 | Shrink(n); | |
244 | else if ( n > m_size ) | |
245 | Extend(n, v); | |
246 | } | |
247 | ||
e966f815 VS |
248 | size_type size() const |
249 | { | |
250 | return m_size; | |
251 | } | |
252 | ||
253 | size_type capacity() const | |
254 | { | |
255 | return m_capacity; | |
256 | } | |
257 | ||
258 | bool empty() const | |
259 | { | |
260 | return size() == 0; | |
261 | } | |
262 | ||
263 | wxVector& operator=(const wxVector& vb) | |
264 | { | |
a09307ab PC |
265 | if (this != &vb) |
266 | { | |
267 | clear(); | |
268 | Copy(vb); | |
269 | } | |
e966f815 VS |
270 | return *this; |
271 | } | |
272 | ||
0516de2c | 273 | void push_back(const value_type& v) |
e966f815 | 274 | { |
0516de2c | 275 | reserve(size() + 1); |
c157b27e VS |
276 | |
277 | // use placement new to initialize new object in preallocated place in | |
278 | // m_values and store 'v' in it: | |
279 | void* const place = m_values + m_size; | |
54742e34 | 280 | ::new(place) value_type(v); |
c157b27e VS |
281 | |
282 | // only increase m_size if the ctor didn't throw an exception; notice | |
283 | // that if it _did_ throw, everything is OK, because we only increased | |
284 | // vector's capacity so far and possibly written some data to | |
285 | // uninitialized memory at the end of m_values | |
286 | m_size++; | |
e966f815 VS |
287 | } |
288 | ||
289 | void pop_back() | |
290 | { | |
0516de2c | 291 | erase(end() - 1); |
e966f815 VS |
292 | } |
293 | ||
294 | const value_type& at(size_type idx) const | |
295 | { | |
296 | wxASSERT(idx < m_size); | |
0516de2c | 297 | return m_values[idx]; |
e966f815 VS |
298 | } |
299 | ||
300 | value_type& at(size_type idx) | |
301 | { | |
302 | wxASSERT(idx < m_size); | |
0516de2c | 303 | return m_values[idx]; |
e966f815 | 304 | } |
3c648a82 | 305 | |
e966f815 VS |
306 | const value_type& operator[](size_type idx) const { return at(idx); } |
307 | value_type& operator[](size_type idx) { return at(idx); } | |
308 | const value_type& front() const { return at(0); } | |
309 | value_type& front() { return at(0); } | |
310 | const value_type& back() const { return at(size() - 1); } | |
311 | value_type& back() { return at(size() - 1); } | |
312 | ||
0516de2c VS |
313 | const_iterator begin() const { return m_values; } |
314 | iterator begin() { return m_values; } | |
315 | const_iterator end() const { return m_values + size(); } | |
316 | iterator end() { return m_values + size(); } | |
df4aed1c | 317 | |
946954d3 RR |
318 | reverse_iterator rbegin() { return reverse_iterator(end() - 1); } |
319 | reverse_iterator rend() { return reverse_iterator(begin() - 1); } | |
320 | ||
0516de2c | 321 | iterator insert(iterator it, const value_type& v = value_type()) |
f631cd8e | 322 | { |
c157b27e VS |
323 | // NB: this must be done before reserve(), because reserve() |
324 | // invalidates iterators! | |
325 | const size_t idx = it - begin(); | |
326 | const size_t after = end() - it; | |
f631cd8e | 327 | |
0516de2c | 328 | reserve(size() + 1); |
9cf33372 | 329 | |
fc31181d VZ |
330 | // the place where the new element is going to be inserted |
331 | value_type * const place = m_values + idx; | |
332 | ||
c157b27e | 333 | // unless we're inserting at the end, move following elements out of |
0516de2c | 334 | // the way: |
c157b27e | 335 | if ( after > 0 ) |
fc31181d | 336 | Ops::MemmoveForward(place + 1, place, after); |
c157b27e | 337 | |
fc31181d VZ |
338 | // if the ctor called below throws an exception, we need to move all |
339 | // the elements back to their original positions in m_values | |
340 | wxScopeGuard moveBack = wxMakeGuard( | |
341 | Ops::MemmoveBackward, place, place + 1, after); | |
342 | if ( !after ) | |
343 | moveBack.Dismiss(); | |
344 | ||
345 | // use placement new to initialize new object in preallocated place in | |
346 | // m_values and store 'v' in it: | |
0e82d270 | 347 | ::new(place) value_type(v); |
e966f815 | 348 | |
fc31181d VZ |
349 | // now that we did successfully add the new element, increment the size |
350 | // and disable moving the items back | |
351 | moveBack.Dismiss(); | |
0516de2c | 352 | m_size++; |
3c648a82 | 353 | |
0516de2c | 354 | return begin() + idx; |
1f32f585 | 355 | } |
3c648a82 | 356 | |
0516de2c | 357 | iterator erase(iterator it) |
3c648a82 | 358 | { |
0516de2c | 359 | return erase(it, it + 1); |
1f32f585 | 360 | } |
3c648a82 | 361 | |
0516de2c | 362 | iterator erase(iterator first, iterator last) |
df4aed1c | 363 | { |
0516de2c VS |
364 | if ( first == last ) |
365 | return first; | |
366 | wxASSERT( first < end() && last <= end() ); | |
f631cd8e | 367 | |
c157b27e VS |
368 | const size_type idx = first - begin(); |
369 | const size_type count = last - first; | |
370 | const size_type after = end() - last; | |
df4aed1c | 371 | |
c157b27e VS |
372 | // erase elements by calling their destructors: |
373 | for ( iterator i = first; i < last; ++i ) | |
6712283c | 374 | i->~T(); |
0516de2c | 375 | |
c157b27e VS |
376 | // once that's done, move following elements over to the freed space: |
377 | if ( after > 0 ) | |
378 | { | |
c9faa9e9 | 379 | Ops::MemmoveBackward(m_values + idx, m_values + idx + count, after); |
c157b27e | 380 | } |
f631cd8e | 381 | |
df4aed1c | 382 | m_size -= count; |
0516de2c | 383 | |
c157b27e | 384 | return begin() + idx; |
df4aed1c | 385 | } |
0516de2c VS |
386 | |
387 | #if WXWIN_COMPATIBILITY_2_8 | |
388 | wxDEPRECATED( size_type erase(size_type n) ); | |
389 | #endif // WXWIN_COMPATIBILITY_2_8 | |
390 | ||
391 | private: | |
f9bf06ac VS |
392 | // VC6 can't compile static const int members |
393 | enum { ALLOC_INITIAL_SIZE = 16 }; | |
394 | enum { ALLOC_MAX_SIZE = 4096 }; | |
0516de2c VS |
395 | |
396 | void Copy(const wxVector& vb) | |
3c648a82 | 397 | { |
0516de2c | 398 | reserve(vb.size()); |
3c648a82 | 399 | |
0516de2c VS |
400 | for ( const_iterator i = vb.begin(); i != vb.end(); ++i ) |
401 | push_back(*i); | |
1f32f585 | 402 | } |
3c648a82 | 403 | |
e966f815 | 404 | private: |
e068310a VZ |
405 | void Shrink(size_type n) |
406 | { | |
407 | for ( size_type i = n; i < m_size; i++ ) | |
408 | m_values[i].~T(); | |
409 | m_size = n; | |
410 | } | |
411 | ||
412 | void Extend(size_type n, const value_type& v) | |
413 | { | |
414 | reserve(n); | |
415 | for ( size_type i = m_size; i < n; i++ ) | |
416 | push_back(v); | |
417 | } | |
418 | ||
e966f815 VS |
419 | size_type m_size, |
420 | m_capacity; | |
0516de2c | 421 | value_type *m_values; |
3c648a82 VZ |
422 | }; |
423 | ||
9cf33372 VS |
424 | #if WXWIN_COMPATIBILITY_2_8 |
425 | template<typename T> | |
32aa5bda | 426 | inline typename wxVector<T>::size_type wxVector<T>::erase(size_type n) |
9cf33372 | 427 | { |
0516de2c | 428 | erase(begin() + n); |
9cf33372 VS |
429 | return n; |
430 | } | |
431 | #endif // WXWIN_COMPATIBILITY_2_8 | |
432 | ||
e966f815 | 433 | #endif // wxUSE_STL/!wxUSE_STL |
5fd588d2 | 434 | |
e966f815 VS |
435 | #if WXWIN_COMPATIBILITY_2_8 |
436 | #define WX_DECLARE_VECTORBASE(obj, cls) typedef wxVector<obj> cls | |
437 | #define _WX_DECLARE_VECTOR(obj, cls, exp) WX_DECLARE_VECTORBASE(obj, cls) | |
438 | #define WX_DECLARE_VECTOR(obj, cls) WX_DECLARE_VECTORBASE(obj, cls) | |
439 | #endif // WXWIN_COMPATIBILITY_2_8 | |
3c648a82 | 440 | |
e966f815 | 441 | #endif // _WX_VECTOR_H_ |