5#include "iterationStream.h"
9 template <
typename TYPE>
12 const uint32_t INNER_SIZE_INIT = 16;
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);
28 class Iterator final :
public randomAccessIterator<TYPE>
30 explicit Iterator(TYPE* ptr,
const vector*
container, int64_t pos);
33 Iterator(
const Iterator& other);
34 Iterator& operator=(
const Iterator& other);
35 Iterator* clone()
const override;
38 [[nodiscard]] std::string className()
const override;
42 vector(
const vector& other);
43 vector(
const std::initializer_list<TYPE>& list);
45 vector& operator=(
const vector& other);
46 vector(vector&& other)
noexcept;
47 vector& operator=(vector&& other)
noexcept;
48 [[nodiscard]] uint32_t size()
const override;
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;
62 [[nodiscard]] std::string className()
const override;
67 template <
typename TYPE>
68 auto original::vector<TYPE>::vectorInit() ->
void
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);
76 template <
typename TYPE>
77 auto original::vector<TYPE>::vectorDestruct() const ->
void
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++) {
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{
96 for (uint32_t i = 0; i < len; i += 1)
98 new_body[inner_idx + offset + len - 1 - i] = old_body[inner_idx + len - 1 - i];
102 for (uint32_t i = 0; i < len; i += 1)
104 new_body[inner_idx + offset + i] = old_body[inner_idx + i];
109 template <
typename TYPE>
110 auto original::vector<TYPE>::toInnerIdx(int64_t index)
const -> uint32_t
112 return this->inner_begin + index;
115 template <
typename TYPE>
116 auto original::vector<TYPE>::outOfMaxSize(uint32_t increment)
const ->
bool
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;
121 template <
typename TYPE>
122 auto original::vector<TYPE>::grow(
const uint32_t new_size) ->
void
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);
130 this->body = new_body;
131 this->max_size = new_size;
132 this->inner_begin = new_begin;
135 template <
typename TYPE>
136 auto original::vector<TYPE>::adjust(uint32_t increment) ->
void {
137 if (!this->outOfMaxSize(increment)) {
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(),
145 this->inner_begin = new_begin;
147 const uint32_t new_max_size = (this->size() + increment) * 2;
148 this->grow(new_max_size);
152 template <
typename TYPE>
153 original::vector<TYPE>::Iterator::Iterator(TYPE* ptr,
const vector* container, int64_t pos)
154 : randomAccessIterator<TYPE>(ptr, container, pos) {}
156 template <
typename TYPE>
157 original::vector<TYPE>::Iterator::Iterator(
const Iterator& other)
158 : randomAccessIterator<TYPE>(nullptr, nullptr, 0)
160 this->operator=(other);
163 template <
typename TYPE>
164 auto original::vector<TYPE>::Iterator::operator=(
const Iterator& other) ->
Iterator&
166 if (
this == &other) {
169 randomAccessIterator<TYPE>::operator=(other);
173 template<
typename TYPE>
174 auto original::vector<TYPE>::Iterator::clone() const ->
Iterator* {
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;
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;
190 template<
typename TYPE>
191 auto original::vector<TYPE>::Iterator::className() const -> std::
string {
192 return "vector::Iterator";
195 template <
typename TYPE>
196 original::vector<TYPE>::vector() : size_(), max_size(), inner_begin()
201 template<
typename TYPE>
202 original::vector<TYPE>::vector(
const vector& other) : vector(){
203 this->operator=(other);
206 template <
typename TYPE>
207 original::vector<TYPE>::vector(
const std::initializer_list<TYPE>& list) : vector()
209 this->adjust(list.size());
210 for (
const TYPE& e: list)
212 this->body[this->inner_begin + this->size()] = e;
217 template<
typename TYPE>
218 auto original::vector<TYPE>::operator=(
const vector& other) -> vector&
223 this->vectorDestruct();
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;
236 template <
typename TYPE>
237 original::vector<TYPE>::vector(vector&& other) noexcept : vector()
239 this->operator=(std::move(other));
242 template <
typename TYPE>
243 auto original::vector<TYPE>::operator=(vector&& other)
noexcept -> vector&
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_;
257 template <
typename TYPE>
258 original::vector<TYPE>::vector(
const array<TYPE>& arr) : vector()
260 this->adjust(arr.size());
261 for (uint32_t i = 0; i < arr.size(); i += 1)
263 this->body[this->toInnerIdx(i)] = arr.get(i);
268 template <
typename TYPE>
269 auto original::vector<TYPE>::size() const -> uint32_t
274 template<
typename TYPE>
275 auto original::vector<TYPE>::data() const -> TYPE& {
276 return this->body[this->toInnerIdx(0)];
279 template <
typename TYPE>
280 auto original::vector<TYPE>::get(int64_t index)
const -> TYPE
282 if (this->indexOutOfBound(index))
284 throw outOfBoundError();
286 index = this->toInnerIdx(this->parseNegIndex(index));
287 return this->body[index];
290 template <
typename TYPE>
291 auto original::vector<TYPE>::operator[](int64_t index) -> TYPE&
293 if (this->indexOutOfBound(index))
295 throw outOfBoundError();
297 index = this->toInnerIdx(this->parseNegIndex(index));
298 return this->body[index];
301 template <
typename TYPE>
302 auto original::vector<TYPE>::set(int64_t index,
const TYPE &e) ->
void
304 if (this->indexOutOfBound(index))
306 throw outOfBoundError();
308 index = this->toInnerIdx(this->parseNegIndex(index));
309 this->body[index] = e;
312 template <
typename TYPE>
313 auto original::vector<TYPE>::indexOf(
const TYPE &e)
const -> uint32_t
315 for (uint32_t i = 0; i < this->size(); i += 1)
317 if (this->get(i) == e)
325 template <
typename TYPE>
326 auto original::vector<TYPE>::pushBegin(
const TYPE &e) ->
void
329 this->inner_begin -= 1;
330 this->body[this->toInnerIdx(0)] = e;
334 template <
typename TYPE>
335 auto original::vector<TYPE>::push(int64_t index,
const TYPE &e) ->
void
337 if (this->parseNegIndex(index) == this->size())
340 }
else if (this->parseNegIndex(index) == 0)
345 if (this->indexOutOfBound(index))
347 throw outOfBoundError();
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)
354 vector::moveElements(this->body, this->inner_begin,
355 rel_idx + 1, this->body, -1);
356 this->inner_begin -= 1;
359 vector::moveElements(this->body, index,
360 this->size() - rel_idx, this->body, 1);
362 this->body[this->toInnerIdx(rel_idx)] = e;
367 template <
typename TYPE>
368 auto original::vector<TYPE>::pushEnd(
const TYPE &e) ->
void
371 this->body[this->toInnerIdx(this->size())] = e;
375 template <
typename TYPE>
376 auto original::vector<TYPE>::popBegin() -> TYPE
378 if (this->size() == 0){
379 throw noElementError();
381 TYPE res = this->get(0);
382 this->inner_begin += 1;
387 template <
typename TYPE>
388 auto original::vector<TYPE>::pop(int64_t index) -> TYPE
390 if (this->parseNegIndex(index) == 0)
392 return this->popBegin();
394 if (this->parseNegIndex(index) == this->size() - 1)
396 return this->popEnd();
398 if (this->indexOutOfBound(index)){
399 throw outOfBoundError();
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)
406 vector::moveElements(this->body, this->inner_begin,
407 rel_idx, this->body, 1);
408 this->inner_begin += 1;
411 vector::moveElements(this->body, index + 1,
412 this->size() - 1 - rel_idx, this->body, -1);
418 template <
typename TYPE>
419 auto original::vector<TYPE>::popEnd() -> TYPE
421 if (this->size() == 0){
422 throw noElementError();
424 TYPE res = this->get(this->size() - 1);
429 template<
typename TYPE>
430 auto original::vector<TYPE>::begins() const ->
Iterator* {
431 return new Iterator(&this->body[this->toInnerIdx(0)],
this, 0);
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);
439 template <
typename TYPE>
440 auto original::vector<TYPE>::className() const -> std::
string
445 template <
typename TYPE>
446 original::vector<TYPE>::~vector() {
447 this->vectorDestruct();
Definition container.h:10
Definition iterationStream.h:12