-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvector.hpp
438 lines (406 loc) · 12.6 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
#pragma once
#ifndef VECTOR_HPP
# define VECTOR_HPP
#include <memory>
#include "iterators/RandomAccessIterator.hpp"
#include "iterators/iterator_traits.hpp"
#include "iterators/reverse_iterator.hpp"
#include "utils/SFINAE.hpp"
#include "utils/lexicographical_compare.hpp"
#include "utils/equal.hpp"
namespace ft {
template <
class T,
class Allocator = std::allocator<T>
>
class vector {
public:
//
// TYPES DEFINITIONS
//
typedef T value_type;
typedef Allocator allocator_type;
typedef std::size_t size_type;
typedef std::ptrdiff_t difference_type;
typedef value_type& reference;
typedef const value_type& const_reference;
typedef typename Allocator::pointer pointer;
typedef typename Allocator::const_pointer const_pointer;
typedef ft::RandomAccessIterator<T> iterator;
typedef ft::RandomAccessIterator<const T> const_iterator;
typedef ft::reverse_iterator<iterator> reverse_iterator;
typedef ft::reverse_iterator<const_iterator> const_reverse_iterator;
//
// CONSTRUCTORS AND DESTRUCTORS
//
// DEFAULT
explicit vector (const allocator_type& alloc = allocator_type())
: _alloc(alloc), _buffer(NULL), _capacity(0), _size(0) { };
// FILL CONSTRUCTEUR
vector (size_type n, const value_type& val = value_type(), const allocator_type& alloc = allocator_type())
: _alloc(alloc), _buffer(_alloc.allocate(n)), _capacity(n), _size(n) {
for (size_t i = 0; i < n; i++)
_alloc.construct(_buffer + i, val);
}
// RANGE
template <class InputIterator>
vector (InputIterator first, InputIterator last, const allocator_type& alloc = allocator_type(), typename ft::enable_if<!ft::is_integral<InputIterator>::value, int>::type =0)
: _alloc(alloc), _buffer(NULL), _capacity(0), _size(0) {
for(; first != last; first++)
push_back(*first);
};
// COPY
vector(const vector& from)
: _alloc(from._alloc), _buffer(_alloc.allocate(from._capacity)), _capacity(from._capacity), _size(from._size) {
for (size_type i = 0; i < _size; i++)
_alloc.construct(_buffer + i, from[i]);
};
~vector() {
if (!_buffer)
return ;
for (size_type i = 0; i < _size; i++)
_alloc.destroy(_buffer + i);
_alloc.deallocate(_buffer, _capacity);
};
vector& operator=(const vector& rhs) {
if (this == &rhs)
return (*this);
manage_array(rhs._size, 0);
for (size_type i = 0; i < rhs._size; i++)
_alloc.construct(_buffer + i, rhs._buffer[i]);
_size = rhs._size;
// _capacity = rhs._capacity;
return (*this);
}
//
// ITERATORS
//
iterator begin() {
return (iterator(_buffer));
}
iterator end() {
return (iterator(_buffer + _size));
}
const_iterator begin() const {
return (const_iterator(_buffer));
}
const_iterator end() const {
return (const_iterator(_buffer + _size));
}
reverse_iterator rbegin() {
return (reverse_iterator(_buffer + _size));
};
const_reverse_iterator rbegin() const {
return (const_reverse_iterator(_buffer + _size));
};
reverse_iterator rend() {
return (reverse_iterator(_buffer));
};
const_reverse_iterator rend() const {
return (const_reverse_iterator(_buffer));
};
//
// CAPACITY
//
size_type size(void) const { return (_size); }
size_type max_size(void) const {
// return std::numeric_limits<size_type>::max() / sizeof(value_type);
return _alloc.max_size();
}
void resize(size_type n, value_type val = value_type()) {
if (n > _size)
insert(end(), n - _size, val);
if (n < _size)
erase(end() - (_size - n), end());
};
size_type capacity(void) const { return (_capacity); }
bool empty() const { return (!_size); }
void reserve(size_type n) {
if (n > _capacity)
return (manage_array(n, _size));
}
//
// ELEMENT ACCESS
//
reference operator[](size_type n) {
return (_buffer[n]);
}
const_reference operator[](size_type n) const {
return (_buffer[n]);
}
reference at(size_type n) {
if (n + 1 < 1 || n >= _size)
throw std::out_of_range("out of bound request");
return (_buffer[n]);
}
const_reference at (size_type n) const {
if (n + 1 < 1 || n >= _size)
throw std::out_of_range("out of bound request");
return (_buffer[n]);
}
reference front() {
return (*_buffer);
}
const_reference front() const {
return (*_buffer);
}
reference back() {
return (_buffer[_size - 1]);
}
const_reference back() const {
return (_buffer[_size - 1]);
}
//
// MODIFIERS
//
template <typename InputIterator>
typename ft::enable_if<!ft::is_integral<InputIterator>::value, void>::type
assign(InputIterator first, InputIterator last) {
typedef typename ft::iterator_traits<InputIterator>::iterator_category tag;
assign(first, last, tag());
}
void assign(size_type n, const value_type& val) {
size_type i = 0;
if (n > _capacity) {
manage_array(n, 0);
for (; i < n; i++)
_alloc.construct(_buffer + i, val);
}
else {
for (; i < _size && i < n; i++) {
_alloc.destroy(_buffer + i);
_alloc.construct(_buffer + i, val);
}
for (; i < _capacity && i < n; i++) {
_alloc.construct(_buffer + i, val);
}
}
_size = n;
}
void push_back(const value_type& val) {
reserve(_size + 1);
_alloc.construct(_buffer + _size, val);
_size++;
}
void pop_back() {
if (_size == 0)
return ;
_alloc.destroy(_buffer + _size - 1);
_size--;
}
void clear() {
for (size_t i = 0; i < _size; i++)
_alloc.destroy(_buffer + i);
_size = 0;
}
iterator insert(iterator position, const value_type& val) {
if (position == end()) {
push_back(val);
return (iterator(_buffer + _size - 1));
}
difference_type diff = ft::distance<iterator>(begin(), position);
if (diff > 0)
diff--;
manage_array(_size + 1, diff, 1);
_alloc.construct(_buffer + diff, val);
return (iterator(_buffer + diff));
}
void insert(iterator position, size_type n, const value_type& val) {
if (position == end()) {
manage_array(_size + n, _size);
for (size_type i = 0; i < n; i++)
_alloc.construct(_buffer + _size + i, val);
_size += n;
return ;
}
difference_type diff = ft::distance<iterator>(begin(), position);
if (diff > 0)
diff--;
manage_array(_size + n, diff, n);
for (size_type i = 0; i < n; i++)
_alloc.construct(_buffer + diff + i, val);
return ;
}
template <class InputIterator>
typename ft::enable_if<!ft::is_integral<InputIterator>::value, void>::type
insert(iterator position, InputIterator first, InputIterator last) {
typedef typename ft::iterator_traits<InputIterator>::iterator_category tag;
insert(position, first, last, tag());
}
iterator erase(iterator position) {
if (position == iterator(_buffer + _size - 1)) {
pop_back();
return (iterator(_buffer + _size));
}
difference_type diff = ft::distance<iterator>(begin(), position);
if (diff > 0)
diff--;
manage_array(_capacity, diff, 0, 1);
return (iterator(_buffer + diff));
}
iterator erase(iterator first, iterator last) {
difference_type length = ft::distance<iterator>(begin(), first);
difference_type to_erase = ft::distance<iterator>(first, last);
if (length > 0)
length--;
if (to_erase > 0)
to_erase--;
manage_array(_capacity, length, 0, to_erase);
return (iterator(_buffer + length));
}
void swap (vector& x) {
const pointer tmp_buffer = x._buffer;
const size_type tmp_size = x._size;
const size_type tmp_capacity = x._capacity;
x._buffer = _buffer;
x._size = _size;
x._capacity = _capacity;
_buffer = tmp_buffer;
_size = tmp_size;
_capacity = tmp_capacity;
}
private:
allocator_type _alloc;
T* _buffer;
size_type _capacity;
size_type _size;
/**
* Function to manage the reallocation of the array using the Allocator passed at the creation.
*
* @param n The new size to allocate.
* @param length The number of first element which will not be affected by this function
* @param insert The number of number of space that will be available,
* if offset != 0 the rest of the array will be copy after the offset.
* @param erase The number of element to erase
*/
void manage_array(size_type n, size_type length, size_type insert = 0, size_type erase = 0) {
T* new_buffer = _buffer;
size_type old_capacity = _capacity;
size_type i;
if (n > _capacity) {
if (n > max_size())
throw std::length_error("max size over");
size_type new_capacity = _capacity ? _capacity * 2 : 1;
while (new_capacity < n)
new_capacity *= 2;
new_buffer = _alloc.allocate(new_capacity);
for (size_type i = 0; i < length; i++)
{
_alloc.construct(new_buffer + i, _buffer[i]);
_alloc.destroy(_buffer + i);
}
_capacity = new_capacity;
}
// if insert > erase, we must copy right to left, if not we must copy left to right
if (insert > erase || (insert && !erase)) // ABC___EF <--
for (i = n < _size
? n + insert - erase - 1
: _size + insert - erase - 1;
i > length + insert - 1;
i--) {
_alloc.construct(new_buffer + i, _buffer[i - insert + erase]);
if (i - insert + erase >= length + insert)
_alloc.destroy(_buffer + i - insert + erase);
}
else if (insert < erase || (erase && !insert)) // ABC_ --> FGH
for (i = length + insert;
i < _size + insert - erase;
i++) {
_alloc.destroy(_buffer + i);
_alloc.construct(new_buffer + i, _buffer[i - insert + erase]);
}
// cleaning remaining insert elements
for (size_type i = length; i < length + insert && i < _size; i++)
_alloc.destroy(_buffer + i);
// cleaning remaining erased elements
for (i = n < _size
? n + insert - erase
: _size + insert - erase;
i < _size;
i++)
_alloc.destroy(_buffer + i);
if (new_buffer != _buffer && _buffer)
_alloc.deallocate(_buffer, old_capacity);
_buffer = new_buffer;
_size += insert - erase;
}
// SPECIALIZATION
template <typename InputIterator>
void assign(InputIterator first, InputIterator last, std::input_iterator_tag) {
clear();
for (; first != last; first++)
push_back(*first);
}
template <typename Iterator>
void assign(Iterator first, Iterator last, std::bidirectional_iterator_tag) {
size_type distance = static_cast<size_type>(ft::distance(first, last));
size_type i = 0;
if (distance > 0)
distance--;
if (distance < 1)
return (clear());
if (distance > _capacity) {
manage_array(distance, 0);
for (; first != last; first++)
_alloc.construct(_buffer + i++, *first);
}
else {
clear();
for (;i < distance; i++)
_alloc.construct(_buffer + i, *(first++));
}
_size = distance;
}
template <typename InputIterator>
void insert(iterator position, InputIterator first, InputIterator last, std::input_iterator_tag) {
typedef typename ft::iterator_traits<iterator>::iterator_category tag;
vector<value_type> temp;
for (; first != last; first++)
temp.push_back(*first);
insert(position, temp.begin(), temp.end(), tag());
}
template <typename InputIterator>
void insert(iterator position, InputIterator first, InputIterator last, typename iterator_traits<InputIterator>::iterator_category) {
size_type j = 0;
difference_type length = ft::distance<iterator>(begin(), position);
difference_type to_insert = static_cast<difference_type>(ft::distance<InputIterator>(first, last));
if (length > 0)
length--;
if (to_insert > 0)
to_insert--;
manage_array(_size + to_insert, length, to_insert);
for (; first != last; first++)
_alloc.construct(_buffer + length + j++, *first);
return ;
}
};
template <class T, class Alloc>
bool operator== (const vector<T,Alloc>& lhs, const vector<T,Alloc>& rhs) {
return (ft::equal(lhs.begin(), lhs.end(), rhs.begin(), rhs.end()));
}
template <class T, class Alloc>
bool operator!= (const vector<T,Alloc>& lhs, const vector<T,Alloc>& rhs) {
return !(operator==(lhs, rhs));
}
template <class T, class Alloc>
bool operator< (const vector<T,Alloc>& lhs, const vector<T,Alloc>& rhs) {
return (ft::lexicographical_compare(lhs.begin(), lhs.end(), rhs.begin(), rhs.end()));
}
template <class T, class Alloc>
bool operator<= (const vector<T,Alloc>& lhs, const vector<T,Alloc>& rhs) {
return !(operator<(rhs, lhs));
}
template <class T, class Alloc>
bool operator> (const vector<T,Alloc>& lhs, const vector<T,Alloc>& rhs) {
return (operator<(rhs, lhs));
}
template <class T, class Alloc>
bool operator>= (const vector<T,Alloc>& lhs, const vector<T,Alloc>& rhs) {
return !(operator<(lhs, rhs));
}
template <class T, class Alloc>
void swap(vector<T,Alloc>& x, vector<T,Alloc>& y) {
x.swap(y);
}
}
#endif