-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvector.hpp
602 lines (491 loc) · 18.2 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
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
#include "stack.hpp"
namespace ft {
template < class T >
class vector_iterator {
public :
typedef T value_type;
typedef value_type& reference;
typedef const value_type& const_reference;
typedef value_type* pointer;
typedef const value_type* const_pointer;
typedef typename std::ptrdiff_t difference_type;
vector_iterator(void) {}
vector_iterator(pointer ptr) { _ptr = ptr; }
vector_iterator(vector_iterator const &src) { *this = src; } ;
vector_iterator &operator=(vector_iterator const &src) { _ptr = src.operator->(); return (*this); }
~vector_iterator() {}
vector_iterator operator ++() {
_ptr++;
return (*this);
}
vector_iterator operator ++(int) {
_ptr++;
return (vector_iterator(_ptr - 1));
}
vector_iterator operator --() {
_ptr--;
return (*this);
}
vector_iterator operator --(int) {
_ptr--;
return (vector_iterator(_ptr + 1));
}
void operator +=(difference_type v) { _ptr += v; }
void operator -=(difference_type v) { _ptr -= v; }
bool operator ==(vector_iterator const& v) const { return _ptr == v._ptr; }
bool operator !=(vector_iterator const& v) const { return _ptr != v._ptr; }
bool operator <(vector_iterator const& v) const { return _ptr < v._ptr; }
bool operator <=(vector_iterator const& v) const { return _ptr <= v._ptr; }
bool operator >(vector_iterator const& v) const { return _ptr > v._ptr; }
bool operator >=(vector_iterator const& v) const { return _ptr >= v._ptr; }
vector_iterator operator +(difference_type v) { return vector_iterator(_ptr + v); }
vector_iterator operator -(difference_type v) { return vector_iterator(_ptr - v); }
difference_type operator +(vector_iterator v) { return _ptr + v._ptr; }
difference_type operator -(vector_iterator v) { return _ptr - v._ptr; }
reference operator *() { return *_ptr; }
const_reference operator *() const { return *_ptr; }
pointer operator ->() { return _ptr; }
pointer operator ->() const { return _ptr; }
reference operator [](difference_type n) { return *(_ptr + n); }
const_reference operator [](difference_type n) const { return *(_ptr + n); }
pointer get_ptr() { return _ptr; }
private :
pointer _ptr;
};
template < class T >
class vector_const_iterator {
public :
typedef T value_type;
typedef value_type& reference;
typedef const value_type& const_reference;
typedef value_type* pointer;
typedef const value_type* const_pointer;
typedef typename std::ptrdiff_t difference_type;
vector_const_iterator(void) {}
vector_const_iterator(pointer ptr) { _ptr = ptr; }
vector_const_iterator(vector_iterator<T> const &src) { _ptr = src.operator->(); }
vector_const_iterator(vector_const_iterator const &src) { *this = src; }
vector_const_iterator &operator=(vector_const_iterator const &src) { _ptr = src.operator->(); return (*this); }
~vector_const_iterator() {}
vector_const_iterator operator ++() {
_ptr++;
return (*this);
}
vector_const_iterator operator ++(int) {
_ptr++;
return (vector_const_iterator(_ptr - 1));
}
vector_const_iterator operator --() {
_ptr--;
return (*this);
}
vector_const_iterator operator --(int) {
_ptr--;
return (vector_const_iterator(_ptr + 1));
}
bool operator ==(vector_const_iterator const& v) const { return _ptr == v._ptr; }
bool operator !=(vector_const_iterator const& v) const { return _ptr != v._ptr; }
bool operator <(vector_const_iterator const& v) const { return _ptr < v._ptr; }
bool operator <=(vector_const_iterator const& v) const { return _ptr <= v._ptr; }
bool operator >(vector_const_iterator const& v) const { return _ptr > v._ptr; }
bool operator >=(vector_const_iterator const& v) const { return _ptr >= v._ptr; }
vector_const_iterator operator +(difference_type v) { return vector_const_iterator(_ptr + v); }
vector_const_iterator operator -(difference_type v) { return vector_const_iterator(_ptr - v); }
difference_type operator +(vector_const_iterator v) { return _ptr + v._ptr; }
difference_type operator -(vector_const_iterator v) { return _ptr - v._ptr; }
const_reference operator *() const { return *_ptr; }
pointer operator ->() { return _ptr; }
pointer operator ->() const { return _ptr; }
const_reference operator [](difference_type n) const { return *(_ptr + n); }
pointer get_ptr() { return _ptr; }
private :
pointer _ptr;
};
template < class iterator >
class vector_reverse_iterator {
public :
typedef iterator iterator_type;
typedef typename iterator::value_type value_type;
typedef typename iterator::reference reference;
typedef typename iterator::const_reference const_reference;
typedef typename iterator::pointer pointer;
typedef typename iterator::const_pointer const_pointer;
typedef typename iterator::difference_type difference_type;
vector_reverse_iterator(void) {}
vector_reverse_iterator(pointer ptr) { _ptr = ptr; }
vector_reverse_iterator(vector_iterator<value_type> const &src) { _ptr = src.operator->(); }
vector_reverse_iterator(vector_reverse_iterator const &src) { *this = src; } ;
vector_reverse_iterator &operator=(vector_reverse_iterator const &src) { _ptr = src.operator->(); return (*this); }
~vector_reverse_iterator() {}
vector_reverse_iterator operator ++() {
_ptr--;
return (*this);
}
vector_reverse_iterator operator ++(int) {
_ptr--;
return (vector_reverse_iterator(_ptr + 1));
}
vector_reverse_iterator operator --() {
_ptr++;
return (*this);
}
vector_reverse_iterator operator --(int) {
_ptr++;
return (vector_reverse_iterator(_ptr - 1));
}
void operator +=(difference_type v) { _ptr -= v; }
void operator -=(difference_type v) { _ptr += v; }
bool operator ==(vector_reverse_iterator const& v) const { return _ptr == v._ptr; }
bool operator !=(vector_reverse_iterator const& v) const { return _ptr != v._ptr; }
bool operator <(vector_reverse_iterator const& v) const { return _ptr < v._ptr; }
bool operator <=(vector_reverse_iterator const& v) const { return _ptr <= v._ptr; }
bool operator >(vector_reverse_iterator const& v) const { return _ptr > v._ptr; }
bool operator >=(vector_reverse_iterator const& v) const { return _ptr >= v._ptr; }
vector_reverse_iterator operator +(difference_type v) { return vector_reverse_iterator(_ptr - v); }
vector_reverse_iterator operator -(difference_type v) { return vector_reverse_iterator(_ptr + v); }
difference_type operator +(vector_reverse_iterator v) { return _ptr - v._ptr; }
difference_type operator -(vector_reverse_iterator v) { return _ptr + v._ptr; }
reference operator *() { return *_ptr; }
const_reference operator *() const { return *_ptr; }
pointer operator ->() { return _ptr; }
pointer operator ->() const { return _ptr; }
reference operator [](difference_type n) { return *(_ptr + n); }
const_reference operator [](difference_type n) const { return *(_ptr + n); }
pointer get_ptr() { return _ptr; }
private :
pointer _ptr;
};
template < class iterator >
class vector_const_reverse_iterator {
public :
typedef iterator iterator_type;
typedef typename iterator::value_type value_type;
typedef typename iterator::reference reference;
typedef typename iterator::const_reference const_reference;
typedef typename iterator::pointer pointer;
typedef typename iterator::const_pointer const_pointer;
typedef typename iterator::difference_type difference_type;
vector_const_reverse_iterator(void) {}
vector_const_reverse_iterator(pointer ptr) { _ptr = ptr; }
vector_const_reverse_iterator(vector_iterator<value_type> const &src) { _ptr = src.operator->(); }
vector_const_reverse_iterator(vector_const_reverse_iterator const &src) { *this = src; } ;
vector_const_reverse_iterator &operator=(vector_const_reverse_iterator const &src) { _ptr = src.operator->(); return (*this); }
~vector_const_reverse_iterator() {}
vector_const_reverse_iterator operator ++() {
_ptr--;
return (*this);
}
vector_const_reverse_iterator operator ++(int) {
_ptr--;
return (vector_const_reverse_iterator(_ptr + 1));
}
vector_const_reverse_iterator operator --() {
_ptr++;
return (*this);
}
vector_const_reverse_iterator operator --(int) {
_ptr++;
return (vector_const_reverse_iterator(_ptr - 1));
}
bool operator ==(vector_const_reverse_iterator const& v) const { return _ptr == v._ptr; }
bool operator !=(vector_const_reverse_iterator const& v) const { return _ptr != v._ptr; }
bool operator <(vector_const_reverse_iterator const& v) const { return _ptr < v._ptr; }
bool operator <=(vector_const_reverse_iterator const& v) const { return _ptr <= v._ptr; }
bool operator >(vector_const_reverse_iterator const& v) const { return _ptr > v._ptr; }
bool operator >=(vector_const_reverse_iterator const& v) const { return _ptr >= v._ptr; }
vector_const_reverse_iterator operator +(difference_type v) { return vector_const_reverse_iterator(_ptr - v); }
vector_const_reverse_iterator operator -(difference_type v) { return vector_const_reverse_iterator(_ptr + v); }
difference_type operator +(vector_const_reverse_iterator v) { return _ptr - v._ptr; }
difference_type operator -(vector_const_reverse_iterator v) { return _ptr + v._ptr; }
const_reference operator *() const { return *_ptr; }
pointer operator ->() { return _ptr; }
pointer operator ->() const { return _ptr; }
const_reference operator [](difference_type n) const { return *(_ptr + n); }
pointer get_ptr() { return _ptr; }
private :
pointer _ptr;
};
template < class T, class Alloc = std::allocator<T> >
class vector {
public :
typedef T value_type;
typedef Alloc allocator_type;
typedef typename std::size_t size_type;
typedef typename std::ptrdiff_t difference_type;
typedef typename Alloc::reference reference;
typedef typename Alloc::const_reference const_reference;
typedef typename Alloc::pointer pointer;
typedef typename Alloc::const_pointer const_pointer;
typedef typename ft::vector_iterator<T> iterator;
typedef typename ft::vector_const_iterator<T> const_iterator;
typedef typename ft::vector_reverse_iterator<iterator> reverse_iterator;
typedef typename ft::vector_const_reverse_iterator<iterator> const_reverse_iterator;
explicit vector (const allocator_type& alloc = allocator_type()) {
_alloc = alloc;
_ptr = NULL;
_size = 0;
_size_alloc = 0;
}
template <class InputIterator>
vector (InputIterator first, InputIterator last, const allocator_type& alloc = allocator_type()) {
_alloc = alloc;
_ptr = NULL;
_size = 0;
_size_alloc = 0;
while (first != last) {
push_back(*first);
first++;
}
}
explicit vector (size_type n, const value_type& val = value_type(), const allocator_type& alloc = allocator_type()) {
_alloc = alloc;
_ptr = NULL;
_size = 0;
_size_alloc = 0;
while (n--)
push_back(val);
}
vector (const vector& x) { *this = x; }
~vector () {
clear();
//if (_size_alloc > 0)
//_alloc.deallocate(_ptr, _size_alloc);
//_size_alloc = 0;
}
// ITERATORS
iterator begin() { return iterator(_ptr); }
const_iterator begin() const { return const_iterator(_ptr); }
iterator end() { return iterator(_ptr + _size); }
const_iterator end() const { return const_iterator(_ptr + _size); }
reverse_iterator rbegin() { return reverse_iterator(_ptr + _size - 1); }
const_reverse_iterator rbegin() const { return rbegin(); }
reverse_iterator rend() { return reverse_iterator(_ptr - 1); }
const_reverse_iterator rend() const { return rend(); }
// CAPACITY
size_type size() const { return _size; }
size_type max_size() const { return std::numeric_limits<size_type>::max(); }
void resize (size_type n, value_type val = value_type()) {
value_type *new_ptr;
new_ptr = _alloc.allocate(n);
if (n > _size) {
for (size_t i = 0; i < _size; i++)
_alloc.construct(new_ptr + i, *(_ptr + i));
for (size_t i = 0; i < n - _size; i++)
_alloc.construct(new_ptr + _size + i, val);
}
else {
for (size_t i = 0; i < n; i++)
_alloc.construct(new_ptr + i, *(_ptr + i));
}
if (_size) {
_alloc.destroy(_ptr);
_alloc.deallocate(_ptr, _size_alloc);
}
_size_alloc = n;
_size = n;
_ptr = new_ptr;
}
size_type capacity() const { return _size_alloc; }
bool empty() const { return (_size == 0); }
void reserve(size_type n) {
value_type *new_ptr;
if (n <= _size_alloc)
return ;
new_ptr = _alloc.allocate(n);
for (size_t i = 0; i < _size; i++)
_alloc.construct(new_ptr + i, *(_ptr + i));
_size_alloc = n;
_ptr = new_ptr;
}
// ELEMENT ACCESS
reference operator[] (size_type n) { return _ptr[n]; }
const_reference operator[] (size_type n) const { return _ptr[n]; }
reference at(size_type n) {
if (n >= _size)
throw std::out_of_range(std::to_string(n));
return (_ptr[n]);
}
const_reference at(size_type n) const {
if (n >= _size)
throw std::out_of_range(std::to_string(n));
return (_ptr[n]);
}
reference front() { return *_ptr; }
const_reference front() const { return *_ptr; }
reference back() { return *(_ptr + (_size - 1)); }
const_reference back() const { return *(_ptr + (_size - 1)); }
// MODIFIERS
template <class InputIterator>
void assign (InputIterator first, InputIterator last) {
clear();
while (first != last)
push_back(first++);
}
void assign (size_type n, const value_type& val) {
clear();
while (n--)
push_back(val);
}
void push_back (const value_type& val) {
if (_size == _size_alloc)
_extend();
_alloc.construct(_ptr + _size, val);
_size++;
}
void pop_back () {
if (!_size)
return ;
_alloc.destroy(_ptr + _size - 1);
_size--;
}
iterator insert(iterator position, const value_type& val) {
iterator fit;
difference_type diff = position - begin();
if (capacity() == size())
_extend();
fit = begin() + diff;
vector tmp(fit, end());
for (size_t i = 0; i < tmp.size(); i++)
pop_back();
push_back(val);
iterator it = tmp.begin();
for (size_t i = 0; i < tmp.size(); i++, it++)
push_back(*it);
return begin() + diff;
};
void insert(iterator position, size_type n, const value_type& val) {
difference_type diff = position - begin();
while (_size_alloc - _size < n)
_extend();
vector tmp(begin() + diff, end());
for (size_t i = 0; i < tmp.size(); i++)
pop_back();
while (n-- > 0)
push_back(val);
for (iterator it = tmp.begin(); it != tmp.end(); it++)
push_back(*it);
}
template <class InputIterator>
void insert (iterator position, InputIterator first, InputIterator last) {
size_t i = 0;
difference_type diff = position - this->begin();
while (first != last) {
first++;
i++;
}
first -= i;
while (_size_alloc - _size < i)
_extend();
vector tmp(begin() + diff, end());
for (size_t j = 0; j < tmp.size(); j++)
pop_back();
while (first != last)
push_back(*(first++));
for (iterator it = tmp.begin(); it != tmp.end(); it++)
push_back(*it);
}
iterator erase (iterator position) {
vector tmp(position + 1, end());
for (size_t i = 0; i <= tmp.size(); i++)
pop_back();
for (iterator it = tmp.begin(); it != tmp.end(); it++)
push_back(*it);
return (position);
}
iterator erase (iterator first, iterator last) {
iterator tmp(first);
while (tmp++ != last)
erase(first);
return (first);
}
void swap (vector& x) {
T *tmp_ptr = x._ptr;
size_t tmp_size = x._size;
size_t tmp_size_alloc = x._size_alloc;
x._ptr = _ptr;
x._size = _size;
x._size_alloc = _size_alloc;
_ptr = tmp_ptr;
_size = tmp_size;
_size_alloc = tmp_size_alloc;
}
void clear()
{
if (_size > 0)
{
iterator it = this->begin();
while (it != this->end())
_alloc.destroy(&(*it++));
_size = 0;
}
}
private :
allocator_type _alloc;
value_type *_ptr;
size_t _size_alloc;
size_t _size;
void _extend(void) {
if (_size_alloc == 0) {
_ptr = _alloc.allocate(1);
_size_alloc++;
}
else {
T *new_arr = _alloc.allocate(_size_alloc * 2);
for (size_t i = 0; i < _size; i++) {
_alloc.construct(new_arr + i, *(_ptr + i));
_alloc.destroy(_ptr + i);
}
_alloc.deallocate(_ptr, _size_alloc);
_ptr = new_arr;
_size_alloc *= 2;
}
};
};
template <class T, class Alloc>
bool operator== (const vector<T,Alloc>& lhs, const vector<T,Alloc>& rhs)
{
typename ft::vector<T>::const_iterator it_lhs = lhs.begin();
typename ft::vector<T>::const_iterator it_rhs = rhs.begin();
if (lhs.size() != rhs.size())
return (false);
while (it_lhs != lhs.end() && it_rhs != rhs.end() && *it_lhs == *it_rhs) {
it_lhs++;
it_rhs++;
}
return (it_lhs == lhs.end() && it_rhs == rhs.end());
}
template <class T, class Alloc>
bool operator!= (const vector<T,Alloc>& lhs, const vector<T,Alloc>& rhs) {
return (!(lhs == rhs));
}
template <class T, class Alloc>
bool operator<(const vector<T,Alloc>& lhs, const vector<T,Alloc>& rhs) {
typename ft::vector<T>::const_iterator it_lhs = lhs.begin();
typename ft::vector<T>::const_iterator it_rhs = rhs.begin();
if (lhs == rhs)
return (false);
while (it_lhs != lhs.end() && it_rhs != rhs.end() && *it_lhs == *it_rhs) {
it_lhs++;
it_rhs++;
}
if (it_rhs != rhs.end())
return (true);
return (false);
}
template <class T, class Alloc>
bool operator<= (const vector<T,Alloc>& lhs, const vector<T,Alloc>& rhs) {
return (lhs == rhs || lhs < rhs) ? true : false;
}
template <class T, class Alloc>
bool operator>(const vector<T,Alloc>& lhs, const vector<T,Alloc>& rhs) {
return (!(lhs == rhs) && !(lhs < rhs)) ? true : false;
}
template <class T, class Alloc>
bool operator>= (const vector<T,Alloc>& lhs, const vector<T,Alloc>& rhs) {
return (!(lhs < rhs)) ? true : false;
}
template <class T, class Alloc>
void swap (vector<T,Alloc>& x, vector<T,Alloc>& y) {
x.swap(y);
}
}