forked from bkaradzic/bx
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvector.h
322 lines (258 loc) · 8.9 KB
/
vector.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
/*-
* Copyright 2012-1015 Matthew Endsley
* All rights reserved
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted providing that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef TINYSTL_VECTOR_H
#define TINYSTL_VECTOR_H
#include "buffer.h"
#include "new.h"
#include "stddef.h"
namespace tinystl {
template<typename T, typename Alloc = TINYSTL_ALLOCATOR>
class vector {
public:
vector();
vector(const vector& other);
vector(size_t _size);
vector(size_t _size, const T& value);
vector(const T* first, const T* last);
~vector();
vector& operator=(const vector& other);
void assign(const T* first, const T* last);
const T* data() const;
T* data();
size_t size() const;
size_t capacity() const;
bool empty() const;
T& operator[](size_t idx);
const T& operator[](size_t idx) const;
const T& front() const;
T& front();
const T& back() const;
T& back();
void resize(size_t size);
void resize(size_t size, const T& value);
void clear();
void reserve(size_t _capacity);
void push_back(const T& t);
void pop_back();
void emplace_back();
template<typename Param>
void emplace_back(const Param& param);
void shrink_to_fit();
void swap(vector& other);
typedef T value_type;
typedef T* iterator;
iterator begin();
iterator end();
typedef const T* const_iterator;
const_iterator begin() const;
const_iterator end() const;
void insert(iterator where);
void insert(iterator where, const T& value);
void insert(iterator where, const T* first, const T* last);
template<typename Param>
void emplace(iterator where, const Param& param);
iterator erase(iterator where);
iterator erase(iterator first, iterator last);
iterator erase_unordered(iterator where);
iterator erase_unordered(iterator first, iterator last);
private:
buffer<T, Alloc> m_buffer;
};
template<typename T, typename Alloc>
inline vector<T, Alloc>::vector() {
buffer_init(&m_buffer);
}
template<typename T, typename Alloc>
inline vector<T, Alloc>::vector(const vector& other) {
buffer_init(&m_buffer);
buffer_reserve(&m_buffer, other.size());
buffer_insert(&m_buffer, m_buffer.last, other.m_buffer.first, other.m_buffer.last);
}
template<typename T, typename Alloc>
inline vector<T, Alloc>::vector(size_t _size) {
buffer_init(&m_buffer);
buffer_resize(&m_buffer, _size);
}
template<typename T, typename Alloc>
inline vector<T, Alloc>::vector(size_t _size, const T& value) {
buffer_init(&m_buffer);
buffer_resize(&m_buffer, _size, value);
}
template<typename T, typename Alloc>
inline vector<T, Alloc>::vector(const T* first, const T* last) {
buffer_init(&m_buffer);
buffer_insert(&m_buffer, m_buffer.last, first, last);
}
template<typename T, typename Alloc>
inline vector<T, Alloc>::~vector() {
buffer_destroy(&m_buffer);
}
template<typename T, typename Alloc>
inline vector<T, Alloc>& vector<T, Alloc>::operator=(const vector& other) {
vector(other).swap(*this);
return *this;
}
template<typename T, typename Alloc>
inline void vector<T, Alloc>::assign(const T* first, const T* last) {
buffer_clear(&m_buffer);
buffer_insert(&m_buffer, m_buffer.last, first, last);
}
template<typename T, typename Alloc>
inline const T* vector<T, Alloc>::data() const {
return m_buffer.first;
}
template<typename T, typename Alloc>
inline T* vector<T, Alloc>::data() {
return m_buffer.first;
}
template<typename T, typename Alloc>
inline size_t vector<T, Alloc>::size() const {
return (size_t)(m_buffer.last - m_buffer.first);
}
template<typename T, typename Alloc>
inline size_t vector<T, Alloc>::capacity() const {
return (size_t)(m_buffer.capacity - m_buffer.first);
}
template<typename T, typename Alloc>
inline bool vector<T, Alloc>::empty() const {
return m_buffer.last == m_buffer.first;
}
template<typename T, typename Alloc>
inline T& vector<T, Alloc>::operator[](size_t idx) {
return m_buffer.first[idx];
}
template<typename T, typename Alloc>
inline const T& vector<T, Alloc>::operator[](size_t idx) const {
return m_buffer.first[idx];
}
template<typename T, typename Alloc>
inline const T& vector<T, Alloc>::front() const {
return m_buffer.first[0];
}
template<typename T, typename Alloc>
inline T& vector<T, Alloc>::front() {
return m_buffer.first[0];
}
template<typename T, typename Alloc>
inline const T& vector<T, Alloc>::back() const {
return m_buffer.last[-1];
}
template<typename T, typename Alloc>
inline T& vector<T, Alloc>::back() {
return m_buffer.last[-1];
}
template<typename T, typename Alloc>
inline void vector<T, Alloc>::resize(size_t _size) {
buffer_resize(&m_buffer, _size);
}
template<typename T, typename Alloc>
inline void vector<T, Alloc>::resize(size_t _size, const T& value) {
buffer_resize(&m_buffer, _size, value);
}
template<typename T, typename Alloc>
inline void vector<T, Alloc>::clear() {
buffer_clear(&m_buffer);
}
template<typename T, typename Alloc>
inline void vector<T, Alloc>::reserve(size_t _capacity) {
buffer_reserve(&m_buffer, _capacity);
}
template<typename T, typename Alloc>
inline void vector<T, Alloc>::push_back(const T& t) {
buffer_append(&m_buffer, &t);
}
template<typename T, typename Alloc>
inline void vector<T, Alloc>::emplace_back() {
buffer_append(&m_buffer);
}
template<typename T, typename Alloc>
template<typename Param>
inline void vector<T, Alloc>::emplace_back(const Param& param) {
buffer_append(&m_buffer, ¶m);
}
template<typename T, typename Alloc>
inline void vector<T, Alloc>::pop_back() {
buffer_erase(&m_buffer, m_buffer.last - 1, m_buffer.last);
}
template<typename T, typename Alloc>
inline void vector<T, Alloc>::shrink_to_fit() {
buffer_shrink_to_fit(&m_buffer);
}
template<typename T, typename Alloc>
inline void vector<T, Alloc>::swap(vector& other) {
buffer_swap(&m_buffer, &other.m_buffer);
}
template<typename T, typename Alloc>
inline typename vector<T, Alloc>::iterator vector<T,Alloc>::begin() {
return m_buffer.first;
}
template<typename T, typename Alloc>
inline typename vector<T, Alloc>::iterator vector<T,Alloc>::end() {
return m_buffer.last;
}
template<typename T, typename Alloc>
inline typename vector<T, Alloc>::const_iterator vector<T,Alloc>::begin() const {
return m_buffer.first;
}
template<typename T, typename Alloc>
inline typename vector<T, Alloc>::const_iterator vector<T,Alloc>::end() const {
return m_buffer.last;
}
template<typename T, typename Alloc>
inline void vector<T, Alloc>::insert(iterator where) {
buffer_insert(&m_buffer, where, 1);
}
template<typename T, typename Alloc>
inline void vector<T, Alloc>::insert(iterator where, const T& value) {
buffer_insert(&m_buffer, where, &value, &value + 1);
}
template<typename T, typename Alloc>
inline void vector<T, Alloc>::insert(iterator where, const T* first, const T* last) {
buffer_insert(&m_buffer, where, first, last);
}
template<typename T, typename Alloc>
inline typename vector<T, Alloc>::iterator vector<T, Alloc>::erase(iterator where) {
return buffer_erase(&m_buffer, where, where + 1);
}
template<typename T, typename Alloc>
inline typename vector<T, Alloc>::iterator vector<T, Alloc>::erase(iterator first, iterator last) {
return buffer_erase(&m_buffer, first, last);
}
template<typename T, typename Alloc>
inline typename vector<T, Alloc>::iterator vector<T, Alloc>::erase_unordered(iterator where) {
return buffer_erase_unordered(&m_buffer, where, where + 1);
}
template<typename T, typename Alloc>
inline typename vector<T, Alloc>::iterator vector<T, Alloc>::erase_unordered(iterator first, iterator last) {
return buffer_erase_unordered(&m_buffer, first, last);
}
template<typename T, typename Alloc>
template<typename Param>
void vector<T, Alloc>::emplace(iterator where, const Param& param) {
buffer_insert(&m_buffer, where, ¶m, ¶m + 1);
}
}
#endif