16 #include <type_traits>
18 #if SEQAN3_WITH_CEREAL
19 # include <cereal/types/array.hpp>
45 template <
typename value_type_,
size_t capacity_>
50 static constexpr
bool is_noexcept = std::is_nothrow_copy_constructible_v<value_type_>;
129 template <
size_t capacity2>
133 static_assert(capacity2 <= capacity_,
"You can only initialize from array that has smaller or equal capacity.");
134 std::ranges::copy(array, data_.
begin());
151 template <
size_t capacity2>
154 static_assert(capacity2 <= capacity_,
"You can only initialize from array that has smaller or equal capacity.");
155 std::ranges::copy(array, data_.
begin());
171 template <
typename... other_value_type>
172 requires (std::same_as<value_type, other_value_type> && ...)
173 constexpr
small_vector(other_value_type... args) noexcept(is_noexcept) :
175 sz{
sizeof...(other_value_type)}
177 static_assert(
sizeof...(other_value_type) <= capacity_,
"Value list must not exceed the capacity.");
197 template <std::forward_iterator begin_it_type,
typename end_it_type>
198 requires std::sentinel_for<end_it_type, begin_it_type>
199 && std::constructible_from<value_type, std::iter_reference_t<begin_it_type>>
220 template <std::ranges::input_range other_range_t>
222 && std::constructible_from<value_type, std::ranges::range_reference_t<other_range_t>>
223 explicit constexpr
small_vector(other_range_t && range) noexcept(is_noexcept) :
261 assign(std::ranges::begin(ilist), std::ranges::end(ilist));
280 assign(std::ranges::begin(ilist), std::ranges::end(ilist));
301 assign(std::ranges::begin(tmp), std::ranges::end(tmp));
319 template <std::ranges::input_range other_range_t>
320 requires std::constructible_from<value_type, std::ranges::range_reference_t<other_range_t>>
321 constexpr
void assign(other_range_t && range) noexcept(is_noexcept)
323 assign(std::ranges::begin(range), std::ranges::end(range));
343 template <std::forward_iterator begin_it_type,
typename end_it_type>
344 requires std::sentinel_for<end_it_type, begin_it_type>
345 && std::constructible_from<value_type, std::iter_reference_t<begin_it_type>>
346 constexpr
void assign(begin_it_type begin_it, end_it_type end_it) noexcept(is_noexcept)
383 return data_.
data() + sz;
389 return data_.
data() + sz;
395 return data_.
data() + sz;
423 throw std::out_of_range{
"Trying to access element behind the last in small_vector."};
433 throw std::out_of_range{
"Trying to access element behind the last in small_vector."};
514 return (*
this)[
size() - 1];
521 return (*
this)[
size() - 1];
556 constexpr
bool empty() const noexcept
652 constexpr
void clear() noexcept
676 return insert(pos, 1, value);
700 return insert(pos, std::ranges::begin(tmp), std::ranges::end(tmp));
725 template <std::forward_iterator begin_it_type,
typename end_it_type>
726 requires std::sentinel_for<end_it_type, begin_it_type>
727 && std::constructible_from<value_type, std::iter_reference_t<begin_it_type>>
730 auto const pos_as_num = std::ranges::distance(
cbegin(), pos);
731 auto const length = std::ranges::distance(begin_it, end_it);
733 assert(pos_as_num + length <=
capacity());
738 for (
size_type i = sz + length - 1; i > pos_as_num + length - 1; --i)
739 data_[i] = data_[i - length];
741 #if SEQAN3_WORKAROUND_GCC_BOGUS_MEMCPY
742 # pragma GCC diagnostic push
743 # pragma GCC diagnostic ignored "-Wstringop-overflow"
745 std::ranges::copy(begin_it, end_it, &data_[pos_as_num]);
746 #if SEQAN3_WORKAROUND_GCC_BOGUS_MEMCPY
747 # pragma GCC diagnostic pop
750 return begin() + pos_as_num;
772 return insert(pos, ilist.begin(), ilist.end());
797 if (begin_it >= end_it)
798 return begin() + std::ranges::distance(
cbegin(), end_it);
800 size_type const length = std::ranges::distance(begin_it, end_it);
801 auto out_it =
begin() + std::ranges::distance(
cbegin(), begin_it);
803 while (end_it !=
cend())
804 *(out_it++) = *(end_it++);
807 return begin() + std::ranges::distance(
cbegin(), begin_it);
832 return erase(pos, pos + 1);
852 assert(sz < capacity_);
896 assert(
count <= capacity_);
906 assert(
count <= capacity_);
907 for (
size_t i = sz; i <
count; ++i)
932 rhs.data_ = tmp.data_;
976 template <
size_t cap2>
979 return std::ranges::equal(lhs, rhs);
986 template <
size_t cap2>
989 return !(lhs == rhs);
996 template <
size_t cap2>
999 for (
size_t i = 0; i <
std::min(lhs.size(), rhs.size()); ++i)
1000 if (lhs[i] > rhs[i])
1002 else if (lhs[i] < rhs[i])
1004 return lhs.size() < rhs.size();
1011 template <
size_t cap2>
1014 for (
size_t i = 0; i <
std::min(lhs.size(), rhs.size()); ++i)
1015 if (lhs[i] < rhs[i])
1017 else if (lhs[i] > rhs[i])
1019 return lhs.size() > rhs.size();
1026 template <
size_t cap2>
1029 return !(lhs > rhs);
1036 template <
size_t cap2>
1039 return !(lhs < rhs);
1058 template <cereal_archive archive_t>
1059 void CEREAL_SERIALIZE_FUNCTION_NAME(archive_t & archive)
1075 template <
size_t capacity2,
typename value_type>
Adaptions of concepts from the Cereal library.
A constexpr vector implementation with dynamic size at compile time.
Definition: small_vector.hpp:47
constexpr void swap(small_vector &rhs) noexcept(is_noexcept)
Swap contents with another instance.
Definition: small_vector.hpp:924
constexpr reference operator[](size_type const i) noexcept
Return the i-th element.
Definition: small_vector.hpp:454
constexpr bool empty() const noexcept
Checks whether the container is empty.
Definition: small_vector.hpp:555
constexpr friend bool operator>(small_vector const &lhs, small_vector< value_type, cap2 > const &rhs) noexcept
Performs element-wise comparison.
Definition: small_vector.hpp:1011
constexpr reference front() noexcept
Return the first element. Calling front on an empty container is undefined.
Definition: small_vector.hpp:482
constexpr friend bool operator==(small_vector const &lhs, small_vector< value_type, cap2 > const &rhs) noexcept
Performs element-wise comparison.
Definition: small_vector.hpp:976
constexpr friend bool operator<(small_vector const &lhs, small_vector< value_type, cap2 > const &rhs) noexcept
Performs element-wise comparison.
Definition: small_vector.hpp:996
constexpr small_vector & operator=(small_vector const &) noexcept=default
Defaulted.
constexpr void shrink_to_fit() const noexcept
Since the capacity is fixed on compile time, this is a no-op.
Definition: small_vector.hpp:630
constexpr const_iterator cbegin() const noexcept
Returns the begin to the string.
Definition: small_vector.hpp:371
constexpr void resize(size_type const count) noexcept
Resizes the container to contain count elements.
Definition: small_vector.hpp:893
constexpr size_type capacity() const noexcept
Returns the number of elements that the container is able to hold and resolves to capacity_.
Definition: small_vector.hpp:612
constexpr small_vector() noexcept=default
Defaulted.
constexpr void push_back(value_type const value) noexcept
Appends the given element value to the end of the container.
Definition: small_vector.hpp:849
constexpr void pop_back() noexcept
Removes the last element of the container.
Definition: small_vector.hpp:872
constexpr void assign(std::initializer_list< value_type > ilist) noexcept(is_noexcept)
Assign from std::initializer_list.
Definition: small_vector.hpp:277
value_type * iterator
The iterator type.
Definition: small_vector.hpp:78
value_type const * const_iterator
The const_iterator type.
Definition: small_vector.hpp:84
constexpr iterator erase(const_iterator begin_it, const_iterator end_it) noexcept
Removes specified elements from the container.
Definition: small_vector.hpp:794
constexpr iterator insert(const_iterator pos, value_type const value) noexcept(is_noexcept)
Inserts value before position in the container.
Definition: small_vector.hpp:673
requires(std::same_as< value_type, other_value_type > &&...) const expr small_vector(other_value_type... args) noexcept(is_noexcept)
Construct from a list of values of value_type.
Definition: small_vector.hpp:172
value_type_ value_type
The value_type type.
Definition: small_vector.hpp:60
detail::min_viable_uint_t< capacity_ > size_type
The size_type type.
Definition: small_vector.hpp:96
value_type const & const_reference
The const_reference type.
Definition: small_vector.hpp:72
requires std::sentinel_for< end_it_type, begin_it_type > &&constexpr std::constructible_from< value_type, std::iter_reference_t< begin_it_type > > small_vector(begin_it_type begin_it, end_it_type end_it) noexcept(is_noexcept)
Construct from two iterators.
Definition: small_vector.hpp:200
constexpr size_type size() const noexcept
Returns the number of elements in the container, i.e. std::distance(begin(), end()).
Definition: small_vector.hpp:573
constexpr const_iterator cend() const noexcept
Returns iterator past the end of the vector.
Definition: small_vector.hpp:392
requires(!std::is_same_v< std::remove_cvref_t< other_range_t >, small_vector >) &&std
Construct from a different range.
Definition: small_vector.hpp:221
constexpr reference back() noexcept
Return the last element.
Definition: small_vector.hpp:510
constexpr friend bool operator<=(small_vector const &lhs, small_vector< value_type, cap2 > const &rhs) noexcept
Performs element-wise comparison.
Definition: small_vector.hpp:1026
constexpr iterator begin() noexcept
Returns the begin to the string.
Definition: small_vector.hpp:359
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.
Definition: small_vector.hpp:152
constexpr void clear() noexcept
Removes all elements from the container.
Definition: small_vector.hpp:651
ptrdiff_t difference_type
The difference_type type.
Definition: small_vector.hpp:90
constexpr iterator end() noexcept
Returns iterator past the end of the vector.
Definition: small_vector.hpp:380
value_type & reference
The reference type.
Definition: small_vector.hpp:66
constexpr friend bool operator!=(small_vector const &lhs, small_vector< value_type, cap2 > const &rhs) noexcept
Performs element-wise comparison.
Definition: small_vector.hpp:986
reference at(size_type const i)
Return the i-th element.
Definition: small_vector.hpp:418
constexpr value_type * data() noexcept
Direct access to the underlying array.
Definition: small_vector.hpp:527
constexpr void reserve(size_type) const noexcept
Since the capacity is fixed on compile time, this is a no-op.
Definition: small_vector.hpp:621
constexpr size_type max_size() const noexcept
Returns the maximum number of elements the container is able to hold and resolves to capacity_.
Definition: small_vector.hpp:594
constexpr friend bool operator>=(small_vector const &lhs, small_vector< value_type, cap2 > const &rhs) noexcept
Performs element-wise comparison.
Definition: small_vector.hpp:1036
constexpr ptrdiff_t count
Count the occurrences of a type in a pack.
Definition: type_pack/traits.hpp:164
constexpr auto repeat_n
A view factory that repeats a given value n times.
Definition: repeat_n.hpp:91
Provides metaprogramming utilities for integer types.
The main SeqAn3 namespace.
Definition: aligned_sequence_concept.hpp:29
small_vector(value_type const (&array)[capacity2]) -> small_vector< value_type, capacity2 >
Deducts the size and value type from an built-in array on construction.
SeqAn specific customisations in the standard namespace.
Provides seqan3::views::repeat_n.
Provides type traits for working with templates.