-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvector.hpp
More file actions
379 lines (309 loc) · 8.35 KB
/
vector.hpp
File metadata and controls
379 lines (309 loc) · 8.35 KB
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* vector.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: badam <badam@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/07/02 19:49:40 by badam #+# #+# */
/* Updated: 2021/10/25 14:24:47 by badam ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef VECTOR_HPP
# define VECTOR_HPP
# include <stdexcept>
# include <cmath>
# include "utils.hpp"
# include "core.hpp"
# include "vector_iterator.hpp"
namespace ft
{
template< class T, class Alloc = std::allocator<T> >
class vector: public ft::core< T, Alloc, T, vector_iterator<T>, vector_iterator<const T> >
{
typedef vector _self;
typedef ft::core< T, Alloc, T, vector_iterator<T>, vector_iterator<const T> > _parent;
using typename _parent::_item;
using typename _parent::_item_ptr;
public:
using typename _parent::value_type;
using typename _parent::allocator_type;
using typename _parent::reference;
using typename _parent::const_reference;
using typename _parent::pointer;
using typename _parent::const_pointer;
using typename _parent::iterator;
using typename _parent::const_iterator;
using typename _parent::reverse_iterator;
using typename _parent::const_reverse_iterator;
using typename _parent::difference_type;
using typename _parent::size_type;
protected:
size_type _capacity;
pointer _content;
void _ensure_capacity(size_type n)
{
if (_capacity < n)
reserve(n);
}
void _extend_size(size_type n)
{
if (_parent::_size + n > _capacity)
_ensure_capacity(_parent::_size + n);
}
void _memmove(_item_ptr dst, _item_ptr src, size_type n)
{
if (dst < src)
{
while(n--)
{
_parent::_alloc->construct(dst++, *src);
_parent::_alloc->destroy(src++);
}
}
else if (dst > src)
{
src += n - 1;
dst += n - 1;
while(n--)
{
_parent::_alloc->construct(dst--, *src);
_parent::_alloc->destroy(src--);
}
}
}
size_type _get_iterator_index(iterator pos)
{
return (pos.getElem() ? pos.getElem() - _content : _parent::_size);
}
void _autoupdate(void)
{
if (_parent::_size)
_parent::_update(_content + 0, _content + _parent::_size - 1);
else
_parent::_update(NULL, NULL);
}
void _init(const allocator_type &alloc, size_type capacity = 10)
{
_capacity = 0;
_content = NULL;
_parent::_init(alloc);
_ensure_capacity(capacity);
}
public:
explicit vector(const allocator_type &alloc = allocator_type())
{
_init(alloc);
};
explicit vector(size_type n, const value_type& val = value_type(),
const allocator_type &alloc = allocator_type())
{
_init(alloc, n);
insert(*_parent::_end, n, val);
};
template <class InputIterator>
vector(InputIterator first, InputIterator last,
const allocator_type& alloc = allocator_type())
{
_init(alloc);
insert(*_parent::_end, first, last);
};
vector(const vector &x): _parent(x)
{
_init(x.get_allocator());
*this = x;
};
virtual ~vector(void)
{
if (_content)
{
while (_parent::_size)
_parent::_alloc->destroy(_content + --_parent::_size);
_parent::_alloc->deallocate(_content, _capacity);
}
};
_self &operator=(const vector &ref)
{
iterator it;
if (this == &ref)
return (*this);
assign(ref.begin(), ref.end());
return (*this);
};
size_type max_size(void) const
{
return (_parent::_max_size());
}
void resize(size_type n, T val = T())
{
iterator it = *_parent::_begin;
while (n && it != *_parent::_end)
{
--n;
++it;
}
if (n)
insert(*_parent::_end, n, val);
else
erase(it, *_parent::_end);
}
size_type capacity() const
{
return (_capacity);
}
void reserve(size_type n)
{
size_type i = 0;
pointer oldcontent = _content;
if (n > max_size())
throw std::length_error("n");
if (n + 10 > _capacity && n < _capacity)
return ;
_content = _parent::_alloc->allocate(n, oldcontent);
while (i < _parent::_size)
{
_parent::_alloc->construct(_content + i, oldcontent[i]);
_parent::_alloc->destroy(oldcontent + i);
++i;
}
if (oldcontent)
_parent::_alloc->deallocate(oldcontent, _capacity);
_capacity = n;
_autoupdate();
}
reference operator[](size_type n)
{
return (_content[n]);
}
const_reference operator[](size_type n) const
{
return (_content[n]);
}
reference at(size_type n)
{
if (n < 0 || n >= _parent::_size)
throw std::out_of_range("n");
return (_content[n]);
}
const_reference at(size_type n) const
{
if (n < 0 || n >= _parent::_size)
throw std::out_of_range("n");
return (_content[n]);
}
reference front(void)
{
return (*_parent::_front);
}
const_reference front(void) const
{
return (*_parent::_front);
}
reference back(void)
{
return (*_parent::_back);
}
const_reference back(void) const
{
return (*_parent::_back);
}
pointer data()
{
return (_content);
}
const_pointer data() const
{
return (_content);
}
template <class InputIterator>
void assign(InputIterator first, InputIterator last)
{
clear();
insert(*_parent::_end, first, last);
}
void assign(size_type n, const value_type &val)
{
clear();
insert(*_parent::_end, n, val);
}
void push_back(const value_type &val)
{
insert(*_parent::_end, val);
}
void pop_back(void)
{
erase(--(this->end()));
}
iterator insert(iterator position, const value_type &val)
{
size_type position_index = _get_iterator_index(position);
insert(position, 1, val);
return (ft::advance(this->begin(), position_index));
}
void insert(iterator position, size_type n, const value_type &val)
{
size_type position_index = _get_iterator_index(position);
size_type i = 0;
_extend_size(n);
if (position_index < _parent::_size)
_memmove(_content + position_index + n, _content + position_index, _parent::_size - position_index);
while (i < n)
_parent::_alloc->construct(_content + position_index + i++, val);
_parent::_size += n;
_autoupdate();
}
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 = static_cast<size_type>(ft::distance<InputIterator>(first, last));
size_type position_index = _get_iterator_index(position);
size_type i = 0;
_extend_size(n);
if (position_index < _parent::_size)
_memmove(_content + position_index + n, _content + position_index, _parent::_size - position_index);
while (i < n)
_parent::_alloc->construct(_content + position_index + i++, *(first++));
_parent::_size += n;
_autoupdate();
}
iterator erase(iterator first, iterator last)
{
size_type first_index = _get_iterator_index(first);
size_type last_index = _get_iterator_index(last);
size_type n = last_index - first_index;
size_type i = n;
while (i)
_parent::_alloc->destroy(_content + first_index + --i);
if (_parent::_size > last_index)
_memmove(_content + first_index, _content + last_index, _parent::_size - last_index);
_parent::_size -= n;
_autoupdate();
reserve(_parent::_size);
return (ft::advance(this->begin(), first_index));
}
iterator erase(iterator position)
{
return (erase(position, ft::advance(position, 1)));
}
void swap(_self &x)
{
_parent::_swap_pointer(&_capacity, &x._capacity);
_parent::_swap_pointer(&_content, &x._content);
_parent::_swap(x);
}
void clear(void)
{
erase(this->begin(), this->end());
}
allocator_type get_allocator(void) const
{
return (_parent::_get_allocator());
}
};
template< class T, class Alloc >
void swap( ft::vector<T,Alloc>& lhs, ft::vector<T,Alloc>& rhs )
{
lhs.swap(rhs);
}
}
#endif