-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvector.hpp
535 lines (460 loc) · 12.5 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
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
#pragma once
#include <cmath>
#include <iostream>
#include <iterator>
#include <algorithm>
#include <stdexcept>
#include <initializer_list>
#include "allocator.hpp"
#include "iterator.hpp"
namespace ctl {
/**
* ctl::Vector Definition
*/
template<typename T, class A = Allocator<T>>
class Vector
{
public:
using value_type = T;
using allocator_type = A;
using size_type = typename A::size_type;
using difference_type = typename A::difference_type;
using reference = typename A::reference;
using const_reference = typename A::const_reference;
using pointer = typename A::pointer;
using const_pointer = typename A::const_pointer;
using iterator = ctl::Iterator<T>;
using const_iterator = ctl::Iterator<const T>;
Vector() {};
Vector(const Vector<T>&);
Vector(size_type);
Vector(size_type, const_reference);
Vector(std::initializer_list<T>);
template<typename IteratorType, class = typename std::enable_if< !std::is_integral<IteratorType>::value >::type>
Vector(IteratorType, IteratorType);
virtual ~Vector();
Vector<T, A>& operator=(const Vector<T, A>&);
Vector<T, A>& operator=(Vector<T, A>&&);
Vector<T, A>& operator=(std::initializer_list<T>);
iterator begin() noexcept;
iterator end() noexcept;
const_iterator cbegin() const noexcept;
const_iterator cend() const noexcept;
size_type size() const noexcept;
size_type max_size() const noexcept;
void resize(size_type);
void resize(size_type, const_reference);
size_type capacity() const noexcept;
bool empty() const noexcept;
void reserve(size_type);
void shrink_to_fit();
reference at(size_type);
reference operator[](size_type) const;
reference front();
reference back();
pointer data() noexcept;
void assign(size_type, const_reference);
void assign(std::initializer_list<T>);
template<typename IteratorType, class = typename std::enable_if< !std::is_integral<IteratorType>::value >::type>
void assign(IteratorType, IteratorType);
void push_back(const_reference);
void push_back(value_type&&);
void pop_back();
iterator insert(iterator, const_reference);
iterator insert(iterator, size_type, const_reference);
iterator insert(iterator, std::initializer_list<T>);
template<typename IteratorType, class = typename std::enable_if< !std::is_integral<IteratorType>::value >::type>
iterator insert(iterator, IteratorType, IteratorType);
iterator erase(iterator);
iterator erase(iterator, iterator);
void swap(Vector<T, A>&);
void clear() noexcept;
template<class... Args>
iterator emplace(iterator, Args&&...);
template<class... Args>
iterator emplace_back(Args&&...);
A get_allocator() const noexcept;
private:
const float _growthFactor = 1.5f;
A _allocator;
pointer _begin = nullptr;
pointer _last = nullptr;
pointer _end = nullptr;
void reallocate(size_type s);
void initialize(iterator, iterator);
void destroy(iterator, iterator);
};
/**
* ctl::Vector Implementation
*/
template<typename T, typename A>
Vector<T, A>::Vector(const Vector<T>& other)
{
reallocate(other.size());
for (size_type i = 0; i < other.size(); ++i) {
_allocator.construct(_begin + i, other[i]);
}
}
template<typename T, typename A>
Vector<T, A>::Vector(size_type count)
{
reallocate(count);
_last = _begin + count;
initialize(begin(), end());
}
template<typename T, typename A>
Vector<T, A>::Vector(size_type count, const_reference value)
{
assign(count, value);
}
template<typename T, typename A>
Vector<T, A>::Vector(std::initializer_list<T> list)
{
assign(list.begin(), list.end());
}
template<typename T, typename A>
template<typename IteratorType, typename isIterator>
Vector<T, A>::Vector(IteratorType first, IteratorType last)
{
assign(first, last);
}
template<typename T, typename A>
Vector<T, A>::~Vector()
{
destroy(begin(), end());
_allocator.deallocate(_begin, capacity());
}
template<typename T, typename A>
Vector<T, A>& Vector<T, A>::operator=(const Vector<T, A>& other)
{
if (this == &other) return *this;
erase(begin(), end());
if (other.size() > capacity()) {
reallocate(other.size());
}
for (size_type i = 0; i < other.size(); ++i) {
_allocator.construct(_begin + i, other[i]);
}
_last = _begin + other.size();
return *this;
}
template<typename T, typename A>
Vector<T, A>& Vector<T, A>::operator=(Vector<T, A>&& other)
{
if (this == &other) return *this;
clear();
swap(other);
return *this;
}
template<typename T, typename A>
Vector<T, A>& Vector<T, A>::operator=(std::initializer_list<T> other)
{
assign(other.begin(), other.end());
return *this;
}
template<typename T, typename A>
typename Vector<T, A>::iterator Vector<T, A>::begin() noexcept
{
return iterator(_begin);
}
template<typename T, typename A>
typename Vector<T, A>::iterator Vector<T, A>::end() noexcept
{
return iterator(_last);
}
template<typename T, typename A>
typename Vector<T, A>::const_iterator Vector<T, A>::cbegin() const noexcept
{
return const_iterator(_begin);
}
template<typename T, typename A>
typename Vector<T, A>::const_iterator Vector<T, A>::cend() const noexcept
{
return const_iterator(_last);
}
template<typename T, typename A>
void Vector<T, A>::assign(size_type n, const_reference value)
{
erase(begin(), end());
if (n > capacity()) {
reallocate(n);
}
for (size_type i = 0; i < n; ++i) {
_allocator.construct(_begin + i, value);
}
_last = _begin + n;
}
template<typename T, typename A>
void Vector<T, A>::assign(std::initializer_list<T> il)
{
assign(il.begin(), il.end());
}
template<typename T, typename A>
void Vector<T, A>::push_back(const_reference value)
{
if (size() + 1 >= capacity()) {
reallocate(size() + 1);
}
_allocator.construct(_last++, T(value));
}
template<typename T, typename A>
void Vector<T, A>::push_back(value_type&& value)
{
if (size() + 1 >= capacity()) {
reallocate(size() + 1);
}
std::swap(*(_last++), value);
}
template<typename T, typename A>
void Vector<T, A>::pop_back()
{
_allocator.destroy(_last);
--_last;
}
template<typename T, typename A>
typename Vector<T, A>::iterator Vector<T, A>::insert(iterator it, const_reference value)
{
size_type index = it - begin();
if (size() + 1 > capacity()) {
reallocate(size() + 1);
}
it = begin() + index;
std::copy_backward(it, end(), it + size() - index + 1);
*it = value;
++_last;
return it;
}
template<typename T, typename A>
typename Vector<T, A>::iterator Vector<T, A>::insert(iterator it, size_type count, const_reference value)
{
size_type newSize = size() + count;
size_type index = it - begin();
if (newSize > capacity()) {
reallocate(newSize);
}
it = begin() + index;
std::copy_backward(it, end(), end() + count);
std::fill(it, it + count, value);
_last = _begin + newSize;
return it;
}
template<typename T, typename A>
typename Vector<T, A>::iterator Vector<T, A>::insert(iterator it, std::initializer_list<T> il)
{
return insert(it, il.begin(), il.end());
}
template<typename T, typename A>
template<typename IteratorType, typename isIterator>
typename Vector<T, A>::iterator Vector<T, A>::insert(iterator from, IteratorType first, IteratorType last)
{
difference_type count = std::distance(first, last);
size_type newSize = size() + count;
size_type index = from - begin();
if (newSize > capacity()) {
reallocate(newSize);
}
from = begin() + index;
std::copy_backward(from, end(), from + count + size() - index);
std::copy(first, last, from);
_last = _begin + newSize;
return from;
}
template<typename T, typename A>
typename Vector<T, A>::iterator Vector<T, A>::erase(iterator it)
{
_allocator.destroy(&*it);
std::copy(it + 1, end(), it);
--_last;
return it;
}
template<typename T, typename A>
typename Vector<T, A>::iterator Vector<T, A>::erase(iterator first, iterator last)
{
for (auto it = first; it != last; ++it) {
_allocator.destroy(&*it);
}
if (end() > last + 1) {
std::copy(last + 1, end(), first + 1);
}
_last -= std::distance(first, last);
return first;
}
template<typename T, typename A>
void Vector<T, A>::swap(Vector<T, A>& other)
{
std::swap(_begin, other._begin);
std::swap(_last, other._last);
std::swap(_end, other._end);
std::swap(_allocator, other._allocator);
}
template<typename T, typename A>
void Vector<T, A>::clear() noexcept
{
_allocator.deallocate(_begin, capacity());
_begin = _last = _end = nullptr;
}
template<typename T, typename A>
typename Vector<T, A>::allocator_type Vector<T, A>::get_allocator() const noexcept
{
return _allocator;
}
template<typename T, typename A>
void Vector<T, A>::resize(size_type newSize)
{
size_type index = size();
if (newSize > capacity()) {
reallocate(newSize);
}
_last = _begin + newSize;
if (index < size()) {
initialize(begin() + index, end());
} else {
destroy(end(), begin() + index);
}
}
template<typename T, typename A>
void Vector<T, A>::resize(size_type newSize, const_reference val)
{
erase(begin(), end());
assign(newSize, val);
}
template<typename T, typename A>
void Vector<T, A>::reserve(size_type newCapacity)
{
if (newCapacity > max_size()) {
throw std::length_error("ctl::Vector: too big capacity to reserve");
}
if (newCapacity > capacity()) {
reallocate(newCapacity);
}
}
template<typename T, typename A>
void Vector<T, A>::shrink_to_fit()
{
reallocate( size() );
}
template<typename T, typename A>
inline typename Vector<T, A>::size_type Vector<T, A>::capacity() const noexcept
{
return _end - _begin;
}
template<typename T, typename A>
inline typename Vector<T, A>::size_type Vector<T, A>::size() const noexcept
{
return _last - _begin;
}
template<typename T, typename A>
typename Vector<T, A>::size_type Vector<T, A>::max_size() const noexcept
{
return static_cast<size_type>(-1 / sizeof(T));
}
template<typename T, typename A>
inline bool Vector<T, A>::empty() const noexcept
{
return size() == 0;
}
template<typename T, typename A>
inline typename Vector<T, A>::reference Vector<T, A>::at(size_type i)
{
if (i >= size()) {
throw std::out_of_range("ctl::Vector: out of range");
}
return _begin[i];
}
template<typename T, typename A>
inline typename Vector<T, A>::reference Vector<T, A>::operator[](size_type i) const
{
return _begin[i];
}
template<typename T, typename A>
typename Vector<T, A>::reference Vector<T, A>::front()
{
return *(begin());
}
template<typename T, typename A>
typename Vector<T, A>::reference Vector<T, A>::back()
{
return *(--end());
}
template<typename T, typename A>
typename Vector<T, A>::pointer Vector<T, A>::data() noexcept
{
return _begin;
}
template<typename T, typename A>
void Vector<T, A>::reallocate(size_type newCapacity)
{
if (newCapacity >= capacity()) {
newCapacity *= _growthFactor;
}
pointer newBegin = _allocator.allocate(newCapacity);
if (newBegin == _begin) return;
if (_begin) {
try {
std::move(
_begin,
_begin + std::min(size(), newCapacity),
newBegin
);
} catch (...) {
_allocator.deallocate(newBegin, newCapacity);
throw;
}
_allocator.deallocate(_begin, capacity());
}
_last = newBegin + size();
_begin = newBegin;
_end = newBegin + newCapacity;
}
template<typename T, typename A>
void Vector<T, A>::initialize(iterator first, iterator last)
{
for (auto it = first; it != last; ++it) {
_allocator.construct(&*it, value_type());
}
}
template<typename T, typename A>
void Vector<T, A>::destroy(iterator first, iterator last)
{
for (auto it = first; it != last; ++it) {
_allocator.destroy(&*it);
}
}
template<typename T, typename A>
template<class... Args>
typename Vector<T, A>::iterator Vector<T, A>::emplace(iterator it, Args&&... args)
{
size_type index = it - begin();
if (size() + 1 > capacity()) {
reallocate(size() + 1);
}
it = begin() + index;
std::move_backward(
it,
end(),
it + size() - index + 1
);
_allocator.construct(&*it, std::forward<Args>(args)...);
return it;
}
template<typename T, typename A>
template<class... Args>
typename Vector<T, A>::iterator Vector<T, A>::emplace_back(Args&&... args)
{
return emplace(end(), std::forward<Args>(args)...);
}
template<typename T, typename A>
template<typename IteratorType, typename isIterator>
void Vector<T, A>::assign(IteratorType first, IteratorType last)
{
erase(begin(), end());
typename std::iterator_traits<IteratorType>::difference_type count = std::distance(first, last);
if (capacity() < count) {
reallocate(count);
}
for (auto it = begin(); first != last; ++it, ++first) {
_allocator.construct(&*it, value_type(*first));
}
_last = _begin + count;
}
} // namespace ctl