-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvector.hpp
560 lines (460 loc) · 16.4 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* my_vector.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: laube <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/02/12 17:11:36 by laube #+# #+# */
/* Updated: 2022/04/26 15:33:26 by laube ### ########.fr */
/* */
/* ************************************************************************** */
#pragma once
#include <iostream>
#include <limits>
#include <memory>
#include <iterator>
#include "iterator.hpp"
#include "utils.hpp"
namespace ft {
template <typename T, typename Allocator = std::allocator<T> >
class vector {
public:
// Member Types
typedef T value_type;
typedef Allocator allocator_type;
typedef typename allocator_type::difference_type diff_type;
typedef std::size_t size_type;
typedef std::ptrdiff_t difference_type;
typedef value_type& reference;
typedef const value_type& const_reference;
typedef T* pointer;
typedef const T* const_pointer;
typedef normal_iterator<pointer, vector> iterator;
typedef normal_iterator<const_pointer, vector> const_iterator;
typedef ft::reverse_iterator<iterator> reverse_iterator;
typedef ft::reverse_iterator<const_iterator> const_reverse_iterator;
private:
pointer m_start;
pointer m_finish;
pointer m_end_of_storage;
allocator_type m_alloc;
public:
// MEMBER FUNCTIONS
// Constructors
vector() : m_start(), m_finish(m_start), m_end_of_storage(m_start), m_alloc() {
}
explicit vector(const Allocator& alloc)
: m_start(NULL), m_finish(m_start), m_end_of_storage(m_start), m_alloc(alloc) {}
explicit vector(size_type count, const T& value = T(),
const Allocator& alloc = Allocator())
: m_start(NULL)
, m_finish(m_start)
, m_end_of_storage(m_start)
, m_alloc(alloc) {
grow_capacity(count);
m_construct_storage(value, count);
}
template <typename InputIt>
vector(typename enable_if<!(is_integral<InputIt>::value),
InputIt>::type first,
InputIt last, const Allocator& alloc = Allocator())
: m_start(NULL)
, m_finish(m_start)
, m_end_of_storage(m_start)
, m_alloc(alloc) {
insert(begin(), first, last);
}
vector(const vector& other)
: m_start(NULL)
, m_finish(m_start)
, m_end_of_storage(m_start)
, m_alloc(Allocator())
{
m_create_storage(other.capacity());
insert(m_start, other.begin(), other.end());
}
// Destructors
~vector() {
clear_complete();
m_start = NULL;
m_finish = NULL;
m_end_of_storage = NULL;
}
// Other member functions
vector& operator=(const vector& other) {
clear_complete();
m_create_storage(other.capacity());
insert(m_start, other.begin(), other.end());
return *this;
}
void assign(size_type count, const T& value) {
clear();
if (count <= 0)
return;
grow_capacity(count);
m_construct_storage(value, count);
}
template <class InputIt>
void assign(InputIt first, typename enable_if<!is_integral<InputIt>::value,
InputIt>::type last) {
vector<value_type> tmp(first, last);
clear_complete();
insert(m_start, tmp.begin(), tmp.end());
}
allocator_type get_allocator() const { return (this->m_alloc); }
// Element accesses
reference at(size_type pos) {
if (pos >= size()) {
throw std::out_of_range("at(): position is greater or equal to size");
}
return *(m_start + pos);
}
const_reference at(size_type pos) const {
if (pos >= size()) {
throw std::out_of_range("at(): position is greater or equal to size");
}
return *(m_start + pos);
}
reference front() {
return *begin();
}
const_reference front() const {
return *begin();
}
reference back() {
return *(end() - 1);
}
const_reference back() const {
return *(end() - 1);
}
T* data() {
if (empty())
return NULL;
return m_start;
}
const T* data() const {
if (empty())
return NULL;
return m_start;
}
reference operator[](size_type pos) {
return *(this->m_start + pos);
}
const_reference operator[](size_type pos) const {
return *(this->m_start + pos);
}
iterator begin() { return iterator(this->m_start); }
const_iterator begin() const { return const_iterator(this->m_start); }
iterator end() { return iterator(this->m_finish); }
const_iterator end() const { return const_iterator(this->m_finish); }
reverse_iterator rbegin() {
return reverse_iterator(this->m_finish);
}
const_reverse_iterator rbegin() const
{
if (empty())
return reverse_iterator(this->m_finish);
return const_reverse_iterator(this->m_finish);
}
reverse_iterator rend() {
return reverse_iterator(this->m_start);
}
const_reverse_iterator rend() const
{
return const_reverse_iterator(this->m_start);
}
bool empty() const {
return begin() == end();
}
size_type size() const {
return std::distance(begin(), end());
}
size_type max_size() const {
return std::min(m_alloc.max_size()
, static_cast<size_type>(std::numeric_limits<diff_type>::max()));
}
size_type capacity() const {
return m_end_of_storage - m_start;
}
void reserve(size_type new_cap) {
pointer new_m_start;
pointer new_m_finish;
if (new_cap > max_size())
{
throw std::length_error("Can't reserve vector size: bigger than max_size()");
}
if (new_cap == 0)
new_cap = 1;
if (new_cap <= capacity())
return;
new_m_start = m_alloc.allocate(new_cap);
new_m_finish = m_construct_storage(new_m_start, m_start, m_finish);
clear_complete();
m_start = new_m_start;
m_finish = new_m_finish;
m_end_of_storage = m_start + new_cap;
}
// Dectucts all nodes without deallocating any
void clear()
{
if (!empty())
destroy_storage(m_start, m_finish);
m_finish = m_start;
}
void insert(iterator pos, size_type count, const T& value)
{
if (count <= 0)
return;
size_type offset = pos - begin();
if (size() + count > max_size())
{
throw std::length_error("Can't reserve vector size: bigger than max_size()");
}
grow_capacity(size() + count);
for (size_type i = 0; i < count; ++i)
m_alloc.construct(m_finish + i, value_type());
pos = begin() + offset;
size_type end_len = size() + count;
pointer first_copy = pos.base();
pointer last_copy = end().base();
pointer d_last_copy = (begin() + end_len).base();
std::copy_backward(first_copy, last_copy, d_last_copy);
for (size_type i = 0; i < count; ++i, ++pos)
{
*pos = value;
}
m_finish += count;
}
iterator insert(iterator pos, const T& value)
{
difference_type offset = pos - begin();
grow_capacity(size() + 1);
m_alloc.construct(end().base(), T());
pointer last = m_finish;
pointer first = m_start + offset;
pointer d_last = last + 1;
std::copy_backward(first, last, d_last);
*(m_start + offset) = value;
m_finish++;
return begin() + offset;
}
template <typename InputIt>
void insert(iterator pos
, typename enable_if<!(is_integral<InputIt>::value), InputIt>::type first
, InputIt last)
{
typedef typename iterator_traits<InputIt>::iterator_category Iter_category;
insert_dispatch(pos, first, last, Iter_category());
}
iterator erase(iterator pos)
{
if (pos == end())
return pos;
pointer first_copy = (pos + 1).base();
pointer last_copy = m_finish;
pointer d_first_copy = pos.base();
std::copy(first_copy, last_copy, d_first_copy);
m_alloc.destroy(m_finish - 1);
--m_finish;
return pos;
}
iterator erase(iterator first, iterator last)
{
size_type count = std::distance(first, last);
size_type new_size = size() - count;
size_type ret_num = last - begin() - count;
pointer first_copy = last.base();
pointer last_copy = m_finish;
pointer d_first_copy = first.base();
std::copy(first_copy, last_copy, d_first_copy);
for (pointer destroy_pointer = m_start + new_size; destroy_pointer != m_finish; ++destroy_pointer)
{
m_alloc.destroy(destroy_pointer);
}
m_finish -= count;
return (begin() + ret_num);
}
void push_back(const T& value)
{
grow_capacity(size() + 1);
if (m_start == NULL)
{
m_alloc.construct(m_start, value);
m_finish = m_start + 1;
} else {
m_alloc.construct(m_finish, value);
m_finish++;
}
}
void pop_back()
{
erase(end() - 1);
}
void resize(size_type count, T value = T())
{
if (count > size())
{
grow_capacity(count);
for (iterator ite = end(); static_cast<size_type>(ite - begin()) < count; ++ite)
{
m_alloc.construct(ite.base(), value);
}
m_finish = begin().base() + count;
}
else if (count < size())
{
destroy_storage(begin() + count, end());
m_finish = begin().base() + count;
}
}
void swap(vector& other)
{
std::swap(m_start, other.m_start);
std::swap(m_finish, other.m_finish);
std::swap(m_end_of_storage, other.m_end_of_storage);
}
private:
void m_create_storage(size_type count) {
if (count <= 0)
return;
m_start = m_alloc.allocate(count);
m_finish = m_start;
m_end_of_storage = m_start + count;
}
iterator m_construct_storage(const T& value, size_type count) {
size_type i = 0;
for (; i < count; ++i) {
m_alloc.construct(m_start + i, value);
}
m_finish = m_start + i;
return m_finish;
}
template <class InputIt>
pointer m_construct_storage(pointer dst, InputIt it, InputIt ite)
{
int i = 0;
for (; it + i != ite; ++i)
{
m_alloc.construct(dst + i, *(it + i));
}
return dst + i;
}
void destroy_storage(iterator ite1, iterator ite2)
{
for (; ite1 != ite2; ++ite1)
{
m_alloc.destroy(ite1.base());
}
}
// Destructs and deallocate all nodes
void clear_complete()
{
clear();
if (capacity() != 0)
{
m_alloc.deallocate(m_start, m_end_of_storage - m_start);
m_start = pointer();
m_finish = m_start;
m_end_of_storage = m_start;
}
}
template <typename InputIt>
void insert_dispatch(iterator pos, InputIt first, InputIt last, std::input_iterator_tag)
{
difference_type offset = pos - begin();
vector<value_type, allocator_type> tmp(begin(), end());
iterator it = tmp.begin();
clear();
for (difference_type i = 0; i < offset; ++i, ++it)
{
push_back(*it);
}
for (; first != last; ++first)
{
push_back(*first);
}
for (; it != tmp.end(); ++it)
{
push_back(*it);
}
}
template<typename InputIt>
void insert_dispatch(iterator pos, InputIt first, InputIt last, std::forward_iterator_tag)
{
size_type offset = pos - begin();
size_type count = 0;
count = std::distance(first, last);
if (size() + count > max_size())
throw std::length_error("Can't reserve vector size: bigger than max_size()");
size_type end_len = size() + count;
grow_capacity(size() + count);
for (size_type i = 0; i < count; ++i)
{
m_alloc.construct(m_finish + i, value_type());
}
pos = begin() + offset;
pointer first_copy = pos.base();
pointer last_copy = end().base();
pointer d_last_copy = (begin() + end_len).base();
std::copy_backward(first_copy, last_copy, d_last_copy);
for (; first != last; ++first, ++pos)
{
*pos = *first;
}
m_finish += count;
}
/*
* Allocates twice the current storage until total storage is greater
* than new_cap. new_cap represents the total storage elements needed
*/
void grow_capacity(size_type new_cap)
{
if (new_cap > max_size())
{
throw std::length_error("Can't reserve vector size: bigger than max_size()");
}
if (new_cap == 0)
new_cap = 1;
while (m_end_of_storage - m_start < static_cast<long>(new_cap))
{
reserve((m_end_of_storage - m_start) * 2);
}
}
};
template <typename T, typename Alloc>
bool operator==(const ft::vector<T, Alloc>& lhs, const ft::vector<T, Alloc>& rhs)
{
return lhs.size() == rhs.size() && ft::equal(lhs.begin(), lhs.end(), rhs.begin());
}
template <typename T, typename Alloc>
bool operator!=(const ft::vector<T, Alloc>& lhs, const ft::vector<T, Alloc>& rhs)
{
return !(lhs == rhs);
}
template <typename T, typename Alloc>
bool operator<(const ft::vector<T, Alloc>& lhs, const ft::vector<T, Alloc>& rhs)
{
return ft::lexicographical_compare(lhs.begin(), lhs.end(), rhs.begin(), rhs.end());
}
template <typename T, typename Alloc>
bool operator<=(const ft::vector<T, Alloc>& lhs, const ft::vector<T, Alloc>& rhs)
{
return (lhs < rhs || lhs == rhs);
}
template <typename T, typename Alloc>
bool operator>(const ft::vector<T, Alloc>& lhs, const ft::vector<T, Alloc>& rhs)
{
return (!(lhs < rhs) && !(lhs == rhs));
}
template <typename T, typename Alloc>
bool operator>=(const ft::vector<T, Alloc>& lhs, const ft::vector<T, Alloc>& rhs)
{
return (lhs > rhs || lhs == rhs);
}
template <typename T, typename Alloc>
void swap(ft::vector<T, Alloc>& lhs, ft::vector<T, Alloc>& rhs)
{
lhs.swap(rhs);
}
}