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