 |
SeqAn3
3.0.2
The Modern C++ library for sequence analysis.
|
|
Go to the documentation of this file.
48 template <
size_t bit_capacity = 58>
66 bitfield data{0u, 0u};
69 class reference_proxy_type
75 constexpr reference_proxy_type() noexcept = default;
76 constexpr reference_proxy_type(reference_proxy_type const &) noexcept = default;
77 constexpr reference_proxy_type(reference_proxy_type &&) noexcept = default;
80 constexpr reference_proxy_type & operator=(reference_proxy_type const rhs) noexcept
87 constexpr reference_proxy_type &
operator=(
bool const value) noexcept
93 ~reference_proxy_type() noexcept = default;
97 constexpr reference_proxy_type(bitfield & internal_,
size_t const pos) noexcept :
98 internal{internal_}, mask{1ULL<<pos}
102 constexpr
operator bool() const noexcept
104 return static_cast<bool>(
internal.bits & mask);
108 constexpr
bool operator~() const noexcept
110 return !
static_cast<bool>(
internal.bits & mask);
114 constexpr reference_proxy_type &
operator|=(
bool const value)
123 constexpr reference_proxy_type &
operator&=(
bool const value)
132 constexpr reference_proxy_type &
operator^=(
bool const value)
134 operator bool() && value ?
reset() :
set();
145 constexpr
void set() noexcept
147 internal.bits |= mask;
151 constexpr
void reset() noexcept
153 internal.bits &= ~mask;
158 static_assert(bit_capacity <= 58,
"The capacity of the dynamic_bitset exceeds the limit of 58.");
170 using iterator = detail::random_access_iterator<dynamic_bitset>;
181 using allocator_type = void;
214 if (detail::popcount(value >> 58) != 0)
217 data.size |= value ? detail::most_significant_bit_set(value) + 1 : 0u;
237 template <std::forward_iterator begin_it_type,
typename end_it_type>
239 requires std::sentinel_for<end_it_type, begin_it_type> &&
240 std::constructible_from<value_type, std::iter_reference_t<begin_it_type>>
263 template <std::ranges::input_range other_range_t>
268 dynamic_bitset{std::ranges::begin(range), std::ranges::end(range)}
306 assign(std::ranges::begin(ilist), std::ranges::end(ilist));
334 static_assert(N <= bit_capacity + 1,
"Length of string literal exceeds capacity of dynamic_bitset.");
358 static_assert(N <= bit_capacity + 1,
"Length of string literal exceeds capacity of dynamic_bitset.");
381 constexpr
void assign(
char const (&lit)[N])
383 static_assert(N <= bit_capacity + 1,
"Length of string literal exceeds capacity of dynamic_bitset.");
384 assert(lit[N - 1] ==
'\0');
387 for (
size_t i = 0; i != N - 1; ++i)
393 else if (lit[i] ==
'1')
400 throw std::invalid_argument{
"The string to construct a dynamic_bitset from may only contain 0 and 1."};
423 assign(std::ranges::begin(ilist), std::ranges::end(ilist));
444 assign(std::ranges::begin(tmp), std::ranges::end(tmp));
462 template <std::ranges::input_range other_range_t>
464 requires std::constructible_from<value_type, std::ranges::range_reference_t<other_range_t>>
466 constexpr
void assign(other_range_t && range) noexcept
468 assign(std::ranges::begin(range), std::ranges::end(range));
488 template <std::forward_iterator begin_it_type,
typename end_it_type>
490 requires std::sentinel_for<end_it_type, begin_it_type> &&
491 std::constructible_from<value_type, std::iter_reference_t<begin_it_type>>
493 constexpr
void assign(begin_it_type begin_it, end_it_type end_it) noexcept
580 assert(
size() == rhs.size());
581 data.bits &= rhs.data.bits;
608 assert(
size() == rhs.size());
609 data.bits |= rhs.data.bits;
636 assert(
size() == rhs.size());
637 data.bits ^= rhs.data.bits;
692 data.bits &= (1ULL <<
size()) - 1ULL;
795 data.bits |= (1ULL <<
size()) - 1ULL;
895 data.bits = ~data.bits;
896 data.bits &= (1ULL <<
size()) - 1ULL;
932 constexpr
bool all() const noexcept
940 constexpr
bool any() const noexcept
948 constexpr
bool none() const noexcept
956 return detail::popcount(data.bits);
1028 return data.bits & 1ULL << i;
1078 return (*
this)[
size() - 1];
1085 return (*
this)[
size() - 1];
1178 return bit_capacity;
1237 return insert(pos, 1, value);
1261 return insert(pos, std::ranges::begin(tmp), std::ranges::end(tmp));
1286 template <std::forward_iterator begin_it_type,
typename end_it_type>
1288 requires std::sentinel_for<end_it_type, begin_it_type> &&
1289 std::constructible_from<value_type, std::iter_reference_t<begin_it_type>>
1293 auto const pos_as_num = std::ranges::distance(
cbegin(), pos);
1294 auto const length = std::ranges::distance(begin_it, end_it);
1300 resize(tmp_size + length);
1302 for (
size_type i = tmp_size + length - 1; i > pos_as_num + length - 1; --i)
1303 (*
this)[i] = (*this)[i - length];
1306 for (
auto i = pos_as_num; begin_it != end_it; ++i, ++begin_it)
1307 (*
this)[i] = *begin_it;
1309 return begin() + pos_as_num;
1331 return insert(pos, ilist.begin(), ilist.end());
1357 if (begin_it >= end_it)
1358 return begin() + std::ranges::distance(
cbegin(), end_it);
1360 auto const length = std::ranges::distance(begin_it, end_it);
1361 auto out_it =
begin() + std::ranges::distance(
cbegin(), begin_it);
1363 while (end_it !=
cend())
1364 *(out_it++) = *(end_it++);
1367 return begin() + std::ranges::distance(
cbegin(), begin_it);
1392 return erase(pos, pos + 1);
1412 assert(
size() < bit_capacity);
1414 (*this)[
size() - 1] = value;
1465 assert(
count <= bit_capacity);
1471 data.bits &= (1ULL <<
size()) - 1ULL;
1532 template <
size_t cap>
1534 requires (cap <= bit_capacity)
1538 assert(lhs.size() == rhs.size());
1552 template <
size_t cap>
1554 requires (cap <= bit_capacity)
1558 assert(lhs.size() == rhs.size());
1572 template <
size_t cap>
1574 requires (cap <= bit_capacity)
1578 assert(lhs.size() == rhs.size());
1588 template <
size_t cap>
1592 return lhs.data.size == rhs.raw_data()->size && lhs.data.bits == rhs.raw_data()->bits;
1596 template <
size_t cap>
1599 return !(lhs == rhs);
1603 template <
size_t cap>
1606 return lhs.data.bits < rhs.raw_data()->bits;
1610 template <
size_t cap>
1613 return lhs.data.bits > rhs.raw_data()->bits;
1617 template <
size_t cap>
1620 return !(lhs > rhs);
1624 template <
size_t cap>
1627 return !(lhs < rhs);
1654 template <
typename char_t =
char>
1659 for (
bool const bit : std::views::reverse(*
this))
1660 bit ? str.push_back(one) : str.push_back(zero);
1684 throw std::overflow_error{
"seqan3::dynamic_bitset cannot be represented as unsigned long."};
1687 return static_cast<unsigned long>(data.bits);
1709 throw std::overflow_error{
"seqan3::dynamic_bitset cannot be represented as unsigned long long."};
1712 return static_cast<unsigned long long>(data.bits);
1755 assert(num_char > 0);
1761 c == is.
widen(
'0') ? tmp.push_back(
false) : tmp.push_back(
true);
1764 arg.
assign(std::views::reverse(tmp));
1766 if (arg.
size() == 0)
1767 is.
setstate(std::ios_base::failbit);
1783 template <
typename char_t>
1801 template <cereal_archive archive_t>
1802 void CEREAL_SERIALIZE_FUNCTION_NAME(archive_t & archive)
1804 uint64_t
size = data.size;
1807 uint64_t bits = data.bits;
1823 template <
size_t cap>
1833 return static_cast<size_t>(arg.to_ullong());
constexpr void clear() noexcept
Removes all elements from the container.
Definition: dynamic_bitset.hpp:1212
constexpr void assign(char const (&lit)[N])
Assign from literal.
Definition: dynamic_bitset.hpp:381
constexpr bool any() const noexcept
Checks if any bit is set.
Definition: dynamic_bitset.hpp:940
constexpr unsigned long long to_ullong() const
Converts the dynamic_bitset to an unsigned long long integer.
Definition: dynamic_bitset.hpp:1704
constexpr dynamic_bitset & operator>>=(size_t const count) noexcept
Performs binary shift right on the current object.
Definition: dynamic_bitset.hpp:714
constexpr unsigned long to_ulong() const
Converts the dynamic_bitset to an unsigned long integer.
Definition: dynamic_bitset.hpp:1679
Provides utility functions for bit twiddling.
constexpr void assign(other_range_t &&range) noexcept
Assign from a different range.
Definition: dynamic_bitset.hpp:466
friend std::ostream & operator<<(std::ostream &os, dynamic_bitset const &arg)
Formatted output for the seqan3::dynamic_bitset.
Definition: dynamic_bitset.hpp:1728
constexpr friend bool operator!=(dynamic_bitset const &lhs, dynamic_bitset< cap > const &rhs) noexcept
Performs element-wise comparison.
Definition: dynamic_bitset.hpp:1597
ptrdiff_t difference_type
A std::ptrdiff_t.
Definition: dynamic_bitset.hpp:174
constexpr auto interleave
A view that interleaves a given range into another range at regular intervals.
Definition: interleave.hpp:384
constexpr void shrink_to_fit() const noexcept
Since the capacity is fixed on compile time, this is a no-op.
Definition: dynamic_bitset.hpp:1188
constexpr size_type size() const noexcept
Returns the number of elements in the container, i.e. std::distance(begin(), end()).
Definition: dynamic_bitset.hpp:1135
A constexpr bitset implementation with dynamic size at compile time.
Definition: dynamic_bitset.hpp:50
constexpr iterator insert(const_iterator pos, size_type const count, value_type const value) noexcept
Inserts count copies of value before position in the container.
Definition: dynamic_bitset.hpp:1258
constexpr size_type max_size() const noexcept
Returns the maximum number of elements the container is able to hold and resolves to bit_capacity.
Definition: dynamic_bitset.hpp:1158
constexpr dynamic_bitset operator>>(size_t const count) const noexcept
Performs binary shift right.
Definition: dynamic_bitset.hpp:740
constexpr const_iterator cbegin() const noexcept
Returns the begin to the dynamic_bitset.
Definition: dynamic_bitset.hpp:523
constexpr friend bool operator>=(dynamic_bitset const &lhs, dynamic_bitset< cap > const &rhs) noexcept
Performs element-wise comparison.
Definition: dynamic_bitset.hpp:1625
constexpr const_reference at(size_t const i) const
Returns the i-th element.
Definition: dynamic_bitset.hpp:983
constexpr dynamic_bitset & operator=(std::initializer_list< value_type > const ilist) noexcept
Assign from std::initializer_list.
Definition: dynamic_bitset.hpp:304
constexpr dynamic_bitset & operator<<=(size_t const count) noexcept
Performs binary shift left on the current object.
Definition: dynamic_bitset.hpp:687
constexpr void pop_back() noexcept
Removes the last element of the container.
Definition: dynamic_bitset.hpp:1433
constexpr const_iterator cend() const noexcept
Returns iterator past the end of the dynamic_bitset.
Definition: dynamic_bitset.hpp:548
constexpr void reserve(size_t) const noexcept
Since the capacity is fixed on compile time, this is a no-op.
Definition: dynamic_bitset.hpp:1182
constexpr const_reference operator[](size_t const i) const noexcept
Returns the i-th element.
Definition: dynamic_bitset.hpp:1025
constexpr const_reference front() const noexcept
Returns the first element.
Definition: dynamic_bitset.hpp:1054
constexpr bool empty() const noexcept
Checks whether the container is empty.
Definition: dynamic_bitset.hpp:1117
constexpr iterator insert(const_iterator pos, begin_it_type begin_it, end_it_type end_it) noexcept
Inserts elements from range [begin_it, end_it) before pos in the container.
Definition: dynamic_bitset.hpp:1291
constexpr friend bool operator==(dynamic_bitset const &lhs, dynamic_bitset< cap > const &rhs) noexcept
Performs element-wise comparison.
Definition: dynamic_bitset.hpp:1590
Provides seqan3::debug_stream and related types.
friend std::istream & operator>>(std::istream &is, dynamic_bitset &arg)
Formatted input for the seqan3::dynamic_bitset.
Definition: dynamic_bitset.hpp:1745
constexpr size_type capacity() const noexcept
Returns the number of elements that the container is able to hold and resolves to bit_capacity.
Definition: dynamic_bitset.hpp:1176
constexpr dynamic_bitset & operator|=(dynamic_bitset const &rhs) noexcept
Sets the bits to the result of binary OR on corresponding pairs of bits of *this and rhs.
Definition: dynamic_bitset.hpp:606
constexpr void swap(dynamic_bitset &rhs) noexcept
Swap contents with another instance.
Definition: dynamic_bitset.hpp:1487
bool const_reference
Equals the value_type.
Definition: dynamic_bitset.hpp:168
constexpr const_reference test(size_t const i) const
Returns the i-th element.
Definition: dynamic_bitset.hpp:992
constexpr const_iterator end() const noexcept
Returns iterator past the end of the dynamic_bitset.
Definition: dynamic_bitset.hpp:542
constexpr dynamic_bitset & reset(size_t const i)
Sets the i'th bit to false.
Definition: dynamic_bitset.hpp:870
constexpr iterator insert(const_iterator pos, std::initializer_list< value_type > const &ilist) noexcept
Inserts elements from initializer list before pos in the container.
Definition: dynamic_bitset.hpp:1329
constexpr dynamic_bitset & operator=(char const (&lit)[N])
Assign from literal.
Definition: dynamic_bitset.hpp:356
constexpr bitfield const * raw_data() const noexcept
Direct access to the underlying bit field.
Definition: dynamic_bitset.hpp:1095
constexpr dynamic_bitset(size_type const n, value_type const value) noexcept
Construct with n times value.
Definition: dynamic_bitset.hpp:285
constexpr friend bool operator<=(dynamic_bitset const &lhs, dynamic_bitset< cap > const &rhs) noexcept
Performs element-wise comparison.
Definition: dynamic_bitset.hpp:1618
A "pretty printer" for most SeqAn data structures and related types.
Definition: debug_stream_type.hpp:71
constexpr reference operator[](size_t const i) noexcept
Returns the i-th element.
Definition: dynamic_bitset.hpp:1018
constexpr void resize(size_type const count, value_type const value=false) noexcept
Resizes the container to contain count elements.
Definition: dynamic_bitset.hpp:1463
constexpr friend bool operator>(dynamic_bitset const &lhs, dynamic_bitset< cap > const &rhs) noexcept
Performs element-wise comparison.
Definition: dynamic_bitset.hpp:1611
detail::random_access_iterator< dynamic_bitset const > const_iterator
The const_iterator type of this container (a random access iterator).
Definition: dynamic_bitset.hpp:172
Provides seqan3::views::repeat_n.
constexpr friend bool operator<(dynamic_bitset const &lhs, dynamic_bitset< cap > const &rhs) noexcept
Performs element-wise comparison.
Definition: dynamic_bitset.hpp:1604
Provides seqan3::views::to.
auto const move
A view that turns lvalue-references into rvalue-references.
Definition: move.hpp:68
constexpr dynamic_bitset(char const (&lit)[N])
Construction from literal.
Definition: dynamic_bitset.hpp:332
friend debug_stream_type< char_t > & operator<<(debug_stream_type< char_t > &s, dynamic_bitset arg)
Formatted debug output for the seqan3::dynamic_bitset.
Definition: dynamic_bitset.hpp:1784
constexpr dynamic_bitset & reset() noexcept
Sets all bits to 0.
Definition: dynamic_bitset.hpp:845
friend class dynamic_bitset
Befriend other template instantiations of dynamic_bitset.
Definition: dynamic_bitset.hpp:54
constexpr iterator erase(const_iterator begin_it, const_iterator end_it) noexcept
Removes specified elements from the container.
Definition: dynamic_bitset.hpp:1355
reference_proxy_type reference
A proxy type that enables assignment.
Definition: dynamic_bitset.hpp:166
constexpr dynamic_bitset & operator^=(dynamic_bitset const &rhs) noexcept
Sets the bits to the result of binary XOR on corresponding pairs of bits of *this and rhs.
Definition: dynamic_bitset.hpp:634
constexpr void assign(begin_it_type begin_it, end_it_type end_it) noexcept
Assign from pair of iterators.
Definition: dynamic_bitset.hpp:493
The main SeqAn3 namespace.
Definition: aligned_sequence_concept.hpp:29
constexpr friend void swap(dynamic_bitset &lhs, dynamic_bitset &rhs) noexcept
Swap contents with another instance.
Definition: dynamic_bitset.hpp:1516
constexpr reference at(size_t const i)
Returns the i-th element.
Definition: dynamic_bitset.hpp:974
constexpr void assign(size_type const count, value_type const value) noexcept
Assign with count times value.
Definition: dynamic_bitset.hpp:440
std::string to_string(char_t zero=char_t{ '0'}, char_t one=char_t{ '1'}) const
Converts the dynamic_bitset to a std::string.
Definition: dynamic_bitset.hpp:1655
constexpr reference front() noexcept
Returns the first element.
Definition: dynamic_bitset.hpp:1047
constexpr dynamic_bitset(begin_it_type begin_it, end_it_type end_it) noexcept
Construct from two iterators.
Definition: dynamic_bitset.hpp:242
constexpr bool none() const noexcept
Checks if no bit is set.
Definition: dynamic_bitset.hpp:948
constexpr iterator insert(const_iterator pos, value_type const value) noexcept
Inserts value before pos in the container.
Definition: dynamic_bitset.hpp:1235
constexpr void assign(std::initializer_list< value_type > const ilist) noexcept
Assign from std::initializer_list.
Definition: dynamic_bitset.hpp:421
constexpr dynamic_bitset & set() noexcept
Sets all bits to 1.
Definition: dynamic_bitset.hpp:793
constexpr iterator end() noexcept
Returns iterator past the end of the dynamic_bitset.
Definition: dynamic_bitset.hpp:536
constexpr void swap(dynamic_bitset &&rhs) noexcept
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: dynamic_bitset.hpp:1495
constexpr reference back() noexcept
Returns the last element.
Definition: dynamic_bitset.hpp:1075
constexpr void push_back(value_type const value) noexcept
Appends the given element value to the end of the container.
Definition: dynamic_bitset.hpp:1410
size_t operator()(seqan3::dynamic_bitset< cap > const arg) const noexcept
Compute the hash for a seqan3::dynamic_bitset.
Definition: dynamic_bitset.hpp:1831
constexpr const_iterator begin() const noexcept
Returns the begin to the dynamic_bitset.
Definition: dynamic_bitset.hpp:517
SeqAn specific customisations in the standard namespace.
constexpr dynamic_bitset & operator=(dynamic_bitset const &) noexcept=default
Defaulted.
constexpr iterator erase(const_iterator pos) noexcept
Removes specified elements from the container.
Definition: dynamic_bitset.hpp:1390
constexpr dynamic_bitset & flip(size_t const i)
Flips the i'th bit (binary NOT).
Definition: dynamic_bitset.hpp:919
constexpr dynamic_bitset() noexcept=default
Defaulted.
Adaptions of concepts from the Cereal library.
constexpr bitfield * raw_data() noexcept
Direct access to the underlying bit field.
Definition: dynamic_bitset.hpp:1089
constexpr auto repeat_n
A view factory that repeats a given value n times.
Definition: repeat_n.hpp:94
constexpr iterator begin() noexcept
Returns the begin to the dynamic_bitset.
Definition: dynamic_bitset.hpp:511
constexpr size_type count() const noexcept
Returns the number of set bits.
Definition: dynamic_bitset.hpp:954
constexpr dynamic_bitset & set(size_t const i, bool const value=true)
Sets the i'th bit to value.
Definition: dynamic_bitset.hpp:819
constexpr dynamic_bitset operator~() const noexcept
Returns a temporary copy of *this with all bits flipped (binary NOT).
Definition: dynamic_bitset.hpp:662
constexpr dynamic_bitset & flip() noexcept
Flips all bits (binary NOT).
Definition: dynamic_bitset.hpp:893
Provides seqan3::views::interleave.
bool value_type
Equals bool.
Definition: dynamic_bitset.hpp:164
constexpr const_reference back() const noexcept
Returns the last element.
Definition: dynamic_bitset.hpp:1082
constexpr bool all() const noexcept
Checks if all bit are set.
Definition: dynamic_bitset.hpp:932
constexpr dynamic_bitset operator<<(size_t const count) const noexcept
Performs binary shift left.
Definition: dynamic_bitset.hpp:767
constexpr dynamic_bitset & operator&=(dynamic_bitset const &rhs) noexcept
Sets the bits to the result of binary AND on corresponding pairs of bits of *this and rhs.
Definition: dynamic_bitset.hpp:578
detail::random_access_iterator< dynamic_bitset > iterator
The iterator type of this container (a random access iterator).
Definition: dynamic_bitset.hpp:170