]>
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
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
;
68 if ( m_capacity
+ increment
> n
)
69 n
= m_capacity
+ increment
;
71 value_type
*mem
= new value_type
[n
];
75 for ( size_type i
= 0; i
< m_size
; ++i
)
84 size_type
size() const
89 size_type
capacity() const
99 wxVector
& operator=(const wxVector
& vb
)
105 void push_back(const value_type
& v
)
108 m_values
[m_size
++] = v
;
116 const value_type
& at(size_type idx
) const
118 wxASSERT(idx
< m_size
);
119 return m_values
[idx
];
122 value_type
& at(size_type idx
)
124 wxASSERT(idx
< m_size
);
125 return m_values
[idx
];
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); }
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(); }
140 iterator
insert(iterator it
, const value_type
& v
= value_type())
142 size_t idx
= it
- begin();
146 // unless we're inserting at the end, move following values out of
148 for ( size_t n
= m_size
; n
!= idx
; --n
)
149 m_values
[n
] = m_values
[n
-1];
154 return begin() + idx
;
157 iterator
erase(iterator it
)
159 return erase(it
, it
+ 1);
162 iterator
erase(iterator first
, iterator last
)
166 wxASSERT( first
< end() && last
<= end() );
168 size_type index
= first
- begin();
169 size_type count
= last
- first
;
171 // move the remaining values over to the freed space:
172 for ( iterator i
= last
; i
< end(); ++i
)
175 // erase items behind the new end of m_values:
176 for ( iterator j
= end() - count
; j
< end(); ++j
)
181 return begin() + index
;
184 #if WXWIN_COMPATIBILITY_2_8
185 wxDEPRECATED( size_type
erase(size_type n
) );
186 #endif // WXWIN_COMPATIBILITY_2_8
189 // VC6 can't compile static const int members
190 enum { ALLOC_INITIAL_SIZE
= 16 };
191 enum { ALLOC_MAX_SIZE
= 4096 };
193 void Copy(const wxVector
& vb
)
198 for ( const_iterator i
= vb
.begin(); i
!= vb
.end(); ++i
)
205 value_type
*m_values
;
208 #if WXWIN_COMPATIBILITY_2_8
210 inline typename wxVector
<T
>::size_type wxVector
<T
>::erase(size_type n
)
215 #endif // WXWIN_COMPATIBILITY_2_8
217 #endif // wxUSE_STL/!wxUSE_STL
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
225 #endif // _WX_VECTOR_H_