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
|
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Vector.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/04/24 16:19:04 by charles #+# #+# */
/* Updated: 2020/04/24 19:16:17 by charles ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef VECTOR_HPP
# define VECTOR_HPP
# include <stdexcept>
# define FT_VECTOR_GROWTH_FACTOR 1.5
namespace ft
{
template < typename T, typename Alloc = 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 ptrdiff_t difference_type;
typedef size_t size_type;
explicit Vector(const allocator_type& alloc = allocator_type())
: m_under(new value_type[0]), m_size(0), m_capacity(0)
{}
explicit Vector(size_type n,
const value_type& val = value_type(),
const allocator_type& alloc = allocator_type())
: m_under(new value_type[n]), m_size(n), m_capacity(n)
{
for (size_type i = 0; i < n; i++)
m_under[i] = val;
}
template <typename InputIterator>
Vector (InputIterator first,
InputIterator last,
const allocator_type& alloc = allocator_type())
: m_under(new value_type[0]), m_size(0), m_capacity(0)
{
for (; first != last; first++)
push_back(*first);
}
Vector (const Vector& x)
{
*this = x;
}
Vector& operator=(const Vector& x)
{
if (this == &x)
return *this;
delete [] m_under;
m_capacity = x.m_capacity;
m_size = x.m_size;
m_under = new value_type[m_capacity];
for (size_type i = 0; i < m_size; i++)
m_under[i] = x.m_under[i];
return *this;
}
~Vector()
{
delete [] m_under;
}
iterator begin() { return iterator(*this, 0, 1); }
const_iterator begin() const { return iterator(*this, 0, 1); }
iterator end() { return const_iterator(*this, m_size, 1); }
const_iterator end() const { return const_iterator(*this, m_size, 1); }
iterator rbegin() { return iterator(*this, m_size - 1, -1); }
const_iterator rbegin() const { return iterator(*this, m_size - 1, -1); }
iterator rend() { return const_iterator(*this, -1, -1); }
const_iterator rend() const { return const_iterator(*this, -1, -1); }
size_type size() const { return m_size; }
size_type max_size() const { return UINT_MAX; }
size_type capacity() const { return m_capacity; }
bool empty() const { return m_size == 0; }
void resize(size_type n, value_type val = value_type())
{
if (n == m_size)
return ;
if (n > m_size)
{
reserve(n);
for (; m_size < n; m_size++)
m_under[m_size] = val;
}
else
{
value_type* newUnder = new value_type[n];
m_capacity = n;
m_size = n;
for (size_type i = 0; i < m_size; i++)
newUnder[i] = m_under[i];
delete [] m_under;
m_under = newUnder;
}
}
void reserve(size_type n)
{
if (m_capacity < 2)
m_capacity = 2;
while (m_capacity < n)
m_capacity *= FT_VECTOR_GROWTH_FACTOR;
value_type* newUnder = new value_type[m_capacity];
for (size_type i = 0; i < m_size; i++)
newUnder[i] = m_under[i];
delete [] m_under;
m_under = newUnder;
}
reference operator[](size_type n) { return m_under[n]; }
const_reference operator[](size_type n) const { return m_under[n]; }
reference at(size_type n)
{
if (n >= m_size)
throw std::out_of_range("vector::_M_range_check");
return m_under[n];
}
const_reference at(size_type n) const
{
if (n >= m_size)
throw std::out_of_range("vector::_M_range_check");
return m_under[n];
}
reference front() { return m_under[0]; }
const_reference front() const { return m_under[0]; }
reference back() { return m_under[m_size - 1]; }
const_reference back() const { return m_under[m_size - 1]; }
template <typename InputIterator>
void assign(InputIterator first, InputIterator last)
{
size_type count;
InputIterator copy(first);
for (count = 0; copy != last; copy++)
count++;
reserve(count);
for (size_type i = 0; first != last; first++, i++)
m_under[i] = *first;
}
void assign(size_type n, const value_type& val)
{
reserve(n);
for (size_type i = 0; i < n; ++)
m_under[i] = val;
}
void push_back(const value_type& val)
{
m_size++;
reserve(m_size);
m_under[m_size - 1] = val;
}
void pop_back()
{
m_size--;
~value_type(m_under[m_size]);
}
iterator insert(iterator position, const value_type& val)
{
m_size++;
reserve(m_size);
for (iterator it = rbegin(); it != position; ++it)
*it = *(it - 1);
*position = val;
}
void insert(iterator position, size_type n, const value_type& val)
{
reserve(m_size + n);
while (n-- > 0)
insert(position, val);
}
template <typename InputIterator>
void insert(iterator position, InputIterator first, InputIterator last)
{
for (; first != last; ++first)
insert(position, *first);
}
iterator erase(iterator position)
{
for (; position != end(); ++position)
*position = *(position + 1);
m_size--;
}
iterator erase(iterator first, iterator last)
{
size_type i;
for (i = 0; first != last && last + i != end(); ++first, i++)
*first = *(last + i);
m_size -= i;
}
void swap(Vector& x)
{
value_type* tmpUnder = m_under;
size_type* tmpSize = m_size;
size_type* tmpCapacity = m_capacity;
m_under = x.m_under;
m_size = x.m_size;
m_capacity = x.m_capacity;
x.m_under = tmpUnder;
x.m_size = tmpSize;
x.m_capacity = tmpCapacity;
}
void clear()
{
delete [] m_under;
m_under = new value_type[0];
m_size = 0;
m_capacity = 0;
}
class iterator
{
public:
iterator() : m_pos(-1) {}
iterator(const iterator& other) { *this = other; }
iterator& operator=(const iterator& other)
{
if (this == &other)
return *this;
m_ref = other.m_ref;
m_pos = other.m_pos;
m_inc = other.m_inc;
return *this;
}
~iterator() {}
value_type& operator*() { return m_ref[m_pos]; }
value_type& operator->() { return m_ref[m_pos]; }
bool operator==(const iterator& other) { return m_pos == other.m_pos; }
bool operator!=(const iterator& other) { return !(operator==(other)); }
bool operator<(const iterator& other) { return m_pos < other.m_pos; }
bool operator>(const iterator& other) { return !(this <= other); }
bool operator<=(const iterator& other) { return !(this > other); }
bool operator>=(const iterator& other) { return !(this < other); }
iterator& operator++() { m_pos += m_inc; return *this; }
iterator& operator--() { m_pos -= m_inc; return *this; }
iterator& operator++(int) { iterator copy(this); m_pos += m_inc; return copy; }
iterator& operator--(int) { iterator copy(this); m_pos -= m_inc; return copy; }
iterator operator+(int n) { iterator copy(this); return copy += n; }
iterator operator-(int n) { iterator copy(this); return copy -= n; }
iterator operator+(iterator& it) { iterator copy(this); return copy += it.m_pos; }
iterator operator-(iterator& it) { iterator copy(this); return copy -= it.m_pos; }
iterator& operator+=(int n) { m_pos += n; return *this; }
iterator& operator-=(int n) { m_pos -= n; return *this; }
value_type& operator[](size_type n) { return m_ref[m_pos + n]; }
private:
iterator(Vector* ref, size_type pos, int inc)
: m_pos(pos), m_ref(ref), m_inc(int) {}
Vector& m_ref;
size_type m_pos;
int inc;
}
class const_iterator : public iterator
{
const value_type& operator*() { return m_ref[m_pos]; }
const value_type& operator->() { return m_ref[m_pos]; }
}
private:
value_type* m_under;
size_type m_size;
size_type m_capacity;
};
}
template <typename T, typename Alloc>
bool operator==(const Vector<T, Alloc>& lhs, const Vector<T, Alloc>& rhs)
{
if (lhs.size() != rhs.size())
return false;
for (Vector::size_type i = 0; i < lhs.size(); i++)
if (!(lhs[i] == rhs[i]))
return false;
return true;
}
template <typename T, typename Alloc>
bool operator!=(const Vector<T, Alloc>& lhs, const Vector<T, Alloc>& rhs)
{
return !(lhs == rhs);
}
template <typename T, typename Alloc>
bool operator<const Vector<T, Alloc>& lhs, const Vector<T, Alloc>& rhs)
{
for (Vector::size_type i = 0; i < lhs.size() && i < rhs.size(); i++)
if (lhs[i] < rhs[i] && !(rhs[i] < lhs[i]))
return true;
return false;
}
template <typename T, typename Alloc>
bool operator<=const Vector<T, Alloc>& lhs, const Vector<T, Alloc>& rhs)
{
return !(rhs < lhs);
}
template <typename T, typename Alloc>
bool operator>const Vector<T, Alloc>& lhs, const Vector<T, Alloc>& rhs)
{
return rhs < lhs;
}
template <typename T, typename Alloc>
bool operator>=const Vector<T, Alloc>& lhs, const Vector<T, Alloc>& rhs)
{
return !(lhs < rhs);
}
template <typename T, typename Alloc>
void swap(Vector<T, Alloc>& x, Vector<T, Alloc>& y)
{
x.swap(y);
}
#endif
|