-
Notifications
You must be signed in to change notification settings - Fork 0
/
vector.hpp
378 lines (343 loc) · 9.11 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
#ifndef VECTOR_HPP
#define VECTOR_HPP
#include <cstddef> //NULL
#include <memory> /* allocator */
#include <iostream>
#include "iterator.hpp"
#include "rest.hpp"
namespace ft {
template <typename T, typename Alloc = std::allocator<T> >
class vector {
public:
typedef T value_type;
typedef Alloc allocator_type;
typedef T& reference;
typedef const T& const_reference;
typedef T* pointer;
typedef const T* const_pointer;
typedef size_t size_type;
//typedef ra_iterator<T, size_type> iterator;
//typedef ra_iterator<const T, size_type> const_iterator;
typedef T* iterator;
typedef const T* const_iterator;
typedef rev_ra_iterator<T> reverse_iterator;
typedef rev_ra_iterator<const T> const_reverse_iterator;
typedef ptrdiff_t difference_type;
private:
allocator_type _alloc;
size_type _size;
size_type _cap;
iterator _data;
public:
//constructor, destructor, assignment
// default constructor
explicit vector (const allocator_type& alloc = allocator_type()) : _alloc(alloc), _size(0), _cap(0), _data() {};
//fill constructor
explicit vector (size_type n, const T& val = T(), const allocator_type& alloc = allocator_type()) : _alloc(alloc), _size(n), _cap(n), _data(_alloc.allocate(n)) {
for (size_type i = 0; i < _size; i++) {
_alloc.construct(&_data[i], val);
}
}
//range constructor
vector (iterator first, iterator last, const allocator_type& alloc = allocator_type()) :
_alloc(alloc), _size(last - first), _cap(_size) {
if (first < last) {
_data = _alloc.allocate(_cap);
}
else if (first == last) {
_data = NULL;
return;
} else {
_size = 0;
_cap = 0;
_data = NULL;
throw std::length_error("vector");
}
for (size_type i = 0; first < last; first++) {
_alloc.construct(&_data[i++], *first);
}
}
//copy constructor
vector (const vector& v) : _alloc(v._alloc), _size(v._size), _cap(v._cap), _data(_alloc.allocate(_cap)) {
for (size_type i = 0; i < _size; i++) {
_alloc.construct(&_data[i], v._data[i]);
}
}
//destructor
~vector() {
if (_size) {
clear();
}
if (_data)
{
_alloc.deallocate(_data, _cap);
_data = NULL;
_cap = 0;
}
}
//copy assignment
//https://www.cplusplus.com/reference/vector/vector/operator=/
//The container preserves its current allocator,
//which is used to allocate storage in case of reallocation.
//these were already constructed
vector &operator=(const vector& v) {
if (this == &v)
return(*this);
if (_size) {
clear();
}
if (_cap && _cap < v._size) {
_alloc.deallocate(_data, _cap);
_data = NULL;
}
if (v._cap && !_data) {
_cap = v._cap;
_data = _alloc.allocate(_cap);
}
_size = v._size;
for (size_type i = 0; i < _size; i++) {
_alloc.construct(&_data[i], v._data[i]);
}
return (*this);
}
//iterators
iterator begin() {
return iterator(_data);
}
const_iterator begin() const {
return iterator(_data);
}
reverse_iterator rbegin() {
return reverse_iterator(_data + _size - 1);
}
const_reverse_iterator rbegin() const {
return reverse_iterator(_data + _size - 1);
}
iterator end() {
return begin() + _size;
}
const_iterator end() const {
return begin() + _size;
}
reverse_iterator rend() {
return reverse_iterator(_data -1);
}
const_reverse_iterator rend() const {
return const_reverse_iterator(_data -1);
}
//Capacity
size_type size() const {return _size;}
size_type max_size() const {return std::numeric_limits<size_type>::max() / sizeof(T);}
void resize(size_type n, value_type val = value_type()) {
if (n < _size) {
for (size_type i = n; i < _size; i++) {
_alloc.destroy(&_data[i]);
}
_size = n;
} else {
size_t newcap = _cap;
if (n > _cap)
newcap = std::max(n, _cap * 2);
reserve(newcap);
for (size_type i = _size; i < n; i++) {
_alloc.construct(&_data[i], val);
}
_size = n;
_cap = newcap;
}
}
size_type capacity() const {return _cap;}
bool empty() const { return !_size; }
void reserve(size_type n) {
if (!_data) {
_cap = n;
_data = _alloc.allocate(_cap);
return;
}
if (n > max_size())
throw std::length_error("vector");
if (n > _cap) {
T* new_data = _alloc.allocate(n);
for (size_type i = 0; i < _size; i++) {
_alloc.construct(&new_data[i], _data[i]);
_alloc.destroy(&_data[i]);
}
_alloc.deallocate(_data, _cap);
_data = new_data;
_cap = n;
}
}
//Element access:
T& operator[](size_type idx) { return _data[idx]; }
const T& operator[](size_type idx) const { return _data[idx]; }
//https://stackoverflow.com/questions/123758/how-do-i-remove-code-duplication-between-similar-const-and-non-const-member-func
T& at(const size_type n) {
return const_cast<T &>(const_cast <const vector<T> &> (*this).at(n));
}
const T& at (size_type n) const {
if (n < 0 || n >= _size)
throw std::out_of_range("vector");
return _data[n];
}
T& front() {
//https://www.cplusplus.com/reference/vector/vector/front/
//Calling this function on an empty container causes undefined behavior.
return _data[0];
}
const T&front() const {
//https://www.cplusplus.com/reference/vector/vector/front/
//Calling this function on an empty container causes undefined behavior.
return _data[0];
}
T& back() {
//https://www.cplusplus.com/reference/vector/vector/back/
//Calling this function on an empty container causes undefined behavior.
return _data[_size - 1];
}
const T&back() const {
//https://www.cplusplus.com/reference/vector/vector/back/
//Calling this function on an empty container causes undefined behavior.
return _data[_size - 1];
}
//Modifiers
//
//assign range
template <class InputIterator>
void assign (InputIterator first, InputIterator last) {
if (!(first < last) && !(first == last)) {
throw std::length_error("vector");
}
reserve(last - first); //so it's exact
resize(last - first);
for (size_type i = 0; i < _size; i++) {
_data[i] = first[i];
}
}
//assign fill
void assign (size_type n, const value_type& val) {
reserve(n); //so it's exact
resize(n);
for (size_type i = 0; i < n; i++) {
_data[i] = val;
}
}
void push_back (const value_type& val) {
if (_cap == _size) {
if (_cap == 0)
reserve(1);
else if (_cap * 2 <= max_size())
reserve(_cap * 2);
else
reserve(_cap + 1);
}
_alloc.construct(&_data[_size], val);
_size++;
}
//If the container is not empty, the function never throws exceptions (no-throw guarantee).
//Otherwise, it causes undefined behavior.
void pop_back() {
resize(_size - 1);
}
//single element
iterator insert (iterator position, const value_type& val) {
iterator position_copy = position;
size_t idx = position - begin();
resize(_size + 1);
T s = val;
for (; begin() + idx < end(); idx++) {
T n = *(begin() + idx);
*(begin() + idx) = s;
s = n;
}
return position_copy;
}
//fill
void insert (iterator position, size_type n, const value_type& val) {
size_type start = position - begin();
resize(_size + n);
for (size_type i = _size - 1; i >= start + n; i--) {
_data[i] = _data[i - n];
}
for (size_type i = 0; i < n ; i++) {
(*this)[start + i] = val;
}
}
//range
template <class InputIterator>
void insert (iterator position, InputIterator first, InputIterator last) {
size_type start = position - begin();
size_type n = last - first;
resize(_size + n);
for (size_type i = _size - 1; i >= start + n; i--) {
_data[i] = _data[i - n];
}
for (size_type i = 0; i < n ; i++) {
(*this)[start + i] = *(first + i);
}
}
//single
iterator erase (iterator position) {
iterator position_copy = position;
_size --;
for (;position < end(); position++) {
*position = *(position + 1);
}
return position_copy;
}
//range
iterator erase (iterator first, iterator last) {
for (;last < end(); last++) {
*first = *last;
first++;
}
_size -= (last-first);
return last - 1;
}
void swap (vector& x) {
std::swap(_alloc, x._alloc);
std::swap(_size, x._size);
std::swap(_cap, x._cap);
std::swap(_data, x._data);
}
void clear() {
resize(0);
}
//Allocator
allocator_type get_allocator() const {
return _alloc;
}
};
//relational operators
template <class T, class Alloc>
bool operator== (const vector<T,Alloc>& lhs, const vector<T,Alloc>& rhs) {
if (lhs.size() != rhs.size())
return false;
return ft::equal(lhs.begin(), lhs.end(), rhs.begin());
}
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) {
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 lhs < rhs || lhs == rhs;
}
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) {
return lhs > rhs || lhs == rhs;
}
//swap
template <class T, class Alloc>
void swap (vector<T,Alloc>& x, vector<T,Alloc>& y) {
x.swap(y);
}
} //namespace ft
#endif