-
Notifications
You must be signed in to change notification settings - Fork 0
/
Vector.hpp
398 lines (346 loc) · 11.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
# pragma once
#include "miscellaneous.hpp"
#include "RandomAccessIterator.hpp"
#include "ReverseIterator.hpp"
// #include "../Iterator.hpp"
// #include "RandomAccessIterator.hpp"
// #include "Enable_if.hpp"
// #include "RelationalOperators.hpp"
// #include "Utilities.hpp"
namespace ft
{
template<class T, class Alloc = std::allocator<T> >
class Vector
{
public :
typedef T value_type;
typedef Alloc allocator_type;
typedef typename allocator_type::reference reference;
typedef typename allocator_type::const_reference const_reference;
typedef typename allocator_type::pointer pointer;
typedef typename allocator_type::const_pointer const_pointer;
typedef ft::RandomAccessIterator<value_type> iterator;
typedef ft::RandomAccessIterator<const value_type> const_iterator;
typedef ft::reverse_iterator<iterator> reverse_iterator;
typedef ft::reverse_iterator<const_iterator> const_reverse_iterator;
typedef size_t size_type;
typedef size_t difference_type;
///////////// Constructors Destructor
explicit Vector (const allocator_type& alloc = allocator_type()) : _alloc(alloc)
{
_array = nullptr;
_size = 0;
_capacity = 0;
}
explicit Vector (size_type n, const value_type& val = value_type(),
const allocator_type& alloc = allocator_type()): _alloc(alloc)
{
_size = n;
_capacity = n;
if (n <= 0)
return ;
_array = _alloc.allocate(n);
for (size_t i = 0; i < n; i++)
_alloc.construct(&_array[i], val);
}
template<class InputIterator>
Vector(InputIterator first, InputIterator last,
const allocator_type& alloc = allocator_type(), typename enable_if<!is_integral<InputIterator>::value, InputIterator>::type = InputIterator()) : _alloc(alloc)
{
difference_type n = last - first;
// if (n < 0)
// n *= -1;
_capacity = n;
_size = n;
if (n <= 0)
return;
_array = _alloc.allocate(n);
for(size_t i = 0; i < n && first != last; i++, first++)
_alloc.construct(&_array[i], *first);
}
Vector (const Vector& copy)
{
_capacity = copy.capacity();
_size = copy.size();
_alloc = copy._alloc;
_array = _alloc.allocate(_capacity);
for(size_t i = 0; i < _capacity; i++)
_alloc.construct(&_array[i], copy._array[i]);
}
~Vector()
{
this->clear();
_alloc.deallocate(_array, _capacity);
}
Vector& operator= (const Vector& x)
{
reAlloc(x);
return (*this);
}
///////Iterators
iterator begin() {return (iterator(_array));}
iterator end() {return (iterator(_array + _size));}
reverse_iterator rbegin() {return (reverse_iterator(end()));}
reverse_iterator rend() {return (reverse_iterator(begin()));}
const_iterator begin() const {return (iterator(_array));}
const_iterator end() const {return (iterator(_array + _size));}
const_reverse_iterator rbegin() const {return (const_reverse_iterator(end()));}
const_reverse_iterator rend() const {return (const_reverse_iterator(begin()));}
///////////// Capacity
size_type size() const { return _size; }
size_type max_size() const { return _alloc.max_size();}
void resize (size_type n, value_type val = value_type())
{
if (n < _size) {
for (size_type i = n; i < _size; i++)
_alloc.destroy(_array + i);
_size = n;
} else if (n <= _capacity) {
for (; _size < n; _size++)
_alloc.construct( _array + _size, val );
} else {
size_type cap = _capacity;
if (_capacity * 2 < n)
_capacity = n;
else
_capacity *= 2;
pointer tmp = _alloc.allocate(_capacity);
for (size_type i = 0; i < n; i++) {
if (i < _size)
_alloc.construct(tmp + i, _array[i]);
else
_alloc.construct(tmp + i, val);
}
clear();
if (_array)
_alloc.deallocate(_array, cap);
_size = n;
_array = tmp;
}
}
size_type capacity() const { return _capacity; }
bool empty() const { return ((_size == 0) ? true : false); }
void reserve (size_type n) { if (n > _capacity) reAlloc(n); }
///////////// Element access:
reference operator[](size_t index) { return _array[index];}
const_reference operator[](size_t index) const { return _array[index];}
reference at (size_type n) { if (n > _size) throw std::out_of_range("out of range"); else return _array[n];}
const_reference at (size_type n) const { if (n > _size) throw std::out_of_range("out of range"); else return _array[n];}
reference front() {return _array[0];}
const_reference front() const {return _array[0];}
reference back() {return (_array[_size - 1]);}
const_reference back() const {return (_array[_size - 1]);}
///////////// Modifiers:
template <class InputIterator>
void assign (InputIterator first, InputIterator last, typename ft::enable_if<!ft::is_integral<InputIterator>::value, InputIterator>::type = InputIterator())
{
size_t dist = last - first;//std::distance(first, last);
for (size_t i = 0; i < _size; i++)
_alloc.destroy(&_array[i]);
if (_capacity < dist)
{
_alloc.deallocate(_array, _capacity);
_array = _alloc.allocate(dist);
_capacity = dist;
_size = dist;
}
for (size_t i = 0; i < dist && first != last; first++, i++)
_alloc.construct(&_array[i], *first);
_size = dist;
}
void assign (size_type n, const value_type& val)
{
for (size_type i = 0; i < _size; i++)
_alloc.destroy(&_array[i]);
if (n > _capacity)
{
_alloc.deallocate(_array, _capacity);
_array = _alloc.allocate(n);
_capacity = n;
}
for (size_type i = 0; i < n; i++)
_alloc.construct(&_array[i], val);
_size = n;
}
void push_back (const value_type& val)
{
if (_size == _capacity)
_capacity == 0 ? reserve(1) : reserve(_capacity * 2);
_alloc.construct(&_array[_size], val);
_size++;
}
void pop_back() { _alloc.destroy(&_array[_size--]);}
iterator insert (iterator position, const value_type& val)
{
difference_type at = position - _array;
if (_size == _capacity)
_capacity == 0 ? reserve(1) : reserve(_capacity * 2);
for (difference_type i = _size; i > at; i--)
_alloc.construct(&_array[i], _array[i - 1]);
_alloc.construct(&_array[at], val);
_size++;
return (_array + at);
}
void insert (iterator position, size_type n, const value_type& val)
{
int at = position - _array;
if ((_size + n) > _capacity)
reserve((n > _size) ? (_size + n) : (_capacity * 2));
else if (_size == 0)
reserve(n);
for (int i = _size - 1; i >= at; i--)
_alloc.construct(&_array[i + n], _array[i]);
for (size_type i = 0; i < n; i++, at++)
_alloc.construct(&_array[at], val);
_size += n;
}
template <class InputIterator>
void insert (iterator position, InputIterator first, InputIterator last, typename ft::enable_if<!ft::is_integral<InputIterator>::value>::type* = 0)
{
size_type n = std::distance(first, last);
size_type i = 0;
size_type pos = position - begin();
if (_capacity < _size + n && n <= _size)
reserve(_capacity * 2);
else if (_size + n > _capacity)
reserve(_capacity + n);
while (_size + n - i > 0)
{
if (_size - i == pos)
{
pos = n;
try
{
while (pos--)
_alloc.construct(&_array[_size - i + pos], *(--last));
}
catch (...)
{
for (size_type i = size(); i != 0; i--)
_alloc.destroy(&_array[i - 1]);
_capacity = 0;
throw "OOR";
}
break;
}
else
_alloc.construct(&_array[_size - i + n - 1] , _array[_size - i - 1]);
i++;
}
_size += n;
}
iterator erase (iterator position)
{
size_t at = position - _array;
//_alloc.destroy(&_array[at]);
for (size_t i = at; i < _size - 1; i++)
_alloc.construct(&_array[i], _array[i + 1]);
_size--;
return (_array + at);
}
iterator erase (iterator first, iterator last)
{
size_type at = first - _array;
size_type n = last - first;
for (size_type i = at; i < _size - n; i++)
{
//_alloc.destroy(&_array[i]);
_alloc.construct(&_array[i], _array[i + n]);
}
_size -= n;
return (_array + at);
}
void swap (Vector& x)
{
value_type *tmp_array;
size_type tmp_size;
size_type tmp_capacity;
tmp_array = x._array;
tmp_size = x._size;
tmp_capacity = x._capacity;
x._array = this->_array;
x._size = this->_size;
x._capacity = this->_capacity;
this->_array = tmp_array;
this->_size = tmp_size;
this->_capacity = tmp_capacity;
}
void clear() { for(size_type i = 0; i < _size; i++) _alloc.destroy(&_array[i]); _size = 0; }
///////////// Allocator
allocator_type get_allocator() const { return (_alloc); }
private:
void reAlloc(const Vector& x)
{
if (this == &x)
return ;
if (_array != nullptr)
_alloc.deallocate(_array, _capacity);
_size = x.size();
_capacity = x.capacity();
_array = _alloc.allocate(_capacity);
for(size_t i = 0; i < _size; i++)
_alloc.construct(&_array[i], x._array[i]);
}
void reAlloc(size_t newCap)
{
if (newCap > _capacity)
{
value_type *newArray = _alloc.allocate(newCap);
for(size_t i = 0; i < _size; i++)
{
_alloc.construct(&newArray[i], _array[i]);
_alloc.destroy(&_array[i]);
}
_alloc.deallocate(_array, _capacity);
_array = newArray;
_capacity = newCap;
}
}
private :
value_type *_array;
size_type _size;
size_type _capacity;
allocator_type _alloc;
pointer _end;
pointer _begin;
pointer _endofcapacity;
};
template<class T, class Alloc>
bool operator== (const Vector<T,Alloc>& lhs, const Vector<T,Alloc>& rhs)
{
if (lhs.size() != rhs.size())
return (lhs.size() == rhs.size());
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 (ft::lexicographical_compare(rhs.begin(), rhs.end(), lhs.begin(), lhs.end()));
}
template<class T, class Alloc>
bool operator<= (const Vector<T,Alloc>& lhs, const Vector<T,Alloc>& rhs)
{
if (lhs < rhs || lhs == rhs)
return (true);
return (false);
}
template<class T, class Alloc>
bool operator>= (const Vector<T,Alloc>& lhs, const Vector<T,Alloc>& rhs)
{
if (lhs > rhs || lhs == rhs)
return (true);
return (false);
}
template<class T, class Alloc>
void swap(Vector<T, Alloc>& x, Vector<T, Alloc>& y) { x.swap(y);}
}