]> git.saurik.com Git - wxWidgets.git/blob - include/wx/vector.h
1. fixed wxVector<T> iterators to actually point to what they're supposed to point...
[wxWidgets.git] / include / wx / vector.h
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 template<typename T>
25 class wxVector
26 {
27 public:
28 typedef size_t size_type;
29 typedef T value_type;
30 typedef value_type* iterator;
31 typedef const value_type* const_iterator;
32 typedef value_type& reference;
33
34 wxVector() : m_size(0), m_capacity(0), m_values(NULL) {}
35
36 wxVector(const wxVector& c)
37 {
38 Copy(c);
39 }
40
41 ~wxVector()
42 {
43 clear();
44 }
45
46 void clear()
47 {
48 delete[] m_values;
49 m_values = NULL;
50 m_size = m_capacity = 0;
51 }
52
53 void reserve(size_type n)
54 {
55 if ( n <= m_capacity )
56 return;
57
58 // increase the size twice, unless we're already too big or unless
59 // more is requested
60 const size_type increment = (m_size > 0)
61 ? wxMin(m_size, ALLOC_MAX_SIZE)
62 : ALLOC_INITIAL_SIZE;
63 if ( m_capacity + increment > n )
64 n = m_capacity + increment;
65
66 value_type *mem = new value_type[n];
67
68 if ( m_values )
69 {
70 for ( size_type i = 0; i < m_size; ++i )
71 mem[i] = m_values[i];
72 delete[] m_values;
73 }
74
75 m_values = mem;
76 m_capacity = n;
77 }
78
79 size_type size() const
80 {
81 return m_size;
82 }
83
84 size_type capacity() const
85 {
86 return m_capacity;
87 }
88
89 bool empty() const
90 {
91 return size() == 0;
92 }
93
94 wxVector& operator=(const wxVector& vb)
95 {
96 Copy(vb);
97 return *this;
98 }
99
100 void push_back(const value_type& v)
101 {
102 reserve(size() + 1);
103 m_values[m_size++] = v;
104 }
105
106 void pop_back()
107 {
108 erase(end() - 1);
109 }
110
111 const value_type& at(size_type idx) const
112 {
113 wxASSERT(idx < m_size);
114 return m_values[idx];
115 }
116
117 value_type& at(size_type idx)
118 {
119 wxASSERT(idx < m_size);
120 return m_values[idx];
121 }
122
123 const value_type& operator[](size_type idx) const { return at(idx); }
124 value_type& operator[](size_type idx) { return at(idx); }
125 const value_type& front() const { return at(0); }
126 value_type& front() { return at(0); }
127 const value_type& back() const { return at(size() - 1); }
128 value_type& back() { return at(size() - 1); }
129
130 const_iterator begin() const { return m_values; }
131 iterator begin() { return m_values; }
132 const_iterator end() const { return m_values + size(); }
133 iterator end() { return m_values + size(); }
134
135 iterator insert(iterator it, const value_type& v = value_type())
136 {
137 size_t idx = it - begin();
138
139 reserve(size() + 1);
140
141 // unless we're inserting at the end, move following values out of
142 // the way:
143 for ( size_t n = m_size; n != idx; --n )
144 m_values[n] = m_values[n-1];
145
146 m_values[idx] = v;
147 m_size++;
148
149 return begin() + idx;
150 }
151
152 iterator erase(iterator it)
153 {
154 return erase(it, it + 1);
155 }
156
157 iterator erase(iterator first, iterator last)
158 {
159 if ( first == last )
160 return first;
161 wxASSERT( first < end() && last <= end() );
162
163 size_type index = first - begin();
164 size_type count = last - first;
165
166 // move the remaining values over to the freed space:
167 for ( iterator i = last; i < end(); ++i )
168 *(i - count) = *i;
169
170 // erase items behind the new end of m_values:
171 for ( iterator i = end() - count; i < end(); ++i )
172 *i = value_type();
173
174 m_size -= count;
175
176 return begin() + index;
177 }
178
179 #if WXWIN_COMPATIBILITY_2_8
180 wxDEPRECATED( size_type erase(size_type n) );
181 #endif // WXWIN_COMPATIBILITY_2_8
182
183 private:
184 static const size_type ALLOC_INITIAL_SIZE = 16;
185 static const size_type ALLOC_MAX_SIZE = 4096;
186
187 void Copy(const wxVector& vb)
188 {
189 clear();
190 reserve(vb.size());
191
192 for ( const_iterator i = vb.begin(); i != vb.end(); ++i )
193 push_back(*i);
194 }
195
196 private:
197 size_type m_size,
198 m_capacity;
199 value_type *m_values;
200 };
201
202 #if WXWIN_COMPATIBILITY_2_8
203 template<typename T>
204 typename wxVector<T>::size_type wxVector<T>::erase(size_type n)
205 {
206 erase(begin() + n);
207 return n;
208 }
209 #endif // WXWIN_COMPATIBILITY_2_8
210
211 #endif // wxUSE_STL/!wxUSE_STL
212
213 #if WXWIN_COMPATIBILITY_2_8
214 #define WX_DECLARE_VECTORBASE(obj, cls) typedef wxVector<obj> cls
215 #define _WX_DECLARE_VECTOR(obj, cls, exp) WX_DECLARE_VECTORBASE(obj, cls)
216 #define WX_DECLARE_VECTOR(obj, cls) WX_DECLARE_VECTORBASE(obj, cls)
217 #endif // WXWIN_COMPATIBILITY_2_8
218
219 #endif // _WX_VECTOR_H_