42 template <
typename TYPE,
typename ALLOC = allocator<TYPE>>
45 static constexpr u_integer INNER_SIZE_INIT = 16;
78 void vectorArrayDestroy()
noexcept;
95 TYPE getElem(
integer pos)
const;
102 void setElem(
integer pos,
const TYPE &e);
110 static void setBufElem(TYPE* buf,
integer pos,
const TYPE& e);
120 static void moveElements(TYPE* old_body,
u_integer inner_idx,
135 [[nodiscard]]
bool outOfMaxSize(
u_integer increment)
const;
204 Iterator(
const Iterator& other);
264 template<
typename... ARGS>
277 vector(
const std::initializer_list<TYPE>& list);
347 using
serial<TYPE, ALLOC>::operator[];
419 Iterator*
ends() const override;
429 template<typename T, typename... ARGS>
454 template<typename T, typename... ARGS>
458 template <typename TYPE, typename ALLOC>
462 this->max_size = INNER_SIZE_INIT;
463 this->inner_begin = (INNER_SIZE_INIT - 1)/2;
464 this->body = vector::vectorArrayInit(INNER_SIZE_INIT);
467 template <
typename TYPE,
typename ALLOC>
471 for (
u_integer i = 0; i < this->max_size; ++i) {
475 this->body =
nullptr;
479 template <
typename TYPE,
typename ALLOC>
488 template <
typename TYPE,
typename ALLOC>
491 if constexpr (std::is_copy_constructible_v<TYPE>) {
492 return this->body[pos];
493 }
else if constexpr (std::is_move_constructible_v<TYPE>) {
494 return std::move(this->body[pos]);
496 staticError<unSupportedMethodError, !std::is_copy_constructible_v<TYPE> && !std::is_move_constructible_v<TYPE>>::asserts();
501 template <
typename TYPE,
typename ALLOC>
504 setBufElem(this->body, pos, e);
507 template <
typename TYPE,
typename ALLOC>
510 if constexpr (std::is_copy_assignable_v<TYPE>) {
512 }
else if constexpr (std::is_move_assignable_v<TYPE>) {
513 buf[pos] = std::move(
const_cast<TYPE&
>(e));
515 staticError<unSupportedMethodError, !std::is_copy_constructible_v<TYPE> && !std::is_move_constructible_v<TYPE>>::asserts();
519 template <
typename TYPE,
typename ALLOC>
526 setBufElem(new_body, inner_idx + offset + len - 1 - i, old_body[inner_idx + len - 1 - i]);
532 setBufElem(new_body, inner_idx + offset + i, old_body[inner_idx + i]);
537 template <
typename TYPE,
typename ALLOC>
540 return this->inner_begin + index;
543 template <
typename TYPE,
typename ALLOC>
546 return this->inner_begin + this->
size() + increment > this->max_size - 1 ||
static_cast<integer>(this->inner_begin) -
static_cast<integer>(increment) < 0;
549 template <
typename TYPE,
typename ALLOC>
552 TYPE* new_body = vector::vectorArrayInit(new_size);
553 u_integer new_begin = (new_size - 1) / 4;
555 vector::moveElements(this->body, this->inner_begin,
556 this->
size(), new_body, offset);
557 this->vectorArrayDestroy();
558 this->body = new_body;
559 this->max_size = new_size;
560 this->inner_begin = new_begin;
563 template <
typename TYPE,
typename ALLOC>
565 if (!this->outOfMaxSize(increment)) {
568 u_integer new_begin = (this->max_size - this->
size() - increment) / 2;
569 if (this->max_size >= this->size_ + increment && new_begin > 0) {
571 vector::moveElements(this->body, this->inner_begin, this->
size(),
573 this->inner_begin = new_begin;
575 const u_integer new_max_size = (this->
size() + increment) * 2;
576 this->grow(new_max_size);
580 template <
typename TYPE,
typename ALLOC>
582 : randomAccessIterator<TYPE, ALLOC>(ptr,
container, pos) {}
584 template <
typename TYPE,
typename ALLOC>
591 template <
typename TYPE,
typename ALLOC>
594 if (
this == &other) {
601 template <
typename TYPE,
typename ALLOC>
606 template <
typename TYPE,
typename ALLOC>
608 auto other_it =
dynamic_cast<const Iterator*
>(other);
609 return this->_ptr + 1 == other_it->
_ptr;
612 template <
typename TYPE,
typename ALLOC>
614 auto other_it =
dynamic_cast<const Iterator*
>(other);
615 return other_it->
_ptr + 1 == this->_ptr;
618 template <
typename TYPE,
typename ALLOC>
620 return "vector::Iterator";
623 template <
typename TYPE,
typename ALLOC>
625 :
baseList<TYPE, ALLOC>(std::move(alloc)), size_(), max_size(), inner_begin()
630template<
typename TYPE,
typename ALLOC>
631template<
typename... ARGS>
633 :
vector(size, std::move(alloc)) {
634 this->body = this->
allocate(this->max_size);
635 for (
u_integer i = 0; i < this->size_; ++i) {
636 this->
construct(&this->body[this->toInnerIdx(i)], std::forward<ARGS>(args)...);
640template<
typename TYPE,
typename ALLOC>
642 :
baseList<TYPE, ALLOC>(std::move(alloc)), size_(size),
643 max_size(size * 4 / 3), inner_begin(size / 3 >= 1 ? size / 3 - 1 : 0), body(nullptr) {
646 template <
typename TYPE,
typename ALLOC>
651 template <
typename TYPE,
typename ALLOC>
654 this->adjust(list.size());
655 for (
const TYPE& e: list)
657 this->body[this->inner_begin + this->
size()] = e;
662 template <
typename TYPE,
typename ALLOC>
668 this->vectorArrayDestroy();
670 this->max_size = other.max_size;
671 this->inner_begin = other.inner_begin;
672 this->size_ = other.size_;
673 this->body = vector::vectorArrayInit(this->max_size);
674 for (
u_integer i = 0; i < this->size(); ++i) {
675 const TYPE& data = other.body[this->toInnerIdx(i)];
676 this->body[this->toInnerIdx(i)] = data;
678 if constexpr (ALLOC::propagate_on_container_copy_assignment::value){
684 template <
typename TYPE,
typename ALLOC>
687 this->operator=(std::move(other));
690 template <
typename TYPE,
typename ALLOC>
696 this->vectorArrayDestroy();
697 this->body = other.body;
698 other.body =
nullptr;
699 this->max_size = other.max_size;
700 this->inner_begin = other.inner_begin;
701 this->size_ = other.size_;
702 if constexpr (ALLOC::propagate_on_container_move_assignment::value){
703 this->
allocator = std::move(other.allocator);
709 template <
typename TYPE,
typename ALLOC>
712 this->adjust(arr.
size());
715 this->body[this->toInnerIdx(i)] = arr.
get(i);
720 template <
typename TYPE,
typename ALLOC>
726 template <
typename TYPE,
typename ALLOC>
729 return this->max_size;
732 template <
typename TYPE,
typename ALLOC>
734 return this->body[this->toInnerIdx(0)];
737 template <
typename TYPE,
typename ALLOC>
740 if (this->indexOutOfBound(index))
742 throw outOfBoundError(
"Index " + std::to_string(this->parseNegIndex(index)) +
743 " out of bound max index " + std::to_string(this->size() - 1) +
".");
745 index = this->toInnerIdx(this->parseNegIndex(index));
746 return this->getElem(index);
749 template <
typename TYPE,
typename ALLOC>
752 if (this->indexOutOfBound(index))
754 throw outOfBoundError(
"Index " + std::to_string(this->parseNegIndex(index)) +
755 " out of bound max index " + std::to_string(this->size() - 1) +
".");
757 index = this->toInnerIdx(this->parseNegIndex(index));
758 return this->body[index];
761 template <
typename TYPE,
typename ALLOC>
764 if (this->indexOutOfBound(index))
766 throw outOfBoundError(
"Index " + std::to_string(this->parseNegIndex(index)) +
767 " out of bound max index " + std::to_string(this->size() - 1) +
".");
769 index = this->toInnerIdx(this->parseNegIndex(index));
770 this->setElem(index, e);
773 template <
typename TYPE,
typename ALLOC>
777 for (
u_integer i = 0; i < this->size(); i += 1)
779 if (this->get(i) == e)
790 template <
typename TYPE,
typename ALLOC>
794 this->inner_begin -= 1;
795 this->setElem(this->toInnerIdx(0), e);
799 template <
typename TYPE,
typename ALLOC>
802 if (this->parseNegIndex(index) == this->size())
805 }
else if (this->parseNegIndex(index) == 0)
810 if (this->indexOutOfBound(index))
812 throw outOfBoundError(
"Index " + std::to_string(this->parseNegIndex(index)) +
813 " out of bound max index " + std::to_string(this->size() - 1) +
".");
816 index = this->toInnerIdx(this->parseNegIndex(index));
817 u_integer rel_idx = index - this->inner_begin;
818 if (index - this->inner_begin <= (this->size() - 1) / 2)
820 vector::moveElements(this->body, this->inner_begin,
821 rel_idx + 1, this->body, -1);
822 this->inner_begin -= 1;
825 vector::moveElements(this->body, index,
826 this->size() - rel_idx, this->body, 1);
828 this->setElem(this->toInnerIdx(rel_idx), e);
833 template <
typename TYPE,
typename ALLOC>
837 this->setElem(this->toInnerIdx(this->size()), e);
841 template <
typename TYPE,
typename ALLOC>
844 if (this->size() == 0){
847 TYPE res = this->get(0);
848 this->inner_begin += 1;
853 template <
typename TYPE,
typename ALLOC>
856 if (this->parseNegIndex(index) == 0)
858 return this->popBegin();
860 if (this->parseNegIndex(index) == this->size() - 1)
862 return this->popEnd();
864 if (this->indexOutOfBound(index)){
865 throw outOfBoundError(
"Index " + std::to_string(this->parseNegIndex(index)) +
866 " out of bound max index " + std::to_string(this->size() - 1) +
".");
868 TYPE res = this->get(index);
869 index = this->toInnerIdx(this->parseNegIndex(index));
870 u_integer rel_idx = index - this->inner_begin;
871 if (index - this->inner_begin <= (this->size() - 1) / 2)
873 vector::moveElements(this->body, this->inner_begin,
874 rel_idx, this->body, 1);
875 this->inner_begin += 1;
878 vector::moveElements(this->body, index + 1,
879 this->size() - 1 - rel_idx, this->body, -1);
885 template <
typename TYPE,
typename ALLOC>
888 if (this->size() == 0){
891 TYPE res = this->get(this->size() - 1);
896 template <
typename TYPE,
typename ALLOC>
898 return new Iterator(&this->body[this->toInnerIdx(0)],
this, 0);
901 template <
typename TYPE,
typename ALLOC>
903 return new Iterator(&this->body[this->toInnerIdx(this->size() - 1)],
this, this->size() - 1);
906 template <
typename TYPE,
typename ALLOC>
912 template <
typename TYPE,
typename ALLOC>
914 this->vectorArrayDestroy();
917 template<
typename T,
typename... ARGS>
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:446
u_integer size() const override
Returns the size of the array.
Definition array.h:435
Base class for variable-size serial containers.
Definition baseList.h:43
Abstract base class for containers.
Definition container.h:26
void deallocate(TYPE *ptr, u_integer size)
Deallocates memory previously allocated by allocate()
Definition container.h:136
container(ALLOC alloc=ALLOC{})
Constructs a container with specified allocator.
Definition container.h:127
TYPE * allocate(u_integer size)
Allocates raw memory for elements.
Definition container.h:131
void destroy(O_TYPE *o_ptr)
Destroys an element.
Definition container.h:148
void construct(O_TYPE *o_ptr, Args &&... args)
Constructs an element in-place.
Definition container.h:142
A stream class that allows iteration, comparison, and printing.
Definition iterationStream.h:32
Base iterator interface that supports common operations for iteration.
Definition iterator.h:37
Exception for missing element requests.
Definition error.h:213
Exception for container index out-of-range errors.
Definition error.h:129
Abstract base class for random-access iterators.
Definition randomAccessIterator.h:39
randomAccessIterator & operator=(const randomAccessIterator &other)
Copy assignment operator.
Definition randomAccessIterator.h:197
TYPE * _ptr
Pointer to the current element.
Definition randomAccessIterator.h:41
Abstract base class for sequential containers with index-based access.
Definition serial.h:34
Abstract base class for unique element containers.
Definition set.h:44
Exception for unimplemented method calls.
Definition error.h:192
Random access iterator implementation for vector.
Definition vector.h:188
Iterator * clone() const override
Clones the iterator.
Definition vector.h:602
Iterator & operator=(const Iterator &other)
Assignment operator for the iterator.
Definition vector.h:592
std::string className() const override
Gets the class name of the iterator.
Definition vector.h:619
bool atNext(const iterator< TYPE > *other) const override
Checks if the iterator is at the next element relative to another iterator.
Definition vector.h:613
bool atPrev(const iterator< TYPE > *other) const override
Checks if the iterator is at the previous element relative to another iterator.
Definition vector.h:607
Dynamic array container with amortized constant time operations.
Definition vector.h:43
u_integer size() const override
Gets the size of the vector.
Definition vector.h:721
vector(const std::initializer_list< TYPE > &list)
Constructs a vector from an initializer list.
Definition vector.h:652
vector & operator=(const vector &other)
Assignment operator for the vector.
Definition vector.h:663
vector(const array< TYPE > &arr)
Constructs a vector from an array.
Definition vector.h:710
TYPE popBegin() override
Removes and returns the first element in the vector.
Definition vector.h:842
TYPE & data() const
Gets a reference to the first element in the vector.
Definition vector.h:733
void push(integer index, const TYPE &e) override
Inserts an element at the specified index in the vector.
Definition vector.h:800
vector(vector &&other) noexcept
Move constructor for the vector.
Definition vector.h:685
~vector() override
Destructor for the vector.
Definition vector.h:913
Iterator * ends() const override
Gets an iterator to the end of the vector.
Definition vector.h:902
u_integer capacity() const noexcept
Gets the current capacity of the vector.
Definition vector.h:727
vector & operator=(vector &&other) noexcept
Move assignment operator for the vector.
Definition vector.h:691
void pushEnd(const TYPE &e) override
Inserts an element at the end of the vector.
Definition vector.h:834
void pushBegin(const TYPE &e) override
Inserts an element at the beginning of the vector.
Definition vector.h:791
std::string className() const override
Gets the class name of the vector.
Definition vector.h:907
vector(u_integer size, ALLOC alloc, ARGS &&... args)
Constructs a vector with specified size and allocator, initializing elements with provided arguments.
Definition vector.h:632
vector(ALLOC alloc=ALLOC{})
Constructs a vector with specified allocator.
Definition vector.h:624
TYPE pop(integer index) override
Removes and returns the element at the specified index.
Definition vector.h:854
u_integer indexOf(const TYPE &e) const override
Finds the index of the first occurrence of the specified element.
Definition vector.h:774
TYPE get(integer index) const override
Gets an element at the specified index.
Definition vector.h:738
void set(integer index, const TYPE &e) override
Sets the element at the specified index.
Definition vector.h:762
Iterator * begins() const override
Gets an iterator to the beginning of the vector.
Definition vector.h:897
TYPE & operator[](integer index) override
Gets a reference to the element at the specified index.
Definition vector.h:750
TYPE popEnd() override
Removes and returns the last element in the vector.
Definition vector.h:886
vector(const vector &other)
Copy constructor for the vector.
Definition vector.h:647
Requires type to support all comparison operators.
Definition types.h:92
std::uint32_t u_integer
32-bit unsigned integer type for sizes and indexes
Definition config.h:263
std::int64_t integer
64-bit signed integer type for arithmetic operations
Definition config.h:254
Provides functionality for an iteration stream.
Main namespace for the project Original.
Definition algorithms.h:21