42 template <
typename TYPE,
typename ALLOC = allocator<TYPE>>
45 static constexpr u_integer INNER_SIZE_INIT = 16;
78 void vectorArrayDestroy()
noexcept;
264 template<
typename...
ARGS>
461 template<
typename TYPE,
typename ALLOC>
465 template <
typename TYPE,
typename ALLOC>
469 this->max_size = INNER_SIZE_INIT;
470 this->inner_begin = (INNER_SIZE_INIT - 1)/2;
471 this->body = vector::vectorArrayInit(INNER_SIZE_INIT);
474 template <
typename TYPE,
typename ALLOC>
478 for (
u_integer i = 0; i < this->max_size; ++i) {
482 this->body =
nullptr;
486 template <
typename TYPE,
typename ALLOC>
495 template <
typename TYPE,
typename ALLOC>
498 if constexpr (std::is_copy_constructible_v<TYPE>) {
499 return this->body[pos];
500 }
else if constexpr (std::is_move_constructible_v<TYPE>) {
501 return std::move(this->body[pos]);
503 staticError<unSupportedMethodError, !std::is_copy_constructible_v<TYPE> && !std::is_move_constructible_v<TYPE>>::asserts();
508 template <
typename TYPE,
typename ALLOC>
511 setBufElem(this->body, pos, e);
514 template <
typename TYPE,
typename ALLOC>
517 if constexpr (std::is_copy_assignable_v<TYPE>) {
519 }
else if constexpr (std::is_move_assignable_v<TYPE>) {
520 buf[pos] = std::move(
const_cast<TYPE&
>(e));
522 staticError<unSupportedMethodError, !std::is_copy_constructible_v<TYPE> && !std::is_move_constructible_v<TYPE>>::asserts();
526 template <
typename TYPE,
typename ALLOC>
533 setBufElem(new_body, inner_idx + offset + len - 1 - i, old_body[inner_idx + len - 1 - i]);
539 setBufElem(new_body, inner_idx + offset + i, old_body[inner_idx + i]);
544 template <
typename TYPE,
typename ALLOC>
547 return this->inner_begin + index;
550 template <
typename TYPE,
typename ALLOC>
553 return this->inner_begin + this->
size() + increment > this->max_size - 1 ||
static_cast<integer>(this->inner_begin) -
static_cast<integer>(increment) < 0;
556 template <
typename TYPE,
typename ALLOC>
559 TYPE* new_body = vector::vectorArrayInit(new_size);
560 u_integer new_begin = (new_size - 1) / 4;
562 vector::moveElements(this->body, this->inner_begin,
563 this->
size(), new_body, offset);
564 this->vectorArrayDestroy();
565 this->body = new_body;
566 this->max_size = new_size;
567 this->inner_begin = new_begin;
570 template <
typename TYPE,
typename ALLOC>
572 if (!this->outOfMaxSize(increment)) {
575 u_integer new_begin = (this->max_size - this->
size() - increment) / 2;
576 if (this->max_size >= this->size_ + increment && new_begin > 0) {
578 vector::moveElements(this->body, this->inner_begin, this->
size(),
580 this->inner_begin = new_begin;
582 const u_integer new_max_size = (this->
size() + increment) * 2;
583 this->grow(new_max_size);
587 template <
typename TYPE,
typename ALLOC>
589 : randomAccessIterator<TYPE, ALLOC>(ptr, container, pos) {}
591 template <
typename TYPE,
typename ALLOC>
598 template <
typename TYPE,
typename ALLOC>
601 if (
this == &
other) {
608 template <
typename TYPE,
typename ALLOC>
613 template <
typename TYPE,
typename ALLOC>
616 return this->_ptr + 1 ==
other_it->_ptr;
619 template <
typename TYPE,
typename ALLOC>
622 return other_it->_ptr + 1 == this->_ptr;
625 template <
typename TYPE,
typename ALLOC>
627 return "vector::Iterator";
630 template <
typename TYPE,
typename ALLOC>
637template<
typename TYPE,
typename ALLOC>
638template<
typename...
ARGS>
641 this->body = this->
allocate(this->max_size);
643 this->
construct(&this->body[this->toInnerIdx(
i)], std::forward<ARGS>(
args)...);
647template<
typename TYPE,
typename ALLOC>
650 max_size(size * 4 / 3), inner_begin(size / 3 >= 1 ? size / 3 - 1 : 0), body(
nullptr) {
653 template <
typename TYPE,
typename ALLOC>
658 template <
typename TYPE,
typename ALLOC>
661 this->adjust(
list.size());
664 this->setElem(this->inner_begin + this->
size(), e);
669 template <
typename TYPE,
typename ALLOC>
675 this->vectorArrayDestroy();
677 this->max_size =
other.max_size;
678 this->inner_begin =
other.inner_begin;
679 this->size_ =
other.size_;
680 this->body = vector::vectorArrayInit(this->max_size);
682 const TYPE& data =
other.body[this->toInnerIdx(
i)];
683 this->setElem(this->toInnerIdx(
i), data);
685 if constexpr (ALLOC::propagate_on_container_copy_assignment::value){
691 template <
typename TYPE,
typename ALLOC>
694 this->operator=(std::move(
other));
697 template <
typename TYPE,
typename ALLOC>
703 this->vectorArrayDestroy();
704 this->body =
other.body;
705 other.body =
nullptr;
706 this->max_size =
other.max_size;
707 this->inner_begin =
other.inner_begin;
708 this->size_ =
other.size_;
709 if constexpr (ALLOC::propagate_on_container_move_assignment::value){
716 template <
typename TYPE,
typename ALLOC>
719 this->adjust(
arr.size());
722 this->setElem(this->toInnerIdx(
i),
arr.
get(
i));
727 template <
typename TYPE,
typename ALLOC>
733 template <
typename TYPE,
typename ALLOC>
736 return this->max_size;
739 template <
typename TYPE,
typename ALLOC>
741 return this->body[this->toInnerIdx(0)];
744 template <
typename TYPE,
typename ALLOC>
747 if (this->indexOutOfBound(index))
750 " out of bound max index " +
std::to_string(this->size() - 1) +
".");
752 index = this->toInnerIdx(this->parseNegIndex(index));
753 return this->getElem(index);
756 template <
typename TYPE,
typename ALLOC>
759 if (this->indexOutOfBound(index))
762 " out of bound max index " +
std::to_string(this->size() - 1) +
".");
764 index = this->toInnerIdx(this->parseNegIndex(index));
765 return this->body[index];
768 template <
typename TYPE,
typename ALLOC>
771 if (this->indexOutOfBound(index))
774 " out of bound max index " +
std::to_string(this->size() - 1) +
".");
776 index = this->toInnerIdx(this->parseNegIndex(index));
777 this->setElem(index,
e);
780 template <
typename TYPE,
typename ALLOC>
786 if (this->get(
i) ==
e)
797 template <
typename TYPE,
typename ALLOC>
801 this->inner_begin -= 1;
802 this->setElem(this->toInnerIdx(0),
e);
806 template <
typename TYPE,
typename ALLOC>
809 if (this->parseNegIndex(index) == this->size())
812 }
else if (this->parseNegIndex(index) == 0)
817 if (this->indexOutOfBound(index))
820 " out of bound max index " +
std::to_string(this->size() - 1) +
".");
823 index = this->toInnerIdx(this->parseNegIndex(index));
825 if (index - this->inner_begin <= (this->size() - 1) / 2)
827 vector::moveElements(this->body, this->inner_begin,
829 this->inner_begin -= 1;
832 vector::moveElements(this->body, index,
833 this->size() -
rel_idx, this->body, 1);
835 this->setElem(this->toInnerIdx(
rel_idx),
e);
840 template <
typename TYPE,
typename ALLOC>
844 this->setElem(this->toInnerIdx(this->size()),
e);
848 template <
typename TYPE,
typename ALLOC>
851 if (this->size() == 0){
855 this->inner_begin += 1;
860 template <
typename TYPE,
typename ALLOC>
863 if (this->parseNegIndex(index) == 0)
865 return this->popBegin();
867 if (this->parseNegIndex(index) == this->size() - 1)
869 return this->popEnd();
871 if (this->indexOutOfBound(index)){
873 " out of bound max index " +
std::to_string(this->size() - 1) +
".");
876 index = this->toInnerIdx(this->parseNegIndex(index));
878 if (index - this->inner_begin <= (this->size() - 1) / 2)
880 vector::moveElements(this->body, this->inner_begin,
882 this->inner_begin += 1;
885 vector::moveElements(this->body, index + 1,
886 this->size() - 1 -
rel_idx, this->body, -1);
892 template <
typename TYPE,
typename ALLOC>
895 if (this->size() == 0){
898 TYPE res = this->get(this->size() - 1);
903 template <
typename TYPE,
typename ALLOC>
905 return new Iterator(&this->body[this->toInnerIdx(0)],
this, 0);
908 template <
typename TYPE,
typename ALLOC>
910 return new Iterator(&this->body[this->toInnerIdx(this->size() - 1)],
this, this->size() - 1);
913 template <
typename TYPE,
typename ALLOC>
919 template <
typename TYPE,
typename ALLOC>
921 this->vectorArrayDestroy();
924 template <
typename TYPE,
typename ALLOC>
934 if constexpr (ALLOC::propagate_on_container_swap::value) {
939 template<
typename T,
typename... ARGS>
944 template <
typename TYPE,
typename ALLOC>
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:153
void swap(autoPtr &other) noexcept
Swaps the reference counters between two autoPtr instances.
Definition autoPtr.h:703
const TYPE * get() const
Get managed pointer const version.
Definition autoPtr.h:629
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
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, hashing and printing.
Definition iterationStream.h:60
Exception for missing element requests.
Definition error.h:298
Exception for container index out-of-range errors.
Definition error.h:192
Unique ownership smart pointer with move semantics.
Definition ownerPtr.h:37
Abstract base class for random-access iterators.
Definition randomAccessIterator.h:39
randomAccessIterator & operator=(const randomAccessIterator &other)
Copy assignment operator.
Definition randomAccessIterator.h:197
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:271
Random access iterator implementation for vector.
Definition vector.h:188
Iterator * clone() const override
Clones the iterator.
Definition vector.h:609
Iterator & operator=(const Iterator &other)
Assignment operator for the iterator.
Definition vector.h:599
std::string className() const override
Gets the class name of the iterator.
Definition vector.h:626
bool atNext(const iterator< TYPE > *other) const override
Checks if the iterator is at the next element relative to another iterator.
Definition vector.h:620
bool atPrev(const iterator< TYPE > *other) const override
Checks if the iterator is at the previous element relative to another iterator.
Definition vector.h:614
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:728
vector(const std::initializer_list< TYPE > &list)
Constructs a vector from an initializer list.
Definition vector.h:659
vector & operator=(const vector &other)
Assignment operator for the vector.
Definition vector.h:670
vector(const array< TYPE > &arr)
Constructs a vector from an array.
Definition vector.h:717
TYPE popBegin() override
Removes and returns the first element in the vector.
Definition vector.h:849
TYPE & data() const
Gets a reference to the first element in the vector.
Definition vector.h:740
void push(integer index, const TYPE &e) override
Inserts an element at the specified index in the vector.
Definition vector.h:807
vector(vector &&other) noexcept
Move constructor for the vector.
Definition vector.h:692
~vector() override
Destructor for the vector.
Definition vector.h:920
Iterator * ends() const override
Gets an iterator to the end of the vector.
Definition vector.h:909
u_integer capacity() const noexcept
Gets the current capacity of the vector.
Definition vector.h:734
vector & operator=(vector &&other) noexcept
Move assignment operator for the vector.
Definition vector.h:698
void pushEnd(const TYPE &e) override
Inserts an element at the end of the vector.
Definition vector.h:841
void pushBegin(const TYPE &e) override
Inserts an element at the beginning of the vector.
Definition vector.h:798
std::string className() const override
Gets the class name of the vector.
Definition vector.h:914
vector(u_integer size, ALLOC alloc, ARGS &&... args)
Constructs a vector with specified size and allocator, initializing elements with provided arguments.
Definition vector.h:639
vector(ALLOC alloc=ALLOC{})
Constructs a vector with specified allocator.
Definition vector.h:631
TYPE pop(integer index) override
Removes and returns the element at the specified index.
Definition vector.h:861
u_integer indexOf(const TYPE &e) const override
Finds the index of the first occurrence of the specified element.
Definition vector.h:781
TYPE get(integer index) const override
Gets an element at the specified index.
Definition vector.h:745
void set(integer index, const TYPE &e) override
Sets the element at the specified index.
Definition vector.h:769
Iterator * begins() const override
Gets an iterator to the beginning of the vector.
Definition vector.h:904
TYPE & operator[](integer index) override
Gets a reference to the element at the specified index.
Definition vector.h:757
TYPE popEnd() override
Removes and returns the last element in the vector.
Definition vector.h:893
vector(const vector &other)
Copy constructor for the vector.
Definition vector.h:654
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 with comparison, hashing and printing.
Main namespace for the project Original.
Definition algorithms.h:21
SERIAL< TYPE > list(coroutine::generator< TYPE > gen)
Collects generator elements into a list container.
Standard namespace extensions for original::alternative.
Definition allocator.h:351
std::string to_string(const T &t)
std::to_string overload for printable-derived types
Definition printable.h:415
void swap(original::objPoolAllocator< TYPE > &lhs, original::objPoolAllocator< TYPE > &rhs) noexcept
Specialization of std::swap for objPoolAllocator.
Definition allocator.h:635