]> git.saurik.com Git - wxWidgets.git/blame - include/wx/vector.h
Add missing wxUSE_OLE checks to safearray.cpp.
[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
01871bf6 17#if wxUSE_STD_CONTAINERS
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 28
01871bf6 29#else // !wxUSE_STD_CONTAINERS
e966f815 30
fc31181d 31#include "wx/scopeguard.h"
6712283c
VS
32#include "wx/meta/movable.h"
33#include "wx/meta/if.h"
7df6b37a 34
c157b27e
VS
35#include "wx/beforestd.h"
36#include <new> // for placement new
37#include "wx/afterstd.h"
38
6712283c
VS
39namespace 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:
47template<typename T>
48struct 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:
64template<typename T>
65struct 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 {
54742e34 75 ::new(mem + i) T(old[i]);
6712283c
VS
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 {
0e82d270 89 ::new(destptr) T(*sourceptr);
6712283c
VS
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 {
0e82d270 101 ::new(destptr) T(*sourceptr);
6712283c
VS
102 sourceptr->~T();
103 }
104 }
105};
106
107
108} // namespace wxPrivate
109
e966f815
VS
110template<typename T>
111class wxVector
3c648a82 112{
c9faa9e9 113private:
0c73d133 114 // This cryptic expression means "typedef Ops to wxVectorMemOpsMovable if
c9faa9e9
VS
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.
25859335
VZ
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),
c9faa9e9
VS
124 wxPrivate::wxVectorMemOpsMovable<T>,
125 wxPrivate::wxVectorMemOpsGeneric<T> >::value
126 Ops;
127
2d6c58d6 128public:
5fd588d2 129 typedef size_t size_type;
946954d3 130 typedef size_t difference_type;
e966f815 131 typedef T value_type;
946954d3 132 typedef value_type* pointer;
df4aed1c 133 typedef value_type* iterator;
0516de2c 134 typedef const value_type* const_iterator;
df4aed1c 135 typedef value_type& reference;
e966f815 136
946954d3
RR
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) { }
deb0c402 143
946954d3
RR
144 reference operator*() const { return *m_ptr; }
145 pointer operator->() const { return m_ptr; }
deb0c402 146
946954d3 147 iterator base() const { return m_ptr; }
deb0c402
VZ
148
149 reverse_iterator& operator++()
150 { --m_ptr; return *this; }
946954d3 151 reverse_iterator operator++(int)
deb0c402
VZ
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
946954d3 158 reverse_iterator operator+(difference_type n) const
deb0c402 159 { return reverse_iterator(m_ptr - n); }
946954d3 160 reverse_iterator& operator+=(difference_type n)
deb0c402 161 { m_ptr -= n; return *this; }
946954d3 162 reverse_iterator operator-(difference_type n) const
deb0c402 163 { return reverse_iterator(m_ptr + n); }
946954d3 164 reverse_iterator& operator-=(difference_type n)
deb0c402
VZ
165 { m_ptr += n; return *this; }
166
946954d3 167 reference operator[](difference_type n) const
deb0c402
VZ
168 { return *(*this + n); }
169
170 bool operator ==(const reverse_iterator& it) const
171 { return m_ptr == it.m_ptr; }
946954d3 172 bool operator !=(const reverse_iterator& it) const
deb0c402
VZ
173 { return m_ptr != it.m_ptr; }
174
946954d3
RR
175 private:
176 value_type *m_ptr;
177 };
178
0516de2c 179 wxVector() : m_size(0), m_capacity(0), m_values(NULL) {}
e966f815 180
f6363458 181 wxVector(size_type p_size)
e068310a 182 : m_size(0), m_capacity(0), m_values(NULL)
664e5ff9 183 {
f6363458
VZ
184 reserve(p_size);
185 for ( size_t n = 0; n < p_size; n++ )
664e5ff9
VZ
186 push_back(value_type());
187 }
188
f6363458 189 wxVector(size_type p_size, const value_type& v)
e068310a 190 : m_size(0), m_capacity(0), m_values(NULL)
664e5ff9 191 {
f6363458
VZ
192 reserve(p_size);
193 for ( size_t n = 0; n < p_size; n++ )
664e5ff9
VZ
194 push_back(v);
195 }
196
a470957a 197 wxVector(const wxVector& c) : m_size(0), m_capacity(0), m_values(NULL)
e966f815 198 {
0516de2c 199 Copy(c);
e966f815
VS
200 }
201
202 ~wxVector()
203 {
204 clear();
205 }
206
2ff86a86
VZ
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
dbe0872f
VZ
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
e966f815
VS
222 void clear()
223 {
c157b27e
VS
224 // call destructors of stored objects:
225 for ( size_type i = 0; i < m_size; i++ )
226 {
82b6efd2 227 m_values[i].~T();
c157b27e
VS
228 }
229
c9faa9e9 230 Ops::Free(m_values);
0516de2c 231 m_values = NULL;
a470957a
VZ
232 m_size =
233 m_capacity = 0;
e966f815
VS
234 }
235
236 void reserve(size_type n)
237 {
0516de2c
VS
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
f5851311 243 //
da63066b
PC
244 // NB: casts to size_type are needed to suppress warnings about
245 // mixing enumeral and non-enumeral type in conditional expression
f5851311 246 const size_type increment = m_size > 0
84523830
VZ
247 ? m_size < ALLOC_MAX_SIZE
248 ? m_size
da63066b 249 : (size_type)ALLOC_MAX_SIZE
f5851311 250 : (size_type)ALLOC_INITIAL_SIZE;
0516de2c
VS
251 if ( m_capacity + increment > n )
252 n = m_capacity + increment;
253
c9faa9e9 254 m_values = Ops::Realloc(m_values, n * sizeof(value_type), m_size);
0516de2c 255 m_capacity = n;
e966f815
VS
256 }
257
e068310a
VZ
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
e966f815
VS
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 {
a09307ab
PC
291 if (this != &vb)
292 {
293 clear();
294 Copy(vb);
295 }
e966f815
VS
296 return *this;
297 }
298
0516de2c 299 void push_back(const value_type& v)
e966f815 300 {
0516de2c 301 reserve(size() + 1);
c157b27e
VS
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;
54742e34 306 ::new(place) value_type(v);
c157b27e
VS
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++;
e966f815
VS
313 }
314
315 void pop_back()
316 {
0516de2c 317 erase(end() - 1);
e966f815
VS
318 }
319
320 const value_type& at(size_type idx) const
321 {
322 wxASSERT(idx < m_size);
0516de2c 323 return m_values[idx];
e966f815
VS
324 }
325
326 value_type& at(size_type idx)
327 {
328 wxASSERT(idx < m_size);
0516de2c 329 return m_values[idx];
e966f815 330 }
3c648a82 331
e966f815
VS
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
0516de2c
VS
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(); }
df4aed1c 343
946954d3
RR
344 reverse_iterator rbegin() { return reverse_iterator(end() - 1); }
345 reverse_iterator rend() { return reverse_iterator(begin() - 1); }
346
0516de2c 347 iterator insert(iterator it, const value_type& v = value_type())
f631cd8e 348 {
c157b27e
VS
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;
f631cd8e 353
0516de2c 354 reserve(size() + 1);
9cf33372 355
fc31181d
VZ
356 // the place where the new element is going to be inserted
357 value_type * const place = m_values + idx;
358
c157b27e 359 // unless we're inserting at the end, move following elements out of
0516de2c 360 // the way:
c157b27e 361 if ( after > 0 )
fc31181d 362 Ops::MemmoveForward(place + 1, place, after);
c157b27e 363
fc31181d
VZ
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:
0e82d270 373 ::new(place) value_type(v);
e966f815 374
fc31181d
VZ
375 // now that we did successfully add the new element, increment the size
376 // and disable moving the items back
377 moveBack.Dismiss();
0516de2c 378 m_size++;
3c648a82 379
0516de2c 380 return begin() + idx;
1f32f585 381 }
3c648a82 382
0516de2c 383 iterator erase(iterator it)
3c648a82 384 {
0516de2c 385 return erase(it, it + 1);
1f32f585 386 }
3c648a82 387
0516de2c 388 iterator erase(iterator first, iterator last)
df4aed1c 389 {
0516de2c
VS
390 if ( first == last )
391 return first;
392 wxASSERT( first < end() && last <= end() );
f631cd8e 393
c157b27e
VS
394 const size_type idx = first - begin();
395 const size_type count = last - first;
396 const size_type after = end() - last;
df4aed1c 397
c157b27e
VS
398 // erase elements by calling their destructors:
399 for ( iterator i = first; i < last; ++i )
6712283c 400 i->~T();
0516de2c 401
c157b27e
VS
402 // once that's done, move following elements over to the freed space:
403 if ( after > 0 )
404 {
c9faa9e9 405 Ops::MemmoveBackward(m_values + idx, m_values + idx + count, after);
c157b27e 406 }
f631cd8e 407
df4aed1c 408 m_size -= count;
0516de2c 409
c157b27e 410 return begin() + idx;
df4aed1c 411 }
0516de2c
VS
412
413#if WXWIN_COMPATIBILITY_2_8
414 wxDEPRECATED( size_type erase(size_type n) );
415#endif // WXWIN_COMPATIBILITY_2_8
416
417private:
f9bf06ac
VS
418 // VC6 can't compile static const int members
419 enum { ALLOC_INITIAL_SIZE = 16 };
420 enum { ALLOC_MAX_SIZE = 4096 };
0516de2c
VS
421
422 void Copy(const wxVector& vb)
3c648a82 423 {
0516de2c 424 reserve(vb.size());
3c648a82 425
0516de2c
VS
426 for ( const_iterator i = vb.begin(); i != vb.end(); ++i )
427 push_back(*i);
1f32f585 428 }
3c648a82 429
e966f815 430private:
e068310a
VZ
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
e966f815
VS
445 size_type m_size,
446 m_capacity;
0516de2c 447 value_type *m_values;
3c648a82
VZ
448};
449
9cf33372
VS
450#if WXWIN_COMPATIBILITY_2_8
451template<typename T>
32aa5bda 452inline typename wxVector<T>::size_type wxVector<T>::erase(size_type n)
9cf33372 453{
0516de2c 454 erase(begin() + n);
9cf33372
VS
455 return n;
456}
457#endif // WXWIN_COMPATIBILITY_2_8
458
38723be1
RD
459
460
461namespace wxPrivate
462{
38723be1 463
d8eff331
VZ
464// This is a helper for the wxVectorSort function, and should not be used
465// directly in user's code.
38723be1 466template<typename T>
25859335 467struct wxVectorComparator
38723be1 468{
449cc351 469 static int
d8eff331
VZ
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};
38723be1
RD
483
484} // namespace wxPrivate
485
486
487
488template<typename T>
489void wxVectorSort(wxVector<T>& v)
490{
491 wxQsort(v.begin(), v.size(), sizeof(T),
25859335 492 wxPrivate::wxVectorComparator<T>::Compare, NULL);
38723be1
RD
493}
494
495
496
01871bf6 497#endif // wxUSE_STD_CONTAINERS/!wxUSE_STD_CONTAINERS
5fd588d2 498
e966f815
VS
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
3c648a82 504
e966f815 505#endif // _WX_VECTOR_H_