-
Notifications
You must be signed in to change notification settings - Fork 0
/
Vector.hpp
513 lines (437 loc) · 12.9 KB
/
Vector.hpp
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
#ifndef _KOUKAN_VECTOR_HPP_
#define _KOUKAN_VECTOR_HPP_
#include <type_traits>
#include <cstdlib>
#include <cstring>
#include <stdexcept>
#include <utility>
namespace koukan
{
template <typename T, int Increment = 1>
class Vector_iterator
{
public:
Vector_iterator() = default;
Vector_iterator(Vector_iterator const& other) = default;
Vector_iterator(Vector_iterator&& other) = default;
Vector_iterator(T* current);
~Vector_iterator() = default;
Vector_iterator& operator=(Vector_iterator const& other) = default;
Vector_iterator& operator=(Vector_iterator&& other) = default;
Vector_iterator& operator++() noexcept;
Vector_iterator operator++(int);
bool operator==(Vector_iterator const& other) const;
bool operator!=(Vector_iterator const& other) const;
T* operator->() const;
T& operator*() const;
T* current = nullptr;
};
template <typename T, int CapacityTick = 16>
class Vector
{
public:
using value_type = T;
using reference = value_type&;
using const_reference = const value_type&;
using iterator = Vector_iterator<T, 1>;
using const_iterator = Vector_iterator<T const, 1>;
using reverse_iterator = Vector_iterator<T, -1>;
using const_reverse_iterator = Vector_iterator<T const, -1>;
Vector() = default;
Vector(Vector const &other);
Vector(Vector &&other);
~Vector();
Vector& operator=(Vector const& other);
Vector& operator=(Vector&& other);
T& operator[](size_t pos) noexcept;
T const& operator[](size_t pos) const noexcept;
T& at(size_t pos);
T const& at(size_t pos) const;
T& front() noexcept;
T const& front() const noexcept;
T& back() noexcept;
T const& back() const noexcept;
void push_back(T const& element);
template <typename... Args>
void emplace_back(Args&&... args);
void pop_back();
inline iterator erase(iterator const& it);
void clear();
void resize(size_t size);
void reserve(size_t size);
size_t size() const noexcept;
bool empty() const noexcept;
T const* data() const noexcept;
iterator begin();
const_iterator begin() const;
const_iterator cbegin() const;
reverse_iterator rbegin();
const_reverse_iterator rbegin() const;
const_reverse_iterator crbegin() const;
iterator end();
const_iterator end() const;
const_iterator cend() const;
reverse_iterator rend();
const_reverse_iterator rend() const;
const_reverse_iterator crend() const;
private:
void _allocate();
void _deallocate();
template <typename A = T>
typename std::enable_if<std::is_pod<A>::value, void>::type _allocate(size_t size);
template <typename A = T>
typename std::enable_if<!std::is_pod<A>::value, void>::type _allocate(size_t size);
template <typename A = T>
typename std::enable_if<std::is_pod<A>::value, void>::type _copy(Vector const& other);
template <typename A = T>
typename std::enable_if<!std::is_pod<A>::value, void>::type _copy(Vector const& other);
template <typename A = T>
typename std::enable_if<std::is_pod<A>::value, void>::type _erase(size_t idx);
template <typename A = T>
typename std::enable_if<!std::is_pod<A>::value, void>::type _erase(size_t idx);
template <typename A = T>
typename std::enable_if<std::is_pod<A>::value, void>::type _clear();
template <typename A = T>
typename std::enable_if<!std::is_pod<A>::value, void>::type _clear();
size_t _capacity = 0;
size_t _size = 0;
T* _array = nullptr;
};
// Definitions
// Iterator
template <typename T, int Increment>
Vector_iterator<T, Increment>::Vector_iterator(T* current) : current(current)
{
}
template <typename T, int Increment>
Vector_iterator<T, Increment>& Vector_iterator<T, Increment>::operator++() noexcept
{
current += Increment;
return *this;
}
template <typename T, int Increment>
Vector_iterator<T, Increment> Vector_iterator<T, Increment>::operator++(int)
{
Vector_iterator other(*this);
current += Increment;
return other;
}
template <typename T, int Increment>
bool Vector_iterator<T, Increment>::operator==(Vector_iterator const& other) const
{
return current == other.current;
}
template <typename T, int Increment>
bool Vector_iterator<T, Increment>::operator!=(Vector_iterator const& other) const
{
return !(*this == other);
}
template <typename T, int Increment>
T* Vector_iterator<T, Increment>::operator->() const
{
return current;
}
template <typename T, int Increment>
T& Vector_iterator<T, Increment>::operator*() const
{
return *current;
}
// Vector
template <typename T, int CapacityTick>
Vector<T, CapacityTick>::Vector(Vector const &other)
: _capacity(other._capacity), _size(other._size), _array(reinterpret_cast<T*>(std::malloc(sizeof(T) * other._capacity)))
{
this->_copy(other);
}
template <typename T, int CapacityTick>
Vector<T, CapacityTick>::Vector(Vector&& other)
: _capacity(other._capacity), _size(other._size), _array(other._array)
{
other._capacity = 0;
other._size = 0;
other._array = nullptr;
}
template <typename T, int CapacityTick>
Vector<T, CapacityTick>::~Vector()
{
this->_clear();
std::free(_array);
}
template <typename T, int CapacityTick>
Vector<T, CapacityTick>& Vector<T, CapacityTick>::operator=(Vector const& other)
{
this->_clear();
if (_capacity != other._capacity)
{
_capacity = other._capacity;
std::free(_array);
_array = reinterpret_cast<T*>(std::malloc(sizeof(T) * _capacity));
}
_size = other._size;
std::memcpy(_array, other._array, _size * sizeof(T));
return *this;
}
template <typename T, int CapacityTick>
Vector<T, CapacityTick>& Vector<T, CapacityTick>::operator=(Vector&& other)
{
if (_array)
{
this->_clear();
std::free(_array);
}
_capacity = other._capacity;
_size = other._size;
_array = other._array;
other._capacity = 0;
other._size = 0;
other._array = nullptr;
return *this;
}
template <typename T, int CapacityTick>
T& Vector<T, CapacityTick>::operator[](size_t pos) noexcept
{
return _array[pos];
}
template <typename T, int CapacityTick>
T const& Vector<T, CapacityTick>::operator[](size_t pos) const noexcept
{
return _array[pos];
}
template <typename T, int CapacityTick>
T& Vector<T, CapacityTick>::at(size_t pos)
{
if (pos >= _size)
throw std::out_of_range("koukan::Vector : out of range");
return _array[pos];
}
template <typename T, int CapacityTick>
T const& Vector<T, CapacityTick>::at(size_t pos) const
{
if (pos >= _size)
throw std::out_of_range("koukan::Vector : out of range");
return _array[pos];
}
template <typename T, int CapacityTick>
T& Vector<T, CapacityTick>::front() noexcept
{
return *_array;
}
template <typename T, int CapacityTick>
T const& Vector<T, CapacityTick>::front() const noexcept
{
return *_array;
}
template <typename T, int CapacityTick>
T& Vector<T, CapacityTick>::back() noexcept
{
return _array[_size - 1];
}
template <typename T, int CapacityTick>
T const& Vector<T, CapacityTick>::back() const noexcept
{
return _array[_size - 1];
}
template <typename T, int CapacityTick>
void Vector<T, CapacityTick>::push_back(T const& element)
{
if (_capacity == _size)
this->_allocate();
new (&_array[_size]) T(element);
++_size;
}
template <typename T, int CapacityTick>
template <typename... Args>
void Vector<T, CapacityTick>::emplace_back(Args&&... args)
{
if (_capacity == _size)
this->_allocate();
new (&_array[_size]) T(std::forward<Args>(args)...);
++_size;
}
template <typename T, int CapacityTick>
void Vector<T, CapacityTick>::pop_back()
{
this->_erase(_size - 1);
}
template <typename T, int CapacityTick>
typename Vector<T, CapacityTick>::iterator Vector<T, CapacityTick>::erase(iterator const& it)
{
size_t idx = it.current - _array;
this->_erase(idx);
return iterator(&_array[idx]);
}
template <typename T, int CapacityTick>
void Vector<T, CapacityTick>::clear()
{
this->_clear();
_size = 0;
}
template <typename T, int CapacityTick>
void Vector<T, CapacityTick>::resize(size_t size)
{
this->reserve(size);
_size = size;
}
template <typename T, int CapacityTick>
void Vector<T, CapacityTick>::reserve(size_t size)
{
if (size < _capacity)
return;
if (size % CapacityTick)
size = size + CapacityTick - (size % CapacityTick);
this->_allocate(size);
}
template <typename T, int CapacityTick>
size_t Vector<T, CapacityTick>::size() const noexcept
{
return _size;
}
template <typename T, int CapacityTick>
bool Vector<T, CapacityTick>::empty() const noexcept
{
return _size == 0;
}
template <typename T, int CapacityTick>
T const* Vector<T, CapacityTick>::data() const noexcept
{
return _array;
}
template <typename T, int CapacityTick>
typename Vector<T, CapacityTick>::iterator Vector<T, CapacityTick>::begin()
{
return iterator(_array);
}
template <typename T, int CapacityTick>
typename Vector<T, CapacityTick>::const_iterator Vector<T, CapacityTick>::begin() const
{
return const_iterator(_array);
}
template <typename T, int CapacityTick>
typename Vector<T, CapacityTick>::const_iterator Vector<T, CapacityTick>::cbegin() const
{
return const_iterator(_array);
}
template <typename T, int CapacityTick>
typename Vector<T, CapacityTick>::reverse_iterator Vector<T, CapacityTick>::rbegin()
{
return reverse_iterator(&_array[_size - 1]);
}
template <typename T, int CapacityTick>
typename Vector<T, CapacityTick>::const_reverse_iterator Vector<T, CapacityTick>::rbegin() const
{
return const_reverse_iterator(&_array[_size - 1]);
}
template <typename T, int CapacityTick>
typename Vector<T, CapacityTick>::const_reverse_iterator Vector<T, CapacityTick>::crbegin() const
{
return const_reverse_iterator(&_array[_size - 1]);
}
template <typename T, int CapacityTick>
typename Vector<T, CapacityTick>::iterator Vector<T, CapacityTick>::end()
{
return iterator(&_array[_size]);
}
template <typename T, int CapacityTick>
typename Vector<T, CapacityTick>::const_iterator Vector<T, CapacityTick>::end() const
{
return const_iterator(&_array[_size]);
}
template <typename T, int CapacityTick>
typename Vector<T, CapacityTick>::const_iterator Vector<T, CapacityTick>::cend() const
{
return const_iterator(&_array[_size]);
}
template <typename T, int CapacityTick>
typename Vector<T, CapacityTick>::reverse_iterator Vector<T, CapacityTick>::rend()
{
return reverse_iterator(_array - 1);
}
template <typename T, int CapacityTick>
typename Vector<T, CapacityTick>::const_reverse_iterator Vector<T, CapacityTick>::rend() const
{
return const_ireverse_iterator(_array - 1);
}
template <typename T, int CapacityTick>
typename Vector<T, CapacityTick>::const_reverse_iterator Vector<T, CapacityTick>::crend() const
{
return const_reverse_iterator(_array - 1);
}
// Private
template <typename T, int CapacityTick>
template <typename A>
typename std::enable_if<std::is_pod<A>::value, void>::type Vector<T, CapacityTick>::_allocate(size_t size)
{
_capacity = size;
_array = reinterpret_cast<T*>(std::realloc(_array, sizeof(T) * _capacity));
}
template <typename T, int CapacityTick>
template <typename A>
typename std::enable_if<!std::is_pod<A>::value, void>::type Vector<T, CapacityTick>::_allocate(size_t size)
{
_capacity = size;
auto tmp = reinterpret_cast<T*>(std::malloc(sizeof(T) * _capacity));
for (size_t i = 0; i < _size; ++i)
new (&tmp[i]) T(std::move(_array[i]));
std::free(_array);
_array = tmp;
}
template <typename T, int CapacityTick>
void Vector<T, CapacityTick>::_allocate()
{
this->_allocate(_capacity + CapacityTick);
}
template <typename T, int CapacityTick>
void Vector<T, CapacityTick>::_deallocate()
{
this->_allocate(_capacity - CapacityTick);
}
template <typename T, int CapacityTick>
template <typename A>
typename std::enable_if<std::is_pod<A>::value, void>::type Vector<T, CapacityTick>::_copy(Vector const& other)
{
std::memcpy(_array, other._array, sizeof(T) * other._size);
}
template <typename T, int CapacityTick>
template <typename A>
typename std::enable_if<!std::is_pod<A>::value, void>::type Vector<T, CapacityTick>::_copy(Vector const& other)
{
for (size_t i = 0; i < _size; ++i)
new (&_array[i]) T(other._array[i]);
}
template <typename T, int CapacityTick>
template <typename A>
typename std::enable_if<std::is_pod<A>::value, void>::type Vector<T, CapacityTick>::_erase(size_t idx)
{
--_size;
size_t size = _size - idx;
if (size > 0)
std::memmove(&_array[idx], &_array[idx + 1], size * sizeof(T));
if ((_capacity - _size) > CapacityTick)
this->_deallocate();
}
template <typename T, int CapacityTick>
template <typename A>
typename std::enable_if<!std::is_pod<A>::value, void>::type Vector<T, CapacityTick>::_erase(size_t idx)
{
_array[idx].~T();
T* elem = &_array[idx];
for (idx = idx + 1; idx < _size; ++idx, ++elem)
new (elem) T(std::move(_array[idx]));
--_size;
if ((_capacity - _size) > CapacityTick)
this->_deallocate();
}
template <typename T, int CapacityTick>
template <typename A>
typename std::enable_if<std::is_pod<A>::value, void>::type Vector<T, CapacityTick>::_clear()
{
}
template <typename T, int CapacityTick>
template <typename A>
typename std::enable_if<!std::is_pod<A>::value, void>::type Vector<T, CapacityTick>::_clear()
{
for (size_t i = 0; i < _size; ++i)
_array[i].~T();
}
}
#endif // _KOUKAN_VECTOR_HPP_