]>
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 ///////////////////////////////////////////////////////////////////////////////
20 #define wxVector std::vector
25 #include "wx/meta/movable.h"
26 #include "wx/meta/if.h"
28 #include "wx/beforestd.h"
29 #include <new> // for placement new
30 #include "wx/afterstd.h"
35 // These templates encapsulate memory operations for use by wxVector; there are
36 // two implementations, both in generic way for any C++ types and as an
37 // optimized version for "movable" types that uses realloc() and memmove().
39 // version for movable types:
41 struct wxVectorMemOpsMovable
43 static void Free(T
* array
)
46 static T
* Realloc(T
* old
, size_t newCapacity
, size_t WXUNUSED(occupiedSize
))
47 { return (T
*)realloc(old
, newCapacity
* sizeof(T
)); }
49 static void MemmoveBackward(T
* dest
, T
* source
, size_t count
)
50 { memmove(dest
, source
, count
* sizeof(T
)); }
52 static void MemmoveForward(T
* dest
, T
* source
, size_t count
)
53 { memmove(dest
, source
, count
* sizeof(T
)); }
56 // generic version for non-movable types:
58 struct wxVectorMemOpsGeneric
60 static void Free(T
* array
)
61 { ::operator delete(array
); }
63 static T
* Realloc(T
* old
, size_t newCapacity
, size_t occupiedSize
)
65 T
*mem
= (T
*)::operator new(newCapacity
* sizeof(T
));
66 for ( size_t i
= 0; i
< occupiedSize
; i
++ )
68 new(mem
+ i
) T(old
[i
]);
71 ::operator delete(old
);
75 static void MemmoveBackward(T
* dest
, T
* source
, size_t count
)
77 wxASSERT( dest
< source
);
79 T
* sourceptr
= source
;
80 for ( size_t i
= count
; i
> 0; --i
, ++destptr
, ++sourceptr
)
82 new(destptr
) T(*sourceptr
);
87 static void MemmoveForward(T
* dest
, T
* source
, size_t count
)
89 wxASSERT( dest
> source
);
90 T
* destptr
= dest
+ count
- 1;
91 T
* sourceptr
= source
+ count
- 1;
92 for ( size_t i
= count
; i
> 0; --i
, --destptr
, --sourceptr
)
94 new(destptr
) T(*sourceptr
);
101 } // namespace wxPrivate
107 // This cryptic expression means "typedef Ops to wxVectorMemOpsMovable if
108 // type T is movable type, otherwise to wxVectorMemOpsGeneric".
110 // Note that we use typedef instead of privately deriving from this (which
111 // would allowed us to omit "Ops::" prefixes below) to keep VC6 happy,
112 // it can't compile code that derives from wxIf<...>::value.
113 typedef typename wxIf
< wxIsMovable
<T
>::value
,
114 wxPrivate::wxVectorMemOpsMovable
<T
>,
115 wxPrivate::wxVectorMemOpsGeneric
<T
> >::value
119 typedef size_t size_type
;
120 typedef T value_type
;
121 typedef value_type
* iterator
;
122 typedef const value_type
* const_iterator
;
123 typedef value_type
& reference
;
125 wxVector() : m_size(0), m_capacity(0), m_values(NULL
) {}
127 wxVector(const wxVector
& c
) : m_size(0), m_capacity(0), m_values(NULL
)
139 // call destructors of stored objects:
140 for ( size_type i
= 0; i
< m_size
; i
++ )
151 void reserve(size_type n
)
153 if ( n
<= m_capacity
)
156 // increase the size twice, unless we're already too big or unless
159 // NB: casts to size_type are needed to suppress mingw32 warnings about
160 // mixing enums and ints in the same expression
161 const size_type increment
= m_size
> 0
162 ? wxMin(m_size
, (size_type
)ALLOC_MAX_SIZE
)
163 : (size_type
)ALLOC_INITIAL_SIZE
;
164 if ( m_capacity
+ increment
> n
)
165 n
= m_capacity
+ increment
;
167 m_values
= Ops::Realloc(m_values
, n
* sizeof(value_type
), m_size
);
171 size_type
size() const
176 size_type
capacity() const
186 wxVector
& operator=(const wxVector
& vb
)
193 void push_back(const value_type
& v
)
197 // use placement new to initialize new object in preallocated place in
198 // m_values and store 'v' in it:
199 void* const place
= m_values
+ m_size
;
200 new(place
) value_type(v
);
202 // only increase m_size if the ctor didn't throw an exception; notice
203 // that if it _did_ throw, everything is OK, because we only increased
204 // vector's capacity so far and possibly written some data to
205 // uninitialized memory at the end of m_values
214 const value_type
& at(size_type idx
) const
216 wxASSERT(idx
< m_size
);
217 return m_values
[idx
];
220 value_type
& at(size_type idx
)
222 wxASSERT(idx
< m_size
);
223 return m_values
[idx
];
226 const value_type
& operator[](size_type idx
) const { return at(idx
); }
227 value_type
& operator[](size_type idx
) { return at(idx
); }
228 const value_type
& front() const { return at(0); }
229 value_type
& front() { return at(0); }
230 const value_type
& back() const { return at(size() - 1); }
231 value_type
& back() { return at(size() - 1); }
233 const_iterator
begin() const { return m_values
; }
234 iterator
begin() { return m_values
; }
235 const_iterator
end() const { return m_values
+ size(); }
236 iterator
end() { return m_values
+ size(); }
238 iterator
insert(iterator it
, const value_type
& v
= value_type())
240 // NB: this must be done before reserve(), because reserve()
241 // invalidates iterators!
242 const size_t idx
= it
- begin();
243 const size_t after
= end() - it
;
247 // unless we're inserting at the end, move following elements out of
251 Ops::MemmoveForward(m_values
+ idx
+ 1, m_values
+ idx
, after
);
258 // use placement new to initialize new object in preallocated place
259 // in m_values and store 'v' in it:
260 void* const place
= m_values
+ idx
;
261 new(place
) value_type(v
);
266 // if the ctor threw an exception, we need to move all the elements
267 // back to their original positions in m_values
270 Ops::MemmoveBackward(m_values
+ idx
, m_values
+ idx
+ 1, after
);
273 throw; // rethrow the exception
275 #endif // wxUSE_EXCEPTIONS
277 // increment m_size only if ctor didn't throw -- if it did, we'll be
278 // left with m_values larger than necessary, but number of elements will
282 return begin() + idx
;
285 iterator
erase(iterator it
)
287 return erase(it
, it
+ 1);
290 iterator
erase(iterator first
, iterator last
)
294 wxASSERT( first
< end() && last
<= end() );
296 const size_type idx
= first
- begin();
297 const size_type count
= last
- first
;
298 const size_type after
= end() - last
;
300 // erase elements by calling their destructors:
301 for ( iterator i
= first
; i
< last
; ++i
)
304 // once that's done, move following elements over to the freed space:
307 Ops::MemmoveBackward(m_values
+ idx
, m_values
+ idx
+ count
, after
);
312 return begin() + idx
;
315 #if WXWIN_COMPATIBILITY_2_8
316 wxDEPRECATED( size_type
erase(size_type n
) );
317 #endif // WXWIN_COMPATIBILITY_2_8
320 // VC6 can't compile static const int members
321 enum { ALLOC_INITIAL_SIZE
= 16 };
322 enum { ALLOC_MAX_SIZE
= 4096 };
324 void Copy(const wxVector
& vb
)
328 for ( const_iterator i
= vb
.begin(); i
!= vb
.end(); ++i
)
335 value_type
*m_values
;
338 #if WXWIN_COMPATIBILITY_2_8
340 inline typename wxVector
<T
>::size_type wxVector
<T
>::erase(size_type n
)
345 #endif // WXWIN_COMPATIBILITY_2_8
347 #endif // wxUSE_STL/!wxUSE_STL
349 #if WXWIN_COMPATIBILITY_2_8
350 #define WX_DECLARE_VECTORBASE(obj, cls) typedef wxVector<obj> cls
351 #define _WX_DECLARE_VECTOR(obj, cls, exp) WX_DECLARE_VECTORBASE(obj, cls)
352 #define WX_DECLARE_VECTOR(obj, cls) WX_DECLARE_VECTORBASE(obj, cls)
353 #endif // WXWIN_COMPATIBILITY_2_8
355 #endif // _WX_VECTOR_H_