-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvector.h
387 lines (334 loc) · 6.54 KB
/
vector.h
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
#include <iostream>
#include <algorithm>
#include <stdexcept>
using namespace std;
template <typename T>
class Vector
{
public:
class Constiterator;
class Iterator;
using value_type = T;
using size_type = size_t;
using difference_type = ptrdiff_t;
using reference = T &;
using const_reference = const T &;
using pointer = T *;
using const_pointer = const T *;
using iterator = Iterator;
using const_iterator = Constiterator;
private:
size_t max_sz;
size_t sz;
static size_t constexpr min_sz = 5;
T *values;
// increasing of capacity
void reserve(size_t new_len)
{
T *new_array = new T[new_len];
for (size_t i{0}; i < sz; i++)
new_array[i] = values[i];
delete[] values;
values = new_array;
this->max_sz = new_len;
}
public:
Vector() : Vector(min_sz)
{
}
Vector(size_t max_sz) : max_sz{max_sz}
{
if (max_sz < min_sz)
max_sz = min_sz;
sz = 0;
values = new T[max_sz];
}
Vector(initializer_list<T> il)
{
values = il.size() > 0 ? new T[il.size()] : nullptr;
max_sz = il.size();
sz = 0;
for (const auto &elem : il)
values[sz++] = elem;
}
// Copy contructor
Vector(const Vector &right)
{
size_t i = 0;
values = new T[right.max_sz];
for (i = 0; i < right.sz; i++)
{
this->values[i] = right.values[i];
}
sz = i;
this->max_sz = right.max_sz;
}
Vector(Vector &&src)
{
values = src.values;
sz = src.sz;
max_sz = src.max_sz;
src.values = nullptr;
src.sz = src.max_sz = 0;
}
// Destructor
~Vector()
{
delete[] values;
}
// returns the size of the vector
size_t size() const
{
return sz;
}
size_t capacity() const
{
return max_sz;
}
// checks if the vector is empty
bool empty()
{
return sz == 0;
}
void clear()
{
sz = 0;
}
// Adds one element to the end of vector
void push_back(T value)
{
if (sz == max_sz)
reserve(max_sz * 2 + 5);
values[sz] = value;
sz++;
}
// delete one lement
void pop_back()
{
if (sz > 0)
sz--;
}
void shrink_to_fit()
{
max_sz = sz;
T *new_values = new T[max_sz];
for (size_t i = 0; i < sz; i++)
new_values[i] = values[i];
delete[] values;
values = new_values;
}
T &operator[](size_t index)
{
return values[index];
}
const T &operator[](size_t index) const
{
return values[index];
}
// overloading of operator =
Vector<T> &operator=(const Vector &src)
{
if (this == &src)
return *this;
delete[] this->values;
size_t i = 0;
this->values = new T[src.max_sz];
for (i = 0; i < src.sz; i++)
{
this->values[i] = src.values[i];
}
this->sz = src.sz;
this->max_sz = src.max_sz;
return *this;
}
Vector &operator=(Vector &&rhs)
{
delete[] values;
values = rhs.values;
sz = rhs.sz;
max_sz = rhs.max_sz;
rhs.values = nullptr;
rhs.sz = rhs.max_sz = 0;
}
class Iterator
{
T *ptr;
Vector<T> *v;
public:
using value_type = Vector ::value_type;
using difference_type = Vector::difference_type;
using reference = Vector::reference;
using pointer = Vector::pointer;
using iterator_category = std::forward_iterator_tag;
Iterator(T *new_iterator, Vector *new_vector)
{
this->ptr = new_iterator;
this->v = new_vector;
}
const Iterator &operator++()
{
if (ptr >= ((*v).end()).ptr)
throw runtime_error("Out of bounds");
this->ptr = this->ptr + 1;
return *this;
}
bool operator!=(const Iterator &src) const
{
return !(this->ptr == src.ptr);
}
T &operator*()
{
if (ptr >= ((*v).end()).ptr)
throw runtime_error("Out of bounds");
return *ptr;
}
const T &operator*() const
{
if (ptr >= ((*v).end()).ptr)
throw runtime_error("Out of bounds");
return *ptr;
}
bool operator==(const Iterator &src) const
{
return this->ptr == src.ptr;
}
Iterator operator++(int)
{
Iterator old{*this};
++*this;
return old;
}
const T *operator->()
{
return ptr;
}
operator Constiterator()
{
return Constiterator(ptr, v);
}
};
class Constiterator
{
T *ptr;
const Vector<T> *v;
public:
using value_type = Vector ::value_type;
using difference_type = Vector::difference_type;
using reference = Vector::reference;
using pointer = Vector::pointer;
using iterator_category = std::forward_iterator_tag;
Constiterator(T *new_iterator, const Vector *new_vector)
{
this->ptr = new_iterator;
this->v = new_vector;
}
Constiterator &operator++()
{
if (ptr >= ((*v).end()).ptr)
throw runtime_error("Out of bounds");
this->ptr = this->ptr + 1;
// aufg1: ptr - 1
return *this;
}
bool operator!=(const Constiterator &src) const
{
return !(this->ptr == src.ptr);
}
const T &operator*() const
{
if (ptr >= ((*v).end()).ptr)
throw runtime_error("Out of bounds");
return *ptr;
}
const T &operator*()
{
if (ptr >= ((*v).end()).ptr)
throw runtime_error("Out of bounds");
return *ptr;
}
bool operator==(const Constiterator &src) const
{
return this->ptr == src.ptr;
}
Constiterator operator++(int)
{
Constiterator old{*this};
++*this;
return old;
}
const T *operator->()
{
return ptr;
}
friend difference_type operator-(const Vector::Constiterator &lop,
const Vector::Constiterator &rop)
{
return lop.ptr - rop.ptr;
}
};
Iterator begin()
{
return Iterator(values, this);
}
Iterator end()
{
return Iterator(values + sz, this);
}
Constiterator begin() const
{
return Constiterator(values, this);
}
Constiterator end() const
{
return Constiterator(values + sz, this);
}
iterator erase(const_iterator pos)
{
auto diff = pos - begin();
if (diff < 0 || static_cast<size_type>(diff) >= sz)
throw runtime_error("Iterator out of bounds");
size_type current{
static_cast<size_type>(diff)};
for (; current < sz - 1; ++current)
values[current] = values[current + 1];
--sz;
return iterator{
values + current, this};
}
iterator insert(const_iterator pos,
Vector<T>::const_reference val)
{
auto diff = pos - begin();
if (diff < 0 || static_cast<size_type>(diff) > sz)
throw runtime_error("Iterator out of bounds");
size_type current{
static_cast<size_type>(diff)};
if (sz >= max_sz)
reserve(max_sz * 2 + 10);
for (size_type i{sz}; i-- > current;)
values[i + 1] = values[i];
values[current] = val;
++sz;
return iterator{values + current, this};
}
ostream &print(ostream &o) const
{
o << "[";
for (size_t i = 0; i < sz; i++)
if (i == sz - 1)
{
o << values[i];
}
else
{
o << values[i] << ",";
}
o << "]";
return o;
}
};
template <typename T>
ostream &operator<<(ostream &o, const Vector<T> &t)
{
return t.print(o);
}