28template <
class ElementType>
31 using DefaultVectorAllocatorType = std::allocator<ElementType>;
35template <is::number ElementType>
45template <
class Allocator>
46struct UnderlyingAllocatorExtractor
48 using Type = Allocator;
51template <
class ElementType>
52struct UnderlyingAllocatorExtractor<DefaultVectorAllocator<ElementType>>
54 using Type =
typename DefaultVectorAllocator<ElementType>::DefaultVectorAllocatorType;
57template <
class T,
template <
class>
class Allocator>
60 using Type = std::vector<T, typename UnderlyingAllocatorExtractor<Allocator<T>>::Type>;
63template <
template <
class>
class Allocator>
64struct StdVector<bool, Allocator>
66 using Type = VectorBoolWorkaround<typename UnderlyingAllocatorExtractor<Allocator<std::byte>>::Type>;
69template <
class T,
template <
class>
class Allocator>
70using StdVectorType =
typename StdVector<T, Allocator>::Type;
97template <is::nonConst ElementType,
template <
class>
class Allocator = DefaultVectorAllocator>
98class Vector :
public VctrBase<ElementType, detail::StdVectorType<ElementType, Allocator>, std::dynamic_extent>
101 using StdVectorType = detail::StdVectorType<ElementType, Allocator>;
104 template <
class OtherAllocator>
105 static constexpr bool isDifferentAllocatorTypeWithSameValueType = (! std::is_same_v<Allocator<ElementType>, OtherAllocator>) &&std::is_same_v<ElementType, typename OtherAllocator::value_type>;
107 using ConstIterator =
typename StdVectorType::const_iterator;
111 using value_type =
typename Vctr::value_type;
120 template <std::convertible_to<Allocator<ElementType>> Alloc>
121 Vector (Alloc&& allocator) :
Vctr (StdVectorType (allocator)) {}
127 constexpr Vector (
size_t size, ElementType initialValue) :
Vctr (StdVectorType (
size, initialValue)) {}
130 constexpr Vector (std::initializer_list<ElementType> il) :
Vctr (StdVectorType (il)) {}
133 template <is::suitableInitializerForElementType<ElementType> First, std::same_as<First>... Other>
134 requires (
sizeof...(Other) > 0) && (! std::convertible_to<First, size_t>)
135 constexpr Vector (First&& first, Other&&... other)
137 reserve (1 +
sizeof...(other));
139 (
push_back (std::forward<Other> (other)), ...);
156 template <is::triviallyCopyableWithDataAndSize OtherContainer>
157 requires (! std::same_as<Vector, OtherContainer>)
158 constexpr Vector (
const OtherContainer& other)
170 template <is::iteratorCopyable OtherContainer>
180 template <is::inputIteratorToConstructValuesOfType<ElementType> Iterator, std::sentinel_for<Iterator> Sentinel>
181 constexpr Vector (Iterator first, Sentinel last) :
Vctr (StdVectorType (first, last)) {}
188 template <is::suitableInitializerFunctionForElementType<ElementType> Fn>
192 for (
size_t i = 0; i <
size; ++i)
197 template <is::expression Expression>
201 Vctr::assignExpressionTemplate (std::forward<Expression> (e));
208 template <has::sizeAndDataWithElementType<ElementType> Container>
214 (! std::is_reference_v<Container>))
216 if constexpr (std::same_as<Container, Vector>)
218 Vctr::storage = std::move (containerToCopyDataFrom.storage);
220 else if constexpr (std::same_as<Container, StdVectorType>)
222 Vctr::storage = std::move (containerToCopyDataFrom);
226 Vctr::storage.assign (std::make_move_iterator (containerToCopyDataFrom.begin()), std::make_move_iterator (containerToCopyDataFrom.end()));
231 Vctr::copyFrom (containerToCopyDataFrom.data(), containerToCopyDataFrom.size());
242 return *
this = other.storage;
248 Vctr::storage.
assign (std::move (elementsToAssign));
256 template <is::expression E>
259 resize (expression.size());
260 Vctr::assignExpressionTemplate (expression);
285 constexpr operator StdVectorType&() {
return Vctr::storage; }
288 constexpr operator const StdVectorType&()
const {
return Vctr::storage; }
295 template <
class OtherAllocator>
296 requires isDifferentAllocatorTypeWithSameValueType<OtherAllocator>
297 operator std::vector<ElementType, OtherAllocator>()
const
309 constexpr void resize (
size_t i) { Vctr::storage.resize (i); }
315 constexpr void reserve (
size_t i) { Vctr::storage.reserve (i); }
327 constexpr void init (
size_t newSize,
const value_type& value)
337 template <is::suitableInitializerFunctionForElementType<ElementType> Fn>
338 constexpr void init (
size_t newSize, Fn&& initializerFunction)
341 for (
size_t i = 0; i < newSize; ++i)
342 Vctr::at (i) = initializerFunction (i);
349 constexpr void clear() noexcept { Vctr::storage.clear(); }
358 constexpr Allocator<ElementType>
get_allocator() const noexcept {
return Vctr::storage.get_allocator(); }
364 constexpr size_t capacity() const noexcept {
return Vctr::storage.capacity(); }
367 constexpr size_t max_size() const noexcept {
return Vctr::storage.max_size(); }
376 constexpr auto erase (ConstIterator elementToErase)
380 return Vctr::storage.erase (elementToErase);
396 constexpr auto erase (ConstIterator first, ConstIterator last)
402 return Vctr::storage.erase (first, last);
406 constexpr auto erase (
size_t startIdx,
size_t numElements)
408 auto it = Vctr::storage.begin() + startIdx;
409 return erase (it, it + numElements);
413 template <std::equality_comparable_with<ElementType> T>
421 template <is::functionWithSignatureOrImplicitlyConvertible<
bool (const ElementType&)> Fn>
432 template <std::equality_comparable_with<ElementType> T>
436 if (beginOfElementsToRemove ==
Vctr::end())
448 template <is::functionWithSignatureOrImplicitlyConvertible<
bool (const ElementType&)> Fn>
451 auto beginOfElementsToRemove = std::remove_if (
Vctr::begin(),
Vctr::end(), std::forward<Fn> (predicate));
452 if (beginOfElementsToRemove ==
Vctr::end())
469 constexpr ElementType&
push_back (ElementType&& newElement)
471 Vctr::storage.push_back (std::move (newElement));
472 return Vctr::storage.back();
482 constexpr ElementType&
push_back (
const ElementType& newElement)
484 Vctr::storage.push_back (newElement);
485 return Vctr::storage.back();
496 auto x = std::move (Vctr::storage.
back());
497 Vctr::storage.pop_back();
505 template <
class... Args>
506 constexpr void emplace_back (Args&&... args) { Vctr::storage.emplace_back (std::forward<Args> (args)...); }
512 constexpr void swap (
Vector& other)
noexcept { Vctr::storage.swap (other.storage); }
515 template <is::anyVctr VctrToAppend>
516 void append (VctrToAppend&& vctrToAppend,
bool moveValuesFromSrc =
false)
518 insert (
Vctr::end(), std::forward<VctrToAppend> (vctrToAppend), moveValuesFromSrc);
527 auto insert (ConstIterator pos,
const ElementType& value)
531 return Vctr::storage.insert (pos, value);
543 auto insert (ConstIterator pos, ElementType&& value)
547 return Vctr::storage.insert (pos, std::move (value));
560 auto insert (ConstIterator pos,
size_t numCopies,
const ElementType& value)
564 return Vctr::storage.insert (pos, numCopies, value);
568 auto insert (
size_t idx,
size_t numCopies,
const ElementType& value) {
return insert (
Vctr::begin() + idx, numCopies, value); }
578 template <is::inputIteratorToConstructValuesOfType<ElementType> InputIterator>
579 auto insert (ConstIterator pos, InputIterator first, InputIterator last)
591 return Vctr::storage.insert (pos, first, last);
599 template <is::inputIteratorToConstructValuesOfType<ElementType> InputIterator>
600 auto insert (
size_t idx, InputIterator first, InputIterator last)
612 auto insert (ConstIterator pos, std::initializer_list<ElementType> initList)
616 return Vctr::storage.insert (pos, initList);
620 auto insert (
size_t idx, std::initializer_list<ElementType> initList) {
return insert (
Vctr::begin() + idx, std::move (initList)); }
629 template <is::anyVctr VctrToInsert>
630 auto insert (ConstIterator pos, VctrToInsert&& vctrToInsert,
bool moveValuesFromSrc =
false)
638 moveValuesFromSrc =
true;
640 if (moveValuesFromSrc)
641 return Vctr::storage.insert (pos, std::make_move_iterator (vctrToInsert.begin()), std::make_move_iterator (vctrToInsert.end()));
643 return Vctr::storage.insert (pos, vctrToInsert.begin(), vctrToInsert.end());
651 template <is::anyVctr VctrToInsert>
652 auto insert (
size_t idx, VctrToInsert&& vctrToInsert,
bool moveValuesFromSrc =
false)
654 return insert (
Vctr::begin() + idx, std::forward<VctrToInsert> (vctrToInsert), moveValuesFromSrc);
659template <
class OwnedElementType>
669 OwnedVector (std::initializer_list<OwnedElementType*> elementsToOwn)
672 for (
auto* ptr : elementsToOwn)
677 template <
class... Args>
685template <is::triviallyCopyableWithDataAndSize OtherContainer>
686Vector (
const OtherContainer& other) -> Vector<ValueType<OtherContainer>>;
688template <is::iteratorCopyable OtherContainer>
689Vector (
const OtherContainer& other) -> Vector<ValueType<OtherContainer>>;
691template <std::input_iterator Iterator, std::sentinel_for<Iterator> Sentinel>
692Vector (Iterator first, Sentinel last) -> Vector<std::iter_value_t<Iterator>>;
694template <is::suitableInitializerFunction Fn>
695Vector (
size_t size, Fn&& initializerFunction) -> Vector<std::invoke_result_t<Fn, size_t>>;
697template <is::expression Expression>
698Vector (Expression&& e) -> Vector<ValueType<Expression>>;
700template <is::uniquePtr FirstArg, is::uniquePtr... Args>
701requires (are::same<FirstArg, Args...>)
702OwnedVector (FirstArg&&, Args&&...) -> OwnedVector<typename FirstArg::element_type>;
Returns aligned pointers when allocations are requested.
Definition: AlignedAllocator.h:29
A handy shortcut for Vector<std::unique_ptr<OwnedElementType>>.
Definition: Vector.h:661
OwnedVector(std::initializer_list< OwnedElementType * > elementsToOwn)
Creates an OwnedVector from an initializer list of raw pointers that will be owned by the Vector.
Definition: Vector.h:669
OwnedVector(Args &&... args)
Forwards every other constructor call to the base class constructor.
Definition: Vector.h:678
The base class to all one dimensional containers and views in the VCTR project.
Definition: VctrBase.h:38
constexpr size_t size() const noexcept
Returns the number of elements.
Definition: VctrBase.h:61
constexpr auto find(const T &valueToLookFor)
Returns an iterator to the first element that equals valueToLookFor or end() if none was found.
Definition: VctrBase.h:452
constexpr auto begin()
Returns an iterator to the begin of the storage.
Definition: VctrBase.h:141
constexpr void assign(std::initializer_list< ElementType > elements)
Assigns elements from the initializer list to this instance.
Definition: VctrBase.h:262
constexpr void copyFrom(const ElementType *otherData, size_t otherSize)
Copies the content from otherData to this instance.
Definition: VctrBase.h:281
VCTR_FORCEDINLINE constexpr auto * data()
Returns a raw pointer to the underlying storage.
Definition: VctrBase.h:123
constexpr auto findIf(Fn &&predicate)
Returns an iterator to the first element in this vector for which predicate returns true or end() if ...
Definition: VctrBase.h:480
constexpr auto && back()
Returns a reference to the last element.
Definition: VctrBase.h:117
constexpr auto & at(size_t i)
Returns a reference to element i.
Definition: VctrBase.h:103
constexpr bool empty() const noexcept
Checks whether the container is empty.
Definition: VctrBase.h:67
constexpr void fill(const value_type &value)
Fills the container with the given value.
Definition: VctrBase.h:299
constexpr auto end()
Returns an iterator to the first element behind the storage.
Definition: VctrBase.h:147
constexpr bool contains(const T &value) const
Returns true if at least one element is equal to value.
Definition: VctrBase.h:552
The heap-allocated container type.
Definition: Vector.h:99
auto insert(ConstIterator pos, size_t numCopies, const ElementType &value)
Inserts numCopies copies of value before the element referenced by pos and returns an iterator to the...
Definition: Vector.h:560
constexpr Vector & operator=(Container &&containerToCopyDataFrom)
Copies or moves the data of the source container to this Vector.
Definition: Vector.h:209
constexpr void init(size_t newSize, Fn &&initializerFunction)
Resizes this vector to newSize and fills it via the given initializerFunction.
Definition: Vector.h:338
auto insert(size_t idx, const ElementType &value)
Inserts value at index idx and returns an iterator to the inserted value.
Definition: Vector.h:535
constexpr Vector(Expression &&e)
Creates a Vector from an expression.
Definition: Vector.h:198
auto insert(ConstIterator pos, std::initializer_list< ElementType > initList)
Inserts a list of values before the element referenced by pos and returns an iterator to the first in...
Definition: Vector.h:612
auto insert(ConstIterator pos, ElementType &&value)
Inserts value before the element referenced by pos and returns an iterator to the inserted value.
Definition: Vector.h:543
constexpr Vector(size_t size)
Creates an uninitialised Vector of the desired size.
Definition: Vector.h:124
constexpr bool eraseAllOccurrencesIf(Fn &&predicate)
Removes all elements inside this Vector for which predicate is true and adjusts its size.
Definition: Vector.h:449
auto insert(ConstIterator pos, const ElementType &value)
Inserts value before the element referenced by pos and returns an iterator to the inserted value.
Definition: Vector.h:527
auto insert(size_t idx, std::initializer_list< ElementType > initList)
Inserts a list of values at index idx and returns an iterator to the first inserted value.
Definition: Vector.h:620
constexpr size_t max_size() const noexcept
Returns the maximum number of elements the container is able to hold.
Definition: Vector.h:367
void append(VctrToAppend &&vctrToAppend, bool moveValuesFromSrc=false)
Appends a Span, Array or Vector to the end of this Vector, optionally by moving elements from the sou...
Definition: Vector.h:516
constexpr auto erase(ConstIterator elementToErase)
Erases the element referenced by elementToErase and returns the iterator to the element behind it.
Definition: Vector.h:376
constexpr void init(size_t newSize, const value_type &value)
Resizes this vector to newSize and fills it with the given value.
Definition: Vector.h:327
auto insert(size_t idx, InputIterator first, InputIterator last)
Inserts a range of values at index idx and returns an iterator to the inserted values.
Definition: Vector.h:600
constexpr auto erase(size_t idx)
Erases the element at index idx and returns the iterator to the element behind it.
Definition: Vector.h:384
constexpr void emplace_back(Args &&... args)
Constructs an element in-place at the end of the Vector.
Definition: Vector.h:506
auto insert(size_t idx, size_t numCopies, const ElementType &value)
Inserts numCopies copies of value at index idx and returns an iterator to the inserted values.
Definition: Vector.h:568
Vector(Alloc &&allocator)
Creates an empty Vector that uses the specified allocator instance.
Definition: Vector.h:121
constexpr Vector()=default
Creates an empty Vector with size = 0.
constexpr auto eraseFirstOccurrenceIf(Fn &&predicate)
Erases the first occurrence for which predicate is true from this Vector and adjusts its size.
Definition: Vector.h:422
auto insert(ConstIterator pos, VctrToInsert &&vctrToInsert, bool moveValuesFromSrc=false)
Inserts a VCTR container before the element referenced by pos and returns an iterator to the first in...
Definition: Vector.h:630
constexpr Vector(std::initializer_list< ElementType > il)
Creates a Vector from an initializer list.
Definition: Vector.h:130
constexpr ElementType pop_back()
Removes the last element in the vector, effectively reducing the container size by one.
Definition: Vector.h:493
constexpr Vector(size_t size, ElementType initialValue)
Creates a Vector with all elements initialised to initialValue of the desired size.
Definition: Vector.h:127
auto insert(size_t idx, ElementType &&value)
Inserts value at index idx and returns an iterator to the inserted value.
Definition: Vector.h:551
constexpr void swap(Vector &other) noexcept
Swaps the underlying memory with the other Vector.
Definition: Vector.h:512
constexpr Vector(const ElementType *data, size_t size)
Copies size elements from the memory location pointed to by data.
Definition: Vector.h:174
constexpr size_t capacity() const noexcept
Returns the number of elements the Vector can currently hold without re-allocation.
Definition: Vector.h:364
constexpr auto erase(size_t startIdx, size_t numElements)
Erases numElements elements starting from index startIdx and returns the iterator to the element behi...
Definition: Vector.h:406
constexpr ElementType & push_back(ElementType &&newElement)
Adds an element to the end of the Vector.
Definition: Vector.h:469
constexpr Allocator< ElementType > get_allocator() const noexcept
Returns the allocator associated with the Vector.
Definition: Vector.h:358
constexpr auto eraseFirstOccurrenceOf(const T &value)
Erases the first occurrence of value from this Vector and adjusts its size.
Definition: Vector.h:414
constexpr const StdVectorType & getUnderlyingVector() const
Returns a reference to the underlying storage.
Definition: Vector.h:276
constexpr ElementType & push_back(const ElementType &newElement)
Adds an element to the end of the Vector.
Definition: Vector.h:482
constexpr void resize(size_t i)
Changes the size of this Vector, potentially allocating memory.
Definition: Vector.h:309
auto insert(ConstIterator pos, InputIterator first, InputIterator last)
Inserts a range of values before the element referenced by pos and returns an iterator to the inserte...
Definition: Vector.h:579
constexpr Vector(const OtherContainer &other)
This constructor will create a Vector instance of the same size as OtherContainer and will copy its v...
Definition: Vector.h:171
constexpr Vector(size_t size, Fn &&initializerFunction)
Creates a Vector of the given size and initialises all elements by calling initializerFunction with t...
Definition: Vector.h:189
auto insert(size_t idx, VctrToInsert &&vctrToInsert, bool moveValuesFromSrc=false)
Inserts a VCTR container at index idx and returns an iterator to the first inserted value.
Definition: Vector.h:652
constexpr Vector(Vector &&other)
Moves the other Vector into this one.
Definition: Vector.h:146
constexpr void reserve(size_t i)
Pre-allocates memory for the given number of elements.
Definition: Vector.h:315
constexpr void clear() noexcept
Erase all elements from the Vector.
Definition: Vector.h:349
constexpr bool eraseAllOccurrencesOf(const T &value)
Removes all occurrences of value from this Vector and adjusts its size.
Definition: Vector.h:433
constexpr Vector(const Vector &other)
Creates a copy of the other Vector.
Definition: Vector.h:143
constexpr StdVectorType & getUnderlyingVector()
Returns a reference to the underlying storage.
Definition: Vector.h:270
constexpr void shrink_to_fit()
Adjusts the allocated memory to the actual number of elements.
Definition: Vector.h:321
constexpr StdVectorType && moveUnderlyingVector() &&
Moves the underlying storage out of this wrapper class.
Definition: Vector.h:282
constexpr Vector(Iterator first, Sentinel last)
Creates an Vector from an iterator and a sentinel by initialising the Vector with the content read fr...
Definition: Vector.h:181
constexpr auto erase(ConstIterator first, ConstIterator last)
Erases the range of elements referenced by it and returns the iterator to the element behind it.
Definition: Vector.h:396
Constrains a type to have a member function begin() or begin() const.
Definition: ContainerAndExpressionConcepts.h:138
Constrains a type to have a member function end() or end() const.
Definition: ContainerAndExpressionConcepts.h:142
Constrains the type to be a contiguous iterator with a value type same as ElementType.
Definition: ContainerAndExpressionConcepts.h:359
Constrains a type to be a view rather than an owning container.
Definition: ContainerAndExpressionConcepts.h:224
The main namespace of the VCTR project.
Definition: Array.h:24
The default allocator choice for non-arithmetic types is a simple std::allocator.
Definition: Vector.h:30