1 ///////////////////////////////////////////////////////////////////////////////
3 // Purpose: STL vector clone
4 // Author: Lindsay Mathieson
5 // Modified by: Vaclav Slavik - make it a template
7 // Copyright: (c) 2001 Lindsay Mathieson <lindsay@mathieson.org>,
8 // 2007 Vaclav Slavik <vslavik@fastmail.fm>
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
20 #define wxVector std::vector
28 typedef size_t size_type
;
30 typedef value_type
* iterator
;
31 typedef const value_type
* const_iterator
;
32 typedef value_type
& reference
;
34 wxVector() : m_size(0), m_capacity(0), m_values(NULL
) {}
36 wxVector(const wxVector
& c
)
50 m_size
= m_capacity
= 0;
53 void reserve(size_type n
)
55 if ( n
<= m_capacity
)
58 // increase the size twice, unless we're already too big or unless
60 const size_type increment
= (m_size
> 0)
61 ? wxMin(m_size
, ALLOC_MAX_SIZE
)
63 if ( m_capacity
+ increment
> n
)
64 n
= m_capacity
+ increment
;
66 value_type
*mem
= new value_type
[n
];
70 for ( size_type i
= 0; i
< m_size
; ++i
)
79 size_type
size() const
84 size_type
capacity() const
94 wxVector
& operator=(const wxVector
& vb
)
100 void push_back(const value_type
& v
)
103 m_values
[m_size
++] = v
;
111 const value_type
& at(size_type idx
) const
113 wxASSERT(idx
< m_size
);
114 return m_values
[idx
];
117 value_type
& at(size_type idx
)
119 wxASSERT(idx
< m_size
);
120 return m_values
[idx
];
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); }
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(); }
135 iterator
insert(iterator it
, const value_type
& v
= value_type())
137 size_t idx
= it
- begin();
141 // unless we're inserting at the end, move following values out of
143 for ( size_t n
= m_size
; n
!= idx
; --n
)
144 m_values
[n
] = m_values
[n
-1];
149 return begin() + idx
;
152 iterator
erase(iterator it
)
154 return erase(it
, it
+ 1);
157 iterator
erase(iterator first
, iterator last
)
161 wxASSERT( first
< end() && last
<= end() );
163 size_type index
= first
- begin();
164 size_type count
= last
- first
;
166 // move the remaining values over to the freed space:
167 for ( iterator i
= last
; i
< end(); ++i
)
170 // erase items behind the new end of m_values:
171 for ( iterator i
= end() - count
; i
< end(); ++i
)
176 return begin() + index
;
179 #if WXWIN_COMPATIBILITY_2_8
180 wxDEPRECATED( size_type
erase(size_type n
) );
181 #endif // WXWIN_COMPATIBILITY_2_8
184 static const size_type ALLOC_INITIAL_SIZE
= 16;
185 static const size_type ALLOC_MAX_SIZE
= 4096;
187 void Copy(const wxVector
& vb
)
192 for ( const_iterator i
= vb
.begin(); i
!= vb
.end(); ++i
)
199 value_type
*m_values
;
202 #if WXWIN_COMPATIBILITY_2_8
204 typename wxVector
<T
>::size_type wxVector
<T
>::erase(size_type n
)
209 #endif // WXWIN_COMPATIBILITY_2_8
211 #endif // wxUSE_STL/!wxUSE_STL
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
219 #endif // _WX_VECTOR_H_