]>
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; |
e966f815 | 120 | typedef T value_type; |
df4aed1c | 121 | typedef value_type* iterator; |
0516de2c | 122 | typedef const value_type* const_iterator; |
df4aed1c | 123 | typedef value_type& reference; |
e966f815 | 124 | |
0516de2c | 125 | wxVector() : m_size(0), m_capacity(0), m_values(NULL) {} |
e966f815 | 126 | |
a470957a | 127 | wxVector(const wxVector& c) : m_size(0), m_capacity(0), m_values(NULL) |
e966f815 | 128 | { |
0516de2c | 129 | Copy(c); |
e966f815 VS |
130 | } |
131 | ||
132 | ~wxVector() | |
133 | { | |
134 | clear(); | |
135 | } | |
136 | ||
137 | void clear() | |
138 | { | |
c157b27e VS |
139 | // call destructors of stored objects: |
140 | for ( size_type i = 0; i < m_size; i++ ) | |
141 | { | |
82b6efd2 | 142 | m_values[i].~T(); |
c157b27e VS |
143 | } |
144 | ||
c9faa9e9 | 145 | Ops::Free(m_values); |
0516de2c | 146 | m_values = NULL; |
a470957a VZ |
147 | m_size = |
148 | m_capacity = 0; | |
e966f815 VS |
149 | } |
150 | ||
151 | void reserve(size_type n) | |
152 | { | |
0516de2c VS |
153 | if ( n <= m_capacity ) |
154 | return; | |
155 | ||
156 | // increase the size twice, unless we're already too big or unless | |
157 | // more is requested | |
f5851311 | 158 | // |
c157b27e | 159 | // NB: casts to size_type are needed to suppress mingw32 warnings about |
f5851311 VZ |
160 | // mixing enums and ints in the same expression |
161 | const size_type increment = m_size > 0 | |
162 | ? wxMin(m_size, (size_type)ALLOC_MAX_SIZE) | |
163 | : (size_type)ALLOC_INITIAL_SIZE; | |
0516de2c VS |
164 | if ( m_capacity + increment > n ) |
165 | n = m_capacity + increment; | |
166 | ||
c9faa9e9 | 167 | m_values = Ops::Realloc(m_values, n * sizeof(value_type), m_size); |
0516de2c | 168 | m_capacity = n; |
e966f815 VS |
169 | } |
170 | ||
171 | size_type size() const | |
172 | { | |
173 | return m_size; | |
174 | } | |
175 | ||
176 | size_type capacity() const | |
177 | { | |
178 | return m_capacity; | |
179 | } | |
180 | ||
181 | bool empty() const | |
182 | { | |
183 | return size() == 0; | |
184 | } | |
185 | ||
186 | wxVector& operator=(const wxVector& vb) | |
187 | { | |
a470957a | 188 | clear(); |
0516de2c | 189 | Copy(vb); |
e966f815 VS |
190 | return *this; |
191 | } | |
192 | ||
0516de2c | 193 | void push_back(const value_type& v) |
e966f815 | 194 | { |
0516de2c | 195 | reserve(size() + 1); |
c157b27e VS |
196 | |
197 | // use placement new to initialize new object in preallocated place in | |
198 | // m_values and store 'v' in it: | |
199 | void* const place = m_values + m_size; | |
200 | new(place) value_type(v); | |
201 | ||
202 | // only increase m_size if the ctor didn't throw an exception; notice | |
203 | // that if it _did_ throw, everything is OK, because we only increased | |
204 | // vector's capacity so far and possibly written some data to | |
205 | // uninitialized memory at the end of m_values | |
206 | m_size++; | |
e966f815 VS |
207 | } |
208 | ||
209 | void pop_back() | |
210 | { | |
0516de2c | 211 | erase(end() - 1); |
e966f815 VS |
212 | } |
213 | ||
214 | const value_type& at(size_type idx) const | |
215 | { | |
216 | wxASSERT(idx < m_size); | |
0516de2c | 217 | return m_values[idx]; |
e966f815 VS |
218 | } |
219 | ||
220 | value_type& at(size_type idx) | |
221 | { | |
222 | wxASSERT(idx < m_size); | |
0516de2c | 223 | return m_values[idx]; |
e966f815 | 224 | } |
3c648a82 | 225 | |
e966f815 VS |
226 | const value_type& operator[](size_type idx) const { return at(idx); } |
227 | value_type& operator[](size_type idx) { return at(idx); } | |
228 | const value_type& front() const { return at(0); } | |
229 | value_type& front() { return at(0); } | |
230 | const value_type& back() const { return at(size() - 1); } | |
231 | value_type& back() { return at(size() - 1); } | |
232 | ||
0516de2c VS |
233 | const_iterator begin() const { return m_values; } |
234 | iterator begin() { return m_values; } | |
235 | const_iterator end() const { return m_values + size(); } | |
236 | iterator end() { return m_values + size(); } | |
df4aed1c | 237 | |
0516de2c | 238 | iterator insert(iterator it, const value_type& v = value_type()) |
f631cd8e | 239 | { |
c157b27e VS |
240 | // NB: this must be done before reserve(), because reserve() |
241 | // invalidates iterators! | |
242 | const size_t idx = it - begin(); | |
243 | const size_t after = end() - it; | |
f631cd8e | 244 | |
0516de2c | 245 | reserve(size() + 1); |
9cf33372 | 246 | |
c157b27e | 247 | // unless we're inserting at the end, move following elements out of |
0516de2c | 248 | // the way: |
c157b27e VS |
249 | if ( after > 0 ) |
250 | { | |
c9faa9e9 | 251 | Ops::MemmoveForward(m_values + idx + 1, m_values + idx, after); |
c157b27e VS |
252 | } |
253 | ||
254 | #if wxUSE_EXCEPTIONS | |
255 | try | |
256 | { | |
257 | #endif | |
258 | // use placement new to initialize new object in preallocated place | |
259 | // in m_values and store 'v' in it: | |
260 | void* const place = m_values + idx; | |
261 | new(place) value_type(v); | |
262 | #if wxUSE_EXCEPTIONS | |
263 | } | |
264 | catch ( ... ) | |
265 | { | |
266 | // if the ctor threw an exception, we need to move all the elements | |
267 | // back to their original positions in m_values | |
268 | if ( after > 0 ) | |
269 | { | |
c9faa9e9 | 270 | Ops::MemmoveBackward(m_values + idx, m_values + idx + 1, after); |
c157b27e VS |
271 | } |
272 | ||
273 | throw; // rethrow the exception | |
274 | } | |
275 | #endif // wxUSE_EXCEPTIONS | |
e966f815 | 276 | |
c157b27e VS |
277 | // increment m_size only if ctor didn't throw -- if it did, we'll be |
278 | // left with m_values larger than necessary, but number of elements will | |
279 | // be the same | |
0516de2c | 280 | m_size++; |
3c648a82 | 281 | |
0516de2c | 282 | return begin() + idx; |
1f32f585 | 283 | } |
3c648a82 | 284 | |
0516de2c | 285 | iterator erase(iterator it) |
3c648a82 | 286 | { |
0516de2c | 287 | return erase(it, it + 1); |
1f32f585 | 288 | } |
3c648a82 | 289 | |
0516de2c | 290 | iterator erase(iterator first, iterator last) |
df4aed1c | 291 | { |
0516de2c VS |
292 | if ( first == last ) |
293 | return first; | |
294 | wxASSERT( first < end() && last <= end() ); | |
f631cd8e | 295 | |
c157b27e VS |
296 | const size_type idx = first - begin(); |
297 | const size_type count = last - first; | |
298 | const size_type after = end() - last; | |
df4aed1c | 299 | |
c157b27e VS |
300 | // erase elements by calling their destructors: |
301 | for ( iterator i = first; i < last; ++i ) | |
6712283c | 302 | i->~T(); |
0516de2c | 303 | |
c157b27e VS |
304 | // once that's done, move following elements over to the freed space: |
305 | if ( after > 0 ) | |
306 | { | |
c9faa9e9 | 307 | Ops::MemmoveBackward(m_values + idx, m_values + idx + count, after); |
c157b27e | 308 | } |
f631cd8e | 309 | |
df4aed1c | 310 | m_size -= count; |
0516de2c | 311 | |
c157b27e | 312 | return begin() + idx; |
df4aed1c | 313 | } |
0516de2c VS |
314 | |
315 | #if WXWIN_COMPATIBILITY_2_8 | |
316 | wxDEPRECATED( size_type erase(size_type n) ); | |
317 | #endif // WXWIN_COMPATIBILITY_2_8 | |
318 | ||
319 | private: | |
f9bf06ac VS |
320 | // VC6 can't compile static const int members |
321 | enum { ALLOC_INITIAL_SIZE = 16 }; | |
322 | enum { ALLOC_MAX_SIZE = 4096 }; | |
0516de2c VS |
323 | |
324 | void Copy(const wxVector& vb) | |
3c648a82 | 325 | { |
0516de2c | 326 | reserve(vb.size()); |
3c648a82 | 327 | |
0516de2c VS |
328 | for ( const_iterator i = vb.begin(); i != vb.end(); ++i ) |
329 | push_back(*i); | |
1f32f585 | 330 | } |
3c648a82 | 331 | |
e966f815 | 332 | private: |
e966f815 VS |
333 | size_type m_size, |
334 | m_capacity; | |
0516de2c | 335 | value_type *m_values; |
3c648a82 VZ |
336 | }; |
337 | ||
9cf33372 VS |
338 | #if WXWIN_COMPATIBILITY_2_8 |
339 | template<typename T> | |
32aa5bda | 340 | inline typename wxVector<T>::size_type wxVector<T>::erase(size_type n) |
9cf33372 | 341 | { |
0516de2c | 342 | erase(begin() + n); |
9cf33372 VS |
343 | return n; |
344 | } | |
345 | #endif // WXWIN_COMPATIBILITY_2_8 | |
346 | ||
e966f815 | 347 | #endif // wxUSE_STL/!wxUSE_STL |
5fd588d2 | 348 | |
e966f815 VS |
349 | #if WXWIN_COMPATIBILITY_2_8 |
350 | #define WX_DECLARE_VECTORBASE(obj, cls) typedef wxVector<obj> cls | |
351 | #define _WX_DECLARE_VECTOR(obj, cls, exp) WX_DECLARE_VECTORBASE(obj, cls) | |
352 | #define WX_DECLARE_VECTOR(obj, cls) WX_DECLARE_VECTORBASE(obj, cls) | |
353 | #endif // WXWIN_COMPATIBILITY_2_8 | |
3c648a82 | 354 | |
e966f815 | 355 | #endif // _WX_VECTOR_H_ |