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