]>
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
26 #include "wx/beforestd.h"
27 #include <new> // for placement new
28 #include "wx/afterstd.h"
34 typedef size_t size_type
;
36 typedef value_type
* iterator
;
37 typedef const value_type
* const_iterator
;
38 typedef value_type
& reference
;
40 wxVector() : m_size(0), m_capacity(0), m_values(NULL
) {}
42 wxVector(const wxVector
& c
)
54 // call destructors of stored objects:
55 for ( size_type i
= 0; i
< m_size
; i
++ )
62 m_size
= m_capacity
= 0;
65 void reserve(size_type n
)
67 if ( n
<= m_capacity
)
70 // increase the size twice, unless we're already too big or unless
73 // NB: casts to size_type are needed to suppress mingw32 warnings about
74 // mixing enums and ints in the same expression
75 const size_type increment
= m_size
> 0
76 ? wxMin(m_size
, (size_type
)ALLOC_MAX_SIZE
)
77 : (size_type
)ALLOC_INITIAL_SIZE
;
78 if ( m_capacity
+ increment
> n
)
79 n
= m_capacity
+ increment
;
81 m_values
= (value_type
*)realloc(m_values
, n
* sizeof(value_type
));
86 size_type
size() const
91 size_type
capacity() const
101 wxVector
& operator=(const wxVector
& vb
)
107 void push_back(const value_type
& v
)
111 // use placement new to initialize new object in preallocated place in
112 // m_values and store 'v' in it:
113 void* const place
= m_values
+ m_size
;
114 new(place
) value_type(v
);
116 // only increase m_size if the ctor didn't throw an exception; notice
117 // that if it _did_ throw, everything is OK, because we only increased
118 // vector's capacity so far and possibly written some data to
119 // uninitialized memory at the end of m_values
128 const value_type
& at(size_type idx
) const
130 wxASSERT(idx
< m_size
);
131 return m_values
[idx
];
134 value_type
& at(size_type idx
)
136 wxASSERT(idx
< m_size
);
137 return m_values
[idx
];
140 const value_type
& operator[](size_type idx
) const { return at(idx
); }
141 value_type
& operator[](size_type idx
) { return at(idx
); }
142 const value_type
& front() const { return at(0); }
143 value_type
& front() { return at(0); }
144 const value_type
& back() const { return at(size() - 1); }
145 value_type
& back() { return at(size() - 1); }
147 const_iterator
begin() const { return m_values
; }
148 iterator
begin() { return m_values
; }
149 const_iterator
end() const { return m_values
+ size(); }
150 iterator
end() { return m_values
+ size(); }
152 iterator
insert(iterator it
, const value_type
& v
= value_type())
154 // NB: this must be done before reserve(), because reserve()
155 // invalidates iterators!
156 const size_t idx
= it
- begin();
157 const size_t after
= end() - it
;
161 // unless we're inserting at the end, move following elements out of
165 memmove(m_values
+ idx
+ 1,
167 after
* sizeof(value_type
));
174 // use placement new to initialize new object in preallocated place
175 // in m_values and store 'v' in it:
176 void* const place
= m_values
+ idx
;
177 new(place
) value_type(v
);
182 // if the ctor threw an exception, we need to move all the elements
183 // back to their original positions in m_values
186 memmove(m_values
+ idx
,
188 after
* sizeof(value_type
));
191 throw; // rethrow the exception
193 #endif // wxUSE_EXCEPTIONS
195 // increment m_size only if ctor didn't throw -- if it did, we'll be
196 // left with m_values larger than necessary, but number of elements will
200 return begin() + idx
;
203 iterator
erase(iterator it
)
205 return erase(it
, it
+ 1);
208 iterator
erase(iterator first
, iterator last
)
212 wxASSERT( first
< end() && last
<= end() );
214 const size_type idx
= first
- begin();
215 const size_type count
= last
- first
;
216 const size_type after
= end() - last
;
218 // erase elements by calling their destructors:
219 for ( iterator i
= first
; i
< last
; ++i
)
222 // once that's done, move following elements over to the freed space:
225 memmove(m_values
+ idx
,
226 m_values
+ idx
+ count
,
227 after
* sizeof(value_type
));
232 return begin() + idx
;
235 #if WXWIN_COMPATIBILITY_2_8
236 wxDEPRECATED( size_type
erase(size_type n
) );
237 #endif // WXWIN_COMPATIBILITY_2_8
240 // VC6 can't compile static const int members
241 enum { ALLOC_INITIAL_SIZE
= 16 };
242 enum { ALLOC_MAX_SIZE
= 4096 };
244 void Copy(const wxVector
& vb
)
249 for ( const_iterator i
= vb
.begin(); i
!= vb
.end(); ++i
)
256 value_type
*m_values
;
259 #if WXWIN_COMPATIBILITY_2_8
261 inline typename wxVector
<T
>::size_type wxVector
<T
>::erase(size_type n
)
266 #endif // WXWIN_COMPATIBILITY_2_8
268 #endif // wxUSE_STL/!wxUSE_STL
270 #if WXWIN_COMPATIBILITY_2_8
271 #define WX_DECLARE_VECTORBASE(obj, cls) typedef wxVector<obj> cls
272 #define _WX_DECLARE_VECTOR(obj, cls, exp) WX_DECLARE_VECTORBASE(obj, cls)
273 #define WX_DECLARE_VECTOR(obj, cls) WX_DECLARE_VECTORBASE(obj, cls)
274 #endif // WXWIN_COMPATIBILITY_2_8
276 #endif // _WX_VECTOR_H_