]>
Commit | Line | Data |
---|---|---|
1 | /////////////////////////////////////////////////////////////////////////////// | |
2 | // Name: wx/vector.h | |
3 | // Purpose: STL vector clone | |
4 | // Author: Lindsay Mathieson | |
5 | // Modified by: Vaclav Slavik - make it a template | |
6 | // Created: 30.07.2001 | |
7 | // Copyright: (c) 2001 Lindsay Mathieson <lindsay@mathieson.org>, | |
8 | // 2007 Vaclav Slavik <vslavik@fastmail.fm> | |
9 | // Licence: wxWindows licence | |
10 | /////////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | #ifndef _WX_VECTOR_H_ | |
13 | #define _WX_VECTOR_H_ | |
14 | ||
15 | #include "wx/defs.h" | |
16 | ||
17 | #if wxUSE_STL | |
18 | ||
19 | #include <vector> | |
20 | #define wxVector std::vector | |
21 | ||
22 | #else // !wxUSE_STL | |
23 | ||
24 | #include "wx/utils.h" | |
25 | ||
26 | #include "wx/beforestd.h" | |
27 | #include <new> // for placement new | |
28 | #include "wx/afterstd.h" | |
29 | ||
30 | template<typename T> | |
31 | class wxVector | |
32 | { | |
33 | public: | |
34 | typedef size_t size_type; | |
35 | typedef T value_type; | |
36 | typedef value_type* iterator; | |
37 | typedef const value_type* const_iterator; | |
38 | typedef value_type& reference; | |
39 | ||
40 | wxVector() : m_size(0), m_capacity(0), m_values(NULL) {} | |
41 | ||
42 | wxVector(const wxVector& c) | |
43 | { | |
44 | Copy(c); | |
45 | } | |
46 | ||
47 | ~wxVector() | |
48 | { | |
49 | clear(); | |
50 | } | |
51 | ||
52 | void clear() | |
53 | { | |
54 | // call destructors of stored objects: | |
55 | for ( size_type i = 0; i < m_size; i++ ) | |
56 | { | |
57 | m_values[i].~value_type(); | |
58 | } | |
59 | ||
60 | free(m_values); | |
61 | m_values = NULL; | |
62 | m_size = m_capacity = 0; | |
63 | } | |
64 | ||
65 | void reserve(size_type n) | |
66 | { | |
67 | if ( n <= m_capacity ) | |
68 | return; | |
69 | ||
70 | // increase the size twice, unless we're already too big or unless | |
71 | // more is requested | |
72 | // | |
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; | |
80 | ||
81 | m_values = (value_type*)realloc(m_values, n * sizeof(value_type)); | |
82 | ||
83 | m_capacity = n; | |
84 | } | |
85 | ||
86 | size_type size() const | |
87 | { | |
88 | return m_size; | |
89 | } | |
90 | ||
91 | size_type capacity() const | |
92 | { | |
93 | return m_capacity; | |
94 | } | |
95 | ||
96 | bool empty() const | |
97 | { | |
98 | return size() == 0; | |
99 | } | |
100 | ||
101 | wxVector& operator=(const wxVector& vb) | |
102 | { | |
103 | Copy(vb); | |
104 | return *this; | |
105 | } | |
106 | ||
107 | void push_back(const value_type& v) | |
108 | { | |
109 | reserve(size() + 1); | |
110 | ||
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); | |
115 | ||
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 | |
120 | m_size++; | |
121 | } | |
122 | ||
123 | void pop_back() | |
124 | { | |
125 | erase(end() - 1); | |
126 | } | |
127 | ||
128 | const value_type& at(size_type idx) const | |
129 | { | |
130 | wxASSERT(idx < m_size); | |
131 | return m_values[idx]; | |
132 | } | |
133 | ||
134 | value_type& at(size_type idx) | |
135 | { | |
136 | wxASSERT(idx < m_size); | |
137 | return m_values[idx]; | |
138 | } | |
139 | ||
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); } | |
146 | ||
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(); } | |
151 | ||
152 | iterator insert(iterator it, const value_type& v = value_type()) | |
153 | { | |
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; | |
158 | ||
159 | reserve(size() + 1); | |
160 | ||
161 | // unless we're inserting at the end, move following elements out of | |
162 | // the way: | |
163 | if ( after > 0 ) | |
164 | { | |
165 | memmove(m_values + idx + 1, | |
166 | m_values + idx, | |
167 | after * sizeof(value_type)); | |
168 | } | |
169 | ||
170 | #if wxUSE_EXCEPTIONS | |
171 | try | |
172 | { | |
173 | #endif | |
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); | |
178 | #if wxUSE_EXCEPTIONS | |
179 | } | |
180 | catch ( ... ) | |
181 | { | |
182 | // if the ctor threw an exception, we need to move all the elements | |
183 | // back to their original positions in m_values | |
184 | if ( after > 0 ) | |
185 | { | |
186 | memmove(m_values + idx, | |
187 | m_values + idx + 1, | |
188 | after * sizeof(value_type)); | |
189 | } | |
190 | ||
191 | throw; // rethrow the exception | |
192 | } | |
193 | #endif // wxUSE_EXCEPTIONS | |
194 | ||
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 | |
197 | // be the same | |
198 | m_size++; | |
199 | ||
200 | return begin() + idx; | |
201 | } | |
202 | ||
203 | iterator erase(iterator it) | |
204 | { | |
205 | return erase(it, it + 1); | |
206 | } | |
207 | ||
208 | iterator erase(iterator first, iterator last) | |
209 | { | |
210 | if ( first == last ) | |
211 | return first; | |
212 | wxASSERT( first < end() && last <= end() ); | |
213 | ||
214 | const size_type idx = first - begin(); | |
215 | const size_type count = last - first; | |
216 | const size_type after = end() - last; | |
217 | ||
218 | // erase elements by calling their destructors: | |
219 | for ( iterator i = first; i < last; ++i ) | |
220 | i->~value_type(); | |
221 | ||
222 | // once that's done, move following elements over to the freed space: | |
223 | if ( after > 0 ) | |
224 | { | |
225 | memmove(m_values + idx, | |
226 | m_values + idx + count, | |
227 | after * sizeof(value_type)); | |
228 | } | |
229 | ||
230 | m_size -= count; | |
231 | ||
232 | return begin() + idx; | |
233 | } | |
234 | ||
235 | #if WXWIN_COMPATIBILITY_2_8 | |
236 | wxDEPRECATED( size_type erase(size_type n) ); | |
237 | #endif // WXWIN_COMPATIBILITY_2_8 | |
238 | ||
239 | private: | |
240 | // VC6 can't compile static const int members | |
241 | enum { ALLOC_INITIAL_SIZE = 16 }; | |
242 | enum { ALLOC_MAX_SIZE = 4096 }; | |
243 | ||
244 | void Copy(const wxVector& vb) | |
245 | { | |
246 | clear(); | |
247 | reserve(vb.size()); | |
248 | ||
249 | for ( const_iterator i = vb.begin(); i != vb.end(); ++i ) | |
250 | push_back(*i); | |
251 | } | |
252 | ||
253 | private: | |
254 | size_type m_size, | |
255 | m_capacity; | |
256 | value_type *m_values; | |
257 | }; | |
258 | ||
259 | #if WXWIN_COMPATIBILITY_2_8 | |
260 | template<typename T> | |
261 | inline typename wxVector<T>::size_type wxVector<T>::erase(size_type n) | |
262 | { | |
263 | erase(begin() + n); | |
264 | return n; | |
265 | } | |
266 | #endif // WXWIN_COMPATIBILITY_2_8 | |
267 | ||
268 | #endif // wxUSE_STL/!wxUSE_STL | |
269 | ||
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 | |
275 | ||
276 | #endif // _WX_VECTOR_H_ |