]>
git.saurik.com Git - wxWidgets.git/blob - include/wx/vector.h
9511ab2ab362b722fb003c62cd5d76859945bd7c
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
24 #include "wx/beforestd.h"
25 #include <new> // for placement new
26 #include "wx/afterstd.h"
32 typedef size_t size_type
;
34 typedef value_type
* iterator
;
35 typedef const value_type
* const_iterator
;
36 typedef value_type
& reference
;
38 wxVector() : m_size(0), m_capacity(0), m_values(NULL
) {}
40 wxVector(const wxVector
& c
)
52 // call destructors of stored objects:
53 for ( size_type i
= 0; i
< m_size
; i
++ )
60 m_size
= m_capacity
= 0;
63 void reserve(size_type n
)
65 if ( n
<= m_capacity
)
68 // increase the size twice, unless we're already too big or unless
71 // NB: casts to size_type are needed to suppress mingw32 warnings about
72 // mixing enums and ints in the same expression
73 const size_type increment
= m_size
> 0
74 ? wxMin(m_size
, (size_type
)ALLOC_MAX_SIZE
)
75 : (size_type
)ALLOC_INITIAL_SIZE
;
76 if ( m_capacity
+ increment
> n
)
77 n
= m_capacity
+ increment
;
79 m_values
= (value_type
*)realloc(m_values
, n
* sizeof(value_type
));
84 size_type
size() const
89 size_type
capacity() const
99 wxVector
& operator=(const wxVector
& vb
)
105 void push_back(const value_type
& v
)
109 // use placement new to initialize new object in preallocated place in
110 // m_values and store 'v' in it:
111 void* const place
= m_values
+ m_size
;
112 new(place
) value_type(v
);
114 // only increase m_size if the ctor didn't throw an exception; notice
115 // that if it _did_ throw, everything is OK, because we only increased
116 // vector's capacity so far and possibly written some data to
117 // uninitialized memory at the end of m_values
126 const value_type
& at(size_type idx
) const
128 wxASSERT(idx
< m_size
);
129 return m_values
[idx
];
132 value_type
& at(size_type idx
)
134 wxASSERT(idx
< m_size
);
135 return m_values
[idx
];
138 const value_type
& operator[](size_type idx
) const { return at(idx
); }
139 value_type
& operator[](size_type idx
) { return at(idx
); }
140 const value_type
& front() const { return at(0); }
141 value_type
& front() { return at(0); }
142 const value_type
& back() const { return at(size() - 1); }
143 value_type
& back() { return at(size() - 1); }
145 const_iterator
begin() const { return m_values
; }
146 iterator
begin() { return m_values
; }
147 const_iterator
end() const { return m_values
+ size(); }
148 iterator
end() { return m_values
+ size(); }
150 iterator
insert(iterator it
, const value_type
& v
= value_type())
152 // NB: this must be done before reserve(), because reserve()
153 // invalidates iterators!
154 const size_t idx
= it
- begin();
155 const size_t after
= end() - it
;
159 // unless we're inserting at the end, move following elements out of
163 memmove(m_values
+ idx
+ 1,
165 after
* sizeof(value_type
));
172 // use placement new to initialize new object in preallocated place
173 // in m_values and store 'v' in it:
174 void* const place
= m_values
+ idx
;
175 new(place
) value_type(v
);
180 // if the ctor threw an exception, we need to move all the elements
181 // back to their original positions in m_values
184 memmove(m_values
+ idx
,
186 after
* sizeof(value_type
));
189 throw; // rethrow the exception
191 #endif // wxUSE_EXCEPTIONS
193 // increment m_size only if ctor didn't throw -- if it did, we'll be
194 // left with m_values larger than necessary, but number of elements will
198 return begin() + idx
;
201 iterator
erase(iterator it
)
203 return erase(it
, it
+ 1);
206 iterator
erase(iterator first
, iterator last
)
210 wxASSERT( first
< end() && last
<= end() );
212 const size_type idx
= first
- begin();
213 const size_type count
= last
- first
;
214 const size_type after
= end() - last
;
216 // erase elements by calling their destructors:
217 for ( iterator i
= first
; i
< last
; ++i
)
220 // once that's done, move following elements over to the freed space:
223 memmove(m_values
+ idx
,
224 m_values
+ idx
+ count
,
225 after
* sizeof(value_type
));
230 return begin() + idx
;
233 #if WXWIN_COMPATIBILITY_2_8
234 wxDEPRECATED( size_type
erase(size_type n
) );
235 #endif // WXWIN_COMPATIBILITY_2_8
238 // VC6 can't compile static const int members
239 enum { ALLOC_INITIAL_SIZE
= 16 };
240 enum { ALLOC_MAX_SIZE
= 4096 };
242 void Copy(const wxVector
& vb
)
247 for ( const_iterator i
= vb
.begin(); i
!= vb
.end(); ++i
)
254 value_type
*m_values
;
257 #if WXWIN_COMPATIBILITY_2_8
259 inline typename wxVector
<T
>::size_type wxVector
<T
>::erase(size_type n
)
264 #endif // WXWIN_COMPATIBILITY_2_8
266 #endif // wxUSE_STL/!wxUSE_STL
268 #if WXWIN_COMPATIBILITY_2_8
269 #define WX_DECLARE_VECTORBASE(obj, cls) typedef wxVector<obj> cls
270 #define _WX_DECLARE_VECTOR(obj, cls, exp) WX_DECLARE_VECTORBASE(obj, cls)
271 #define WX_DECLARE_VECTOR(obj, cls) WX_DECLARE_VECTORBASE(obj, cls)
272 #endif // WXWIN_COMPATIBILITY_2_8
274 #endif // _WX_VECTOR_H_