A constexpr vector implementation with dynamic size at compile time. More...
#include <bio/ranges/container/small_vector.hpp>
Public Types | |
Associated types | |
using | value_type = value_type_ |
The value_type type. | |
using | reference = value_type & |
The reference type. | |
using | const_reference = value_type const & |
The const_reference type. | |
using | iterator = value_type * |
The iterator type. | |
using | const_iterator = value_type const * |
The const_iterator type. | |
using | difference_type = ptrdiff_t |
The difference_type type. | |
using | size_type = meta::detail::min_viable_uint_t< capacity_ > |
The size_type type. | |
Public Member Functions | |
Constructors, destructor and assignment | |
constexpr | small_vector () noexcept=default |
Defaulted. | |
constexpr | small_vector (small_vector const &) noexcept=default |
Defaulted. | |
constexpr | small_vector (small_vector &&) noexcept=default |
Defaulted. | |
constexpr small_vector & | operator= (small_vector const &) noexcept=default |
Defaulted. | |
constexpr small_vector & | operator= (small_vector &&) noexcept=default |
Defaulted. | |
~small_vector () noexcept=default | |
Defaulted. | |
constexpr | small_vector (std::array< value_type, capacity_ > const &array) noexcept(is_noexcept) |
Construct from an (smaller or equally sized) array over the same value type. | |
template<size_t capacity2> | |
constexpr | small_vector (value_type const (&array)[capacity2]) noexcept(is_noexcept) |
Construct from a (smaller or equally sized) built in array over the same value type. | |
template<typename... other_value_type> requires (std::same_as<value_type, other_value_type> && ...) | |
constexpr | small_vector (other_value_type... args) noexcept(is_noexcept) |
Construct from a list of values of value_type. | |
template<std::forward_iterator begin_it_type, typename end_it_type > requires (std::sentinel_for<end_it_type, begin_it_type> && std::constructible_from<value_type, std::iter_reference_t<begin_it_type>>) | |
constexpr | small_vector (begin_it_type begin_it, end_it_type end_it) noexcept(is_noexcept) |
Construct from two iterators. | |
template<meta::different_from< small_vector > other_range_t> requires (std::ranges::input_range<other_range_t>) | |
constexpr | small_vector (other_range_t &&range) noexcept(is_noexcept) |
Construct from a different range. | |
constexpr | small_vector (size_type n, value_type value) noexcept(is_noexcept) |
Construct with n times value . | |
constexpr small_vector & | operator= (std::initializer_list< value_type > ilist) noexcept(is_noexcept) |
Assign from std::initializer_list . | |
constexpr void | assign (std::initializer_list< value_type > ilist) noexcept(is_noexcept) |
Assign from std::initializer_list . | |
constexpr void | assign (size_type const count, value_type const value) noexcept(is_noexcept) |
Assign with count times value . | |
template<std::ranges::input_range other_range_t> requires std::constructible_from<value_type, std::ranges::range_reference_t<other_range_t>> | |
constexpr void | assign (other_range_t &&range) noexcept(is_noexcept) |
Assign from a different range. | |
template<std::forward_iterator begin_it_type, typename end_it_type > requires (std::sentinel_for<end_it_type, begin_it_type> && std::constructible_from<value_type, std::iter_reference_t<begin_it_type>>) | |
constexpr void | assign (begin_it_type begin_it, end_it_type end_it) noexcept(is_noexcept) |
Assign from pair of iterators. | |
Iterators | |
constexpr iterator | begin () noexcept |
Returns the begin iterator of the vector. | |
constexpr const_iterator | begin () const noexcept |
Returns the begin iterator of the vector. | |
constexpr const_iterator | cbegin () const noexcept |
Returns the begin iterator of the vector. | |
constexpr iterator | end () noexcept |
Returns iterator past the end of the vector. | |
constexpr const_iterator | end () const noexcept |
Returns iterator past the end of the vector. | |
constexpr const_iterator | cend () const noexcept |
Returns iterator past the end of the vector. | |
Element access | |
reference | at (size_type const i) |
Return the i-th element. | |
const_reference | at (size_type const i) const |
Return the i-th element. | |
constexpr reference | operator[] (size_type const i) noexcept |
Return the i-th element. | |
constexpr const_reference | operator[] (size_type const i) const noexcept |
Return the i-th element. | |
constexpr reference | front () noexcept |
Return the first element. Calling front on an empty container is undefined. | |
constexpr const_reference | front () const noexcept |
Return the first element. Calling front on an empty container is undefined. | |
constexpr reference | back () noexcept |
Return the last element. | |
constexpr const_reference | back () const noexcept |
Return the last element. | |
constexpr value_type * | data () noexcept |
Direct access to the underlying array. | |
constexpr value_type const * | data () const noexcept |
Direct access to the underlying array. | |
Capacity | |
constexpr bool | empty () const noexcept |
Checks whether the container is empty. | |
constexpr size_type | size () const noexcept |
Returns the number of elements in the container, i.e. std::distance(begin(), end()). | |
constexpr size_type | max_size () const noexcept |
Returns the maximum number of elements the container is able to hold and resolves to capacity_ . | |
constexpr size_type | capacity () const noexcept |
Returns the number of elements that the container is able to hold and resolves to capacity_ . | |
constexpr void | reserve (size_type) const noexcept |
Since the capacity is fixed on compile time, this is a no-op. | |
constexpr void | shrink_to_fit () const noexcept |
Since the capacity is fixed on compile time, this is a no-op. | |
Modifiers | |
constexpr void | clear () noexcept |
Removes all elements from the container. | |
constexpr iterator | insert (const_iterator pos, value_type const value) noexcept(is_noexcept) |
Inserts value before position in the container. | |
constexpr iterator | insert (const_iterator pos, size_type const count, value_type const value) noexcept(is_noexcept) |
Inserts count copies of value before position in the container. | |
template<std::forward_iterator begin_it_type, typename end_it_type > requires (std::sentinel_for<end_it_type, begin_it_type> && std::constructible_from<value_type, std::iter_reference_t<begin_it_type>>) | |
constexpr iterator | insert (const_iterator pos, begin_it_type begin_it, end_it_type end_it) noexcept(is_noexcept) |
Inserts elements from range [begin_it, end_it) before position in the container. | |
constexpr iterator | insert (const_iterator pos, std::initializer_list< value_type > const &ilist) noexcept(is_noexcept) |
Inserts elements from initializer list before position in the container. | |
constexpr iterator | erase (const_iterator begin_it, const_iterator end_it) noexcept |
Removes specified elements from the container. | |
constexpr iterator | erase (const_iterator pos) noexcept |
Removes specified elements from the container. | |
constexpr void | push_back (value_type const value) noexcept |
Appends the given element value to the end of the container. | |
constexpr void | pop_back () noexcept |
Removes the last element of the container. | |
constexpr void | resize (size_type const count) noexcept |
Resizes the container to contain count elements. | |
constexpr void | resize (size_type const count, value_type const value) noexcept |
Resizes the container to contain count elements. | |
constexpr void | swap (small_vector &rhs) noexcept(is_noexcept) |
Swap contents with another instance. | |
constexpr void | swap (small_vector &&rhs) noexcept(is_noexcept) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. | |
Friends | |
constexpr void | swap (small_vector &&lhs, small_vector &&rhs) noexcept(is_noexcept) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. | |
constexpr void | swap (small_vector &lhs, small_vector &rhs) noexcept(is_noexcept) |
Swap contents with another instance. | |
Comparison operators | |
template<size_t cap2> | |
constexpr bool | operator== (small_vector const &lhs, small_vector< value_type, cap2 > const &rhs) noexcept |
Performs element-wise comparison. | |
template<size_t cap2> | |
constexpr bool | operator< (small_vector const &lhs, small_vector< value_type, cap2 > const &rhs) noexcept |
Performs element-wise comparison. | |
template<size_t cap2> | |
constexpr bool | operator> (small_vector const &lhs, small_vector< value_type, cap2 > const &rhs) noexcept |
Performs element-wise comparison. | |
template<size_t cap2> | |
constexpr bool | operator<= (small_vector const &lhs, small_vector< value_type, cap2 > const &rhs) noexcept |
Performs element-wise comparison. | |
template<size_t cap2> | |
constexpr bool | operator>= (small_vector const &lhs, small_vector< value_type, cap2 > const &rhs) noexcept |
Performs element-wise comparison. | |
A constexpr vector implementation with dynamic size at compile time.
value_type_ | The underlying value type stored in the vector. |
capacity_ | The capacity of the constexpr vector. |
This implementation of a vector can be constructed, accessed and modified at compile time. It has a fixed capacity but a dynamic size and provides all functionality of a sequence container. Note that it also models a reservable sequence container but all associated member functions are no-op because the capacity is fixed.
|
inlineexplicitconstexprnoexcept |
Construct from an (smaller or equally sized) array over the same value type.
[in] | array | The array to copy the data_ from. |
Linear in the size of array
.
No-throw guarantee if value_type is std::is_nothrow_copy_constructible.
|
inlineexplicitconstexprnoexcept |
Construct from a (smaller or equally sized) built in array over the same value type.
[in] | array | The array to copy the data_ from. |
Linear in capacity_
.
No-throw guarantee if value_type is std::is_nothrow_copy_constructible.
|
inlineconstexprnoexcept |
Construct from a list of values of value_type.
other_value_type | A parameter pack where each type is equal to value_type. |
[in] | args | The values to construct from. Complexity |
Linear in the number of elements.
No-throw guarantee if value_type is std::is_nothrow_copy_constructible.
|
inlineconstexprnoexcept |
Construct from two iterators.
begin_it_type | Must model std::forward_iterator and value_type must be constructible from the reference type of begin_it_type. |
end_it_type | Must satisfy std::sentinel_for. |
[in] | begin_it | Begin of range to construct/assign from. |
[in] | end_it | End of range to construct/assign from. |
Linear in the distance between begin_it
and end_it
.
No-throw guarantee if value_type is std::is_nothrow_copy_constructible.
|
inlineexplicitconstexprnoexcept |
Construct from a different range.
other_range_t | The type of range to be inserted; must satisfy std::ranges::input_range and value_type must be constructible from std::ranges::range_reference_t<other_range_t>. |
[in] | range | The sequences to construct/assign from. |
Linear in the size of range
.
No-throw guarantee if value_type is std::is_nothrow_copy_constructible.
|
inlineconstexprnoexcept |
Construct with n
times value
.
[in] | n | Number of elements. |
[in] | value | The initial value to be assigned. |
Linear in n
.
No-throw guarantee if value_type is std::is_nothrow_copy_constructible.
|
inlineconstexprnoexcept |
Assign from pair of iterators.
begin_it_type | Must satisfy std::forward_iterator and the value_type must be constructible from the reference type of begin_it_type. |
end_it_type | Must satisfy std::sentinel_for. |
[in] | begin_it | Begin of range to construct/assign from. |
[in] | end_it | End of range to construct/assign from. |
Linear in the distance between begin_it
and end_it
.
No-throw guarantee if value_type is std::is_nothrow_copy_constructible.
|
inlineconstexprnoexcept |
Assign from a different range.
other_range_t | The type of range to be inserted; must satisfy std::ranges::input_range and value_type must be constructible from std::ranges::range_reference_t<other_range_t>. |
[in] | range | The sequences to construct/assign from. |
Linear in the size of range
.
No-throw guarantee if value_type is std::is_nothrow_copy_constructible.
|
inlineconstexprnoexcept |
Assign with count
times value
.
[in] | count | Number of elements. |
[in] | value | The initial value to be assigned. |
In .
No-throw guarantee if value_type is std::is_nothrow_copy_constructible.
|
inlineconstexprnoexcept |
Assign from std::initializer_list
.
[in] | ilist | A std::initializer_list of value_type. |
Linear in the size of ilist
.
No-throw guarantee if value_type is std::is_nothrow_copy_constructible.
|
inline |
Return the i-th element.
[in] | i | Index of the element to retrieve. |
std::out_of_range | If you access an element behind the last. |
i
.Constant.
Throws std::out_of_range if i >= size()
.
|
inline |
Return the i-th element.
[in] | i | Index of the element to retrieve. |
std::out_of_range | If you access an element behind the last. |
i
.Constant.
Throws std::out_of_range if i >= size()
.
|
inlineconstexprnoexcept |
Return the last element.
Calling back on an empty container is undefined. In debug mode an assertion checks the size of the container.
Constant.
No-throw guarantee.
|
inlineconstexprnoexcept |
Return the last element.
Calling back on an empty container is undefined. In debug mode an assertion checks the size of the container.
Constant.
No-throw guarantee.
|
inlineconstexprnoexcept |
Returns the begin iterator of the vector.
|
inlineconstexprnoexcept |
Returns the number of elements that the container is able to hold and resolves to capacity_
.
Constant.
No-throw guarantee.
|
inlineconstexprnoexcept |
Returns the begin iterator of the vector.
|
inlineconstexprnoexcept |
Returns iterator past the end of the vector.
|
inlineconstexprnoexcept |
Removes all elements from the container.
Constant.
No-throw guarantee.
|
inlineconstexprnoexcept |
Direct access to the underlying array.
|
inlineconstexprnoexcept |
Checks whether the container is empty.
true
if the container is empty, false
otherwise.Constant.
No-throw guarantee.
|
inlineconstexprnoexcept |
Returns iterator past the end of the vector.
|
inlineconstexprnoexcept |
Removes specified elements from the container.
begin_it | Begin of range to erase. |
end_it | Behind the end of range to erase. |
pos
refers to the last element, the end() iterator is returned.Invalidates iterators and references at or after the point of the erase, including the end() iterator.
The iterator begin_it does not need to be dereferenceable if begin_it==end_it: erasing an empty range is a no-op.
Linear in size().
No-throw guarantee.
|
inlineconstexprnoexcept |
Removes specified elements from the container.
pos | Remove the element at pos. |
pos
refers to the last element, the end() iterator is returned.Invalidates iterators and references at or after the point of the erase, including the end() iterator.
The iterator pos
must be valid and dereferenceable. Thus the end() iterator (which is valid, but is not dereferencable) cannot be used as a value for pos.
Linear in size().
No-throw guarantee.
|
inlineconstexprnoexcept |
Return the first element. Calling front on an empty container is undefined.
Calling front on an empty container is undefined. In debug mode an assertion checks the size of the container.
Constant.
No-throw guarantee.
|
inlineconstexprnoexcept |
Return the first element. Calling front on an empty container is undefined.
Calling front on an empty container is undefined. In debug mode an assertion checks the size of the container.
Constant.
No-throw guarantee.
|
inlineconstexprnoexcept |
Inserts elements from range [begin_it, end_it)
before position in the container.
begin_it_type | Must satisfy std::forward_iterator and the value_type must be constructible from the reference type of begin_it_type. |
end_it_type | Must satisfy std::sentinel_for. |
[in] | pos | Iterator before which the content will be inserted. pos may be the end() iterator. |
[in] | begin_it | Begin of range to construct/assign from. |
[in] | end_it | End of range to construct/assign from. |
pos
if begin_it==end_it
.The behaviour is undefined if begin_it and end_it are iterators into *this
or if, given the size n
of the range represented by [begin_t, end_it), size()
+ n
> capacity()
.
Worst-case linear in size().
No-throw guarantee if value_type is std::is_nothrow_copy_constructible.
|
inlineconstexprnoexcept |
Inserts count copies of value before position in the container.
pos | Iterator before which the content will be inserted. pos may be the end() iterator. |
count | Number of copies. |
value | Element value to insert. |
pos
if count==0
.If size()
+ count
> capacity()
this function results in undefined behaviour.
Worst-case linear in size().
No-throw guarantee if value_type is std::is_nothrow_copy_constructible.
|
inlineconstexprnoexcept |
Inserts elements from initializer list before position in the container.
pos | Iterator before which the content will be inserted. pos may be the end() iterator. |
ilist | Initializer list with values to insert. |
pos
if ilist
is empty.Given the size n
of ilist
, this function results in undefined behaviour if size()
+ n
> capacity()
.
Worst-case linear in size().
No-throw guarantee if value_type is std::is_nothrow_copy_constructible.
|
inlineconstexprnoexcept |
Inserts value before position in the container.
pos | Iterator before which the content will be inserted. pos may be the end() iterator. |
value | Element value to insert. |
Inserting a value although the maximum capacity is reached is undefined behaviour.
Worst-case linear in size().
No-throw guarantee if value_type is std::is_nothrow_copy_constructible.
|
inlineconstexprnoexcept |
Returns the maximum number of elements the container is able to hold and resolves to capacity_
.
This value typically reflects the theoretical limit on the size of the container. At runtime, the size of the container may be limited to a value smaller than max_size() by the amount of RAM available.
Constant.
No-throw guarantee.
|
inlineconstexprnoexcept |
Assign from std::initializer_list
.
[in] | ilist | A std::initializer_list of value_type. |
Linear in the size of ilist
.
No-throw guarantee if value_type is std::is_nothrow_copy_constructible.
|
inlineconstexprnoexcept |
Return the i-th element.
i | The element to retrieve. |
i
.Accessing an element behind the last causes undefined behaviour. In debug mode an assertion checks the size of the container.
Constant.
No-throw guarantee.
|
inlineconstexprnoexcept |
Return the i-th element.
i | The element to retrieve. |
i
.Accessing an element behind the last causes undefined behaviour. In debug mode an assertion checks the size of the container.
Constant.
No-throw guarantee.
|
inlineconstexprnoexcept |
Removes the last element of the container.
Calling pop_back() on an empty container is undefined. In debug mode an assertion will be thrown.
No iterators or references except for back() and end() are invalidated.
Constant.
No-throw guarantee.
|
inlineconstexprnoexcept |
Appends the given element value to the end of the container.
value | The value to append. |
If the new size() is greater than capacity() this is undefined behaviour.
Constant.
No-throw guarantee.
|
inlineconstexprnoexcept |
Resizes the container to contain count elements.
[in] | count | The new size. |
If count is greater than capacity this is undefined behaviour.
Constant.
No-throw guarantee.
|
inlineconstexprnoexcept |
Resizes the container to contain count elements.
[in] | value | Append copies of value when resizing. |
[in] | count | The new size. |
If count is greater than capacity this is undefined behaviour.
Constant.
No-throw guarantee.
|
inlineconstexprnoexcept |
Returns the number of elements in the container, i.e. std::distance(begin(), end()).
Constant.
No-throw guarantee.
|
inlineconstexprnoexcept |
Swap contents with another instance.
rhs | The other instance to swap with. |
Linear in the size of both containers.
No-throw guarantee if value_type is std::is_nothrow_copy_constructible.
|
friend |
Swap contents with another instance.
lhs | The first instance. |
rhs | The other instance to swap with. |
Linear in the size of both containers.
No-throw guarantee if value_type is std::is_nothrow_copy_constructible.