]> git.saurik.com Git - wxWidgets.git/blame - include/wx/vector.h
fix bug with parsing negative time zones in ParseRfc822Date()
[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
2d615fe9
JS
24#include "wx/utils.h"
25
e966f815
VS
26template<typename T>
27class wxVector
3c648a82 28{
2d6c58d6 29public:
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
f5851311
VZ
62 //
63 // NB: casts to size_t are needed to suppress mingw32 warnings about
64 // mixing enums and ints in the same expression
65 const size_type increment = m_size > 0
66 ? wxMin(m_size, (size_type)ALLOC_MAX_SIZE)
67 : (size_type)ALLOC_INITIAL_SIZE;
0516de2c
VS
68 if ( m_capacity + increment > n )
69 n = m_capacity + increment;
70
71 value_type *mem = new value_type[n];
72
73 if ( m_values )
e966f815 74 {
0516de2c
VS
75 for ( size_type i = 0; i < m_size; ++i )
76 mem[i] = m_values[i];
77 delete[] m_values;
e966f815 78 }
0516de2c
VS
79
80 m_values = mem;
81 m_capacity = n;
e966f815
VS
82 }
83
84 size_type size() const
85 {
86 return m_size;
87 }
88
89 size_type capacity() const
90 {
91 return m_capacity;
92 }
93
94 bool empty() const
95 {
96 return size() == 0;
97 }
98
99 wxVector& operator=(const wxVector& vb)
100 {
0516de2c 101 Copy(vb);
e966f815
VS
102 return *this;
103 }
104
0516de2c 105 void push_back(const value_type& v)
e966f815 106 {
0516de2c
VS
107 reserve(size() + 1);
108 m_values[m_size++] = v;
e966f815
VS
109 }
110
111 void pop_back()
112 {
0516de2c 113 erase(end() - 1);
e966f815
VS
114 }
115
116 const value_type& at(size_type idx) const
117 {
118 wxASSERT(idx < m_size);
0516de2c 119 return m_values[idx];
e966f815
VS
120 }
121
122 value_type& at(size_type idx)
123 {
124 wxASSERT(idx < m_size);
0516de2c 125 return m_values[idx];
e966f815 126 }
3c648a82 127
e966f815
VS
128 const value_type& operator[](size_type idx) const { return at(idx); }
129 value_type& operator[](size_type idx) { return at(idx); }
130 const value_type& front() const { return at(0); }
131 value_type& front() { return at(0); }
132 const value_type& back() const { return at(size() - 1); }
133 value_type& back() { return at(size() - 1); }
134
0516de2c
VS
135 const_iterator begin() const { return m_values; }
136 iterator begin() { return m_values; }
137 const_iterator end() const { return m_values + size(); }
138 iterator end() { return m_values + size(); }
df4aed1c 139
0516de2c 140 iterator insert(iterator it, const value_type& v = value_type())
f631cd8e 141 {
0516de2c 142 size_t idx = it - begin();
f631cd8e 143
0516de2c 144 reserve(size() + 1);
9cf33372 145
0516de2c
VS
146 // unless we're inserting at the end, move following values out of
147 // the way:
148 for ( size_t n = m_size; n != idx; --n )
149 m_values[n] = m_values[n-1];
e966f815 150
0516de2c
VS
151 m_values[idx] = v;
152 m_size++;
3c648a82 153
0516de2c 154 return begin() + idx;
1f32f585 155 }
3c648a82 156
0516de2c 157 iterator erase(iterator it)
3c648a82 158 {
0516de2c 159 return erase(it, it + 1);
1f32f585 160 }
3c648a82 161
0516de2c 162 iterator erase(iterator first, iterator last)
df4aed1c 163 {
0516de2c
VS
164 if ( first == last )
165 return first;
166 wxASSERT( first < end() && last <= end() );
f631cd8e 167
0516de2c
VS
168 size_type index = first - begin();
169 size_type count = last - first;
df4aed1c 170
0516de2c
VS
171 // move the remaining values over to the freed space:
172 for ( iterator i = last; i < end(); ++i )
173 *(i - count) = *i;
174
175 // erase items behind the new end of m_values:
1cffe600
CE
176 for ( iterator j = end() - count; j < end(); ++j )
177 *j = value_type();
f631cd8e 178
df4aed1c 179 m_size -= count;
0516de2c
VS
180
181 return begin() + index;
df4aed1c 182 }
0516de2c
VS
183
184#if WXWIN_COMPATIBILITY_2_8
185 wxDEPRECATED( size_type erase(size_type n) );
186#endif // WXWIN_COMPATIBILITY_2_8
187
188private:
f9bf06ac
VS
189 // VC6 can't compile static const int members
190 enum { ALLOC_INITIAL_SIZE = 16 };
191 enum { ALLOC_MAX_SIZE = 4096 };
0516de2c
VS
192
193 void Copy(const wxVector& vb)
3c648a82
VZ
194 {
195 clear();
0516de2c 196 reserve(vb.size());
3c648a82 197
0516de2c
VS
198 for ( const_iterator i = vb.begin(); i != vb.end(); ++i )
199 push_back(*i);
1f32f585 200 }
3c648a82 201
e966f815 202private:
e966f815
VS
203 size_type m_size,
204 m_capacity;
0516de2c 205 value_type *m_values;
3c648a82
VZ
206};
207
9cf33372
VS
208#if WXWIN_COMPATIBILITY_2_8
209template<typename T>
32aa5bda 210inline typename wxVector<T>::size_type wxVector<T>::erase(size_type n)
9cf33372 211{
0516de2c 212 erase(begin() + n);
9cf33372
VS
213 return n;
214}
215#endif // WXWIN_COMPATIBILITY_2_8
216
e966f815 217#endif // wxUSE_STL/!wxUSE_STL
5fd588d2 218
e966f815
VS
219#if WXWIN_COMPATIBILITY_2_8
220 #define WX_DECLARE_VECTORBASE(obj, cls) typedef wxVector<obj> cls
221 #define _WX_DECLARE_VECTOR(obj, cls, exp) WX_DECLARE_VECTORBASE(obj, cls)
222 #define WX_DECLARE_VECTOR(obj, cls) WX_DECLARE_VECTORBASE(obj, cls)
223#endif // WXWIN_COMPATIBILITY_2_8
3c648a82 224
e966f815 225#endif // _WX_VECTOR_H_