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