]> git.saurik.com Git - wxWidgets.git/blob - include/wx/vector.h
Don't include wx/utils.h from wx/vector.h.
[wxWidgets.git] / include / wx / vector.h
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_STD_CONTAINERS
18
19 #include <vector>
20 #include <algorithm>
21
22 #define wxVector std::vector
23 template<typename T>
24 inline void wxVectorSort(wxVector<T>& v)
25 {
26 std::sort(v.begin(), v.end());
27 }
28
29 #else // !wxUSE_STD_CONTAINERS
30
31 #include "wx/scopeguard.h"
32 #include "wx/meta/movable.h"
33 #include "wx/meta/if.h"
34
35 #include "wx/beforestd.h"
36 #include <new> // for placement new
37 #include "wx/afterstd.h"
38
39 namespace wxPrivate
40 {
41
42 // These templates encapsulate memory operations for use by wxVector; there are
43 // two implementations, both in generic way for any C++ types and as an
44 // optimized version for "movable" types that uses realloc() and memmove().
45
46 // version for movable types:
47 template<typename T>
48 struct wxVectorMemOpsMovable
49 {
50 static void Free(T* array)
51 { free(array); }
52
53 static T* Realloc(T* old, size_t newCapacity, size_t WXUNUSED(occupiedSize))
54 { return (T*)realloc(old, newCapacity * sizeof(T)); }
55
56 static void MemmoveBackward(T* dest, T* source, size_t count)
57 { memmove(dest, source, count * sizeof(T)); }
58
59 static void MemmoveForward(T* dest, T* source, size_t count)
60 { memmove(dest, source, count * sizeof(T)); }
61 };
62
63 // generic version for non-movable types:
64 template<typename T>
65 struct wxVectorMemOpsGeneric
66 {
67 static void Free(T* array)
68 { ::operator delete(array); }
69
70 static T* Realloc(T* old, size_t newCapacity, size_t occupiedSize)
71 {
72 T *mem = (T*)::operator new(newCapacity * sizeof(T));
73 for ( size_t i = 0; i < occupiedSize; i++ )
74 {
75 ::new(mem + i) T(old[i]);
76 old[i].~T();
77 }
78 ::operator delete(old);
79 return mem;
80 }
81
82 static void MemmoveBackward(T* dest, T* source, size_t count)
83 {
84 wxASSERT( dest < source );
85 T* destptr = dest;
86 T* sourceptr = source;
87 for ( size_t i = count; i > 0; --i, ++destptr, ++sourceptr )
88 {
89 ::new(destptr) T(*sourceptr);
90 sourceptr->~T();
91 }
92 }
93
94 static void MemmoveForward(T* dest, T* source, size_t count)
95 {
96 wxASSERT( dest > source );
97 T* destptr = dest + count - 1;
98 T* sourceptr = source + count - 1;
99 for ( size_t i = count; i > 0; --i, --destptr, --sourceptr )
100 {
101 ::new(destptr) T(*sourceptr);
102 sourceptr->~T();
103 }
104 }
105 };
106
107
108 } // namespace wxPrivate
109
110 template<typename T>
111 class wxVector
112 {
113 private:
114 // This cryptic expression means "typedef Ops to wxVectorMemOpsMovable if
115 // type T is movable type, otherwise to wxVectorMemOpsGeneric".
116 //
117 // Note that we use typedef instead of privately deriving from this (which
118 // would allowed us to omit "Ops::" prefixes below) to keep VC6 happy,
119 // it can't compile code that derives from wxIf<...>::value.
120 //
121 // Note that bcc needs the extra parentheses for non-type template
122 // arguments to compile this expression.
123 typedef typename wxIf< (wxIsMovable<T>::value),
124 wxPrivate::wxVectorMemOpsMovable<T>,
125 wxPrivate::wxVectorMemOpsGeneric<T> >::value
126 Ops;
127
128 public:
129 typedef size_t size_type;
130 typedef size_t difference_type;
131 typedef T value_type;
132 typedef value_type* pointer;
133 typedef value_type* iterator;
134 typedef const value_type* const_iterator;
135 typedef value_type& reference;
136
137 class reverse_iterator
138 {
139 public:
140 reverse_iterator() : m_ptr(NULL) { }
141 wxEXPLICIT reverse_iterator(iterator it) : m_ptr(it) { }
142 reverse_iterator(const reverse_iterator& it) : m_ptr(it.m_ptr) { }
143
144 reference operator*() const { return *m_ptr; }
145 pointer operator->() const { return m_ptr; }
146
147 iterator base() const { return m_ptr; }
148
149 reverse_iterator& operator++()
150 { --m_ptr; return *this; }
151 reverse_iterator operator++(int)
152 { reverse_iterator tmp = *this; --m_ptr; return tmp; }
153 reverse_iterator& operator--()
154 { ++m_ptr; return *this; }
155 reverse_iterator operator--(int)
156 { reverse_iterator tmp = *this; ++m_ptr; return tmp; }
157
158 reverse_iterator operator+(difference_type n) const
159 { return reverse_iterator(m_ptr - n); }
160 reverse_iterator& operator+=(difference_type n)
161 { m_ptr -= n; return *this; }
162 reverse_iterator operator-(difference_type n) const
163 { return reverse_iterator(m_ptr + n); }
164 reverse_iterator& operator-=(difference_type n)
165 { m_ptr += n; return *this; }
166
167 reference operator[](difference_type n) const
168 { return *(*this + n); }
169
170 bool operator ==(const reverse_iterator& it) const
171 { return m_ptr == it.m_ptr; }
172 bool operator !=(const reverse_iterator& it) const
173 { return m_ptr != it.m_ptr; }
174
175 private:
176 value_type *m_ptr;
177 };
178
179 wxVector() : m_size(0), m_capacity(0), m_values(NULL) {}
180
181 wxVector(size_type p_size)
182 : m_size(0), m_capacity(0), m_values(NULL)
183 {
184 reserve(p_size);
185 for ( size_t n = 0; n < p_size; n++ )
186 push_back(value_type());
187 }
188
189 wxVector(size_type p_size, const value_type& v)
190 : m_size(0), m_capacity(0), m_values(NULL)
191 {
192 reserve(p_size);
193 for ( size_t n = 0; n < p_size; n++ )
194 push_back(v);
195 }
196
197 wxVector(const wxVector& c) : m_size(0), m_capacity(0), m_values(NULL)
198 {
199 Copy(c);
200 }
201
202 ~wxVector()
203 {
204 clear();
205 }
206
207 void assign(size_type p_size, const value_type& v)
208 {
209 clear();
210 reserve(p_size);
211 for ( size_t n = 0; n < p_size; n++ )
212 push_back(v);
213 }
214
215 void swap(wxVector& v)
216 {
217 wxSwap(m_size, v.m_size);
218 wxSwap(m_capacity, v.m_capacity);
219 wxSwap(m_values, v.m_values);
220 }
221
222 void clear()
223 {
224 // call destructors of stored objects:
225 for ( size_type i = 0; i < m_size; i++ )
226 {
227 m_values[i].~T();
228 }
229
230 Ops::Free(m_values);
231 m_values = NULL;
232 m_size =
233 m_capacity = 0;
234 }
235
236 void reserve(size_type n)
237 {
238 if ( n <= m_capacity )
239 return;
240
241 // increase the size twice, unless we're already too big or unless
242 // more is requested
243 //
244 // NB: casts to size_type are needed to suppress mingw32 warnings about
245 // mixing enums and ints in the same expression
246 const size_type increment = m_size > 0
247 ? m_size < ALLOC_MAX_SIZE
248 ? m_size
249 : ALLOC_MAX_SIZE
250 : (size_type)ALLOC_INITIAL_SIZE;
251 if ( m_capacity + increment > n )
252 n = m_capacity + increment;
253
254 m_values = Ops::Realloc(m_values, n * sizeof(value_type), m_size);
255 m_capacity = n;
256 }
257
258 void resize(size_type n)
259 {
260 if ( n < m_size )
261 Shrink(n);
262 else if ( n > m_size )
263 Extend(n, value_type());
264 }
265
266 void resize(size_type n, const value_type& v)
267 {
268 if ( n < m_size )
269 Shrink(n);
270 else if ( n > m_size )
271 Extend(n, v);
272 }
273
274 size_type size() const
275 {
276 return m_size;
277 }
278
279 size_type capacity() const
280 {
281 return m_capacity;
282 }
283
284 bool empty() const
285 {
286 return size() == 0;
287 }
288
289 wxVector& operator=(const wxVector& vb)
290 {
291 if (this != &vb)
292 {
293 clear();
294 Copy(vb);
295 }
296 return *this;
297 }
298
299 void push_back(const value_type& v)
300 {
301 reserve(size() + 1);
302
303 // use placement new to initialize new object in preallocated place in
304 // m_values and store 'v' in it:
305 void* const place = m_values + m_size;
306 ::new(place) value_type(v);
307
308 // only increase m_size if the ctor didn't throw an exception; notice
309 // that if it _did_ throw, everything is OK, because we only increased
310 // vector's capacity so far and possibly written some data to
311 // uninitialized memory at the end of m_values
312 m_size++;
313 }
314
315 void pop_back()
316 {
317 erase(end() - 1);
318 }
319
320 const value_type& at(size_type idx) const
321 {
322 wxASSERT(idx < m_size);
323 return m_values[idx];
324 }
325
326 value_type& at(size_type idx)
327 {
328 wxASSERT(idx < m_size);
329 return m_values[idx];
330 }
331
332 const value_type& operator[](size_type idx) const { return at(idx); }
333 value_type& operator[](size_type idx) { return at(idx); }
334 const value_type& front() const { return at(0); }
335 value_type& front() { return at(0); }
336 const value_type& back() const { return at(size() - 1); }
337 value_type& back() { return at(size() - 1); }
338
339 const_iterator begin() const { return m_values; }
340 iterator begin() { return m_values; }
341 const_iterator end() const { return m_values + size(); }
342 iterator end() { return m_values + size(); }
343
344 reverse_iterator rbegin() { return reverse_iterator(end() - 1); }
345 reverse_iterator rend() { return reverse_iterator(begin() - 1); }
346
347 iterator insert(iterator it, const value_type& v = value_type())
348 {
349 // NB: this must be done before reserve(), because reserve()
350 // invalidates iterators!
351 const size_t idx = it - begin();
352 const size_t after = end() - it;
353
354 reserve(size() + 1);
355
356 // the place where the new element is going to be inserted
357 value_type * const place = m_values + idx;
358
359 // unless we're inserting at the end, move following elements out of
360 // the way:
361 if ( after > 0 )
362 Ops::MemmoveForward(place + 1, place, after);
363
364 // if the ctor called below throws an exception, we need to move all
365 // the elements back to their original positions in m_values
366 wxScopeGuard moveBack = wxMakeGuard(
367 Ops::MemmoveBackward, place, place + 1, after);
368 if ( !after )
369 moveBack.Dismiss();
370
371 // use placement new to initialize new object in preallocated place in
372 // m_values and store 'v' in it:
373 ::new(place) value_type(v);
374
375 // now that we did successfully add the new element, increment the size
376 // and disable moving the items back
377 moveBack.Dismiss();
378 m_size++;
379
380 return begin() + idx;
381 }
382
383 iterator erase(iterator it)
384 {
385 return erase(it, it + 1);
386 }
387
388 iterator erase(iterator first, iterator last)
389 {
390 if ( first == last )
391 return first;
392 wxASSERT( first < end() && last <= end() );
393
394 const size_type idx = first - begin();
395 const size_type count = last - first;
396 const size_type after = end() - last;
397
398 // erase elements by calling their destructors:
399 for ( iterator i = first; i < last; ++i )
400 i->~T();
401
402 // once that's done, move following elements over to the freed space:
403 if ( after > 0 )
404 {
405 Ops::MemmoveBackward(m_values + idx, m_values + idx + count, after);
406 }
407
408 m_size -= count;
409
410 return begin() + idx;
411 }
412
413 #if WXWIN_COMPATIBILITY_2_8
414 wxDEPRECATED( size_type erase(size_type n) );
415 #endif // WXWIN_COMPATIBILITY_2_8
416
417 private:
418 // VC6 can't compile static const int members
419 enum { ALLOC_INITIAL_SIZE = 16 };
420 enum { ALLOC_MAX_SIZE = 4096 };
421
422 void Copy(const wxVector& vb)
423 {
424 reserve(vb.size());
425
426 for ( const_iterator i = vb.begin(); i != vb.end(); ++i )
427 push_back(*i);
428 }
429
430 private:
431 void Shrink(size_type n)
432 {
433 for ( size_type i = n; i < m_size; i++ )
434 m_values[i].~T();
435 m_size = n;
436 }
437
438 void Extend(size_type n, const value_type& v)
439 {
440 reserve(n);
441 for ( size_type i = m_size; i < n; i++ )
442 push_back(v);
443 }
444
445 size_type m_size,
446 m_capacity;
447 value_type *m_values;
448 };
449
450 #if WXWIN_COMPATIBILITY_2_8
451 template<typename T>
452 inline typename wxVector<T>::size_type wxVector<T>::erase(size_type n)
453 {
454 erase(begin() + n);
455 return n;
456 }
457 #endif // WXWIN_COMPATIBILITY_2_8
458
459
460
461 namespace wxPrivate
462 {
463
464 // This is a helper for the wxVectorSort function, and should not be used
465 // directly in user's code.
466 template<typename T>
467 struct wxVectorComparator
468 {
469 static int
470 Compare(const void* pitem1, const void* pitem2, const void* )
471 {
472 const T& item1 = *reinterpret_cast<const T*>(pitem1);
473 const T& item2 = *reinterpret_cast<const T*>(pitem2);
474
475 if (item1 < item2)
476 return -1;
477 else if (item2 < item1)
478 return 1;
479 else
480 return 0;
481 }
482 };
483
484 } // namespace wxPrivate
485
486
487
488 template<typename T>
489 void wxVectorSort(wxVector<T>& v)
490 {
491 wxQsort(v.begin(), v.size(), sizeof(T),
492 wxPrivate::wxVectorComparator<T>::Compare, NULL);
493 }
494
495
496
497 #endif // wxUSE_STD_CONTAINERS/!wxUSE_STD_CONTAINERS
498
499 #if WXWIN_COMPATIBILITY_2_8
500 #define WX_DECLARE_VECTORBASE(obj, cls) typedef wxVector<obj> cls
501 #define _WX_DECLARE_VECTOR(obj, cls, exp) WX_DECLARE_VECTORBASE(obj, cls)
502 #define WX_DECLARE_VECTOR(obj, cls) WX_DECLARE_VECTORBASE(obj, cls)
503 #endif // WXWIN_COMPATIBILITY_2_8
504
505 #endif // _WX_VECTOR_H_