]>
Commit | Line | Data |
---|---|---|
3c648a82 VZ |
1 | /////////////////////////////////////////////////////////////////////////////// |
2 | // Name: wx/vector.h | |
3 | // Purpose: STL vector clone | |
4 | // Author: Lindsay Mathieson | |
e966f815 | 5 | // Modified by: Vaclav Slavik - make it a template |
3c648a82 | 6 | // Created: 30.07.2001 |
e966f815 VS |
7 | // Copyright: (c) 2001 Lindsay Mathieson <lindsay@mathieson.org>, |
8 | // 2007 Vaclav Slavik <vslavik@fastmail.fm> | |
65571936 | 9 | // Licence: wxWindows licence |
3c648a82 VZ |
10 | /////////////////////////////////////////////////////////////////////////////// |
11 | ||
12 | #ifndef _WX_VECTOR_H_ | |
13 | #define _WX_VECTOR_H_ | |
14 | ||
15 | #include "wx/defs.h" | |
16 | ||
df4aed1c | 17 | #if wxUSE_STL |
e966f815 | 18 | |
e966f815 VS |
19 | #include <vector> |
20 | #define wxVector std::vector | |
21 | ||
22 | #else // !wxUSE_STL | |
23 | ||
2d615fe9 JS |
24 | #include "wx/utils.h" |
25 | ||
e966f815 VS |
26 | template<typename T> |
27 | class wxVector | |
3c648a82 | 28 | { |
2d6c58d6 | 29 | public: |
5fd588d2 | 30 | typedef size_t size_type; |
e966f815 | 31 | typedef T value_type; |
df4aed1c | 32 | typedef value_type* iterator; |
0516de2c | 33 | typedef const value_type* const_iterator; |
df4aed1c | 34 | typedef value_type& reference; |
e966f815 | 35 | |
0516de2c | 36 | wxVector() : m_size(0), m_capacity(0), m_values(NULL) {} |
e966f815 VS |
37 | |
38 | wxVector(const wxVector& c) | |
39 | { | |
0516de2c | 40 | Copy(c); |
e966f815 VS |
41 | } |
42 | ||
43 | ~wxVector() | |
44 | { | |
45 | clear(); | |
46 | } | |
47 | ||
48 | void clear() | |
49 | { | |
0516de2c VS |
50 | delete[] m_values; |
51 | m_values = NULL; | |
e966f815 VS |
52 | m_size = m_capacity = 0; |
53 | } | |
54 | ||
55 | void reserve(size_type n) | |
56 | { | |
0516de2c VS |
57 | if ( n <= m_capacity ) |
58 | return; | |
59 | ||
60 | // increase the size twice, unless we're already too big or unless | |
61 | // more is requested | |
62 | const size_type increment = (m_size > 0) | |
63 | ? wxMin(m_size, ALLOC_MAX_SIZE) | |
64 | : ALLOC_INITIAL_SIZE; | |
65 | if ( m_capacity + increment > n ) | |
66 | n = m_capacity + increment; | |
67 | ||
68 | value_type *mem = new value_type[n]; | |
69 | ||
70 | if ( m_values ) | |
e966f815 | 71 | { |
0516de2c VS |
72 | for ( size_type i = 0; i < m_size; ++i ) |
73 | mem[i] = m_values[i]; | |
74 | delete[] m_values; | |
e966f815 | 75 | } |
0516de2c VS |
76 | |
77 | m_values = mem; | |
78 | m_capacity = n; | |
e966f815 VS |
79 | } |
80 | ||
81 | size_type size() const | |
82 | { | |
83 | return m_size; | |
84 | } | |
85 | ||
86 | size_type capacity() const | |
87 | { | |
88 | return m_capacity; | |
89 | } | |
90 | ||
91 | bool empty() const | |
92 | { | |
93 | return size() == 0; | |
94 | } | |
95 | ||
96 | wxVector& operator=(const wxVector& vb) | |
97 | { | |
0516de2c | 98 | Copy(vb); |
e966f815 VS |
99 | return *this; |
100 | } | |
101 | ||
0516de2c | 102 | void push_back(const value_type& v) |
e966f815 | 103 | { |
0516de2c VS |
104 | reserve(size() + 1); |
105 | m_values[m_size++] = v; | |
e966f815 VS |
106 | } |
107 | ||
108 | void pop_back() | |
109 | { | |
0516de2c | 110 | erase(end() - 1); |
e966f815 VS |
111 | } |
112 | ||
113 | const value_type& at(size_type idx) const | |
114 | { | |
115 | wxASSERT(idx < m_size); | |
0516de2c | 116 | return m_values[idx]; |
e966f815 VS |
117 | } |
118 | ||
119 | value_type& at(size_type idx) | |
120 | { | |
121 | wxASSERT(idx < m_size); | |
0516de2c | 122 | return m_values[idx]; |
e966f815 | 123 | } |
3c648a82 | 124 | |
e966f815 VS |
125 | const value_type& operator[](size_type idx) const { return at(idx); } |
126 | value_type& operator[](size_type idx) { return at(idx); } | |
127 | const value_type& front() const { return at(0); } | |
128 | value_type& front() { return at(0); } | |
129 | const value_type& back() const { return at(size() - 1); } | |
130 | value_type& back() { return at(size() - 1); } | |
131 | ||
0516de2c VS |
132 | const_iterator begin() const { return m_values; } |
133 | iterator begin() { return m_values; } | |
134 | const_iterator end() const { return m_values + size(); } | |
135 | iterator end() { return m_values + size(); } | |
df4aed1c | 136 | |
0516de2c | 137 | iterator insert(iterator it, const value_type& v = value_type()) |
f631cd8e | 138 | { |
0516de2c | 139 | size_t idx = it - begin(); |
f631cd8e | 140 | |
0516de2c | 141 | reserve(size() + 1); |
9cf33372 | 142 | |
0516de2c VS |
143 | // unless we're inserting at the end, move following values out of |
144 | // the way: | |
145 | for ( size_t n = m_size; n != idx; --n ) | |
146 | m_values[n] = m_values[n-1]; | |
e966f815 | 147 | |
0516de2c VS |
148 | m_values[idx] = v; |
149 | m_size++; | |
3c648a82 | 150 | |
0516de2c | 151 | return begin() + idx; |
1f32f585 | 152 | } |
3c648a82 | 153 | |
0516de2c | 154 | iterator erase(iterator it) |
3c648a82 | 155 | { |
0516de2c | 156 | return erase(it, it + 1); |
1f32f585 | 157 | } |
3c648a82 | 158 | |
0516de2c | 159 | iterator erase(iterator first, iterator last) |
df4aed1c | 160 | { |
0516de2c VS |
161 | if ( first == last ) |
162 | return first; | |
163 | wxASSERT( first < end() && last <= end() ); | |
f631cd8e | 164 | |
0516de2c VS |
165 | size_type index = first - begin(); |
166 | size_type count = last - first; | |
df4aed1c | 167 | |
0516de2c VS |
168 | // move the remaining values over to the freed space: |
169 | for ( iterator i = last; i < end(); ++i ) | |
170 | *(i - count) = *i; | |
171 | ||
172 | // erase items behind the new end of m_values: | |
173 | for ( iterator i = end() - count; i < end(); ++i ) | |
174 | *i = value_type(); | |
f631cd8e | 175 | |
df4aed1c | 176 | m_size -= count; |
0516de2c VS |
177 | |
178 | return begin() + index; | |
df4aed1c | 179 | } |
0516de2c VS |
180 | |
181 | #if WXWIN_COMPATIBILITY_2_8 | |
182 | wxDEPRECATED( size_type erase(size_type n) ); | |
183 | #endif // WXWIN_COMPATIBILITY_2_8 | |
184 | ||
185 | private: | |
f9bf06ac VS |
186 | // VC6 can't compile static const int members |
187 | enum { ALLOC_INITIAL_SIZE = 16 }; | |
188 | enum { ALLOC_MAX_SIZE = 4096 }; | |
0516de2c VS |
189 | |
190 | void Copy(const wxVector& vb) | |
3c648a82 VZ |
191 | { |
192 | clear(); | |
0516de2c | 193 | reserve(vb.size()); |
3c648a82 | 194 | |
0516de2c VS |
195 | for ( const_iterator i = vb.begin(); i != vb.end(); ++i ) |
196 | push_back(*i); | |
1f32f585 | 197 | } |
3c648a82 | 198 | |
e966f815 | 199 | private: |
e966f815 VS |
200 | size_type m_size, |
201 | m_capacity; | |
0516de2c | 202 | value_type *m_values; |
3c648a82 VZ |
203 | }; |
204 | ||
9cf33372 VS |
205 | #if WXWIN_COMPATIBILITY_2_8 |
206 | template<typename T> | |
207 | typename wxVector<T>::size_type wxVector<T>::erase(size_type n) | |
208 | { | |
0516de2c | 209 | erase(begin() + n); |
9cf33372 VS |
210 | return n; |
211 | } | |
212 | #endif // WXWIN_COMPATIBILITY_2_8 | |
213 | ||
e966f815 | 214 | #endif // wxUSE_STL/!wxUSE_STL |
5fd588d2 | 215 | |
e966f815 VS |
216 | #if WXWIN_COMPATIBILITY_2_8 |
217 | #define WX_DECLARE_VECTORBASE(obj, cls) typedef wxVector<obj> cls | |
218 | #define _WX_DECLARE_VECTOR(obj, cls, exp) WX_DECLARE_VECTORBASE(obj, cls) | |
219 | #define WX_DECLARE_VECTOR(obj, cls) WX_DECLARE_VECTORBASE(obj, cls) | |
220 | #endif // WXWIN_COMPATIBILITY_2_8 | |
3c648a82 | 221 | |
e966f815 | 222 | #endif // _WX_VECTOR_H_ |