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