-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCustomVector.hpp
More file actions
301 lines (268 loc) · 4.47 KB
/
CustomVector.hpp
File metadata and controls
301 lines (268 loc) · 4.47 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
#include <stdio.h>
#include "macros.h"
#include <exception>
typedef unsigned long long size_t;
template <class Value>
class CustomVector
{
private:
size_t _capacity;
size_t _size;
Value *array;
unsigned long long int newCapacity(size_t number)
{
size_t tmp = 1;
while (tmp < number)
{
tmp *= 2;
}
return tmp;
}
void buildArray()
{
if (this->array == nullptr)
{
this->array = new Value[this->_capacity];
// set to 0
for (int i = 0; i < this->_capacity; i++)
{
this->array[i] = 0;
}
return;
}
Value *tmp = this->array;
this->array = new Value[this->_capacity];
for (int i = 0; i < this->_capacity; i++)
{
if (i < _size)
{
this->array[i] = tmp[i];
}
else
{
this->array[i] = 0;
}
}
delete[] tmp;
}
class BaseIterator
{
protected:
Value *ptr;
Value *end;
Value *start;
public:
bool operator==(const BaseIterator &other)
{
return this->ptr == other.ptr;
}
bool operator!=(const BaseIterator &other)
{
return this->ptr != other.ptr;
}
Value &operator*()
{
return *(this->ptr);
}
BaseIterator operator->()
{
return this;
}
BaseIterator &operator=(const BaseIterator &other)
{
this->ptr = other->ptr;
this->start = other->start;
this->end = other->end;
}
};
public:
CustomVector()
{
this->_size = 0;
this->_capacity = 0;
this->array = nullptr;
}
CustomVector(const int &size)
{
this->assign(size, 0);
}
~CustomVector()
{
delete[] this->array;
this->_size = 0;
this->_capacity = 0;
}
size_t size()
{
return this->_size;
}
void resize(size_t size)
{
this->_size = size;
this->_capacity = size;
this->buildArray();
}
bool isempty()
{
return this->_size == 0;
}
void clear()
{
this->~CustomVector();
this->_size = 0;
this->_capacity = 0;
this->array = nullptr;
}
void push_back(const Value &value)
{
if (this->_size >= _capacity)
{
this->_capacity = this->_capacity == 0 ? 1 : newCapacity(this->_size);
this->buildArray();
}
this->array[_size] = value;
this->_size++;
}
void pop_back()
{
this->array[_size - 1] = 0;
this->_size--;
}
void assign(const size_t &newSize, const Value &value)
{
this->clear();
this->_capacity = newCapacity(newSize);
this->_size = newSize;
this->array = new Value[_capacity];
for (int i = 0; i < this->_size; i++)
{
this->array[i] = value;
}
}
Value &operator[](const size_t &index)
{
return this->array[index];
}
Value &at(size_t index)
{
if (index >= this->_size)
{
// out of bounds
throw std::exception("Out of bounds");
}
if (index < 0)
{
// go backwards
if (index * -1 > this->_size)
index %= this->_size;
return this->array[this->_size - index];
}
return this->array[index];
}
Value &front()
{
return this->array[0];
}
Value &back()
{
return this->array[this->_size - 1];
}
size_t capacity()
{
return this->_capacity;
}
Value *data()
{
return this->array;
}
class Iterator : public BaseIterator
{
public:
Iterator()
{
this->ptr = nullptr;
this->end = nullptr;
this->start = nullptr;
}
Iterator(Value *ptr, Value *start, Value *end)
{
this->ptr = ptr;
this->end = end;
this->start = start;
}
~Iterator() {}
void operator++(int)
{
this->ptr += 1;
if (this->ptr == this->end)
this->ptr = nullptr;
}
void operator--(int)
{
this->ptr -= 1;
if (this->ptr < this->start)
this->ptr = nullptr;
}
};
Iterator begin()
{
return Iterator(this->array, this->array, this->array + this->_size);
}
Iterator end()
{
return Iterator();
}
const Iterator cbegin()
{
return this->begin();
}
const Iterator cend()
{
return this->end();
}
class ReverseIterator : public BaseIterator
{
public:
ReverseIterator()
{
this->ptr = nullptr;
this->end = nullptr;
this->start = nullptr;
}
ReverseIterator(Value *ptr, Value *start, Value *end)
{
this->ptr = ptr;
this->end = end;
this->start = start;
}
~ReverseIterator(){};
void operator++(int)
{
this->ptr -= 1;
if (this->ptr < this->start)
this->ptr = nullptr;
};
void operator--(int)
{
this->ptr += 1;
if (this->ptr == this->end)
this->ptr = nullptr;
};
};
ReverseIterator rbegin()
{
return ReverseIterator(this->array, this->array + this->_size);
}
ReverseIterator rend()
{
return ReverseIterator();
}
const ReverseIterator crbegin()
{
return this->rbegin();
}
const ReverseIterator crend()
{
return this->rend();
}
};