41 template <
typename TYPE,
typename ALLOC = allocator<TYPE>>
76 void vectorArrayDestruct()
noexcept;
96 static void moveElements(TYPE* old_body,
u_integer inner_idx,
111 [[nodiscard]]
bool outOfMaxSize(
u_integer increment)
const;
156 Iterator(
const Iterator& other);
210 vector(
const std::initializer_list<TYPE>& list);
326 Iterator*
ends()
const override;
341 template <
typename TYPE,
typename ALLOC>
342 auto original::vector<TYPE, ALLOC>::vectorInit() ->
void
345 this->max_size = this->INNER_SIZE_INIT;
346 this->inner_begin = (this->INNER_SIZE_INIT - 1)/2;
347 this->body = vector::vectorArrayInit(this->INNER_SIZE_INIT);
350 template <
typename TYPE,
typename ALLOC>
351 auto original::vector<TYPE, ALLOC>::vectorArrayDestruct() noexcept ->
void
354 for (u_integer i = 0; i < this->max_size; ++i) {
355 this->destroy(&this->body[i]);
357 this->deallocate(this->body, this->max_size);
358 this->body =
nullptr;
362 template <
typename TYPE,
typename ALLOC>
363 auto original::vector<TYPE, ALLOC>::vectorArrayInit(
const u_integer size) -> TYPE* {
364 auto arr = this->allocate(size);
365 for (u_integer i = 0; i < size; i++) {
366 this->construct(&arr[i]);
371 template <
typename TYPE,
typename ALLOC>
372 auto original::vector<TYPE, ALLOC>::moveElements(TYPE* old_body,
const u_integer inner_idx,
373 const u_integer len, TYPE* new_body,
const integer offset) ->
void{
376 for (u_integer i = 0; i < len; i += 1)
378 new_body[inner_idx + offset + len - 1 - i] = old_body[inner_idx + len - 1 - i];
382 for (u_integer i = 0; i < len; i += 1)
384 new_body[inner_idx + offset + i] = old_body[inner_idx + i];
389 template <
typename TYPE,
typename ALLOC>
390 auto original::vector<TYPE, ALLOC>::toInnerIdx(integer index)
const ->
u_integer
392 return this->inner_begin + index;
395 template <
typename TYPE,
typename ALLOC>
396 auto original::vector<TYPE, ALLOC>::outOfMaxSize(u_integer increment)
const ->
bool
398 return this->inner_begin + this->size() + increment > this->max_size - 1 ||
static_cast<integer>(this->inner_begin) -
static_cast<integer>(increment) < 0;
401 template <
typename TYPE,
typename ALLOC>
402 auto original::vector<TYPE, ALLOC>::grow(
const u_integer new_size) ->
void
404 TYPE* new_body = vector::vectorArrayInit(new_size);
405 u_integer new_begin = (new_size - 1) / 4;
407 vector::moveElements(this->body, this->inner_begin,
408 this->size(), new_body, offset);
409 this->vectorArrayDestruct();
410 this->body = new_body;
411 this->max_size = new_size;
412 this->inner_begin = new_begin;
415 template <
typename TYPE,
typename ALLOC>
416 auto original::vector<TYPE, ALLOC>::adjust(u_integer increment) ->
void {
417 if (!this->outOfMaxSize(increment)) {
420 u_integer new_begin = (this->max_size - this->size() - increment) / 2;
421 if (this->max_size >= this->size_ + increment && new_begin > 0) {
423 vector::moveElements(this->body, this->inner_begin, this->size(),
425 this->inner_begin = new_begin;
427 const u_integer new_max_size = (this->size() + increment) * 2;
428 this->grow(new_max_size);
432 template <
typename TYPE,
typename ALLOC>
433 original::vector<TYPE, ALLOC>::Iterator::Iterator(TYPE* ptr,
const vector* container, integer pos)
434 : randomAccessIterator<TYPE, ALLOC>(ptr, container, pos) {}
436 template <
typename TYPE,
typename ALLOC>
437 original::vector<TYPE, ALLOC>::Iterator::Iterator(
const Iterator& other)
443 template <
typename TYPE,
typename ALLOC>
446 if (
this == &other) {
453 template <
typename TYPE,
typename ALLOC>
455 return new Iterator(*
this);
458 template <
typename TYPE,
typename ALLOC>
460 auto other_it =
dynamic_cast<const Iterator*
>(other);
461 return this->
_ptr + 1 == other_it->_ptr;
464 template <
typename TYPE,
typename ALLOC>
466 auto other_it =
dynamic_cast<const Iterator*
>(other);
467 return other_it->_ptr + 1 == this->
_ptr;
470 template <
typename TYPE,
typename ALLOC>
472 return "vector::Iterator";
475 template <
typename TYPE,
typename ALLOC>
477 :
baseList<TYPE, ALLOC>(std::move(alloc)), size_(), max_size(), inner_begin()
482 template <
typename TYPE,
typename ALLOC>
487 template <
typename TYPE,
typename ALLOC>
490 this->adjust(list.size());
491 for (
const TYPE& e: list)
493 this->body[this->inner_begin + this->
size()] = e;
498 template <
typename TYPE,
typename ALLOC>
504 this->vectorArrayDestruct();
506 this->max_size = other.max_size;
507 this->inner_begin = other.inner_begin;
508 this->size_ = other.size_;
509 this->body = vector::vectorArrayInit(this->max_size);
511 const TYPE&
data = other.body[this->toInnerIdx(i)];
512 this->body[this->toInnerIdx(i)] =
data;
514 if constexpr (ALLOC::propagate_on_container_copy_assignment::value){
520 template <
typename TYPE,
typename ALLOC>
526 template <
typename TYPE,
typename ALLOC>
532 this->vectorArrayDestruct();
533 this->body = other.body;
534 other.body =
nullptr;
535 this->max_size = other.max_size;
536 this->inner_begin = other.inner_begin;
537 this->size_ = other.size_;
538 if constexpr (ALLOC::propagate_on_container_move_assignment::value){
539 this->
allocator = std::move(other.allocator);
545 template <
typename TYPE,
typename ALLOC>
548 this->adjust(arr.
size());
551 this->body[this->toInnerIdx(i)] = arr.
get(i);
556 template <
typename TYPE,
typename ALLOC>
562 template <
typename TYPE,
typename ALLOC>
564 return this->body[this->toInnerIdx(0)];
567 template <
typename TYPE,
typename ALLOC>
575 return this->body[index];
578 template <
typename TYPE,
typename ALLOC>
586 return this->body[index];
589 template <
typename TYPE,
typename ALLOC>
597 this->body[index] = e;
600 template <
typename TYPE,
typename ALLOC>
605 if (this->
get(i) == e)
613 template <
typename TYPE,
typename ALLOC>
617 this->inner_begin -= 1;
618 this->body[this->toInnerIdx(0)] = e;
622 template <
typename TYPE,
typename ALLOC>
639 u_integer rel_idx = index - this->inner_begin;
640 if (index - this->inner_begin <= (this->
size() - 1) / 2)
642 vector::moveElements(this->body, this->inner_begin,
643 rel_idx + 1, this->body, -1);
644 this->inner_begin -= 1;
647 vector::moveElements(this->body, index,
648 this->
size() - rel_idx, this->body, 1);
650 this->body[this->toInnerIdx(rel_idx)] = e;
655 template <
typename TYPE,
typename ALLOC>
659 this->body[this->toInnerIdx(this->
size())] = e;
663 template <
typename TYPE,
typename ALLOC>
666 if (this->
size() == 0){
669 TYPE res = this->
get(0);
670 this->inner_begin += 1;
675 template <
typename TYPE,
typename ALLOC>
689 TYPE res = this->
get(index);
691 u_integer rel_idx = index - this->inner_begin;
692 if (index - this->inner_begin <= (this->
size() - 1) / 2)
694 vector::moveElements(this->body, this->inner_begin,
695 rel_idx, this->body, 1);
696 this->inner_begin += 1;
699 vector::moveElements(this->body, index + 1,
700 this->
size() - 1 - rel_idx, this->body, -1);
706 template <
typename TYPE,
typename ALLOC>
709 if (this->
size() == 0){
712 TYPE res = this->
get(this->
size() - 1);
717 template <
typename TYPE,
typename ALLOC>
719 return new Iterator(&this->body[this->toInnerIdx(0)],
this, 0);
722 template <
typename TYPE,
typename ALLOC>
724 return new Iterator(&this->body[this->toInnerIdx(this->
size() - 1)],
this, this->
size() - 1);
727 template <
typename TYPE,
typename ALLOC>
733 template <
typename TYPE,
typename ALLOC>
735 this->vectorArrayDestruct();
Provides the array class for a fixed-size container with random access.
Provides a base class for variable-size serial containers.
Default memory allocator using allocators utilities.
Definition allocator.h:154
A fixed-size array container with random access.
Definition array.h:41
TYPE get(integer index) const override
Retrieves an element at a specified index.
Definition array.h:393
u_integer size() const override
Returns the size of the array.
Definition array.h:382
Base class for variable-size serial containers.
Definition baseList.h:43
Abstract base class for containers.
Definition container.h:28
A stream class that allows iteration, comparison, and printing.
Definition iterationStream.h:33
Base iterator interface that supports common operations for iteration.
Definition iterator.h:35
Exception for missing element requests.
Definition error.h:136
Exception for container index out-of-range errors.
Definition error.h:84
randomAccessIterator(TYPE *ptr, const container< TYPE, ALLOC > *container, integer pos)
Protected constructor for derived classes.
Definition randomAccessIterator.h:181
randomAccessIterator & operator=(const randomAccessIterator &other)
Copy assignment operator.
Definition randomAccessIterator.h:197
TYPE * _ptr
Pointer to the current element.
Definition randomAccessIterator.h:41
bool indexOutOfBound(integer index) const
Checks if the provided index is out of bounds.
Definition serial.h:133
integer parseNegIndex(integer index) const
Converts negative indices into valid positive indices.
Definition serial.h:140
Random access iterator implementation for vector.
Definition vector.h:141
Iterator * clone() const override
Clones the iterator.
Definition vector.h:454
Iterator & operator=(const Iterator &other)
Assignment operator for the iterator.
Definition vector.h:444
std::string className() const override
Gets the class name of the iterator.
Definition vector.h:471
bool atNext(const iterator< TYPE > *other) const override
Checks if the iterator is at the next element relative to another iterator.
Definition vector.h:465
bool atPrev(const iterator< TYPE > *other) const override
Checks if the iterator is at the previous element relative to another iterator.
Definition vector.h:459
u_integer size() const override
Gets the size of the vector.
Definition vector.h:557
vector(const std::initializer_list< TYPE > &list)
Constructs a vector from an initializer list.
Definition vector.h:488
vector & operator=(const vector &other)
Assignment operator for the vector.
Definition vector.h:499
vector(const array< TYPE > &arr)
Constructs a vector from an array.
Definition vector.h:546
TYPE popBegin() override
Removes and returns the first element in the vector.
Definition vector.h:664
TYPE & data() const
Gets a reference to the first element in the vector.
Definition vector.h:563
void push(integer index, const TYPE &e) override
Inserts an element at the specified index in the vector.
Definition vector.h:623
vector(vector &&other) noexcept
Move constructor for the vector.
Definition vector.h:521
~vector() override
Destructor for the vector.
Definition vector.h:734
Iterator * ends() const override
Gets an iterator to the end of the vector.
Definition vector.h:723
vector & operator=(vector &&other) noexcept
Move assignment operator for the vector.
Definition vector.h:527
void pushEnd(const TYPE &e) override
Inserts an element at the end of the vector.
Definition vector.h:656
void pushBegin(const TYPE &e) override
Inserts an element at the beginning of the vector.
Definition vector.h:614
std::string className() const override
Gets the class name of the vector.
Definition vector.h:728
vector(ALLOC alloc=ALLOC{})
Constructs a vector with specified allocator.
Definition vector.h:476
TYPE pop(integer index) override
Removes and returns the element at the specified index.
Definition vector.h:676
u_integer indexOf(const TYPE &e) const override
Finds the index of the first occurrence of the specified element.
Definition vector.h:601
TYPE get(integer index) const override
Gets an element at the specified index.
Definition vector.h:568
void set(integer index, const TYPE &e) override
Sets the element at the specified index.
Definition vector.h:590
Iterator * begins() const override
Gets an iterator to the beginning of the vector.
Definition vector.h:718
TYPE & operator[](integer index) override
Gets a reference to the element at the specified index.
Definition vector.h:579
TYPE popEnd() override
Removes and returns the last element in the vector.
Definition vector.h:707
vector(const vector &other)
Copy constructor for the vector.
Definition vector.h:483
Provides functionality for an iteration stream.
Main namespace for the project Original.
Definition algorithms.h:21
std::uint32_t u_integer
32-bit unsigned integer type for sizes/indexes
Definition config.h:17
std::int64_t integer
64-bit signed integer type for arithmetic operations
Definition config.h:15