]>
git.saurik.com Git - wxWidgets.git/blob - include/wx/vector.h
1 ///////////////////////////////////////////////////////////////////////////////
3 // Purpose: STL vector clone
4 // Author: Lindsay Mathieson
5 // Modified by: Vaclav Slavik - make it a template
7 // Copyright: (c) 2001 Lindsay Mathieson <lindsay@mathieson.org>,
8 // 2007 Vaclav Slavik <vslavik@fastmail.fm>
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
17 #if wxUSE_STD_CONTAINERS
22 #define wxVector std::vector
24 inline void wxVectorSort(wxVector
<T
>& v
)
26 std::sort(v
.begin(), v
.end());
29 #else // !wxUSE_STD_CONTAINERS
31 #include "wx/scopeguard.h"
32 #include "wx/meta/movable.h"
33 #include "wx/meta/if.h"
35 #include "wx/beforestd.h"
36 #include <new> // for placement new
37 #include "wx/afterstd.h"
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().
46 // version for movable types:
48 struct wxVectorMemOpsMovable
50 static void Free(T
* array
)
53 static T
* Realloc(T
* old
, size_t newCapacity
, size_t WXUNUSED(occupiedSize
))
54 { return (T
*)realloc(old
, newCapacity
* sizeof(T
)); }
56 static void MemmoveBackward(T
* dest
, T
* source
, size_t count
)
57 { memmove(dest
, source
, count
* sizeof(T
)); }
59 static void MemmoveForward(T
* dest
, T
* source
, size_t count
)
60 { memmove(dest
, source
, count
* sizeof(T
)); }
63 // generic version for non-movable types:
65 struct wxVectorMemOpsGeneric
67 static void Free(T
* array
)
68 { ::operator delete(array
); }
70 static T
* Realloc(T
* old
, size_t newCapacity
, size_t occupiedSize
)
72 T
*mem
= (T
*)::operator new(newCapacity
* sizeof(T
));
73 for ( size_t i
= 0; i
< occupiedSize
; i
++ )
75 ::new(mem
+ i
) T(old
[i
]);
78 ::operator delete(old
);
82 static void MemmoveBackward(T
* dest
, T
* source
, size_t count
)
84 wxASSERT( dest
< source
);
86 T
* sourceptr
= source
;
87 for ( size_t i
= count
; i
> 0; --i
, ++destptr
, ++sourceptr
)
89 ::new(destptr
) T(*sourceptr
);
94 static void MemmoveForward(T
* dest
, T
* source
, size_t count
)
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
)
101 ::new(destptr
) T(*sourceptr
);
108 } // namespace wxPrivate
114 // This cryptic expression means "typedef Ops to wxVectorMemOpsMovable if
115 // type T is movable type, otherwise to wxVectorMemOpsGeneric".
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.
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
),
124 wxPrivate::wxVectorMemOpsMovable
<T
>,
125 wxPrivate::wxVectorMemOpsGeneric
<T
> >::value
129 typedef size_t size_type
;
130 typedef size_t difference_type
;
131 typedef T value_type
;
132 typedef value_type
* pointer
;
133 typedef value_type
* iterator
;
134 typedef const value_type
* const_iterator
;
135 typedef value_type
& reference
;
137 class reverse_iterator
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
) { }
144 reference
operator*() const { return *m_ptr
; }
145 pointer
operator->() const { return m_ptr
; }
147 iterator
base() const { return m_ptr
; }
149 reverse_iterator
& operator++()
150 { --m_ptr
; return *this; }
151 reverse_iterator
operator++(int)
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
; }
158 reverse_iterator
operator+(difference_type n
) const
159 { return reverse_iterator(m_ptr
- n
); }
160 reverse_iterator
& operator+=(difference_type n
)
161 { m_ptr
-= n
; return *this; }
162 reverse_iterator
operator-(difference_type n
) const
163 { return reverse_iterator(m_ptr
+ n
); }
164 reverse_iterator
& operator-=(difference_type n
)
165 { m_ptr
+= n
; return *this; }
167 reference
operator[](difference_type n
) const
168 { return *(*this + n
); }
170 bool operator ==(const reverse_iterator
& it
) const
171 { return m_ptr
== it
.m_ptr
; }
172 bool operator !=(const reverse_iterator
& it
) const
173 { return m_ptr
!= it
.m_ptr
; }
179 wxVector() : m_size(0), m_capacity(0), m_values(NULL
) {}
181 wxVector(size_type p_size
)
182 : m_size(0), m_capacity(0), m_values(NULL
)
185 for ( size_t n
= 0; n
< p_size
; n
++ )
186 push_back(value_type());
189 wxVector(size_type p_size
, const value_type
& v
)
190 : m_size(0), m_capacity(0), m_values(NULL
)
193 for ( size_t n
= 0; n
< p_size
; n
++ )
197 wxVector(const wxVector
& c
) : m_size(0), m_capacity(0), m_values(NULL
)
207 void assign(size_type p_size
, const value_type
& v
)
211 for ( size_t n
= 0; n
< p_size
; n
++ )
215 void swap(wxVector
& v
)
217 wxSwap(m_size
, v
.m_size
);
218 wxSwap(m_capacity
, v
.m_capacity
);
219 wxSwap(m_values
, v
.m_values
);
224 // call destructors of stored objects:
225 for ( size_type i
= 0; i
< m_size
; i
++ )
236 void reserve(size_type n
)
238 if ( n
<= m_capacity
)
241 // increase the size twice, unless we're already too big or unless
244 // NB: casts to size_type are needed to suppress warnings about
245 // mixing enumeral and non-enumeral type in conditional expression
246 const size_type increment
= m_size
> 0
247 ? m_size
< ALLOC_MAX_SIZE
249 : (size_type
)ALLOC_MAX_SIZE
250 : (size_type
)ALLOC_INITIAL_SIZE
;
251 if ( m_capacity
+ increment
> n
)
252 n
= m_capacity
+ increment
;
254 m_values
= Ops::Realloc(m_values
, n
* sizeof(value_type
), m_size
);
258 void resize(size_type n
)
262 else if ( n
> m_size
)
263 Extend(n
, value_type());
266 void resize(size_type n
, const value_type
& v
)
270 else if ( n
> m_size
)
274 size_type
size() const
279 size_type
capacity() const
289 wxVector
& operator=(const wxVector
& vb
)
299 void push_back(const value_type
& v
)
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
;
306 ::new(place
) value_type(v
);
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
320 const value_type
& at(size_type idx
) const
322 wxASSERT(idx
< m_size
);
323 return m_values
[idx
];
326 value_type
& at(size_type idx
)
328 wxASSERT(idx
< m_size
);
329 return m_values
[idx
];
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); }
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(); }
344 reverse_iterator
rbegin() { return reverse_iterator(end() - 1); }
345 reverse_iterator
rend() { return reverse_iterator(begin() - 1); }
347 iterator
insert(iterator it
, const value_type
& v
= value_type())
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
;
356 // the place where the new element is going to be inserted
357 value_type
* const place
= m_values
+ idx
;
359 // unless we're inserting at the end, move following elements out of
362 Ops::MemmoveForward(place
+ 1, place
, after
);
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
);
371 // use placement new to initialize new object in preallocated place in
372 // m_values and store 'v' in it:
373 ::new(place
) value_type(v
);
375 // now that we did successfully add the new element, increment the size
376 // and disable moving the items back
380 return begin() + idx
;
383 iterator
erase(iterator it
)
385 return erase(it
, it
+ 1);
388 iterator
erase(iterator first
, iterator last
)
392 wxASSERT( first
< end() && last
<= end() );
394 const size_type idx
= first
- begin();
395 const size_type count
= last
- first
;
396 const size_type after
= end() - last
;
398 // erase elements by calling their destructors:
399 for ( iterator i
= first
; i
< last
; ++i
)
402 // once that's done, move following elements over to the freed space:
405 Ops::MemmoveBackward(m_values
+ idx
, m_values
+ idx
+ count
, after
);
410 return begin() + idx
;
413 #if WXWIN_COMPATIBILITY_2_8
414 wxDEPRECATED( size_type
erase(size_type n
) );
415 #endif // WXWIN_COMPATIBILITY_2_8
418 // VC6 can't compile static const int members
419 enum { ALLOC_INITIAL_SIZE
= 16 };
420 enum { ALLOC_MAX_SIZE
= 4096 };
422 void Copy(const wxVector
& vb
)
426 for ( const_iterator i
= vb
.begin(); i
!= vb
.end(); ++i
)
431 void Shrink(size_type n
)
433 for ( size_type i
= n
; i
< m_size
; i
++ )
438 void Extend(size_type n
, const value_type
& v
)
441 for ( size_type i
= m_size
; i
< n
; i
++ )
447 value_type
*m_values
;
450 #if WXWIN_COMPATIBILITY_2_8
452 inline typename wxVector
<T
>::size_type wxVector
<T
>::erase(size_type n
)
457 #endif // WXWIN_COMPATIBILITY_2_8
464 // This is a helper for the wxVectorSort function, and should not be used
465 // directly in user's code.
467 struct wxVectorComparator
470 Compare(const void* pitem1
, const void* pitem2
, const void* )
472 const T
& item1
= *reinterpret_cast<const T
*>(pitem1
);
473 const T
& item2
= *reinterpret_cast<const T
*>(pitem2
);
477 else if (item2
< item1
)
484 } // namespace wxPrivate
489 void wxVectorSort(wxVector
<T
>& v
)
491 wxQsort(v
.begin(), v
.size(), sizeof(T
),
492 wxPrivate::wxVectorComparator
<T
>::Compare
, NULL
);
497 #endif // wxUSE_STD_CONTAINERS/!wxUSE_STD_CONTAINERS
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
505 #endif // _WX_VECTOR_H_