]>
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 | { | |
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 | ||
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) { } | |
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 | { return 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 | { return 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 | ||
0516de2c | 170 | wxVector() : m_size(0), m_capacity(0), m_values(NULL) {} |
e966f815 | 171 | |
a470957a | 172 | wxVector(const wxVector& c) : m_size(0), m_capacity(0), m_values(NULL) |
e966f815 | 173 | { |
0516de2c | 174 | Copy(c); |
e966f815 VS |
175 | } |
176 | ||
177 | ~wxVector() | |
178 | { | |
179 | clear(); | |
180 | } | |
181 | ||
182 | void clear() | |
183 | { | |
c157b27e VS |
184 | // call destructors of stored objects: |
185 | for ( size_type i = 0; i < m_size; i++ ) | |
186 | { | |
82b6efd2 | 187 | m_values[i].~T(); |
c157b27e VS |
188 | } |
189 | ||
c9faa9e9 | 190 | Ops::Free(m_values); |
0516de2c | 191 | m_values = NULL; |
a470957a VZ |
192 | m_size = |
193 | m_capacity = 0; | |
e966f815 VS |
194 | } |
195 | ||
196 | void reserve(size_type n) | |
197 | { | |
0516de2c VS |
198 | if ( n <= m_capacity ) |
199 | return; | |
200 | ||
201 | // increase the size twice, unless we're already too big or unless | |
202 | // more is requested | |
f5851311 | 203 | // |
c157b27e | 204 | // NB: casts to size_type are needed to suppress mingw32 warnings about |
f5851311 VZ |
205 | // mixing enums and ints in the same expression |
206 | const size_type increment = m_size > 0 | |
207 | ? wxMin(m_size, (size_type)ALLOC_MAX_SIZE) | |
208 | : (size_type)ALLOC_INITIAL_SIZE; | |
0516de2c VS |
209 | if ( m_capacity + increment > n ) |
210 | n = m_capacity + increment; | |
211 | ||
c9faa9e9 | 212 | m_values = Ops::Realloc(m_values, n * sizeof(value_type), m_size); |
0516de2c | 213 | m_capacity = n; |
e966f815 VS |
214 | } |
215 | ||
216 | size_type size() const | |
217 | { | |
218 | return m_size; | |
219 | } | |
220 | ||
221 | size_type capacity() const | |
222 | { | |
223 | return m_capacity; | |
224 | } | |
225 | ||
226 | bool empty() const | |
227 | { | |
228 | return size() == 0; | |
229 | } | |
230 | ||
231 | wxVector& operator=(const wxVector& vb) | |
232 | { | |
a470957a | 233 | clear(); |
0516de2c | 234 | Copy(vb); |
e966f815 VS |
235 | return *this; |
236 | } | |
237 | ||
0516de2c | 238 | void push_back(const value_type& v) |
e966f815 | 239 | { |
0516de2c | 240 | reserve(size() + 1); |
c157b27e VS |
241 | |
242 | // use placement new to initialize new object in preallocated place in | |
243 | // m_values and store 'v' in it: | |
244 | void* const place = m_values + m_size; | |
245 | new(place) value_type(v); | |
246 | ||
247 | // only increase m_size if the ctor didn't throw an exception; notice | |
248 | // that if it _did_ throw, everything is OK, because we only increased | |
249 | // vector's capacity so far and possibly written some data to | |
250 | // uninitialized memory at the end of m_values | |
251 | m_size++; | |
e966f815 VS |
252 | } |
253 | ||
254 | void pop_back() | |
255 | { | |
0516de2c | 256 | erase(end() - 1); |
e966f815 VS |
257 | } |
258 | ||
259 | const value_type& at(size_type idx) const | |
260 | { | |
261 | wxASSERT(idx < m_size); | |
0516de2c | 262 | return m_values[idx]; |
e966f815 VS |
263 | } |
264 | ||
265 | value_type& at(size_type idx) | |
266 | { | |
267 | wxASSERT(idx < m_size); | |
0516de2c | 268 | return m_values[idx]; |
e966f815 | 269 | } |
3c648a82 | 270 | |
e966f815 VS |
271 | const value_type& operator[](size_type idx) const { return at(idx); } |
272 | value_type& operator[](size_type idx) { return at(idx); } | |
273 | const value_type& front() const { return at(0); } | |
274 | value_type& front() { return at(0); } | |
275 | const value_type& back() const { return at(size() - 1); } | |
276 | value_type& back() { return at(size() - 1); } | |
277 | ||
0516de2c VS |
278 | const_iterator begin() const { return m_values; } |
279 | iterator begin() { return m_values; } | |
280 | const_iterator end() const { return m_values + size(); } | |
281 | iterator end() { return m_values + size(); } | |
df4aed1c | 282 | |
946954d3 RR |
283 | reverse_iterator rbegin() { return reverse_iterator(end() - 1); } |
284 | reverse_iterator rend() { return reverse_iterator(begin() - 1); } | |
285 | ||
0516de2c | 286 | iterator insert(iterator it, const value_type& v = value_type()) |
f631cd8e | 287 | { |
c157b27e VS |
288 | // NB: this must be done before reserve(), because reserve() |
289 | // invalidates iterators! | |
290 | const size_t idx = it - begin(); | |
291 | const size_t after = end() - it; | |
f631cd8e | 292 | |
0516de2c | 293 | reserve(size() + 1); |
9cf33372 | 294 | |
fc31181d VZ |
295 | // the place where the new element is going to be inserted |
296 | value_type * const place = m_values + idx; | |
297 | ||
c157b27e | 298 | // unless we're inserting at the end, move following elements out of |
0516de2c | 299 | // the way: |
c157b27e | 300 | if ( after > 0 ) |
fc31181d | 301 | Ops::MemmoveForward(place + 1, place, after); |
c157b27e | 302 | |
fc31181d VZ |
303 | // if the ctor called below throws an exception, we need to move all |
304 | // the elements back to their original positions in m_values | |
305 | wxScopeGuard moveBack = wxMakeGuard( | |
306 | Ops::MemmoveBackward, place, place + 1, after); | |
307 | if ( !after ) | |
308 | moveBack.Dismiss(); | |
309 | ||
310 | // use placement new to initialize new object in preallocated place in | |
311 | // m_values and store 'v' in it: | |
312 | new(place) value_type(v); | |
e966f815 | 313 | |
fc31181d VZ |
314 | // now that we did successfully add the new element, increment the size |
315 | // and disable moving the items back | |
316 | moveBack.Dismiss(); | |
0516de2c | 317 | m_size++; |
3c648a82 | 318 | |
0516de2c | 319 | return begin() + idx; |
1f32f585 | 320 | } |
3c648a82 | 321 | |
0516de2c | 322 | iterator erase(iterator it) |
3c648a82 | 323 | { |
0516de2c | 324 | return erase(it, it + 1); |
1f32f585 | 325 | } |
3c648a82 | 326 | |
0516de2c | 327 | iterator erase(iterator first, iterator last) |
df4aed1c | 328 | { |
0516de2c VS |
329 | if ( first == last ) |
330 | return first; | |
331 | wxASSERT( first < end() && last <= end() ); | |
f631cd8e | 332 | |
c157b27e VS |
333 | const size_type idx = first - begin(); |
334 | const size_type count = last - first; | |
335 | const size_type after = end() - last; | |
df4aed1c | 336 | |
c157b27e VS |
337 | // erase elements by calling their destructors: |
338 | for ( iterator i = first; i < last; ++i ) | |
6712283c | 339 | i->~T(); |
0516de2c | 340 | |
c157b27e VS |
341 | // once that's done, move following elements over to the freed space: |
342 | if ( after > 0 ) | |
343 | { | |
c9faa9e9 | 344 | Ops::MemmoveBackward(m_values + idx, m_values + idx + count, after); |
c157b27e | 345 | } |
f631cd8e | 346 | |
df4aed1c | 347 | m_size -= count; |
0516de2c | 348 | |
c157b27e | 349 | return begin() + idx; |
df4aed1c | 350 | } |
0516de2c VS |
351 | |
352 | #if WXWIN_COMPATIBILITY_2_8 | |
353 | wxDEPRECATED( size_type erase(size_type n) ); | |
354 | #endif // WXWIN_COMPATIBILITY_2_8 | |
355 | ||
356 | private: | |
f9bf06ac VS |
357 | // VC6 can't compile static const int members |
358 | enum { ALLOC_INITIAL_SIZE = 16 }; | |
359 | enum { ALLOC_MAX_SIZE = 4096 }; | |
0516de2c VS |
360 | |
361 | void Copy(const wxVector& vb) | |
3c648a82 | 362 | { |
0516de2c | 363 | reserve(vb.size()); |
3c648a82 | 364 | |
0516de2c VS |
365 | for ( const_iterator i = vb.begin(); i != vb.end(); ++i ) |
366 | push_back(*i); | |
1f32f585 | 367 | } |
3c648a82 | 368 | |
e966f815 | 369 | private: |
e966f815 VS |
370 | size_type m_size, |
371 | m_capacity; | |
0516de2c | 372 | value_type *m_values; |
3c648a82 VZ |
373 | }; |
374 | ||
9cf33372 VS |
375 | #if WXWIN_COMPATIBILITY_2_8 |
376 | template<typename T> | |
32aa5bda | 377 | inline typename wxVector<T>::size_type wxVector<T>::erase(size_type n) |
9cf33372 | 378 | { |
0516de2c | 379 | erase(begin() + n); |
9cf33372 VS |
380 | return n; |
381 | } | |
382 | #endif // WXWIN_COMPATIBILITY_2_8 | |
383 | ||
e966f815 | 384 | #endif // wxUSE_STL/!wxUSE_STL |
5fd588d2 | 385 | |
e966f815 VS |
386 | #if WXWIN_COMPATIBILITY_2_8 |
387 | #define WX_DECLARE_VECTORBASE(obj, cls) typedef wxVector<obj> cls | |
388 | #define _WX_DECLARE_VECTOR(obj, cls, exp) WX_DECLARE_VECTORBASE(obj, cls) | |
389 | #define WX_DECLARE_VECTOR(obj, cls) WX_DECLARE_VECTORBASE(obj, cls) | |
390 | #endif // WXWIN_COMPATIBILITY_2_8 | |
3c648a82 | 391 | |
e966f815 | 392 | #endif // _WX_VECTOR_H_ |