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"
39 // wxQsort is declared in wx/utils.h, but can't include that file here,
40 // it indirectly includes this file. Just lovely...
41 typedef int (*wxSortCallback
)(const void* pItem1
,
43 const void* user_data
);
44 WXDLLIMPEXP_BASE
void wxQsort(void* pbase
, size_t total_elems
,
45 size_t size
, wxSortCallback cmp
,
46 const void* user_data
);
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().
55 // version for movable types:
57 struct wxVectorMemOpsMovable
59 static void Free(T
* array
)
62 static T
* Realloc(T
* old
, size_t newCapacity
, size_t WXUNUSED(occupiedSize
))
63 { return (T
*)realloc(old
, newCapacity
* sizeof(T
)); }
65 static void MemmoveBackward(T
* dest
, T
* source
, size_t count
)
66 { memmove(dest
, source
, count
* sizeof(T
)); }
68 static void MemmoveForward(T
* dest
, T
* source
, size_t count
)
69 { memmove(dest
, source
, count
* sizeof(T
)); }
72 // generic version for non-movable types:
74 struct wxVectorMemOpsGeneric
76 static void Free(T
* array
)
77 { ::operator delete(array
); }
79 static T
* Realloc(T
* old
, size_t newCapacity
, size_t occupiedSize
)
81 T
*mem
= (T
*)::operator new(newCapacity
* sizeof(T
));
82 for ( size_t i
= 0; i
< occupiedSize
; i
++ )
84 ::new(mem
+ i
) T(old
[i
]);
87 ::operator delete(old
);
91 static void MemmoveBackward(T
* dest
, T
* source
, size_t count
)
93 wxASSERT( dest
< source
);
95 T
* sourceptr
= source
;
96 for ( size_t i
= count
; i
> 0; --i
, ++destptr
, ++sourceptr
)
98 ::new(destptr
) T(*sourceptr
);
103 static void MemmoveForward(T
* dest
, T
* source
, size_t count
)
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
)
110 ::new(destptr
) T(*sourceptr
);
117 } // namespace wxPrivate
123 // This cryptic expression means "typedef Ops to wxVectorMemOpsMovable if
124 // type T is movable type, otherwise to wxVectorMemOpsGeneric".
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.
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
),
133 wxPrivate::wxVectorMemOpsMovable
<T
>,
134 wxPrivate::wxVectorMemOpsGeneric
<T
> >::value
138 typedef size_t size_type
;
139 typedef size_t difference_type
;
140 typedef T value_type
;
141 typedef value_type
* pointer
;
142 typedef const value_type
* const_pointer
;
143 typedef value_type
* iterator
;
144 typedef const value_type
* const_iterator
;
145 typedef value_type
& reference
;
146 typedef const value_type
& const_reference
;
148 class reverse_iterator
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
) { }
155 reference
operator*() const { return *m_ptr
; }
156 pointer
operator->() const { return m_ptr
; }
158 iterator
base() const { return m_ptr
; }
160 reverse_iterator
& operator++()
161 { --m_ptr
; return *this; }
162 reverse_iterator
operator++(int)
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
; }
169 reverse_iterator
operator+(difference_type n
) const
170 { return reverse_iterator(m_ptr
- n
); }
171 reverse_iterator
& operator+=(difference_type n
)
172 { m_ptr
-= n
; return *this; }
173 reverse_iterator
operator-(difference_type n
) const
174 { return reverse_iterator(m_ptr
+ n
); }
175 reverse_iterator
& operator-=(difference_type n
)
176 { m_ptr
+= n
; return *this; }
178 reference
operator[](difference_type n
) const
179 { return *(*this + n
); }
181 bool operator ==(const reverse_iterator
& it
) const
182 { return m_ptr
== it
.m_ptr
; }
183 bool operator !=(const reverse_iterator
& it
) const
184 { return m_ptr
!= it
.m_ptr
; }
189 friend class const_reverse_iterator
;
192 class const_reverse_iterator
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
) { }
200 const_reference
operator*() const { return *m_ptr
; }
201 const_pointer
operator->() const { return m_ptr
; }
203 const_iterator
base() const { return m_ptr
; }
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
; }
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; }
223 const_reference
operator[](difference_type n
) const
224 { return *(*this + n
); }
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
; }
232 const value_type
*m_ptr
;
235 wxVector() : m_size(0), m_capacity(0), m_values(NULL
) {}
237 wxVector(size_type p_size
)
238 : m_size(0), m_capacity(0), m_values(NULL
)
241 for ( size_t n
= 0; n
< p_size
; n
++ )
242 push_back(value_type());
245 wxVector(size_type p_size
, const value_type
& v
)
246 : m_size(0), m_capacity(0), m_values(NULL
)
249 for ( size_t n
= 0; n
< p_size
; n
++ )
253 wxVector(const wxVector
& c
) : m_size(0), m_capacity(0), m_values(NULL
)
258 template <class InputIterator
>
259 wxVector(InputIterator first
, InputIterator last
)
260 : m_size(0), m_capacity(0), m_values(NULL
)
270 void assign(size_type p_size
, const value_type
& v
)
274 for ( size_t n
= 0; n
< p_size
; n
++ )
278 template <class InputIterator
>
279 void assign(InputIterator first
, InputIterator last
)
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.
287 for ( InputIterator it
= first
; it
!= last
; ++it
)
291 void swap(wxVector
& v
)
293 wxSwap(m_size
, v
.m_size
);
294 wxSwap(m_capacity
, v
.m_capacity
);
295 wxSwap(m_values
, v
.m_values
);
300 // call destructors of stored objects:
301 for ( size_type i
= 0; i
< m_size
; i
++ )
312 void reserve(size_type n
)
314 if ( n
<= m_capacity
)
317 // increase the size twice, unless we're already too big or unless
320 // NB: casts to size_type are needed to suppress warnings about
321 // mixing enumeral and non-enumeral type in conditional expression
322 const size_type increment
= m_size
> 0
323 ? m_size
< ALLOC_MAX_SIZE
325 : (size_type
)ALLOC_MAX_SIZE
326 : (size_type
)ALLOC_INITIAL_SIZE
;
327 if ( m_capacity
+ increment
> n
)
328 n
= m_capacity
+ increment
;
330 m_values
= Ops::Realloc(m_values
, n
* sizeof(value_type
), m_size
);
334 void resize(size_type n
)
338 else if ( n
> m_size
)
339 Extend(n
, value_type());
342 void resize(size_type n
, const value_type
& v
)
346 else if ( n
> m_size
)
350 size_type
size() const
355 size_type
capacity() const
365 wxVector
& operator=(const wxVector
& vb
)
375 void push_back(const value_type
& v
)
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
;
382 ::new(place
) value_type(v
);
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
396 const value_type
& at(size_type idx
) const
398 wxASSERT(idx
< m_size
);
399 return m_values
[idx
];
402 value_type
& at(size_type idx
)
404 wxASSERT(idx
< m_size
);
405 return m_values
[idx
];
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); }
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(); }
420 reverse_iterator
rbegin() { return reverse_iterator(end() - 1); }
421 reverse_iterator
rend() { return reverse_iterator(begin() - 1); }
423 const_reverse_iterator
rbegin() const { return const_reverse_iterator(end() - 1); }
424 const_reverse_iterator
rend() const { return const_reverse_iterator(begin() - 1); }
426 iterator
insert(iterator it
, const value_type
& v
= value_type())
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
;
435 // the place where the new element is going to be inserted
436 value_type
* const place
= m_values
+ idx
;
438 // unless we're inserting at the end, move following elements out of
441 Ops::MemmoveForward(place
+ 1, place
, after
);
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
);
450 // use placement new to initialize new object in preallocated place in
451 // m_values and store 'v' in it:
452 ::new(place
) value_type(v
);
454 // now that we did successfully add the new element, increment the size
455 // and disable moving the items back
459 return begin() + idx
;
462 iterator
erase(iterator it
)
464 return erase(it
, it
+ 1);
467 iterator
erase(iterator first
, iterator last
)
471 wxASSERT( first
< end() && last
<= end() );
473 const size_type idx
= first
- begin();
474 const size_type count
= last
- first
;
475 const size_type after
= end() - last
;
477 // erase elements by calling their destructors:
478 for ( iterator i
= first
; i
< last
; ++i
)
481 // once that's done, move following elements over to the freed space:
484 Ops::MemmoveBackward(m_values
+ idx
, m_values
+ idx
+ count
, after
);
489 return begin() + idx
;
492 #if WXWIN_COMPATIBILITY_2_8
493 wxDEPRECATED( size_type
erase(size_type n
) );
494 #endif // WXWIN_COMPATIBILITY_2_8
497 // VC6 can't compile static const int members
498 enum { ALLOC_INITIAL_SIZE
= 16 };
499 enum { ALLOC_MAX_SIZE
= 4096 };
501 void Copy(const wxVector
& vb
)
505 for ( const_iterator i
= vb
.begin(); i
!= vb
.end(); ++i
)
510 void Shrink(size_type n
)
512 for ( size_type i
= n
; i
< m_size
; i
++ )
517 void Extend(size_type n
, const value_type
& v
)
520 for ( size_type i
= m_size
; i
< n
; i
++ )
526 value_type
*m_values
;
529 #if WXWIN_COMPATIBILITY_2_8
531 inline typename wxVector
<T
>::size_type wxVector
<T
>::erase(size_type n
)
536 #endif // WXWIN_COMPATIBILITY_2_8
543 // This is a helper for the wxVectorSort function, and should not be used
544 // directly in user's code.
546 struct wxVectorComparator
549 Compare(const void* pitem1
, const void* pitem2
, const void* )
551 const T
& item1
= *reinterpret_cast<const T
*>(pitem1
);
552 const T
& item2
= *reinterpret_cast<const T
*>(pitem2
);
556 else if (item2
< item1
)
563 } // namespace wxPrivate
568 void wxVectorSort(wxVector
<T
>& v
)
570 wxQsort(v
.begin(), v
.size(), sizeof(T
),
571 wxPrivate::wxVectorComparator
<T
>::Compare
, NULL
);
576 #endif // wxUSE_STD_CONTAINERS/!wxUSE_STD_CONTAINERS
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
584 #endif // _WX_VECTOR_H_