ORIGINAL
Loading...
Searching...
No Matches
vector.h
1#ifndef VECTOR_H
2#define VECTOR_H
3
4#include "baseList.h"
5#include "iterationStream.h"
6#include "array.h"
7
8namespace original{
9 template <typename TYPE>
10 class vector final : public baseList<TYPE>, public iterationStream<TYPE, vector<TYPE>>{
11 uint32_t size_;
12 const uint32_t INNER_SIZE_INIT = 16;
13 uint32_t max_size;
14 uint32_t inner_begin;
15 TYPE* body;
16
17 void vectorInit();
18 void vectorDestruct() const;
19 static TYPE* vectorArrayInit(uint32_t size);
20 static void moveElements(TYPE* old_body, uint32_t inner_idx,
21 uint32_t len, TYPE* new_body, int64_t offset);
22 [[nodiscard]] uint32_t toInnerIdx(int64_t index) const;
23 [[nodiscard]] bool outOfMaxSize(uint32_t increment) const;
24 void grow(uint32_t new_size);
25 void adjust(uint32_t increment);
26
27 public:
28 class Iterator final : public randomAccessIterator<TYPE>
29 {
30 explicit Iterator(TYPE* ptr, const vector* container, int64_t pos);
31 public:
32 friend vector;
33 Iterator(const Iterator& other);
34 Iterator& operator=(const Iterator& other);
35 Iterator* clone() const override;
36 bool atPrev(const iterator<TYPE> *other) const override;
37 bool atNext(const iterator<TYPE> *other) const override;
38 [[nodiscard]] std::string className() const override;
39 };
40
41 explicit vector();
42 vector(const vector& other);
43 vector(const std::initializer_list<TYPE>& list);
44 explicit vector(const array<TYPE>& arr);
45 vector& operator=(const vector& other);
46 vector(vector&& other) noexcept;
47 vector& operator=(vector&& other) noexcept;
48 [[nodiscard]] uint32_t size() const override;
49 TYPE& data() const;
50 TYPE get(int64_t index) const override;
51 TYPE& operator[](int64_t index) override;
52 void set(int64_t index, const TYPE &e) override;
53 uint32_t indexOf(const TYPE &e) const override;
54 void pushBegin(const TYPE &e) override;
55 void push(int64_t index, const TYPE &e) override;
56 void pushEnd(const TYPE &e) override;
57 TYPE popBegin() override;
58 TYPE pop(int64_t index) override;
59 TYPE popEnd() override;
60 Iterator* begins() const override;
61 Iterator* ends() const override;
62 [[nodiscard]] std::string className() const override;
63 ~vector() override;
64 };
65}
66
67 template <typename TYPE>
68 auto original::vector<TYPE>::vectorInit() -> void
69 {
70 this->size_ = 0;
71 this->max_size = this->INNER_SIZE_INIT;
72 this->inner_begin = (this->INNER_SIZE_INIT - 1)/2;
73 this->body = vector::vectorArrayInit(this->INNER_SIZE_INIT);
74 }
75
76 template <typename TYPE>
77 auto original::vector<TYPE>::vectorDestruct() const -> void
78 {
79 delete[] this->body;
80 }
81
82 template <typename TYPE>
83 auto original::vector<TYPE>::vectorArrayInit(const uint32_t size) -> TYPE* {
84 auto arr = new TYPE[size];
85 for (uint32_t i = 0; i < size; i++) {
86 arr[i] = TYPE{};
87 }
88 return arr;
89 }
90
91 template <typename TYPE>
92 auto original::vector<TYPE>::moveElements(TYPE* old_body, const uint32_t inner_idx,
93 const uint32_t len, TYPE* new_body, const int64_t offset) -> void{
94 if (offset > 0)
95 {
96 for (uint32_t i = 0; i < len; i += 1)
97 {
98 new_body[inner_idx + offset + len - 1 - i] = old_body[inner_idx + len - 1 - i];
99 }
100 }else
101 {
102 for (uint32_t i = 0; i < len; i += 1)
103 {
104 new_body[inner_idx + offset + i] = old_body[inner_idx + i];
105 }
106 }
107 }
108
109 template <typename TYPE>
110 auto original::vector<TYPE>::toInnerIdx(int64_t index) const -> uint32_t
111 {
112 return this->inner_begin + index;
113 }
114
115 template <typename TYPE>
116 auto original::vector<TYPE>::outOfMaxSize(uint32_t increment) const -> bool
117 {
118 return this->inner_begin + this->size() + increment > this->max_size - 1 || static_cast<int64_t>(this->inner_begin) - static_cast<int64_t>(increment) < 0;
119 }
120
121 template <typename TYPE>
122 auto original::vector<TYPE>::grow(const uint32_t new_size) -> void
123 {
124 TYPE* new_body = vector::vectorArrayInit(new_size);
125 uint32_t new_begin = (new_size - 1) / 4;
126 const int64_t offset = static_cast<int64_t>(new_begin) - static_cast<int64_t>(this->inner_begin);
127 vector::moveElements(this->body, this->inner_begin,
128 this->size(), new_body, offset);
129 delete[] this->body;
130 this->body = new_body;
131 this->max_size = new_size;
132 this->inner_begin = new_begin;
133 }
134
135 template <typename TYPE>
136 auto original::vector<TYPE>::adjust(uint32_t increment) -> void {
137 if (!this->outOfMaxSize(increment)) {
138 return;
139 }
140 uint32_t new_begin = (this->max_size - this->size() - increment) / 2;
141 if (this->max_size >= this->size_ + increment && new_begin > 0) {
142 const int64_t offset = static_cast<int64_t>(new_begin) - static_cast<int64_t>(this->inner_begin);
143 vector::moveElements(this->body, this->inner_begin, this->size(),
144 this->body, offset);
145 this->inner_begin = new_begin;
146 } else {
147 const uint32_t new_max_size = (this->size() + increment) * 2;
148 this->grow(new_max_size);
149 }
150 }
151
152 template <typename TYPE>
153 original::vector<TYPE>::Iterator::Iterator(TYPE* ptr, const vector* container, int64_t pos)
154 : randomAccessIterator<TYPE>(ptr, container, pos) {}
155
156 template <typename TYPE>
157 original::vector<TYPE>::Iterator::Iterator(const Iterator& other)
158 : randomAccessIterator<TYPE>(nullptr, nullptr, 0)
159 {
160 this->operator=(other);
161 }
162
163 template <typename TYPE>
164 auto original::vector<TYPE>::Iterator::operator=(const Iterator& other) -> Iterator&
165 {
166 if (this == &other) {
167 return *this;
168 }
169 randomAccessIterator<TYPE>::operator=(other);
170 return *this;
171 }
172
173 template<typename TYPE>
174 auto original::vector<TYPE>::Iterator::clone() const -> Iterator* {
175 return new Iterator(*this);
176 }
177
178 template<typename TYPE>
179 auto original::vector<TYPE>::Iterator::atPrev(const iterator<TYPE> *other) const -> bool {
180 auto other_it = dynamic_cast<const Iterator*>(other);
181 return this->_ptr + 1 == other_it->_ptr;
182 }
183
184 template<typename TYPE>
185 auto original::vector<TYPE>::Iterator::atNext(const iterator<TYPE> *other) const -> bool {
186 auto other_it = dynamic_cast<const Iterator*>(other);
187 return other_it->_ptr + 1 == this->_ptr;
188 }
189
190 template<typename TYPE>
191 auto original::vector<TYPE>::Iterator::className() const -> std::string {
192 return "vector::Iterator";
193 }
194
195 template <typename TYPE>
196 original::vector<TYPE>::vector() : size_(), max_size(), inner_begin()
197 {
198 this->vectorInit();
199 }
200
201 template<typename TYPE>
202 original::vector<TYPE>::vector(const vector& other) : vector(){
203 this->operator=(other);
204 }
205
206 template <typename TYPE>
207 original::vector<TYPE>::vector(const std::initializer_list<TYPE>& list) : vector()
208 {
209 this->adjust(list.size());
210 for (const TYPE& e: list)
211 {
212 this->body[this->inner_begin + this->size()] = e;
213 this->size_ += 1;
214 }
215 }
216
217 template<typename TYPE>
218 auto original::vector<TYPE>::operator=(const vector& other) -> vector&
219 {
220 if (this == &other)
221 return *this;
222
223 this->vectorDestruct();
224
225 this->max_size = other.max_size;
226 this->inner_begin = other.inner_begin;
227 this->size_ = other.size_;
228 this->body = vector::vectorArrayInit(this->max_size);
229 for (uint32_t i = 0; i < this->size(); ++i) {
230 const TYPE& data = other.body[this->toInnerIdx(i)];
231 this->body[this->toInnerIdx(i)] = data;
232 }
233 return *this;
234 }
235
236 template <typename TYPE>
237 original::vector<TYPE>::vector(vector&& other) noexcept : vector()
238 {
239 this->operator=(std::move(other));
240 }
241
242 template <typename TYPE>
243 auto original::vector<TYPE>::operator=(vector&& other) noexcept -> vector&
244 {
245 if (this == &other)
246 return *this;
247
248 this->vectorDestruct();
249 this->body = std::move(other.body);
250 this->max_size = other.max_size;
251 this->inner_begin = other.inner_begin;
252 this->size_ = other.size_;
253 other.vectorInit();
254 return *this;
255 }
256
257 template <typename TYPE>
258 original::vector<TYPE>::vector(const array<TYPE>& arr) : vector()
259 {
260 this->adjust(arr.size());
261 for (uint32_t i = 0; i < arr.size(); i += 1)
262 {
263 this->body[this->toInnerIdx(i)] = arr.get(i);
264 this->size_ += 1;
265 }
266 }
267
268 template <typename TYPE>
269 auto original::vector<TYPE>::size() const -> uint32_t
270 {
271 return this->size_;
272 }
273
274 template<typename TYPE>
275 auto original::vector<TYPE>::data() const -> TYPE& {
276 return this->body[this->toInnerIdx(0)];
277 }
278
279 template <typename TYPE>
280 auto original::vector<TYPE>::get(int64_t index) const -> TYPE
281 {
282 if (this->indexOutOfBound(index))
283 {
284 throw outOfBoundError();
285 }
286 index = this->toInnerIdx(this->parseNegIndex(index));
287 return this->body[index];
288 }
289
290 template <typename TYPE>
291 auto original::vector<TYPE>::operator[](int64_t index) -> TYPE&
292 {
293 if (this->indexOutOfBound(index))
294 {
295 throw outOfBoundError();
296 }
297 index = this->toInnerIdx(this->parseNegIndex(index));
298 return this->body[index];
299 }
300
301 template <typename TYPE>
302 auto original::vector<TYPE>::set(int64_t index, const TYPE &e) -> void
303 {
304 if (this->indexOutOfBound(index))
305 {
306 throw outOfBoundError();
307 }
308 index = this->toInnerIdx(this->parseNegIndex(index));
309 this->body[index] = e;
310 }
311
312 template <typename TYPE>
313 auto original::vector<TYPE>::indexOf(const TYPE &e) const -> uint32_t
314 {
315 for (uint32_t i = 0; i < this->size(); i += 1)
316 {
317 if (this->get(i) == e)
318 {
319 return i;
320 }
321 }
322 return this->size();
323 }
324
325 template <typename TYPE>
326 auto original::vector<TYPE>::pushBegin(const TYPE &e) -> void
327 {
328 this->adjust(1);
329 this->inner_begin -= 1;
330 this->body[this->toInnerIdx(0)] = e;
331 this->size_ += 1;
332 }
333
334 template <typename TYPE>
335 auto original::vector<TYPE>::push(int64_t index, const TYPE &e) -> void
336 {
337 if (this->parseNegIndex(index) == this->size())
338 {
339 this->pushEnd(e);
340 }else if (this->parseNegIndex(index) == 0)
341 {
342 this->pushBegin(e);
343 }else
344 {
345 if (this->indexOutOfBound(index))
346 {
347 throw outOfBoundError();
348 }
349 this->adjust(1);
350 index = this->toInnerIdx(this->parseNegIndex(index));
351 uint32_t rel_idx = index - this->inner_begin;
352 if (index - this->inner_begin <= (this->size() - 1) / 2)
353 {
354 vector::moveElements(this->body, this->inner_begin,
355 rel_idx + 1, this->body, -1);
356 this->inner_begin -= 1;
357 }else
358 {
359 vector::moveElements(this->body, index,
360 this->size() - rel_idx, this->body, 1);
361 }
362 this->body[this->toInnerIdx(rel_idx)] = e;
363 this->size_ += 1;
364 }
365 }
366
367 template <typename TYPE>
368 auto original::vector<TYPE>::pushEnd(const TYPE &e) -> void
369 {
370 this->adjust(1);
371 this->body[this->toInnerIdx(this->size())] = e;
372 this->size_ += 1;
373 }
374
375 template <typename TYPE>
376 auto original::vector<TYPE>::popBegin() -> TYPE
377 {
378 if (this->size() == 0){
379 throw noElementError();
380 }
381 TYPE res = this->get(0);
382 this->inner_begin += 1;
383 this->size_ -= 1;
384 return res;
385 }
386
387 template <typename TYPE>
388 auto original::vector<TYPE>::pop(int64_t index) -> TYPE
389 {
390 if (this->parseNegIndex(index) == 0)
391 {
392 return this->popBegin();
393 }
394 if (this->parseNegIndex(index) == this->size() - 1)
395 {
396 return this->popEnd();
397 }
398 if (this->indexOutOfBound(index)){
399 throw outOfBoundError();
400 }
401 TYPE res = this->get(index);
402 index = this->toInnerIdx(this->parseNegIndex(index));
403 uint32_t rel_idx = index - this->inner_begin;
404 if (index - this->inner_begin <= (this->size() - 1) / 2)
405 {
406 vector::moveElements(this->body, this->inner_begin,
407 rel_idx, this->body, 1);
408 this->inner_begin += 1;
409 }else
410 {
411 vector::moveElements(this->body, index + 1,
412 this->size() - 1 - rel_idx, this->body, -1);
413 }
414 this->size_ -= 1;
415 return res;
416 }
417
418 template <typename TYPE>
419 auto original::vector<TYPE>::popEnd() -> TYPE
420 {
421 if (this->size() == 0){
422 throw noElementError();
423 }
424 TYPE res = this->get(this->size() - 1);
425 this->size_ -= 1;
426 return res;
427 }
428
429 template<typename TYPE>
430 auto original::vector<TYPE>::begins() const -> Iterator* {
431 return new Iterator(&this->body[this->toInnerIdx(0)], this, 0);
432 }
433
434 template<typename TYPE>
435 auto original::vector<TYPE>::ends() const -> Iterator* {
436 return new Iterator(&this->body[this->toInnerIdx(this->size() - 1)], this, this->size() - 1);
437 }
438
439 template <typename TYPE>
440 auto original::vector<TYPE>::className() const -> std::string
441 {
442 return "vector";
443 }
444
445 template <typename TYPE>
446 original::vector<TYPE>::~vector() {
447 this->vectorDestruct();
448 }
449
450#endif //VECTOR_H
Definition array.h:15
Definition baseList.h:7
Definition container.h:10
Definition iterationStream.h:12
Definition iterator.h:11
Definition vector.h:29