]>
git.saurik.com Git - wxWidgets.git/blob - include/wx/vector.h
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
30 typedef size_t size_type
;
32 typedef value_type
* iterator
;
33 typedef const value_type
* const_iterator
;
34 typedef value_type
& reference
;
36 wxVector() : m_size(0), m_capacity(0), m_values(NULL
) {}
38 wxVector(const wxVector
& c
)
52 m_size
= m_capacity
= 0;
55 void reserve(size_type n
)
57 if ( n
<= m_capacity
)
60 // increase the size twice, unless we're already too big or unless
62 const size_type increment
= (m_size
> 0)
63 ? wxMin(m_size
, ALLOC_MAX_SIZE
)
65 if ( m_capacity
+ increment
> n
)
66 n
= m_capacity
+ increment
;
68 value_type
*mem
= new value_type
[n
];
72 for ( size_type i
= 0; i
< m_size
; ++i
)
81 size_type
size() const
86 size_type
capacity() const
96 wxVector
& operator=(const wxVector
& vb
)
102 void push_back(const value_type
& v
)
105 m_values
[m_size
++] = v
;
113 const value_type
& at(size_type idx
) const
115 wxASSERT(idx
< m_size
);
116 return m_values
[idx
];
119 value_type
& at(size_type idx
)
121 wxASSERT(idx
< m_size
);
122 return m_values
[idx
];
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); }
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(); }
137 iterator
insert(iterator it
, const value_type
& v
= value_type())
139 size_t idx
= it
- begin();
143 // unless we're inserting at the end, move following values out of
145 for ( size_t n
= m_size
; n
!= idx
; --n
)
146 m_values
[n
] = m_values
[n
-1];
151 return begin() + idx
;
154 iterator
erase(iterator it
)
156 return erase(it
, it
+ 1);
159 iterator
erase(iterator first
, iterator last
)
163 wxASSERT( first
< end() && last
<= end() );
165 size_type index
= first
- begin();
166 size_type count
= last
- first
;
168 // move the remaining values over to the freed space:
169 for ( iterator i
= last
; i
< end(); ++i
)
172 // erase items behind the new end of m_values:
173 for ( iterator i
= end() - count
; i
< end(); ++i
)
178 return begin() + index
;
181 #if WXWIN_COMPATIBILITY_2_8
182 wxDEPRECATED( size_type
erase(size_type n
) );
183 #endif // WXWIN_COMPATIBILITY_2_8
186 // VC6 can't compile static const int members
187 enum { ALLOC_INITIAL_SIZE
= 16 };
188 enum { ALLOC_MAX_SIZE
= 4096 };
190 void Copy(const wxVector
& vb
)
195 for ( const_iterator i
= vb
.begin(); i
!= vb
.end(); ++i
)
202 value_type
*m_values
;
205 #if WXWIN_COMPATIBILITY_2_8
207 typename wxVector
<T
>::size_type wxVector
<T
>::erase(size_type n
)
212 #endif // WXWIN_COMPATIBILITY_2_8
214 #endif // wxUSE_STL/!wxUSE_STL
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
222 #endif // _WX_VECTOR_H_