-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvector.hpp
310 lines (268 loc) · 10.7 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* vector.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mannouao <mannouao@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/14 10:10:38 by mannouao #+# #+# */
/* Updated: 2022/07/29 14:56:43 by mannouao ### ########.fr */
/* */
/* ************************************************************************** */
# ifndef VECTOR_HPP
# define VECTOR_HPP
# include "iterator.hpp"
# include "type_traits.hpp"
# include "algorithm.hpp"
# include "utility.hpp"
# include <memory>
# include <stdexcept>
namespace ft
{
template<typename _Tp, typename _Allocater = std::allocator<_Tp> >
class vector
{
public:
typedef _Tp value_type;
typedef _Allocater allocater_type;
typedef typename allocater_type::reference reference;
typedef typename allocater_type::const_reference const_reference;
typedef typename allocater_type::pointer pointer;
typedef typename allocater_type::const_pointer const_pointer;
typedef ft::__wrap_iter<pointer> iterator;
typedef ft::__wrap_iter<const_pointer> const_iterator;
typedef ft::reverse_iterator<iterator> reverse_iterator;
typedef ft::reverse_iterator<const_iterator> const_reverse_iterator;
typedef typename allocater_type::difference_type difference_type;
typedef typename allocater_type::size_type size_type;
explicit vector(const allocater_type& alloc = allocater_type())
: __begin_(NULL),
__end_(NULL),
__capacity_(0),
__alloc_(alloc)
{}
explicit vector(size_type __n, const value_type& val = value_type(), const allocater_type& alloc = allocater_type())
: __begin_(NULL),
__end_(NULL),
__capacity_(0),
__alloc_(alloc)
{
if (__n > 0)
{
if(__n > max_size()) std::__throw_length_error("vector");
__begin_ = __end_ = __alloc_.allocate(__n);
for (; __capacity_ < __n ; ++__capacity_) __alloc_.construct(__end_++, val);
}
}
template<typename _Iter>
explicit vector(_Iter first,
typename ft::enable_if<std::__is_forward_iterator<_Iter>::value, _Iter>::type secend,
const allocater_type& alloc = allocater_type())
: __begin_(NULL),
__end_(NULL),
__capacity_(0),
__alloc_(alloc)
{
size_type __n = static_cast<size_type>(ft::distance(first, secend));
if (__n > 0)
{
if(__n > max_size()) std::__throw_length_error("vector");
__begin_ = __end_ = __alloc_.allocate(__n);
for (; first != secend ; ++first, ++__capacity_) __alloc_.construct(__end_++, *first);
}
}
template<typename _Iter>
explicit vector(_Iter first,
typename ft::enable_if<std::__is_input_iterator<_Iter>::value && !std::__is_forward_iterator<_Iter>::value, _Iter>::type secend,
const allocater_type& alloc = allocater_type())
: __begin_(NULL),
__end_(NULL),
__capacity_(0),
__alloc_(alloc)
{
while (first != secend)
push_back(*first++);
}
vector(const vector& other)
: __begin_(NULL),
__end_(NULL),
__capacity_(0),
__alloc_(other.get_allocator())
{
if (other.size() > 0)
{
if(other.size() > max_size()) std::__throw_length_error("vector");
__begin_ = __end_ = __alloc_.allocate(other.size());
for (size_type i = 0; i < other.size(); ++i, ++__capacity_) __alloc_.construct(__end_++, other[i]);
}
}
vector& operator = (const vector& other)
{
if (this != &other)
assign(other.begin(), other.end());
return *this;
}
~vector() { clear(); if(__begin_) __alloc_.deallocate(__begin_, __capacity_); }
reverse_iterator rbegin() { return reverse_iterator(end()); }
const_reverse_iterator rbegin() const { return const_reverse_iterator(end()); }
reverse_iterator rend() { return reverse_iterator(begin()); }
const_reverse_iterator rend() const { return const_reverse_iterator(begin()); }
iterator begin() { return iterator(__begin_); }
const_iterator begin() const { return const_iterator(__begin_); }
iterator end() { return iterator(__end_); }
const_iterator end() const { return const_iterator(__end_); }
size_type max_size() const { return __alloc_.max_size(); }
size_type capacity() const { return __capacity_; }
size_type size() const { return static_cast<size_type>(ft::distance(__begin_, __end_)); }
bool empty() const { return size() == 0; };
allocater_type get_allocator() const { return __alloc_; }
void clear() { while ( __end_ != __begin_ ) __alloc_.destroy(--__end_); }
void push_back(const value_type& val)
{
if (__capacity_ == size()) reserve(__capacity_ ? __capacity_ * 2 : 1);
__alloc_.construct(__end_++, val);
}
void pop_back() { if(!empty()) __alloc_.destroy(--__end_); }
void reserve(size_type __n)
{
if (__n > __capacity_)
{
if(__n > max_size()) std::__throw_length_error("vector");
vector __tmp(*this);
clear();
if (__begin_ != NULL) __alloc_.deallocate(__begin_, __capacity_);
__begin_ = __end_ = __alloc_.allocate(__n);
__capacity_ = __n;
for (size_type i = 0; i < __tmp.size(); ++i) __alloc_.construct(__end_++, __tmp[i]);
}
}
void resize(size_type __n, value_type val = value_type())
{
size_type __cs = size();
if (__n < __cs) while (__end_ != __begin_ + __n) __alloc_.destroy(--__end_);
else if (__n > __cs)
{
if (__n > __capacity_) reserve(__n > __capacity_ * 2 ? __n : __capacity_ * 2);
while (size() < __n) __alloc_.construct(__end_++, val);
}
}
template<typename _Iter>
void assign(_Iter _first,
typename ft::enable_if<std::__is_forward_iterator<_Iter>::value, _Iter>::type _last)
{
size_type __n = static_cast<size_type>(ft::distance(_first, _last));
clear();
if (__n > __capacity_) reserve(__n);
while (_first != _last)
__alloc_.construct(__end_++, *_first++);
}
template<typename _Iter>
void assign(_Iter _first,
typename ft::enable_if<std::__is_input_iterator<_Iter>::value && !std::__is_forward_iterator<_Iter>::value, _Iter>::type _last)
{
clear();
while (_first != _last)
push_back(*_first++);
}
void assign(size_type __n, const value_type& val)
{
clear();
if (__n > __capacity_) reserve(__n);
while (__n-- > 0) __alloc_.construct(__end_++, val);
}
reference front() { return *__begin_; }
const_reference front() const { return *__begin_; }
reference back() { return *(__end_ - 1); }
const_reference back() const { return *(__end_ - 1); }
reference operator [] (size_type __n) { return __begin_[__n]; }
const_reference operator [] (size_type __n) const { return __begin_[__n]; }
reference at(size_type __n) { if(__n >= size()) std::__throw_out_of_range("vector"); return __begin_[__n]; }
const_reference at(size_type __n) const { if(__n >= size()) std::__throw_out_of_range("vector"); return __begin_[__n]; }
iterator insert(iterator __pos, const value_type& val)
{
size_type __len = static_cast<size_type>(ft::distance(begin(), __pos));
insert(__pos, 1, val);
return begin() + __len;
}
void insert(iterator __pos, size_type __n, const value_type& val)
{
size_type __len = static_cast<size_type>(ft::distance(begin(), __pos));
if(__n + size() > __capacity_ * 2) reserve(__n + __capacity_);
else if (__n + size() > __capacity_) reserve(__capacity_ * 2);
__pos = begin() + __len;
for (iterator __tp = end(); __tp != __pos; --__tp)
{
__alloc_.construct(__tp.base() + __n - 1, *(__tp - 1));
__alloc_.destroy(__tp.base() - 1);
}
for (size_type i = 0; i < __n; ++i, ++__end_)
__alloc_.construct(__pos.base() + i, val);
}
template<typename _Iter>
void insert(iterator __pos, _Iter __first, _Iter __last)
{
size_type __len = static_cast<size_type>(ft::distance(begin(), __pos));
size_type __n = static_cast<size_type>(ft::distance(__first, __last));
if(__n + size() > __capacity_ * 2) reserve(__n + __capacity_);
else if (__n + size() > __capacity_) reserve(__capacity_ * 2);
__pos = begin() + __len;
for (iterator __tp = end(); __tp != __pos; --__tp)
{
__alloc_.construct(__tp.base() + __n - 1, *(__tp - 1));
__alloc_.destroy(__tp.base() - 1);
}
for (size_type i = 0; i < __n; ++i, ++__end_, ++__first)
__alloc_.construct(__pos.base() + i, *__first);
}
iterator erase(iterator __pos) { return (erase(__pos, __pos + 1)); }
iterator erase(iterator __first, iterator __last)
{
if (__first == __last)
return __first;
for (iterator __tp = __first; __tp != __last; ++__tp)
__alloc_.destroy(__tp.base());
size_type __len = ft::distance(__last, end());
for (size_type i = 0; i < __len; ++i)
{
__alloc_.construct(__first.base() + i, *(__last + i));
__alloc_.destroy(__last.base() + i);
}
__end_ -= ft::distance(__first, __last);
return __first;
}
void swap(vector& __x)
{
ft::swap(__begin_, __x.__begin_);
ft::swap(__end_, __x.__end_);
ft::swap(__capacity_, __x.__capacity_);
ft::swap(__alloc_, __x.__alloc_);
}
private:
pointer __begin_;
pointer __end_;
size_type __capacity_;
allocater_type __alloc_;
};
template<typename _Tp, typename _Allocater>
bool operator == (const ft::vector<_Tp, _Allocater>& __x, const ft::vector<_Tp, _Allocater>& __y)
{ return __x.size() == __y.size() && ft::equal(__x.begin(), __x.end(), __y.begin()); }
template<typename _Tp, typename _Allocater>
bool operator != (const ft::vector<_Tp, _Allocater>& __x, const ft::vector<_Tp, _Allocater>& __y)
{ return !(__x == __y); }
template<typename _Tp, typename _Allocater>
bool operator < (const ft::vector<_Tp, _Allocater>& __x, const ft::vector<_Tp, _Allocater>& __y)
{ return ft::lexicographical_compare(__x.begin(), __x.end(), __y.begin(), __y.end()); }
template<typename _Tp, typename _Allocater>
bool operator <= (const ft::vector<_Tp, _Allocater>& __x, const ft::vector<_Tp, _Allocater>& __y)
{ return !(__y < __x); }
template<typename _Tp, typename _Allocater>
bool operator > (const ft::vector<_Tp, _Allocater>& __x, const ft::vector<_Tp, _Allocater>& __y)
{ return __y < __x; }
template<typename _Tp, typename _Allocater>
bool operator >= (const ft::vector<_Tp, _Allocater>& __x, const ft::vector<_Tp, _Allocater>& __y)
{ return !(__x < __y); }
template<typename _Tp, typename _Allocater>
void swap (ft::vector<_Tp, _Allocater>& __x, ft::vector<_Tp, _Allocater>& __y)
{ __x.swap(__y); }
} // ft
# endif