]>
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 ///////////////////////////////////////////////////////////////////////////////
16 #include "wx/dataobj.h"
21 #define wxVector std::vector
26 #include "wx/scopeguard.h"
27 #include "wx/meta/movable.h"
28 #include "wx/meta/if.h"
30 #include "wx/beforestd.h"
31 #include <new> // for placement new
32 #include "wx/afterstd.h"
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().
41 // version for movable types:
43 struct wxVectorMemOpsMovable
45 static void Free(T
* array
)
48 static T
* Realloc(T
* old
, size_t newCapacity
, size_t WXUNUSED(occupiedSize
))
49 { return (T
*)realloc(old
, newCapacity
* sizeof(T
)); }
51 static void MemmoveBackward(T
* dest
, T
* source
, size_t count
)
52 { memmove(dest
, source
, count
* sizeof(T
)); }
54 static void MemmoveForward(T
* dest
, T
* source
, size_t count
)
55 { memmove(dest
, source
, count
* sizeof(T
)); }
58 // generic version for non-movable types:
60 struct wxVectorMemOpsGeneric
62 static void Free(T
* array
)
63 { ::operator delete(array
); }
65 static T
* Realloc(T
* old
, size_t newCapacity
, size_t occupiedSize
)
67 T
*mem
= (T
*)::operator new(newCapacity
* sizeof(T
));
68 for ( size_t i
= 0; i
< occupiedSize
; i
++ )
70 ::new(mem
+ i
) T(old
[i
]);
73 ::operator delete(old
);
77 static void MemmoveBackward(T
* dest
, T
* source
, size_t count
)
79 wxASSERT( dest
< source
);
81 T
* sourceptr
= source
;
82 for ( size_t i
= count
; i
> 0; --i
, ++destptr
, ++sourceptr
)
84 ::new(destptr
) T(*sourceptr
);
89 static void MemmoveForward(T
* dest
, T
* source
, size_t count
)
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
)
96 ::new(destptr
) T(*sourceptr
);
103 } // namespace wxPrivate
109 // This cryptic expression means "typedef Ops to wxVectorMemOpsMovable if
110 // type T is movable type, otherwise to wxVectorMemOpsGeneric".
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
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
;
129 class reverse_iterator
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
) { }
136 reference
operator*() const { return *m_ptr
; }
137 pointer
operator->() const { return m_ptr
; }
139 iterator
base() const { return m_ptr
; }
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
; }
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; }
159 reference
operator[](difference_type n
) const
160 { return *(*this + n
); }
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
; }
171 wxVector() : m_size(0), m_capacity(0), m_values(NULL
) {}
173 wxVector(size_type size
)
174 : m_size(0), m_capacity(0), m_values(NULL
)
177 for ( size_t n
= 0; n
< size
; n
++ )
178 push_back(value_type());
181 wxVector(size_type size
, const value_type
& v
)
182 : m_size(0), m_capacity(0), m_values(NULL
)
185 for ( size_t n
= 0; n
< size
; n
++ )
189 wxVector(const wxVector
& c
) : m_size(0), m_capacity(0), m_values(NULL
)
201 // call destructors of stored objects:
202 for ( size_type i
= 0; i
< m_size
; i
++ )
213 void reserve(size_type n
)
215 if ( n
<= m_capacity
)
218 // increase the size twice, unless we're already too big or unless
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
;
229 m_values
= Ops::Realloc(m_values
, n
* sizeof(value_type
), m_size
);
233 void resize(size_type n
)
237 else if ( n
> m_size
)
238 Extend(n
, value_type());
241 void resize(size_type n
, const value_type
& v
)
245 else if ( n
> m_size
)
249 size_type
size() const
254 size_type
capacity() const
264 wxVector
& operator=(const wxVector
& vb
)
274 void push_back(const value_type
& v
)
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
);
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
295 const value_type
& at(size_type idx
) const
297 wxASSERT(idx
< m_size
);
298 return m_values
[idx
];
301 value_type
& at(size_type idx
)
303 wxASSERT(idx
< m_size
);
304 return m_values
[idx
];
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); }
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(); }
319 reverse_iterator
rbegin() { return reverse_iterator(end() - 1); }
320 reverse_iterator
rend() { return reverse_iterator(begin() - 1); }
322 iterator
insert(iterator it
, const value_type
& v
= value_type())
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
;
331 // the place where the new element is going to be inserted
332 value_type
* const place
= m_values
+ idx
;
334 // unless we're inserting at the end, move following elements out of
337 Ops::MemmoveForward(place
+ 1, place
, after
);
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
);
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
);
350 // now that we did successfully add the new element, increment the size
351 // and disable moving the items back
355 return begin() + idx
;
358 iterator
erase(iterator it
)
360 return erase(it
, it
+ 1);
363 iterator
erase(iterator first
, iterator last
)
367 wxASSERT( first
< end() && last
<= end() );
369 const size_type idx
= first
- begin();
370 const size_type count
= last
- first
;
371 const size_type after
= end() - last
;
373 // erase elements by calling their destructors:
374 for ( iterator i
= first
; i
< last
; ++i
)
377 // once that's done, move following elements over to the freed space:
380 Ops::MemmoveBackward(m_values
+ idx
, m_values
+ idx
+ count
, after
);
385 return begin() + idx
;
388 #if WXWIN_COMPATIBILITY_2_8
389 wxDEPRECATED( size_type
erase(size_type n
) );
390 #endif // WXWIN_COMPATIBILITY_2_8
393 // VC6 can't compile static const int members
394 enum { ALLOC_INITIAL_SIZE
= 16 };
395 enum { ALLOC_MAX_SIZE
= 4096 };
397 void Copy(const wxVector
& vb
)
401 for ( const_iterator i
= vb
.begin(); i
!= vb
.end(); ++i
)
406 void Shrink(size_type n
)
408 for ( size_type i
= n
; i
< m_size
; i
++ )
413 void Extend(size_type n
, const value_type
& v
)
416 for ( size_type i
= m_size
; i
< n
; i
++ )
422 value_type
*m_values
;
425 #if WXWIN_COMPATIBILITY_2_8
427 inline typename wxVector
<T
>::size_type wxVector
<T
>::erase(size_type n
)
432 #endif // WXWIN_COMPATIBILITY_2_8
434 #endif // wxUSE_STL/!wxUSE_STL
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
442 #endif // _WX_VECTOR_H_