]>
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 ///////////////////////////////////////////////////////////////////////////////
22 #define wxVector std::vector
24 inline void wxVectorSort(wxVector
<T
>& v
)
26 std::sort(v
.begin(), v
.end());
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.
121 typedef typename wxIf
< wxIsMovable
<T
>::value
,
122 wxPrivate::wxVectorMemOpsMovable
<T
>,
123 wxPrivate::wxVectorMemOpsGeneric
<T
> >::value
127 typedef size_t size_type
;
128 typedef size_t difference_type
;
129 typedef T value_type
;
130 typedef value_type
* pointer
;
131 typedef value_type
* iterator
;
132 typedef const value_type
* const_iterator
;
133 typedef value_type
& reference
;
135 class reverse_iterator
138 reverse_iterator() : m_ptr(NULL
) { }
139 wxEXPLICIT
reverse_iterator(iterator it
) : m_ptr(it
) { }
140 reverse_iterator(const reverse_iterator
& it
) : m_ptr(it
.m_ptr
) { }
142 reference
operator*() const { return *m_ptr
; }
143 pointer
operator->() const { return m_ptr
; }
145 iterator
base() const { return m_ptr
; }
147 reverse_iterator
& operator++()
148 { --m_ptr
; return *this; }
149 reverse_iterator
operator++(int)
150 { reverse_iterator tmp
= *this; --m_ptr
; return tmp
; }
151 reverse_iterator
& operator--()
152 { ++m_ptr
; return *this; }
153 reverse_iterator
operator--(int)
154 { reverse_iterator tmp
= *this; ++m_ptr
; return tmp
; }
156 reverse_iterator
operator+(difference_type n
) const
157 { return reverse_iterator(m_ptr
- n
); }
158 reverse_iterator
& operator+=(difference_type n
)
159 { m_ptr
-= n
; return *this; }
160 reverse_iterator
operator-(difference_type n
) const
161 { return reverse_iterator(m_ptr
+ n
); }
162 reverse_iterator
& operator-=(difference_type n
)
163 { m_ptr
+= n
; return *this; }
165 reference
operator[](difference_type n
) const
166 { return *(*this + n
); }
168 bool operator ==(const reverse_iterator
& it
) const
169 { return m_ptr
== it
.m_ptr
; }
170 bool operator !=(const reverse_iterator
& it
) const
171 { return m_ptr
!= it
.m_ptr
; }
177 wxVector() : m_size(0), m_capacity(0), m_values(NULL
) {}
179 wxVector(size_type p_size
)
180 : m_size(0), m_capacity(0), m_values(NULL
)
183 for ( size_t n
= 0; n
< p_size
; n
++ )
184 push_back(value_type());
187 wxVector(size_type p_size
, const value_type
& v
)
188 : m_size(0), m_capacity(0), m_values(NULL
)
191 for ( size_t n
= 0; n
< p_size
; n
++ )
195 wxVector(const wxVector
& c
) : m_size(0), m_capacity(0), m_values(NULL
)
205 void swap(wxVector
& v
)
207 wxSwap(m_size
, v
.m_size
);
208 wxSwap(m_capacity
, v
.m_capacity
);
209 wxSwap(m_values
, v
.m_values
);
214 // call destructors of stored objects:
215 for ( size_type i
= 0; i
< m_size
; i
++ )
226 void reserve(size_type n
)
228 if ( n
<= m_capacity
)
231 // increase the size twice, unless we're already too big or unless
234 // NB: casts to size_type are needed to suppress mingw32 warnings about
235 // mixing enums and ints in the same expression
236 const size_type increment
= m_size
> 0
237 ? wxMin(m_size
, (size_type
)ALLOC_MAX_SIZE
)
238 : (size_type
)ALLOC_INITIAL_SIZE
;
239 if ( m_capacity
+ increment
> n
)
240 n
= m_capacity
+ increment
;
242 m_values
= Ops::Realloc(m_values
, n
* sizeof(value_type
), m_size
);
246 void resize(size_type n
)
250 else if ( n
> m_size
)
251 Extend(n
, value_type());
254 void resize(size_type n
, const value_type
& v
)
258 else if ( n
> m_size
)
262 size_type
size() const
267 size_type
capacity() const
277 wxVector
& operator=(const wxVector
& vb
)
287 void push_back(const value_type
& v
)
291 // use placement new to initialize new object in preallocated place in
292 // m_values and store 'v' in it:
293 void* const place
= m_values
+ m_size
;
294 ::new(place
) value_type(v
);
296 // only increase m_size if the ctor didn't throw an exception; notice
297 // that if it _did_ throw, everything is OK, because we only increased
298 // vector's capacity so far and possibly written some data to
299 // uninitialized memory at the end of m_values
308 const value_type
& at(size_type idx
) const
310 wxASSERT(idx
< m_size
);
311 return m_values
[idx
];
314 value_type
& at(size_type idx
)
316 wxASSERT(idx
< m_size
);
317 return m_values
[idx
];
320 const value_type
& operator[](size_type idx
) const { return at(idx
); }
321 value_type
& operator[](size_type idx
) { return at(idx
); }
322 const value_type
& front() const { return at(0); }
323 value_type
& front() { return at(0); }
324 const value_type
& back() const { return at(size() - 1); }
325 value_type
& back() { return at(size() - 1); }
327 const_iterator
begin() const { return m_values
; }
328 iterator
begin() { return m_values
; }
329 const_iterator
end() const { return m_values
+ size(); }
330 iterator
end() { return m_values
+ size(); }
332 reverse_iterator
rbegin() { return reverse_iterator(end() - 1); }
333 reverse_iterator
rend() { return reverse_iterator(begin() - 1); }
335 iterator
insert(iterator it
, const value_type
& v
= value_type())
337 // NB: this must be done before reserve(), because reserve()
338 // invalidates iterators!
339 const size_t idx
= it
- begin();
340 const size_t after
= end() - it
;
344 // the place where the new element is going to be inserted
345 value_type
* const place
= m_values
+ idx
;
347 // unless we're inserting at the end, move following elements out of
350 Ops::MemmoveForward(place
+ 1, place
, after
);
352 // if the ctor called below throws an exception, we need to move all
353 // the elements back to their original positions in m_values
354 wxScopeGuard moveBack
= wxMakeGuard(
355 Ops::MemmoveBackward
, place
, place
+ 1, after
);
359 // use placement new to initialize new object in preallocated place in
360 // m_values and store 'v' in it:
361 ::new(place
) value_type(v
);
363 // now that we did successfully add the new element, increment the size
364 // and disable moving the items back
368 return begin() + idx
;
371 iterator
erase(iterator it
)
373 return erase(it
, it
+ 1);
376 iterator
erase(iterator first
, iterator last
)
380 wxASSERT( first
< end() && last
<= end() );
382 const size_type idx
= first
- begin();
383 const size_type count
= last
- first
;
384 const size_type after
= end() - last
;
386 // erase elements by calling their destructors:
387 for ( iterator i
= first
; i
< last
; ++i
)
390 // once that's done, move following elements over to the freed space:
393 Ops::MemmoveBackward(m_values
+ idx
, m_values
+ idx
+ count
, after
);
398 return begin() + idx
;
401 #if WXWIN_COMPATIBILITY_2_8
402 wxDEPRECATED( size_type
erase(size_type n
) );
403 #endif // WXWIN_COMPATIBILITY_2_8
406 // VC6 can't compile static const int members
407 enum { ALLOC_INITIAL_SIZE
= 16 };
408 enum { ALLOC_MAX_SIZE
= 4096 };
410 void Copy(const wxVector
& vb
)
414 for ( const_iterator i
= vb
.begin(); i
!= vb
.end(); ++i
)
419 void Shrink(size_type n
)
421 for ( size_type i
= n
; i
< m_size
; i
++ )
426 void Extend(size_type n
, const value_type
& v
)
429 for ( size_type i
= m_size
; i
< n
; i
++ )
435 value_type
*m_values
;
438 #if WXWIN_COMPATIBILITY_2_8
440 inline typename wxVector
<T
>::size_type wxVector
<T
>::erase(size_type n
)
445 #endif // WXWIN_COMPATIBILITY_2_8
452 // This is a helper for the wxVectorSort function, and should not be used
453 // directly in user's code.
457 static int wxCMPFUNC_CONV
458 Compare(const void* pitem1
, const void* pitem2
, const void* )
460 const T
& item1
= *reinterpret_cast<const T
*>(pitem1
);
461 const T
& item2
= *reinterpret_cast<const T
*>(pitem2
);
465 else if (item2
< item1
)
472 } // namespace wxPrivate
477 void wxVectorSort(wxVector
<T
>& v
)
479 wxQsort(v
.begin(), v
.size(), sizeof(T
),
480 wxPrivate::wxVectorSort
<T
>::Compare
, NULL
);
485 #endif // wxUSE_STL/!wxUSE_STL
487 #if WXWIN_COMPATIBILITY_2_8
488 #define WX_DECLARE_VECTORBASE(obj, cls) typedef wxVector<obj> cls
489 #define _WX_DECLARE_VECTOR(obj, cls, exp) WX_DECLARE_VECTORBASE(obj, cls)
490 #define WX_DECLARE_VECTOR(obj, cls) WX_DECLARE_VECTORBASE(obj, cls)
491 #endif // WXWIN_COMPATIBILITY_2_8
493 #endif // _WX_VECTOR_H_