]> git.saurik.com Git - wxWidgets.git/blame - include/wx/vector.h
Compile fix on Mac (at least)
[wxWidgets.git] / include / wx / vector.h
CommitLineData
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
24template<typename T>
25class wxVector
3c648a82 26{
2d6c58d6 27public:
5fd588d2 28 typedef size_t size_type;
e966f815 29 typedef T value_type;
df4aed1c 30 typedef value_type* iterator;
0516de2c 31 typedef const value_type* const_iterator;
df4aed1c 32 typedef value_type& reference;
e966f815 33
0516de2c 34 wxVector() : m_size(0), m_capacity(0), m_values(NULL) {}
e966f815
VS
35
36 wxVector(const wxVector& c)
37 {
0516de2c 38 Copy(c);
e966f815
VS
39 }
40
41 ~wxVector()
42 {
43 clear();
44 }
45
46 void clear()
47 {
0516de2c
VS
48 delete[] m_values;
49 m_values = NULL;
e966f815
VS
50 m_size = m_capacity = 0;
51 }
52
53 void reserve(size_type n)
54 {
0516de2c
VS
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 )
e966f815 69 {
0516de2c
VS
70 for ( size_type i = 0; i < m_size; ++i )
71 mem[i] = m_values[i];
72 delete[] m_values;
e966f815 73 }
0516de2c
VS
74
75 m_values = mem;
76 m_capacity = n;
e966f815
VS
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 {
0516de2c 96 Copy(vb);
e966f815
VS
97 return *this;
98 }
99
0516de2c 100 void push_back(const value_type& v)
e966f815 101 {
0516de2c
VS
102 reserve(size() + 1);
103 m_values[m_size++] = v;
e966f815
VS
104 }
105
106 void pop_back()
107 {
0516de2c 108 erase(end() - 1);
e966f815
VS
109 }
110
111 const value_type& at(size_type idx) const
112 {
113 wxASSERT(idx < m_size);
0516de2c 114 return m_values[idx];
e966f815
VS
115 }
116
117 value_type& at(size_type idx)
118 {
119 wxASSERT(idx < m_size);
0516de2c 120 return m_values[idx];
e966f815 121 }
3c648a82 122
e966f815
VS
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
0516de2c
VS
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(); }
df4aed1c 134
0516de2c 135 iterator insert(iterator it, const value_type& v = value_type())
f631cd8e 136 {
0516de2c 137 size_t idx = it - begin();
f631cd8e 138
0516de2c 139 reserve(size() + 1);
9cf33372 140
0516de2c
VS
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];
e966f815 145
0516de2c
VS
146 m_values[idx] = v;
147 m_size++;
3c648a82 148
0516de2c 149 return begin() + idx;
1f32f585 150 }
3c648a82 151
0516de2c 152 iterator erase(iterator it)
3c648a82 153 {
0516de2c 154 return erase(it, it + 1);
1f32f585 155 }
3c648a82 156
0516de2c 157 iterator erase(iterator first, iterator last)
df4aed1c 158 {
0516de2c
VS
159 if ( first == last )
160 return first;
161 wxASSERT( first < end() && last <= end() );
f631cd8e 162
0516de2c
VS
163 size_type index = first - begin();
164 size_type count = last - first;
df4aed1c 165
0516de2c
VS
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();
f631cd8e 173
df4aed1c 174 m_size -= count;
0516de2c
VS
175
176 return begin() + index;
df4aed1c 177 }
0516de2c
VS
178
179#if WXWIN_COMPATIBILITY_2_8
180 wxDEPRECATED( size_type erase(size_type n) );
181#endif // WXWIN_COMPATIBILITY_2_8
182
183private:
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)
3c648a82
VZ
188 {
189 clear();
0516de2c 190 reserve(vb.size());
3c648a82 191
0516de2c
VS
192 for ( const_iterator i = vb.begin(); i != vb.end(); ++i )
193 push_back(*i);
1f32f585 194 }
3c648a82 195
e966f815 196private:
e966f815
VS
197 size_type m_size,
198 m_capacity;
0516de2c 199 value_type *m_values;
3c648a82
VZ
200};
201
9cf33372
VS
202#if WXWIN_COMPATIBILITY_2_8
203template<typename T>
204typename wxVector<T>::size_type wxVector<T>::erase(size_type n)
205{
0516de2c 206 erase(begin() + n);
9cf33372
VS
207 return n;
208}
209#endif // WXWIN_COMPATIBILITY_2_8
210
e966f815 211#endif // wxUSE_STL/!wxUSE_STL
5fd588d2 212
e966f815
VS
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
3c648a82 218
e966f815 219#endif // _WX_VECTOR_H_