-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvector.h
More file actions
454 lines (377 loc) · 11.5 KB
/
vector.h
File metadata and controls
454 lines (377 loc) · 11.5 KB
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
#pragma once
#include "iterator.h"
#include <allocators>
#include <exception>
#include <initializer_list>
template<class T, class Allocator = std::allocator<T>>
class Vector {
public:
//Member types
using value_type = T;
using allocator_type = Allocator;
using size_type = std::size_t;
using differnce_type = std::ptrdiff_t;
using reference = T&;
using const_reference = const T&;
using pointer = typename std::allocator_traits<Allocator>::pointer;
using const_pointer = typename std::allocator_traits<Allocator>::const_pointer;
using iterator = typename Iterator<T>;
using const_iterator = typename const Iterator<T>;
using reverse_iterator = typename std::reverse_iterator<iterator>;
using const_reverse_iterator = typename const std::reverse_iterator<iterator>;
//Constructors
Vector() noexcept(noexcept(Allocator()))
: _size(0),
_capacity(_size),
_alloc(Allocator()),
_ptr(std::allocator_traits<Allocator>::allocate(_alloc, _capacity)) {}
explicit Vector(const Allocator& alloc) noexcept
: _size(0),
_capacity(_size),
_alloc(alloc),
_ptr(std::allocator_traits<Allocator>::allocate(_alloc, _capacity)) {}
explicit Vector(size_type count, const T& value, const Allocator& alloc = Allocator())
: _size(count),
_capacity(_size),
_alloc(alloc),
_ptr(std::allocator_traits<Allocator>::allocate(_alloc, _capacity)) {
for (size_type i = 0; i < count; i++)
std::allocator_traits<Allocator>::construct(_alloc, _ptr + i, value);
}
explicit Vector(size_type count, const Allocator& alloc = Allocator())
: _size(count),
_capacity(_size),
_alloc(alloc),
_ptr(std::allocator_traits<Allocator>::allocate(_alloc, _capacity)) {
for (size_type i = 0; i < count; i++)
std::allocator_traits<Allocator>::construct(_alloc, _ptr + i);
}
template<class InputIt, class = typename std::enable_if<!std::is_integral<InputIt>::value>::type>
Vector(InputIt first, InputIt last, const Allocator& alloc = Allocator())
: _size(std::distance(first, last)),
_capacity(_size),
_alloc(alloc),
_ptr(std::allocator_traits<Allocator>::allocate(_alloc, _capacity)) {
for (auto it = first; it != last; it++)
std::allocator_traits<Allocator>::construct(_alloc, _ptr + (it - first), *it);
}
Vector(const Vector& other)
: _size(other._size),
_capacity(other._capacity),
_alloc(other._alloc),
_ptr(std::allocator_traits<Allocator>::allocate(_alloc, _capacity)) {
for (size_type i = 0; i < other._size; i++)
std::allocator_traits<Allocator>::construct(_alloc, _ptr + i, other[i]);
}
Vector(const Vector& other, const Allocator& )
: _size(other._size),
_capacity(other._capacity),
_alloc(alloc),
_ptr(std::allocator_traits<Allocator>::allocate(_alloc, _capacity)) {
for (size_type i = 0; i < other._size; i++)
std::allocator_traits<Allocator>::construct(_alloc, _ptr + i, other[i]);
}
Vector(const Vector&& other) noexcept
: _size(other._size),
_capacity(other._capacity),
_alloc(other._alloc),
_ptr(other._ptr) {}
Vector(const Vector&& other, const Allocator& alloc) noexcept
: _size(other._size),
_capacity(other._capacity),
_alloc(alloc),
_ptr(other._ptr) {}
Vector(std::initializer_list<T> init, const Allocator& alloc = Allocator())
: Vector(init.begin(), init.end(), alloc) {}
//operator= and assign
Vector& operator=(const Vector& other) {
clear();
_size = other._size;
_capacity = other._capacity;
_alloc = other._alloc;
_ptr = other._ptr;
return *this;
}
Vector& operator=(Vector&& other) noexcept {
clear();
swap(other);
return *this;
}
Vector& operator=(std::initializer_list<T> ilist) {
return operator=(Vector(ilist));
}
void assign(size_type count, const T& value) {
clear();
insert(begin(), count, value);
}
template<class InputIt, class = typename std::enable_if<!std::is_integral<InputIt>::value>::type>
void assign(InputIt first, InputIt last) {
clear();
insert(begin(), first, last);
}
void assign(std::initializer_list<T> ilist) {
clear();
insert(begin(), ilist);
}
allocator_type getAllocator() const {
return _alloc;
}
//Element access
reference at(size_type pos) {
if (pos >= _size)
throw std::out_of_range("Vector subscript out of range");
return _ptr[pos];
}
const_reference at(size_type pos) const {
return _ptr[pos];
}
reference operator[](size_type pos) {
return _ptr[pos];
}
const_reference operator[](size_type pos) const {
return _ptr[pos];
}
reference front() {
return _ptr[0];
}
const_reference front() const {
return _ptr[0];
}
reference back() {
return _ptr[_size - 1];
}
const_reference back() const {
return _ptr[_size - 1];
}
T* data() noexcept {
return _ptr;
}
const T* data() const noexcept {
return _ptr;
}
//Iterators
iterator begin() noexcept {
return iterator(_ptr);
}
const_iterator begin() const noexcept {
return iterator(_ptr);
}
const_iterator cbegin() noexcept {
return const_iterator(_ptr);
}
iterator end() noexcept {
return iterator(_ptr + _size);
}
const_iterator end() const noexcept {
return iterator(_ptr + _size);
}
const_iterator cend() noexcept {
return const_iterator(_ptr + _size);
}
reverse_iterator rbegin() noexcept {
return reverse_iterator(_ptr + _size);
}
const_reverse_iterator rbegin() const noexcept {
return const_reverse_iterator(_ptr + _size);
}
const_reverse_iterator crbegin() noexcept {
return const_reverse_iterator(_ptr + _size);
}
reverse_iterator rend() noexcept {
return reverse_iterator(_ptr);
}
const_reverse_iterator rend() const noexcept {
return const_reverse_iterator(_ptr);
}
const_reverse_iterator crend() noexcept {
return const_reverse_iterator(_ptr);
}
//Capacity
bool empty() const noexcept {
return !(_size);
}
size_type size() const noexcept {
return _size;
}
size_type max_size() const noexcept {
return std::numeric_limits<size_type>::max() / sizeof(value_type);
}
void reserve(size_type new_cap) {
if (new_cap <= _capacity)
return;
try {
reallocate(new_cap);
}
catch (std::exception&) {
throw;
}
}
size_type capacity() const noexcept {
return _capacity;
}
void shrink_to_fit() {
try {
reallocate(_size);
}
catch (std::exception& ex) {
throw ex;
}
}
//Modifiers
void clear() noexcept {
for (auto it = begin(); it != end(); it++)
std::allocator_traits<Allocator>::destroy(_alloc, _ptr + (it - begin()));
_size = 0;
}
iterator insert(const_iterator pos, const T& value) {
return insert(pos, 1, value);
}
iterator insert(const_iterator pos, T&& value) {
return insert(pos, value);
}
iterator insert(const_iterator pos, size_type count, const T& value) {
size_type new_size = _size + count;
size_type index = pos - begin();
if (new_size > _capacity)
reallocate(new_size);
auto insert_iter = begin() + index;
std::copy_backward(insert_iter, end(), begin() + new_size);
std::fill(insert_iter, insert_iter + count, value);
_capacity = new_size;
_size = new_size;
return insert_iter;
}
template< class InputIt >
void insert(const_iterator pos, InputIt first, InputIt last) {
size_type new_size = _size + last - first;
size_type index = pos - begin();
if (new_size > _capacity)
reallocate(new_size);
auto insert_iter = begin() + index;
std::copy_backward(insert_iter, end(), begin() + new_size);
std::copy(first, last, insert_iter);
_capacity = new_size;
_size = new_size;
}
iterator insert(const_iterator pos, std::initializer_list<T> ilist) {
insert(pos, ilist.begin(), ilist.end());
return pos;
}
template<class... Args>
iterator emplace(const_iterator pos, Args&&... args) {
size_type index = pos - begin();
size_type new_size = _size + 1;
reallocate(new_size);
auto iter = begin() + index;
for (size_t i = _size; i >= index; i--)
_ptr[i] = _ptr[i - 1];
_alloc.construct(&*iter, std::forward<Args>(args)...);
_size++;
return iter;
}
iterator erase(const_iterator pos) {
std::allocator_traits<Allocator>::destroy(_alloc, &*pos);
std::copy(pos + 1, end(), pos);
_size--;
return pos;
}
iterator erase(const_iterator first, const_iterator last) {
for (auto it = first; it != last; it++)
std::allocator_traits<Allocator>::destroy(_alloc, &*it);
std::copy(last, end(), first);
_size -= std::distance(first, last);
return first;
}
void push_back(const T& value) {
emplace_back(value);
}
void push_back(T&& value) {
emplace_back(std::forward<T&&>(value));
}
template<class... Args>
void emplace_back(Args&&... args) {
size_type new_size = _size + 1;
if (new_size > _capacity)
reallocate(new_size);
std::allocator_traits<Allocator>::construct(
_alloc,
_ptr + _size,
std::forward<Args>(args)...
);
_size = new_size;
}
void pop_back() {
erase(begin() + _size - 1);
}
void resize(size_type count) {
resize(count, T());
}
void resize(size_type count, const value_type& value) {
if (count < _size)
for (auto it = begin() + count; it != end(); it++)
std::allocator_traits<Allocator>::destroy(_alloc, &*it);
else if (count > _size) {
reallocate(count);
for (auto it = end(); it != end() + count - _size; it++)
std::allocator_traits<Allocator>::construct(
_alloc,
_ptr + _size + (it - end()),
value
);
}
_size = count;
}
void swap(Vector& other) noexcept {
std::swap(this->_size, other._size);
std::swap(this->_capacity, other._capacity);
std::swap(this->_ptr, other._ptr);
std::swap(this->_alloc, other._alloc);
}
private:
void reallocate(size_type new_cap) {
if (new_cap > max_size())
throw std::length_error("New capacity over limit");
pointer new_ptr = std::allocator_traits<Allocator>::allocate(_alloc, new_cap);
for (auto it = begin(); it != end(); it++)
std::allocator_traits<Allocator>::construct(
_alloc,
new_ptr + (it - begin()),
std::forward<T>(*it)
);
_capacity = new_cap;
_ptr = new_ptr;
}
size_type _size;
size_type _capacity;
allocator_type _alloc;
pointer _ptr;
};
template <class T, class Allocator>
bool operator==(const Vector<T, Allocator>& lhs, const Vector<T, Allocator>& rhs) {
if (lhs.size() != rhs.size())
return false;
for (size_t i = 0; i < lhs.size(); i++)
if (lhs[i] != rhs[i])
return false;
return true;
}
template <class T, class Allocator>
bool operator!=(const Vector<T, Allocator>& lhs, const Vector<T, Allocator>& rhs) {
return !(lhs == rhs);
}
template <class T, class Allocator>
bool operator<(const Vector<T, Allocator>& lhs, const Vector<T, Allocator>& rhs) {
return std::lexicographical_compare(lhs.begin(), lhs.end(), rhs.begin(), rhs.end());
}
template <class T, class Allocator>
bool operator>(const Vector<T, Allocator>& lhs, const Vector<T, Allocator>& rhs) {
return rhs < lhs;
}
template <class T, class Allocator>
bool operator>=(const Vector<T, Allocator>& lhs, const Vector<T, Allocator>& rhs) {
return !(lhs < rhs);
}
template <class T, class Allocator>
bool operator<=(const Vector<T, Allocator>& lhs, const Vector<T, Allocator>& rhs) {
return !(rhs < lhs);
}