]>
git.saurik.com Git - wxWidgets.git/blob - include/wx/vector.h
e3792543e6570d3a4dbf714da1c0698ab57d30a1
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 value_type
& reference
;
33 wxVector() : m_allocsize(16), m_size(0), m_capacity(0), m_objects(0) {}
35 wxVector(const wxVector
& c
)
37 wxCHECK2(Copy(c
), return);
47 for (size_type i
= 0; i
< size(); i
++)
51 m_size
= m_capacity
= 0;
54 void reserve(size_type n
)
58 wxFAIL_MSG( _T("out of memory in wxVector::reserve()") );
62 size_type
size() const
67 size_type
capacity() const
77 wxVector
& operator=(const wxVector
& vb
)
79 wxCHECK(Copy(vb
), *this);
83 void push_back(const value_type
& o
)
85 wxCHECK2(Alloc(size() + 1), return);
86 Append(new value_type(o
));
94 const value_type
& at(size_type idx
) const
96 wxASSERT(idx
< m_size
);
97 return *m_objects
[idx
];
100 value_type
& at(size_type idx
)
102 wxASSERT(idx
< m_size
);
103 return *m_objects
[idx
];
106 const value_type
& operator[](size_type idx
) const { return at(idx
); }
107 value_type
& operator[](size_type idx
) { return at(idx
); }
108 const value_type
& front() const { return at(0); }
109 value_type
& front() { return at(0); }
110 const value_type
& back() const { return at(size() - 1); }
111 value_type
& back() { return at(size() - 1); }
113 iterator
begin() { return m_objects
[0]; }
114 iterator
end() { return m_objects
[size()]; }
116 iterator
erase(iterator first
, iterator last
)
118 size_type idx
= first
- begin();
119 RemoveAt(idx
, last
- first
);
120 return begin() + idx
;
122 iterator
erase(iterator it
)
124 size_type idx
= it
- begin();
126 return begin() + idx
;
129 iterator
insert(iterator it
, const value_type
& v
= value_type())
131 wxCHECK2(Alloc(size() + 1), return 0);
132 size_type idx
= it
- begin();
133 InsertAt(new value_type(o
), idx
);
134 return begin() + idx
;
138 bool Alloc(size_type sz
)
140 // work in multiples of m_allocsize;
141 sz
= (sz
/ m_allocsize
+ 1) * m_allocsize
;
142 if (sz
<= m_capacity
)
146 void *mem
= realloc(m_objects
, sizeof(value_type
*) * sz
);
148 return false; // failed
150 m_objects
= (value_type
**) mem
;
155 void Append(value_type
*obj
)
157 wxASSERT(m_size
< m_capacity
);
158 m_objects
[m_size
] = obj
;
162 void InsertAt(size_type idx
, value_type
*obj
)
164 wxASSERT(idx
<= m_size
);
165 wxASSERT(m_size
< m_capacity
);
170 ( m_size
- idx
) * sizeof(value_type
*) );
175 void RemoveAt(size_type idx
)
177 wxASSERT(idx
< m_size
);
178 delete m_objects
[idx
];
179 if (idx
< m_size
- 1)
183 ( m_size
- idx
- 1 ) * sizeof(value_type
*) );
187 void RemoveAt(size_type idx
, size_type count
)
191 wxASSERT(idx
< m_size
);
193 for (i
= 0; i
< count
; i
++)
194 delete m_objects
[idx
+1];
195 if (idx
< m_size
- count
)
198 m_objects
+ idx
+ count
,
199 ( m_size
- idx
- count
) * sizeof(value_type
*) );
202 bool Copy(const wxVector
& vb
)
205 if (! Alloc(vb
.size()))
208 for (size_type i
= 0; i
< vb
.size(); i
++)
210 value_type
*o
= new value_type(vb
.at(i
));
220 size_type m_allocsize
;
223 value_type
**m_objects
;
226 #endif // wxUSE_STL/!wxUSE_STL
228 #if WXWIN_COMPATIBILITY_2_8
229 #define WX_DECLARE_VECTORBASE(obj, cls) typedef wxVector<obj> cls
230 #define _WX_DECLARE_VECTOR(obj, cls, exp) WX_DECLARE_VECTORBASE(obj, cls)
231 #define WX_DECLARE_VECTOR(obj, cls) WX_DECLARE_VECTORBASE(obj, cls)
232 #endif // WXWIN_COMPATIBILITY_2_8
234 #endif // _WX_VECTOR_H_