60template <alphabet::writable_semialphabet alphabet_type>
61 requires std::regular<alphabet_type>
66 static constexpr size_t bits_per_letter =
std::bit_width(alphabet::size<alphabet_type>);
67 static_assert(bits_per_letter <= 64,
"alphabet must be representable in at most 64bit.");
70 using word_type = uint64_t;
72 static constexpr size_t word_size =
sizeof(word_type) * CHAR_BIT;
74 static constexpr size_t letters_per_word = word_size / bits_per_letter;
76 static constexpr uint64_t mask = (1ull << bits_per_letter) - 1ull;
88 static uint64_t get_rank(
data_type const & vec,
size_t const i)
noexcept
90 assert(i / letters_per_word < vec.
size());
91 uint64_t
const word = vec[i / letters_per_word];
92 size_t const offset = (i % letters_per_word) * bits_per_letter;
93 return (word >> offset) & mask;
97 static void set_rank(
data_type & vec,
size_t const i, uint64_t
const rank)
noexcept
99 assert(i / letters_per_word < vec.
size());
100 uint64_t & word = vec[i / letters_per_word];
101 size_t const offset = (i % letters_per_word) * bits_per_letter;
102 word &= ~(mask << offset);
103 word |= rank << offset;
107 void clear_unused_bits_in_last_word()
109 if (data.empty() || (
size() % letters_per_word == 0))
112 size_t const offset = (
size() % letters_per_word) * bits_per_letter;
113 data.back() &= ((1ull << offset) - 1ull);
134 reference_proxy_type() =
delete;
135 constexpr reference_proxy_type(reference_proxy_type
const &)
noexcept =
default;
136 constexpr reference_proxy_type(reference_proxy_type &&)
noexcept =
default;
137 ~reference_proxy_type()
noexcept =
default;
140 using base_t::operator=;
144 constexpr reference_proxy_type & operator=(reference_proxy_type
const & rhs)
146 return assign_rank(rhs.to_rank());
151 constexpr reference_proxy_type
const & operator=(reference_proxy_type
const & rhs)
const
153 return assign_rank(rhs.to_rank());
157 reference_proxy_type(
data_type *
const data_ptr_,
size_t const index_) noexcept :
158 data_ptr{data_ptr_}, index{index_}
168 set_rank(*data_ptr, index, r);
175 set_rank(*data_ptr, index, r);
183 template <
typename t>
184 requires std::is_same_v<std::ranges::range_value_t<std::remove_cvref_t<t>>, alphabet_type>
185 static constexpr bool has_same_value_type_v =
true;
199 using iterator = detail::random_access_iterator<bitcompressed_vector>;
201 using const_iterator = detail::random_access_iterator<bitcompressed_vector const>;
210 using allocator_type = void;
237 requires(
std::ranges::input_range<other_range_t> && has_same_value_type_v<other_range_t>)
271 template <std::forward_iterator begin_iterator_type,
typename end_iterator_type>
273 requires(std::sentinel_for<end_iterator_type, begin_iterator_type> &&
274 std::common_reference_with<std::iter_value_t<begin_iterator_type>,
value_type>)
324 template <std::ranges::input_range other_range_t>
326 requires std::common_reference_with<std::ranges::range_value_t<other_range_t>,
value_type>
365 template <std::forward_iterator begin_iterator_type,
typename end_iterator_type>
366 void assign(begin_iterator_type begin_it, end_iterator_type end_it)
367 requires(std::sentinel_for<end_iterator_type, begin_iterator_type> &&
368 std::common_reference_with<std::iter_value_t<begin_iterator_type>,
value_type>)
455 throw std::out_of_range{
"Trying to access element behind the last in bitcompressed_vector."};
465 throw std::out_of_range{
"Trying to access element behind the last in bitcompressed_vector."};
540 return (*
this)[
size() - 1];
547 return (*
this)[
size() - 1];
609 return std::max<size_type>(data.max_size(), data.max_size() * letters_per_word);
649 data.reserve(new_cap / letters_per_word + (new_cap % letters_per_word != 0));
727 return insert(pos, v.begin(), v.end());
751 template <std::forward_iterator begin_iterator_type,
typename end_iterator_type>
753 requires(std::sentinel_for<end_iterator_type, begin_iterator_type> &&
754 std::common_reference_with<std::iter_value_t<begin_iterator_type>,
value_type>)
759 size_t const size_of_insert =
std::distance(begin_it, end_it);
768 return begin() + pos_as_num;
813 if (begin_it == end_it)
818 size_t const size_of_removal =
std::distance(begin_it, end_it);
826 return begin() + begin_pos_of_removal + size_of_removal;
863 if (data.size() * letters_per_word == size_)
889 if ((size_--) % letters_per_word == 1)
892 clear_unused_bits_in_last_word();
924 data.resize(count / letters_per_word + (count % letters_per_word != 0));
926 clear_unused_bits_in_last_word();
936 size_t old_size = size_;
938 for (
size_t i = old_size; i <
size(); ++i)
989 template <
typename archive_t>
990 void serialize(archive_t & archive)
A CRTP-base that eases the definition of proxy types returned in place of regular alphabets.
Definition: proxy_base.hpp:63
A space-optimised version of std::vector that compresses multiple letters into a single byte.
Definition: bitcompressed_vector.hpp:63
size_type capacity() const noexcept
Returns the number of elements that the container has currently allocated space for.
Definition: bitcompressed_vector.hpp:627
friend constexpr void swap(bitcompressed_vector &&lhs, bitcompressed_vector &&rhs) noexcept
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: bitcompressed_vector.hpp:973
void resize(size_type const count, value_type const value)
Resizes the container to contain count elements.
Definition: bitcompressed_vector.hpp:933
reference at(size_type const i)
Return the i-th element.
Definition: bitcompressed_vector.hpp:451
const_reference back() const noexcept
Return the last element.
Definition: bitcompressed_vector.hpp:544
friend auto operator<=>(bitcompressed_vector const &lhs, bitcompressed_vector const &rhs) noexcept=default
Comparison operators.
void assign(std::initializer_list< value_type > ilist)
Assign from std::initializer_list.
Definition: bitcompressed_vector.hpp:385
bitcompressed_vector()=default
Defaulted.
constexpr void swap(bitcompressed_vector &&rhs) noexcept
Swap contents with another instance.
Definition: bitcompressed_vector.hpp:956
void assign(other_range_t &&range)
Assign from a different range.
Definition: bitcompressed_vector.hpp:325
void clear() noexcept
Removes all elements from the container.
Definition: bitcompressed_vector.hpp:683
const_reference front() const noexcept
Return the first element. Calling front on an empty container is undefined.
Definition: bitcompressed_vector.hpp:518
iterator insert(const_iterator pos, begin_iterator_type begin_it, end_iterator_type end_it)
Inserts elements from range [begin_it, end_it) before position in the container.
Definition: bitcompressed_vector.hpp:752
iterator insert(const_iterator pos, value_type const value)
Inserts value before position in the container.
Definition: bitcompressed_vector.hpp:706
alphabet_type const_reference
Equals the alphabet_type / value_type.
Definition: bitcompressed_vector.hpp:197
reference front() noexcept
Return the first element. Calling front on an empty container is undefined.
Definition: bitcompressed_vector.hpp:511
void reserve(size_type const new_cap)
Increase the capacity to a value that's greater or equal to new_cap.
Definition: bitcompressed_vector.hpp:647
const_iterator cend() const noexcept
Returns an iterator to the element following the last element of the container.
Definition: bitcompressed_vector.hpp:432
reference_proxy_type reference
A proxy type that enables assignment, if the underlying data structure also provides a proxy.
Definition: bitcompressed_vector.hpp:195
const_iterator cbegin() const noexcept
Returns an iterator to the first element of the container.
Definition: bitcompressed_vector.hpp:411
alphabet_type value_type
Equals the alphabet_type.
Definition: bitcompressed_vector.hpp:193
constexpr bitcompressed_vector(bitcompressed_vector &&) noexcept=default
Defaulted.
iterator erase(const_iterator begin_it, const_iterator end_it)
Removes specified elements from the container.
Definition: bitcompressed_vector.hpp:811
std::ranges::range_size_t< data_type > size_type
An unsigned integer type (usually std::size_t)
Definition: bitcompressed_vector.hpp:205
constexpr void swap(bitcompressed_vector &rhs) noexcept
Swap contents with another instance.
Definition: bitcompressed_vector.hpp:953
void push_back(value_type const value)
Appends the given element value to the end of the container.
Definition: bitcompressed_vector.hpp:861
constexpr data_type & raw_data() noexcept
Provides direct, unsafe access to underlying data structures.
Definition: bitcompressed_vector.hpp:557
constexpr bitcompressed_vector(bitcompressed_vector const &)=default
Defaulted.
const_iterator begin() const noexcept
Returns an iterator to the first element of the container.
Definition: bitcompressed_vector.hpp:408
iterator begin() noexcept
Returns an iterator to the first element of the container.
Definition: bitcompressed_vector.hpp:405
friend constexpr void swap(bitcompressed_vector &lhs, bitcompressed_vector &rhs) noexcept
Swap contents with another instance.
Definition: bitcompressed_vector.hpp:970
bitcompressed_vector & operator=(std::initializer_list< value_type > ilist)
Assign from std::initializer_list.
Definition: bitcompressed_vector.hpp:305
constexpr data_type const & raw_data() const noexcept
Provides direct, unsafe access to underlying data structures.
Definition: bitcompressed_vector.hpp:560
reference back() noexcept
Return the last element.
Definition: bitcompressed_vector.hpp:537
void assign(size_type const count, value_type const value)
Assign with count times value.
Definition: bitcompressed_vector.hpp:344
iterator end() noexcept
Returns an iterator to the element following the last element of the container.
Definition: bitcompressed_vector.hpp:426
const_reference operator[](size_type const i) const noexcept
Return the i-th element.
Definition: bitcompressed_vector.hpp:492
bitcompressed_vector(std::initializer_list< value_type > ilist)
Construct from std::initializer_list.
Definition: bitcompressed_vector.hpp:290
bitcompressed_vector(begin_iterator_type begin_it, end_iterator_type end_it)
Construct from pair of iterators.
Definition: bitcompressed_vector.hpp:272
iterator insert(const_iterator pos, std::initializer_list< value_type > const &ilist)
Inserts elements from initializer list before position in the container.
Definition: bitcompressed_vector.hpp:786
void assign(begin_iterator_type begin_it, end_iterator_type end_it)
Assign from pair of iterators.
Definition: bitcompressed_vector.hpp:366
size_type max_size() const noexcept
Returns the maximum number of elements the container is able to hold due to system or library impleme...
Definition: bitcompressed_vector.hpp:606
iterator erase(const_iterator pos)
Removes specified elements from the container.
Definition: bitcompressed_vector.hpp:844
std::ranges::range_difference_t< data_type > difference_type
A signed integer type (usually std::ptrdiff_t)
Definition: bitcompressed_vector.hpp:203
void pop_back()
Removes the last element of the container.
Definition: bitcompressed_vector.hpp:886
detail::random_access_iterator< bitcompressed_vector const > const_iterator
The const_iterator type of this container (a random access iterator).
Definition: bitcompressed_vector.hpp:201
iterator insert(const_iterator pos, size_type const count, value_type const value)
Inserts count copies of value before position in the container.
Definition: bitcompressed_vector.hpp:724
const_reference at(size_type const i) const
Return the i-th element.
Definition: bitcompressed_vector.hpp:461
const_iterator end() const noexcept
Returns an iterator to the element following the last element of the container.
Definition: bitcompressed_vector.hpp:429
size_type size() const noexcept
Returns the number of elements in the container, i.e. std::distance(begin(), end()).
Definition: bitcompressed_vector.hpp:590
detail::random_access_iterator< bitcompressed_vector > iterator
The iterator type of this container (a random access iterator).
Definition: bitcompressed_vector.hpp:199
void resize(size_type const count)
Resizes the container to contain count elements.
Definition: bitcompressed_vector.hpp:921
bool empty() const noexcept
Checks whether the container is empty.
Definition: bitcompressed_vector.hpp:577
bitcompressed_vector(size_type const count, value_type const value)
Construct with count times value.
Definition: bitcompressed_vector.hpp:254
void shrink_to_fit()
Requests the removal of unused capacity.
Definition: bitcompressed_vector.hpp:667
reference operator[](size_type const i) noexcept
Return the i-th element.
Definition: bitcompressed_vector.hpp:485
Refines bio::alphabet::alphabet and adds assignability.
Definition: concept.hpp:688
constexpr auto to_rank
Return the rank representation of a (semi-)alphabet object.
Definition: concept.hpp:70
decltype(bio::alphabet::to_rank(std::declval< semi_alphabet_type >())) rank_t
The rank_type of the semi-alphabet; defined as the return type of bio::alphabet::to_rank.
Definition: concept.hpp:90
constexpr auto assign_rank_to
Assign a rank to an alphabet object.
Definition: concept.hpp:138
constexpr auto repeat_n
A view factory that repeats a given value n times.
Definition: repeat_n.hpp:98
The ranges module's namespace.
Provides bio::alphabet::proxy_base.
Provides the bio::ranges::detail::random_access_iterator class.
Provides bio::views::convert.
Provides bio::views::repeat_n.
Provides bio::views::to_char.
Provides bio::views::to_rank.