]> git.saurik.com Git - wxWidgets.git/blame - include/wx/vector.h
wxRTC text box layout fixes
[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;
df4aed1c 142 typedef value_type* iterator;
0516de2c 143 typedef const value_type* const_iterator;
df4aed1c 144 typedef value_type& reference;
e966f815 145
946954d3
RR
146 class reverse_iterator
147 {
148 public:
149 reverse_iterator() : m_ptr(NULL) { }
150 wxEXPLICIT reverse_iterator(iterator it) : m_ptr(it) { }
151 reverse_iterator(const reverse_iterator& it) : m_ptr(it.m_ptr) { }
deb0c402 152
946954d3
RR
153 reference operator*() const { return *m_ptr; }
154 pointer operator->() const { return m_ptr; }
deb0c402 155
946954d3 156 iterator base() const { return m_ptr; }
deb0c402
VZ
157
158 reverse_iterator& operator++()
159 { --m_ptr; return *this; }
946954d3 160 reverse_iterator operator++(int)
deb0c402
VZ
161 { reverse_iterator tmp = *this; --m_ptr; return tmp; }
162 reverse_iterator& operator--()
163 { ++m_ptr; return *this; }
164 reverse_iterator operator--(int)
165 { reverse_iterator tmp = *this; ++m_ptr; return tmp; }
166
946954d3 167 reverse_iterator operator+(difference_type n) const
deb0c402 168 { return reverse_iterator(m_ptr - n); }
946954d3 169 reverse_iterator& operator+=(difference_type n)
deb0c402 170 { m_ptr -= n; return *this; }
946954d3 171 reverse_iterator operator-(difference_type n) const
deb0c402 172 { return reverse_iterator(m_ptr + n); }
946954d3 173 reverse_iterator& operator-=(difference_type n)
deb0c402
VZ
174 { m_ptr += n; return *this; }
175
946954d3 176 reference operator[](difference_type n) const
deb0c402
VZ
177 { return *(*this + n); }
178
179 bool operator ==(const reverse_iterator& it) const
180 { return m_ptr == it.m_ptr; }
946954d3 181 bool operator !=(const reverse_iterator& it) const
deb0c402
VZ
182 { return m_ptr != it.m_ptr; }
183
946954d3
RR
184 private:
185 value_type *m_ptr;
186 };
187
0516de2c 188 wxVector() : m_size(0), m_capacity(0), m_values(NULL) {}
e966f815 189
f6363458 190 wxVector(size_type p_size)
e068310a 191 : m_size(0), m_capacity(0), m_values(NULL)
664e5ff9 192 {
f6363458
VZ
193 reserve(p_size);
194 for ( size_t n = 0; n < p_size; n++ )
664e5ff9
VZ
195 push_back(value_type());
196 }
197
f6363458 198 wxVector(size_type p_size, const value_type& v)
e068310a 199 : m_size(0), m_capacity(0), m_values(NULL)
664e5ff9 200 {
f6363458
VZ
201 reserve(p_size);
202 for ( size_t n = 0; n < p_size; n++ )
664e5ff9
VZ
203 push_back(v);
204 }
205
a470957a 206 wxVector(const wxVector& c) : m_size(0), m_capacity(0), m_values(NULL)
e966f815 207 {
0516de2c 208 Copy(c);
e966f815
VS
209 }
210
211 ~wxVector()
212 {
213 clear();
214 }
215
2ff86a86
VZ
216 void assign(size_type p_size, const value_type& v)
217 {
218 clear();
219 reserve(p_size);
220 for ( size_t n = 0; n < p_size; n++ )
221 push_back(v);
222 }
223
dbe0872f
VZ
224 void swap(wxVector& v)
225 {
226 wxSwap(m_size, v.m_size);
227 wxSwap(m_capacity, v.m_capacity);
228 wxSwap(m_values, v.m_values);
229 }
230
e966f815
VS
231 void clear()
232 {
c157b27e
VS
233 // call destructors of stored objects:
234 for ( size_type i = 0; i < m_size; i++ )
235 {
82b6efd2 236 m_values[i].~T();
c157b27e
VS
237 }
238
c9faa9e9 239 Ops::Free(m_values);
0516de2c 240 m_values = NULL;
a470957a
VZ
241 m_size =
242 m_capacity = 0;
e966f815
VS
243 }
244
245 void reserve(size_type n)
246 {
0516de2c
VS
247 if ( n <= m_capacity )
248 return;
249
250 // increase the size twice, unless we're already too big or unless
251 // more is requested
f5851311 252 //
da63066b
PC
253 // NB: casts to size_type are needed to suppress warnings about
254 // mixing enumeral and non-enumeral type in conditional expression
f5851311 255 const size_type increment = m_size > 0
84523830
VZ
256 ? m_size < ALLOC_MAX_SIZE
257 ? m_size
da63066b 258 : (size_type)ALLOC_MAX_SIZE
f5851311 259 : (size_type)ALLOC_INITIAL_SIZE;
0516de2c
VS
260 if ( m_capacity + increment > n )
261 n = m_capacity + increment;
262
c9faa9e9 263 m_values = Ops::Realloc(m_values, n * sizeof(value_type), m_size);
0516de2c 264 m_capacity = n;
e966f815
VS
265 }
266
e068310a
VZ
267 void resize(size_type n)
268 {
269 if ( n < m_size )
270 Shrink(n);
271 else if ( n > m_size )
272 Extend(n, value_type());
273 }
274
275 void resize(size_type n, const value_type& v)
276 {
277 if ( n < m_size )
278 Shrink(n);
279 else if ( n > m_size )
280 Extend(n, v);
281 }
282
e966f815
VS
283 size_type size() const
284 {
285 return m_size;
286 }
287
288 size_type capacity() const
289 {
290 return m_capacity;
291 }
292
293 bool empty() const
294 {
295 return size() == 0;
296 }
297
298 wxVector& operator=(const wxVector& vb)
299 {
a09307ab
PC
300 if (this != &vb)
301 {
302 clear();
303 Copy(vb);
304 }
e966f815
VS
305 return *this;
306 }
307
0516de2c 308 void push_back(const value_type& v)
e966f815 309 {
0516de2c 310 reserve(size() + 1);
c157b27e
VS
311
312 // use placement new to initialize new object in preallocated place in
313 // m_values and store 'v' in it:
314 void* const place = m_values + m_size;
54742e34 315 ::new(place) value_type(v);
c157b27e
VS
316
317 // only increase m_size if the ctor didn't throw an exception; notice
318 // that if it _did_ throw, everything is OK, because we only increased
319 // vector's capacity so far and possibly written some data to
320 // uninitialized memory at the end of m_values
321 m_size++;
e966f815
VS
322 }
323
324 void pop_back()
325 {
0516de2c 326 erase(end() - 1);
e966f815
VS
327 }
328
329 const value_type& at(size_type idx) const
330 {
331 wxASSERT(idx < m_size);
0516de2c 332 return m_values[idx];
e966f815
VS
333 }
334
335 value_type& at(size_type idx)
336 {
337 wxASSERT(idx < m_size);
0516de2c 338 return m_values[idx];
e966f815 339 }
3c648a82 340
e966f815
VS
341 const value_type& operator[](size_type idx) const { return at(idx); }
342 value_type& operator[](size_type idx) { return at(idx); }
343 const value_type& front() const { return at(0); }
344 value_type& front() { return at(0); }
345 const value_type& back() const { return at(size() - 1); }
346 value_type& back() { return at(size() - 1); }
347
0516de2c
VS
348 const_iterator begin() const { return m_values; }
349 iterator begin() { return m_values; }
350 const_iterator end() const { return m_values + size(); }
351 iterator end() { return m_values + size(); }
df4aed1c 352
946954d3
RR
353 reverse_iterator rbegin() { return reverse_iterator(end() - 1); }
354 reverse_iterator rend() { return reverse_iterator(begin() - 1); }
355
0516de2c 356 iterator insert(iterator it, const value_type& v = value_type())
f631cd8e 357 {
c157b27e
VS
358 // NB: this must be done before reserve(), because reserve()
359 // invalidates iterators!
360 const size_t idx = it - begin();
361 const size_t after = end() - it;
f631cd8e 362
0516de2c 363 reserve(size() + 1);
9cf33372 364
fc31181d
VZ
365 // the place where the new element is going to be inserted
366 value_type * const place = m_values + idx;
367
c157b27e 368 // unless we're inserting at the end, move following elements out of
0516de2c 369 // the way:
c157b27e 370 if ( after > 0 )
fc31181d 371 Ops::MemmoveForward(place + 1, place, after);
c157b27e 372
fc31181d
VZ
373 // if the ctor called below throws an exception, we need to move all
374 // the elements back to their original positions in m_values
375 wxScopeGuard moveBack = wxMakeGuard(
376 Ops::MemmoveBackward, place, place + 1, after);
377 if ( !after )
378 moveBack.Dismiss();
379
380 // use placement new to initialize new object in preallocated place in
381 // m_values and store 'v' in it:
0e82d270 382 ::new(place) value_type(v);
e966f815 383
fc31181d
VZ
384 // now that we did successfully add the new element, increment the size
385 // and disable moving the items back
386 moveBack.Dismiss();
0516de2c 387 m_size++;
3c648a82 388
0516de2c 389 return begin() + idx;
1f32f585 390 }
3c648a82 391
0516de2c 392 iterator erase(iterator it)
3c648a82 393 {
0516de2c 394 return erase(it, it + 1);
1f32f585 395 }
3c648a82 396
0516de2c 397 iterator erase(iterator first, iterator last)
df4aed1c 398 {
0516de2c
VS
399 if ( first == last )
400 return first;
401 wxASSERT( first < end() && last <= end() );
f631cd8e 402
c157b27e
VS
403 const size_type idx = first - begin();
404 const size_type count = last - first;
405 const size_type after = end() - last;
df4aed1c 406
c157b27e
VS
407 // erase elements by calling their destructors:
408 for ( iterator i = first; i < last; ++i )
6712283c 409 i->~T();
0516de2c 410
c157b27e
VS
411 // once that's done, move following elements over to the freed space:
412 if ( after > 0 )
413 {
c9faa9e9 414 Ops::MemmoveBackward(m_values + idx, m_values + idx + count, after);
c157b27e 415 }
f631cd8e 416
df4aed1c 417 m_size -= count;
0516de2c 418
c157b27e 419 return begin() + idx;
df4aed1c 420 }
0516de2c
VS
421
422#if WXWIN_COMPATIBILITY_2_8
423 wxDEPRECATED( size_type erase(size_type n) );
424#endif // WXWIN_COMPATIBILITY_2_8
425
426private:
f9bf06ac
VS
427 // VC6 can't compile static const int members
428 enum { ALLOC_INITIAL_SIZE = 16 };
429 enum { ALLOC_MAX_SIZE = 4096 };
0516de2c
VS
430
431 void Copy(const wxVector& vb)
3c648a82 432 {
0516de2c 433 reserve(vb.size());
3c648a82 434
0516de2c
VS
435 for ( const_iterator i = vb.begin(); i != vb.end(); ++i )
436 push_back(*i);
1f32f585 437 }
3c648a82 438
e966f815 439private:
e068310a
VZ
440 void Shrink(size_type n)
441 {
442 for ( size_type i = n; i < m_size; i++ )
443 m_values[i].~T();
444 m_size = n;
445 }
446
447 void Extend(size_type n, const value_type& v)
448 {
449 reserve(n);
450 for ( size_type i = m_size; i < n; i++ )
451 push_back(v);
452 }
453
e966f815
VS
454 size_type m_size,
455 m_capacity;
0516de2c 456 value_type *m_values;
3c648a82
VZ
457};
458
9cf33372
VS
459#if WXWIN_COMPATIBILITY_2_8
460template<typename T>
32aa5bda 461inline typename wxVector<T>::size_type wxVector<T>::erase(size_type n)
9cf33372 462{
0516de2c 463 erase(begin() + n);
9cf33372
VS
464 return n;
465}
466#endif // WXWIN_COMPATIBILITY_2_8
467
38723be1
RD
468
469
470namespace wxPrivate
471{
38723be1 472
d8eff331
VZ
473// This is a helper for the wxVectorSort function, and should not be used
474// directly in user's code.
38723be1 475template<typename T>
25859335 476struct wxVectorComparator
38723be1 477{
449cc351 478 static int
d8eff331
VZ
479 Compare(const void* pitem1, const void* pitem2, const void* )
480 {
481 const T& item1 = *reinterpret_cast<const T*>(pitem1);
482 const T& item2 = *reinterpret_cast<const T*>(pitem2);
483
484 if (item1 < item2)
485 return -1;
486 else if (item2 < item1)
487 return 1;
488 else
489 return 0;
490 }
491};
38723be1
RD
492
493} // namespace wxPrivate
494
495
496
497template<typename T>
498void wxVectorSort(wxVector<T>& v)
499{
500 wxQsort(v.begin(), v.size(), sizeof(T),
25859335 501 wxPrivate::wxVectorComparator<T>::Compare, NULL);
38723be1
RD
502}
503
504
505
01871bf6 506#endif // wxUSE_STD_CONTAINERS/!wxUSE_STD_CONTAINERS
5fd588d2 507
e966f815
VS
508#if WXWIN_COMPATIBILITY_2_8
509 #define WX_DECLARE_VECTORBASE(obj, cls) typedef wxVector<obj> cls
510 #define _WX_DECLARE_VECTOR(obj, cls, exp) WX_DECLARE_VECTORBASE(obj, cls)
511 #define WX_DECLARE_VECTOR(obj, cls) WX_DECLARE_VECTORBASE(obj, cls)
512#endif // WXWIN_COMPATIBILITY_2_8
3c648a82 513
e966f815 514#endif // _WX_VECTOR_H_