]>
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
32 #include "wx/scopeguard.h"
33 #include "wx/meta/movable.h"
34 #include "wx/meta/if.h"
36 #include "wx/beforestd.h"
37 #include <new> // for placement new
38 #include "wx/afterstd.h"
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().
47 // version for movable types:
49 struct wxVectorMemOpsMovable
51 static void Free(T
* array
)
54 static T
* Realloc(T
* old
, size_t newCapacity
, size_t WXUNUSED(occupiedSize
))
55 { return (T
*)realloc(old
, newCapacity
* sizeof(T
)); }
57 static void MemmoveBackward(T
* dest
, T
* source
, size_t count
)
58 { memmove(dest
, source
, count
* sizeof(T
)); }
60 static void MemmoveForward(T
* dest
, T
* source
, size_t count
)
61 { memmove(dest
, source
, count
* sizeof(T
)); }
64 // generic version for non-movable types:
66 struct wxVectorMemOpsGeneric
68 static void Free(T
* array
)
69 { ::operator delete(array
); }
71 static T
* Realloc(T
* old
, size_t newCapacity
, size_t occupiedSize
)
73 T
*mem
= (T
*)::operator new(newCapacity
* sizeof(T
));
74 for ( size_t i
= 0; i
< occupiedSize
; i
++ )
76 ::new(mem
+ i
) T(old
[i
]);
79 ::operator delete(old
);
83 static void MemmoveBackward(T
* dest
, T
* source
, size_t count
)
85 wxASSERT( dest
< source
);
87 T
* sourceptr
= source
;
88 for ( size_t i
= count
; i
> 0; --i
, ++destptr
, ++sourceptr
)
90 ::new(destptr
) T(*sourceptr
);
95 static void MemmoveForward(T
* dest
, T
* source
, size_t count
)
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
)
102 ::new(destptr
) T(*sourceptr
);
109 } // namespace wxPrivate
115 // This cryptic expression means "typedef Ops to wxVectorMemOpsMovable if
116 // type T is movable type, otherwise to wxVectorMemOpsGeneric".
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.
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
),
125 wxPrivate::wxVectorMemOpsMovable
<T
>,
126 wxPrivate::wxVectorMemOpsGeneric
<T
> >::value
130 typedef size_t size_type
;
131 typedef size_t difference_type
;
132 typedef T value_type
;
133 typedef value_type
* pointer
;
134 typedef value_type
* iterator
;
135 typedef const value_type
* const_iterator
;
136 typedef value_type
& reference
;
138 class reverse_iterator
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
) { }
145 reference
operator*() const { return *m_ptr
; }
146 pointer
operator->() const { return m_ptr
; }
148 iterator
base() const { return m_ptr
; }
150 reverse_iterator
& operator++()
151 { --m_ptr
; return *this; }
152 reverse_iterator
operator++(int)
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
; }
159 reverse_iterator
operator+(difference_type n
) const
160 { return reverse_iterator(m_ptr
- n
); }
161 reverse_iterator
& operator+=(difference_type n
)
162 { m_ptr
-= n
; return *this; }
163 reverse_iterator
operator-(difference_type n
) const
164 { return reverse_iterator(m_ptr
+ n
); }
165 reverse_iterator
& operator-=(difference_type n
)
166 { m_ptr
+= n
; return *this; }
168 reference
operator[](difference_type n
) const
169 { return *(*this + n
); }
171 bool operator ==(const reverse_iterator
& it
) const
172 { return m_ptr
== it
.m_ptr
; }
173 bool operator !=(const reverse_iterator
& it
) const
174 { return m_ptr
!= it
.m_ptr
; }
180 wxVector() : m_size(0), m_capacity(0), m_values(NULL
) {}
182 wxVector(size_type p_size
)
183 : m_size(0), m_capacity(0), m_values(NULL
)
186 for ( size_t n
= 0; n
< p_size
; n
++ )
187 push_back(value_type());
190 wxVector(size_type p_size
, const value_type
& v
)
191 : m_size(0), m_capacity(0), m_values(NULL
)
194 for ( size_t n
= 0; n
< p_size
; n
++ )
198 wxVector(const wxVector
& c
) : m_size(0), m_capacity(0), m_values(NULL
)
208 void assign(size_type p_size
, const value_type
& v
)
212 for ( size_t n
= 0; n
< p_size
; n
++ )
216 void swap(wxVector
& v
)
218 wxSwap(m_size
, v
.m_size
);
219 wxSwap(m_capacity
, v
.m_capacity
);
220 wxSwap(m_values
, v
.m_values
);
225 // call destructors of stored objects:
226 for ( size_type i
= 0; i
< m_size
; i
++ )
237 void reserve(size_type n
)
239 if ( n
<= m_capacity
)
242 // increase the size twice, unless we're already too big or unless
245 // NB: casts to size_type are needed to suppress mingw32 warnings about
246 // mixing enums and ints in the same expression
247 const size_type increment
= m_size
> 0
248 ? wxMin(m_size
, (size_type
)ALLOC_MAX_SIZE
)
249 : (size_type
)ALLOC_INITIAL_SIZE
;
250 if ( m_capacity
+ increment
> n
)
251 n
= m_capacity
+ increment
;
253 m_values
= Ops::Realloc(m_values
, n
* sizeof(value_type
), m_size
);
257 void resize(size_type n
)
261 else if ( n
> m_size
)
262 Extend(n
, value_type());
265 void resize(size_type n
, const value_type
& v
)
269 else if ( n
> m_size
)
273 size_type
size() const
278 size_type
capacity() const
288 wxVector
& operator=(const wxVector
& vb
)
298 void push_back(const value_type
& v
)
302 // use placement new to initialize new object in preallocated place in
303 // m_values and store 'v' in it:
304 void* const place
= m_values
+ m_size
;
305 ::new(place
) value_type(v
);
307 // only increase m_size if the ctor didn't throw an exception; notice
308 // that if it _did_ throw, everything is OK, because we only increased
309 // vector's capacity so far and possibly written some data to
310 // uninitialized memory at the end of m_values
319 const value_type
& at(size_type idx
) const
321 wxASSERT(idx
< m_size
);
322 return m_values
[idx
];
325 value_type
& at(size_type idx
)
327 wxASSERT(idx
< m_size
);
328 return m_values
[idx
];
331 const value_type
& operator[](size_type idx
) const { return at(idx
); }
332 value_type
& operator[](size_type idx
) { return at(idx
); }
333 const value_type
& front() const { return at(0); }
334 value_type
& front() { return at(0); }
335 const value_type
& back() const { return at(size() - 1); }
336 value_type
& back() { return at(size() - 1); }
338 const_iterator
begin() const { return m_values
; }
339 iterator
begin() { return m_values
; }
340 const_iterator
end() const { return m_values
+ size(); }
341 iterator
end() { return m_values
+ size(); }
343 reverse_iterator
rbegin() { return reverse_iterator(end() - 1); }
344 reverse_iterator
rend() { return reverse_iterator(begin() - 1); }
346 iterator
insert(iterator it
, const value_type
& v
= value_type())
348 // NB: this must be done before reserve(), because reserve()
349 // invalidates iterators!
350 const size_t idx
= it
- begin();
351 const size_t after
= end() - it
;
355 // the place where the new element is going to be inserted
356 value_type
* const place
= m_values
+ idx
;
358 // unless we're inserting at the end, move following elements out of
361 Ops::MemmoveForward(place
+ 1, place
, after
);
363 // if the ctor called below throws an exception, we need to move all
364 // the elements back to their original positions in m_values
365 wxScopeGuard moveBack
= wxMakeGuard(
366 Ops::MemmoveBackward
, place
, place
+ 1, after
);
370 // use placement new to initialize new object in preallocated place in
371 // m_values and store 'v' in it:
372 ::new(place
) value_type(v
);
374 // now that we did successfully add the new element, increment the size
375 // and disable moving the items back
379 return begin() + idx
;
382 iterator
erase(iterator it
)
384 return erase(it
, it
+ 1);
387 iterator
erase(iterator first
, iterator last
)
391 wxASSERT( first
< end() && last
<= end() );
393 const size_type idx
= first
- begin();
394 const size_type count
= last
- first
;
395 const size_type after
= end() - last
;
397 // erase elements by calling their destructors:
398 for ( iterator i
= first
; i
< last
; ++i
)
401 // once that's done, move following elements over to the freed space:
404 Ops::MemmoveBackward(m_values
+ idx
, m_values
+ idx
+ count
, after
);
409 return begin() + idx
;
412 #if WXWIN_COMPATIBILITY_2_8
413 wxDEPRECATED( size_type
erase(size_type n
) );
414 #endif // WXWIN_COMPATIBILITY_2_8
417 // VC6 can't compile static const int members
418 enum { ALLOC_INITIAL_SIZE
= 16 };
419 enum { ALLOC_MAX_SIZE
= 4096 };
421 void Copy(const wxVector
& vb
)
425 for ( const_iterator i
= vb
.begin(); i
!= vb
.end(); ++i
)
430 void Shrink(size_type n
)
432 for ( size_type i
= n
; i
< m_size
; i
++ )
437 void Extend(size_type n
, const value_type
& v
)
440 for ( size_type i
= m_size
; i
< n
; i
++ )
446 value_type
*m_values
;
449 #if WXWIN_COMPATIBILITY_2_8
451 inline typename wxVector
<T
>::size_type wxVector
<T
>::erase(size_type n
)
456 #endif // WXWIN_COMPATIBILITY_2_8
463 // This is a helper for the wxVectorSort function, and should not be used
464 // directly in user's code.
466 struct wxVectorComparator
469 Compare(const void* pitem1
, const void* pitem2
, const void* )
471 const T
& item1
= *reinterpret_cast<const T
*>(pitem1
);
472 const T
& item2
= *reinterpret_cast<const T
*>(pitem2
);
476 else if (item2
< item1
)
483 } // namespace wxPrivate
488 void wxVectorSort(wxVector
<T
>& v
)
490 wxQsort(v
.begin(), v
.size(), sizeof(T
),
491 wxPrivate::wxVectorComparator
<T
>::Compare
, NULL
);
496 #endif // wxUSE_STD_CONTAINERS/!wxUSE_STD_CONTAINERS
498 #if WXWIN_COMPATIBILITY_2_8
499 #define WX_DECLARE_VECTORBASE(obj, cls) typedef wxVector<obj> cls
500 #define _WX_DECLARE_VECTOR(obj, cls, exp) WX_DECLARE_VECTORBASE(obj, cls)
501 #define WX_DECLARE_VECTOR(obj, cls) WX_DECLARE_VECTORBASE(obj, cls)
502 #endif // WXWIN_COMPATIBILITY_2_8
504 #endif // _WX_VECTOR_H_