-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvector.hpp
563 lines (506 loc) · 16.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
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
#pragma once
#include <memory>
#include <stdexcept>
#include <string>
#include "algorithm.hpp"
#include "iterator.hpp"
#include "memory.hpp"
#include "type_traits.hpp"
namespace ft {
template <class T, class Allocator = std::allocator<T> >
class vector {
public:
// types
typedef typename Allocator::reference reference;
typedef typename Allocator::const_reference const_reference;
typedef typename Allocator::pointer iterator;
typedef typename Allocator::const_pointer const_iterator;
typedef typename Allocator::size_type size_type;
typedef typename Allocator::difference_type difference_type;
typedef T value_type;
typedef Allocator allocator_type;
typedef typename Allocator::pointer pointer;
typedef typename Allocator::const_pointer const_pointer;
typedef ft::reverse_iterator<iterator> reverse_iterator;
typedef ft::reverse_iterator<const_iterator> const_reverse_iterator;
// constructor/copy/destroy
explicit vector(const Allocator &allocator = Allocator());
explicit vector(size_type n, const T &value = T(),
const Allocator &allocator = Allocator());
template <class InputIterator>
vector(typename ft::enable_if<!ft::is_integral<InputIterator>::value,
InputIterator>::type first,
InputIterator last, const Allocator &allocator = Allocator());
vector(const vector<T, Allocator> &x);
~vector();
vector<T, Allocator> &operator=(const vector<T, Allocator> &x);
void assign(size_type n, const T &u);
template <class InputIterator>
void assign(typename ft::enable_if<!ft::is_integral<InputIterator>::value,
InputIterator>::type first,
InputIterator last);
allocator_type get_allocator() const;
// iterators
iterator begin();
const_iterator begin() const;
iterator end();
const_iterator end() const;
reverse_iterator rbegin();
const_reverse_iterator rbegin() const;
reverse_iterator rend();
const_reverse_iterator rend() const;
// capacity
size_type size() const;
size_type max_size() const;
void resize(size_type n, T val = T());
size_type capacity() const;
bool empty() const;
void reserve(size_type n);
// element access
reference operator[](size_type n);
const_reference operator[](size_type n) const;
reference at(size_type n);
const_reference at(size_type n) const;
reference front();
const_reference front() const;
reference back();
const_reference back() const;
// modifiers
void push_back(const T &x);
void pop_back();
iterator insert(iterator position, const T &x);
void insert(iterator position, size_type n, const T &x);
template <class InputIterator>
void insert(iterator position,
typename ft::enable_if<!ft::is_integral<InputIterator>::value,
InputIterator>::type first,
InputIterator last);
iterator erase(iterator position);
iterator erase(iterator first, iterator last);
void swap(vector<T, Allocator> &);
void clear();
protected:
allocator_type m_allocator;
iterator m_begin;
iterator m_end;
iterator m_end_of_storage;
size_type m_calculate_new_capacity(size_type n);
};
template <class T, class Allocator>
vector<T, Allocator>::vector(const Allocator &allocator)
: m_allocator(allocator), m_begin(0), m_end(0), m_end_of_storage(0) {}
template <class T, class Allocator>
vector<T, Allocator>::vector(
size_type n, const T &value, const Allocator &allocator)
: m_allocator(allocator), m_begin(m_allocator.allocate(n, this)),
m_end(m_begin + n), m_end_of_storage(m_end) {
ft::uninitialized_fill_n(begin(), n, value, get_allocator());
}
template <class T, class Allocator>
template <class InputIterator>
vector<T, Allocator>::vector(
typename ft::enable_if<!ft::is_integral<InputIterator>::value,
InputIterator>::type first,
InputIterator last, const Allocator &allocator) {
const size_type n = ft::distance(first, last);
m_allocator = allocator;
m_begin = m_allocator.allocate(n, this);
m_end = m_begin + n;
m_end_of_storage = m_end;
ft::uninitialized_copy(first, last, begin(), get_allocator());
}
template <class T, class Allocator>
vector<T, Allocator>::vector(const vector<T, Allocator> &x) {
m_allocator = x.get_allocator();
m_begin = m_allocator.allocate(x.size(), this);
m_end = m_begin + x.size();
m_end_of_storage = m_end;
ft::uninitialized_copy(x.begin(), x.end(), begin(), get_allocator());
}
template <class T, class Allocator>
vector<T, Allocator>::~vector() {
clear();
m_allocator.deallocate(m_begin, capacity());
}
template <class T, class Allocator>
vector<T, Allocator> &vector<T, Allocator>::operator=(
const vector<T, Allocator> &x) {
if (this == &x) {
return *this;
}
if (capacity() < x.size()) {
clear();
m_allocator.deallocate(m_begin, capacity());
m_begin = m_allocator.allocate(x.size(), this);
m_end = m_begin + x.size();
m_end_of_storage = m_end;
ft::uninitialized_copy(x.begin(), x.end(), begin(), get_allocator());
} else if (size() >= x.size()) {
iterator dst = ft::copy(x.begin(), x.end(), begin());
ft::destroy(dst, end(), get_allocator());
m_end = m_begin + x.size();
} else {
const_iterator src = x.begin();
iterator dst = begin();
while (dst != end()) {
*dst++ = *src++;
}
ft::uninitialized_copy(src, x.end(), dst, get_allocator());
m_end = m_begin + x.size();
}
return *this;
}
template <class T, class Allocator>
void vector<T, Allocator>::assign(size_type n, const T &u) {
if (capacity() < n) {
clear();
m_allocator.deallocate(begin(), capacity());
m_begin = m_allocator.allocate(n, this);
m_end = begin() + n;
m_end_of_storage = m_end;
ft::uninitialized_fill(begin(), end(), u, get_allocator());
} else if (size() > n) {
iterator dst = ft::fill_n(begin(), n, u);
ft::destroy(dst, end(), get_allocator());
m_end = begin() + n;
} else {
iterator dst = ft::fill_n(begin(), size(), u);
ft::uninitialized_fill(dst, begin() + n, u, get_allocator());
m_end = begin() + n;
}
}
template <class T, class Allocator>
template <class InputIterator>
void vector<T, Allocator>::assign(
typename ft::enable_if<!ft::is_integral<InputIterator>::value,
InputIterator>::type first,
InputIterator last) {
const size_type n = ft::distance(first, last);
if (capacity() < n) {
clear();
m_allocator.deallocate(begin(), capacity());
m_begin = m_allocator.allocate(n, this);
m_end = begin() + n;
m_end_of_storage = m_end;
ft::uninitialized_copy(first, last, begin(), get_allocator());
} else if (size() > n) {
iterator dst = ft::copy(first, last, begin());
ft::destroy(dst, end(), get_allocator());
m_end = begin() + n;
} else {
iterator dst = begin();
while (dst != end()) {
*dst++ = *first++;
}
while (dst != begin() + n) {
m_allocator.construct(dst++, *first++);
}
m_end = begin() + n;
}
}
template <class T, class Allocator>
typename vector<T, Allocator>::allocator_type
vector<T, Allocator>::get_allocator() const {
return m_allocator;
}
template <class T, class Allocator>
typename vector<T, Allocator>::iterator vector<T, Allocator>::begin() {
return m_begin;
}
template <class T, class Allocator>
typename vector<T, Allocator>::const_iterator
vector<T, Allocator>::begin() const {
return m_begin;
}
template <class T, class Allocator>
typename vector<T, Allocator>::iterator vector<T, Allocator>::end() {
return m_end;
}
template <class T, class Allocator>
typename vector<T, Allocator>::const_iterator
vector<T, Allocator>::end() const {
return m_end;
}
template <class T, class Allocator>
typename vector<T, Allocator>::reverse_iterator vector<T, Allocator>::rbegin() {
return reverse_iterator(end());
}
template <class T, class Allocator>
typename vector<T, Allocator>::const_reverse_iterator
vector<T, Allocator>::rbegin() const {
return const_reverse_iterator(end());
}
template <class T, class Allocator>
typename vector<T, Allocator>::reverse_iterator vector<T, Allocator>::rend() {
return reverse_iterator(begin());
}
template <class T, class Allocator>
typename vector<T, Allocator>::const_reverse_iterator
vector<T, Allocator>::rend() const {
return const_reverse_iterator(begin());
}
template <class T, class Allocator>
typename vector<T, Allocator>::size_type vector<T, Allocator>::size() const {
return end() - begin();
}
template <class T, class Allocator>
typename vector<T, Allocator>::size_type
vector<T, Allocator>::max_size() const {
return m_allocator.max_size();
}
template <class T, class Allocator>
void vector<T, Allocator>::resize(size_type n, T val) {
if (n > capacity()) {
const size_type new_capacity = m_calculate_new_capacity(n - size());
const iterator new_begin = m_allocator.allocate(new_capacity, this);
iterator dst =
ft::uninitialized_copy(begin(), end(), new_begin, get_allocator());
clear();
m_allocator.deallocate(begin(), capacity());
m_begin = new_begin;
m_end = new_begin + n;
m_end_of_storage = m_begin + new_capacity;
ft::uninitialized_fill(dst, end(), val, get_allocator());
} else if (n < size()) {
ft::destroy(begin() + n, end(), get_allocator());
m_end = begin() + n;
} else {
ft::uninitialized_fill(end(), begin() + n, val, get_allocator());
m_end = begin() + n;
}
}
template <class T, class Allocator>
typename vector<T, Allocator>::size_type
vector<T, Allocator>::capacity() const {
return m_end_of_storage - begin();
}
template <class T, class Allocator>
bool vector<T, Allocator>::empty() const {
return begin() == end();
}
template <class T, class Allocator>
void vector<T, Allocator>::reserve(size_type n) {
if (n <= capacity()) {
return;
} else if (n > max_size()) {
throw std::length_error(std::string("vector: reserve: n > max_size"));
}
const iterator new_begin = m_allocator.allocate(n, this);
const iterator new_end =
ft::uninitialized_copy(begin(), end(), new_begin, get_allocator());
clear();
m_allocator.deallocate(begin(), capacity());
m_begin = new_begin;
m_end = new_end;
m_end_of_storage = m_begin + n;
}
template <class T, class Allocator>
typename vector<T, Allocator>::reference vector<T, Allocator>::operator[](
size_type n) {
return m_begin[n];
}
template <class T, class Allocator>
typename vector<T, Allocator>::const_reference vector<T, Allocator>::operator[](
size_type n) const {
return m_begin[n];
}
template <class T, class Allocator>
typename vector<T, Allocator>::reference vector<T, Allocator>::at(size_type n) {
if (n >= size()) {
throw std::out_of_range(std::string("vector: index out of range"));
}
return m_begin[n];
}
template <class T, class Allocator>
typename vector<T, Allocator>::const_reference vector<T, Allocator>::at(
size_type n) const {
if (n >= size()) {
throw std::out_of_range(std::string("vector: index out of range"));
}
return m_begin[n];
}
template <class T, class Allocator>
typename vector<T, Allocator>::reference vector<T, Allocator>::front() {
return *m_begin;
}
template <class T, class Allocator>
typename vector<T, Allocator>::const_reference
vector<T, Allocator>::front() const {
return *m_begin;
}
template <class T, class Allocator>
typename vector<T, Allocator>::reference vector<T, Allocator>::back() {
return *(m_end - 1);
}
template <class T, class Allocator>
typename vector<T, Allocator>::const_reference
vector<T, Allocator>::back() const {
return *(m_end - 1);
}
template <class T, class Allocator>
void vector<T, Allocator>::push_back(const T &x) {
insert(end(), x);
}
template <class T, class Allocator>
void vector<T, Allocator>::pop_back() {
if (size()) {
m_end--;
m_allocator.destroy(m_end);
}
}
template <class T, class Allocator>
typename vector<T, Allocator>::iterator vector<T, Allocator>::insert(
iterator position, const T &x) {
const size_type n = position - begin();
insert(position, 1, x);
return begin() + n;
}
template <class T, class Allocator>
void vector<T, Allocator>::insert(
iterator position, size_type count, const T &x) {
if (count == 0) {
return;
}
if (size() + count <= capacity()) {
if (static_cast<size_type>(end() - position) > count) {
ft::uninitialized_copy(
end() - count, end(), end(), get_allocator());
ft::copy_backward(position, end() - count, end());
ft::fill(position, position + count, x);
} else {
ft::uninitialized_copy(
position, end(), position + count, get_allocator());
ft::uninitialized_fill_n(
end(), count - (end() - position), x, get_allocator());
ft::fill(position, end(), x);
}
m_end += count;
} else {
const size_type new_capacity = m_calculate_new_capacity(count);
const iterator new_begin = m_allocator.allocate(new_capacity, this);
iterator dst = ft::uninitialized_copy(
begin(), position, new_begin, get_allocator());
ft::uninitialized_fill_n(dst, count, x, get_allocator());
dst = ft::uninitialized_copy(
position, end(), dst + count, get_allocator());
clear();
m_allocator.deallocate(m_begin, m_end_of_storage - m_begin);
m_begin = new_begin;
m_end = dst;
m_end_of_storage = m_begin + new_capacity;
}
}
template <class T, class Allocator>
template <class InputIterator>
void vector<T, Allocator>::insert(iterator position,
typename ft::enable_if<!ft::is_integral<InputIterator>::value,
InputIterator>::type first,
InputIterator last) {
if (first == last) {
return;
}
const size_type n = ft::distance(first, last);
if (size() + n <= capacity()) {
if (static_cast<size_type>(end() - position) > n) {
ft::uninitialized_copy(end() - n, end(), end(), get_allocator());
ft::copy_backward(position, position + n, end());
ft::copy(first, last, position);
} else {
ft::uninitialized_copy(
position, end(), position + n, get_allocator());
InputIterator it = first;
ft::advance(it, end() - position);
ft::copy(first, it, position);
ft::uninitialized_copy(it, last, end(), get_allocator());
}
m_end += n;
} else {
const size_type new_capacity = m_calculate_new_capacity(n);
const iterator new_begin = m_allocator.allocate(new_capacity, this);
iterator dst = ft::uninitialized_copy(
begin(), position, new_begin, get_allocator());
ft::uninitialized_copy(position, end(), dst + n, get_allocator());
ft::uninitialized_copy(first, last, dst, get_allocator());
dst = ft::uninitialized_copy(position, end(), dst + n, get_allocator());
clear();
m_allocator.deallocate(m_begin, m_end_of_storage - m_begin);
m_begin = new_begin;
m_end = dst;
m_end_of_storage = m_begin + new_capacity;
}
return;
}
template <class T, class Allocator>
typename vector<T, Allocator>::iterator vector<T, Allocator>::erase(
iterator position) {
iterator ret = position;
for (; position + 1 != end(); position++) {
*position = *(position + 1);
}
m_allocator.destroy(m_end);
--m_end;
return ret;
}
template <class T, class Allocator>
typename vector<T, Allocator>::iterator vector<T, Allocator>::erase(
iterator first, iterator last) {
iterator src = last;
iterator dst = first;
while (src != end()) {
*dst++ = *src++;
}
ft::destroy(dst, end(), get_allocator());
m_end = dst;
return first;
}
template <class T, class Allocator>
void vector<T, Allocator>::swap(vector<T, Allocator> &x) {
ft::swap(m_allocator, x.m_allocator);
ft::swap(m_begin, x.m_begin);
ft::swap(m_end, x.m_end);
ft::swap(m_end_of_storage, x.m_end_of_storage);
}
template <class T, class Allocator>
void vector<T, Allocator>::clear() {
ft::destroy(begin(), end(), get_allocator());
m_end = m_begin;
}
template <class T, class Allocator>
typename vector<T, Allocator>::size_type
vector<T, Allocator>::m_calculate_new_capacity(size_type n) {
if (max_size() - size() < n) {
throw std::length_error(
std::string("vector: capacity: size() + n > max_size"));
}
const size_type len = size() + ft::max(size(), n);
return (len < size() || len > max_size()) ? max_size() : len;
}
template <class T, class Allocator>
bool operator==(const vector<T, Allocator> &x, const vector<T, Allocator> &y) {
return x.size() == y.size() && ft::equal(x.begin(), x.end(), y.begin());
}
template <class T, class Allocator>
bool operator!=(const vector<T, Allocator> &x, const vector<T, Allocator> &y) {
return !(x == y);
}
template <class T, class Allocator>
bool operator<(const vector<T, Allocator> &x, const vector<T, Allocator> &y) {
return ft::lexicographical_compare(x.begin(), x.end(), y.begin(), y.end());
}
template <class T, class Allocator>
bool operator>(const vector<T, Allocator> &x, const vector<T, Allocator> &y) {
return y < x;
}
template <class T, class Allocator>
bool operator<=(const vector<T, Allocator> &x, const vector<T, Allocator> &y) {
return !(y < x);
}
template <class T, class Allocator>
bool operator>=(const vector<T, Allocator> &x, const vector<T, Allocator> &y) {
return !(x < y);
}
template <class T, class Allocator>
void swap(vector<T, Allocator> &x, vector<T, Allocator> &y) {
x.swap(y);
}
} // namespace ft