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