3#ifndef TETL_BITSET_BASIC_BITSET_HPP
4#define TETL_BITSET_BASIC_BITSET_HPP
30template <etl::
size_t Bits, etl::
unsigned_
integral WordType = etl::
size_t>
35 constexpr auto operator=(
bool x)
noexcept -> reference&
41 constexpr auto operator=(reference
const& x)
noexcept -> reference&
43 *_word =
etl::set_bit(*_word, _offset,
static_cast<bool>(x));
47 [[nodiscard]]
constexpr operator bool() const noexcept {
return etl::test_bit(*_word, _offset); }
49 [[nodiscard]]
constexpr auto operator~() const noexcept ->
bool {
return not
static_cast<bool>(*this); }
51 constexpr auto flip() noexcept -> reference&
58 constexpr explicit reference(WordType& word, WordType offset) noexcept
85 [[nodiscard]]
constexpr auto size() const noexcept ->
etl::
size_t {
return Bits; }
100 return reference{_words[word_index(pos)], offset_in_word(pos)};
104 [[nodiscard]]
constexpr auto all() const noexcept ->
bool
106 auto const allSet = [](
auto word) {
return word == ones; };
108 if constexpr (has_padding) {
110 auto const tail = _words[num_words - 1] == padding_mask_inv;
111 return head and tail;
113 return etl::all_of(_words.cbegin(), _words.cend(), allSet);
118 [[nodiscard]]
constexpr auto any() const noexcept ->
bool {
return not
none(); }
121 [[nodiscard]]
constexpr auto none() const noexcept ->
bool
123 return etl::all_of(_words.cbegin(), _words.cend(), [](
auto word) { return word == WordType(0); });
127 [[nodiscard]]
constexpr auto count() const noexcept ->
etl::
size_t
130 return static_cast<etl::size_t>(etl::popcount(word));
139 return etl::test_bit(_words[word_index(pos)], offset_in_word(pos));
145 if constexpr (has_padding) {
147 _words[num_words - 1] = padding_mask_inv;
149 etl::fill(_words.begin(), _words.end(), ones);
160 return transform_bit(pos, [value](
auto word,
auto bit) {
return etl::set_bit(word, bit, value); });
166 etl::fill(_words.begin(), _words.end(), WordType(0));
175 return transform_bit(pos, [](
auto word,
auto bit) {
return etl::reset_bit(word, bit); });
181 etl::transform(_words.cbegin(), _words.cend(), _words.begin(), [](
auto word) {
182 return static_cast<WordType>(~word);
185 if constexpr (has_padding) {
186 _words[num_words - 1] &= padding_mask_inv;
197 return transform_bit(pos, [](
auto word,
auto bit) {
return etl::flip_bit(word, bit); });
203 etl::transform(_words.begin(), _words.end(), other._words.begin(), _words.begin(), [](
auto lhs,
auto rhs) {
204 return static_cast<WordType>(lhs & rhs);
212 etl::transform(_words.begin(), _words.end(), other._words.begin(), _words.begin(), [](
auto lhs,
auto rhs) {
213 return static_cast<WordType>(lhs | rhs);
221 etl::transform(_words.begin(), _words.end(), other._words.begin(), _words.begin(), [](
auto lhs,
auto rhs) {
222 return static_cast<WordType>(lhs ^ rhs);
251 static constexpr auto num_words = (Bits + bits_per_word - 1) / bits_per_word;
252 static constexpr auto padding = num_words * bits_per_word - Bits;
253 static constexpr auto has_padding = padding != 0;
254 static constexpr auto padding_mask = [] {
255 auto mask = WordType{};
256 for (
auto i{bits_per_word - padding}; i < bits_per_word; ++i) {
261 static constexpr auto padding_mask_inv =
static_cast<WordType
>(~padding_mask);
263 [[nodiscard]]
static constexpr auto word_index(
etl::size_t pos) ->
etl::size_t {
return pos / bits_per_word; }
265 [[nodiscard]]
static constexpr auto offset_in_word(
etl::size_t pos) -> WordType
267 return static_cast<WordType
>(pos & (bits_per_word -
etl::size_t(1)));
272 auto& word = _words[word_index(pos)];
273 word = op(word, offset_in_word(pos));
277 etl::array<WordType, num_words> _words{};
#define TETL_PRECONDITION(...)
Definition check.hpp:16
constexpr auto all_of(InputIt first, InputIt last, Predicate p) -> bool
Checks if unary predicate p returns true for all elements in the range [first, last).
Definition all_of.hpp:15
constexpr auto transform(InputIt first, InputIt last, OutputIt dest, UnaryOp op) -> OutputIt
Applies the given function to a range and stores the result in another range, beginning at dest....
Definition transform.hpp:24
constexpr auto min(Type const &a, Type const &b, Compare comp) noexcept -> Type const &
Returns the smaller of a and b, using a compare function.
Definition min.hpp:13
constexpr auto fill(ForwardIt first, ForwardIt last, T const &value) -> void
Assigns the given value to the elements in the range [first, last).
Definition fill.hpp:11
constexpr auto set_bit(UInt word, UInt pos) noexcept -> UInt
Set bit at position pos.
Definition set_bit.hpp:19
constexpr auto reset_bit(UInt word, UInt pos) noexcept -> UInt
Reset bit at position pos.
Definition reset_bit.hpp:19
constexpr auto flip_bit(UInt word, UInt pos) noexcept -> UInt
Flip bit at position pos.
Definition flip_bit.hpp:19
constexpr auto test_bit(UInt word, UInt pos) noexcept -> bool
Test bit at position pos.
Definition test_bit.hpp:19
constexpr auto prev(BidirIt it, typename iterator_traits< BidirIt >::difference_type n=1) -> BidirIt
Return the nth predecessor of iterator it.
Definition prev.hpp:14
constexpr auto transform_reduce(InputIt1 first1, InputIt1 last1, InputIt2 first2, T init, BinaryReductionOp reduce, BinaryTransformOp transform) -> T
https://en.cppreference.com/w/cpp/algorithm/transform_reduce
Definition transform_reduce.hpp:13
Definition adjacent_find.hpp:8
constexpr auto addressof(T &arg) noexcept -> T *
Obtains the actual address of the object or function arg, even in presence of overloaded operator&.
Definition addressof.hpp:15
TETL_BUILTIN_SIZET size_t
etl::size_t is the unsigned integer type of the result of the sizeof operator.
Definition size_t.hpp:14
Definition basic_bitset.hpp:33
constexpr auto operator=(bool x) noexcept -> reference &
Definition basic_bitset.hpp:35
constexpr auto operator~() const noexcept -> bool
Definition basic_bitset.hpp:49
constexpr auto flip() noexcept -> reference &
Definition basic_bitset.hpp:51
constexpr auto operator=(reference const &x) noexcept -> reference &
Definition basic_bitset.hpp:41
constexpr auto count() const noexcept -> etl::size_t
Returns the number of bits that are set to true.
Definition basic_bitset.hpp:127
constexpr auto none() const noexcept -> bool
Checks if none of the bits are set to true.
Definition basic_bitset.hpp:121
constexpr auto set() noexcept -> basic_bitset &
Sets all bits to true.
Definition basic_bitset.hpp:143
constexpr auto operator^=(basic_bitset const &other) noexcept -> basic_bitset &
Sets the bits to the result of binary XOR on corresponding pairs of bits of *this and other
Definition basic_bitset.hpp:219
constexpr auto any() const noexcept -> bool
Checks if any bits are set to true.
Definition basic_bitset.hpp:118
constexpr auto reset() noexcept -> basic_bitset &
Sets all bits to false.
Definition basic_bitset.hpp:164
constexpr auto operator|=(basic_bitset const &other) noexcept -> basic_bitset &
Sets the bits to the result of binary OR on corresponding pairs of bits of *this and other
Definition basic_bitset.hpp:210
constexpr auto unchecked_reset(etl::size_t pos) -> basic_bitset &
Sets the bit at position pos to false.
Definition basic_bitset.hpp:172
friend constexpr auto operator&(basic_bitset const &lhs, basic_bitset const &rhs) noexcept -> basic_bitset
Returns a basic_bitset containing the result of binary AND on corresponding pairs of bits of lhs and ...
Definition basic_bitset.hpp:231
constexpr auto unchecked_test(etl::size_t pos) const -> bool
Returns true if the bit at position pos is set.
Definition basic_bitset.hpp:136
constexpr auto flip() noexcept -> basic_bitset &
Flips all bits.
Definition basic_bitset.hpp:179
constexpr basic_bitset(unsigned long long val) noexcept
Constructs a bitset, initializing the first (rightmost, least significant) M bit positions to the cor...
Definition basic_bitset.hpp:75
friend constexpr auto operator^(basic_bitset const &lhs, basic_bitset const &rhs) noexcept -> basic_bitset
Returns a basic_bitset containing the result of binary XOR on corresponding pairs of bits of lhs and ...
Definition basic_bitset.hpp:243
constexpr auto operator&=(basic_bitset const &other) noexcept -> basic_bitset &
Sets the bits to the result of binary AND on corresponding pairs of bits of *this and other
Definition basic_bitset.hpp:201
constexpr auto all() const noexcept -> bool
Checks if all bits are set to true.
Definition basic_bitset.hpp:104
constexpr auto unchecked_flip(etl::size_t pos) -> basic_bitset &
Flip bit at position pos.
Definition basic_bitset.hpp:194
constexpr auto unchecked_set(etl::size_t pos, bool value=true) -> basic_bitset &
Set bit at position pos to value.
Definition basic_bitset.hpp:157
friend constexpr auto operator|(basic_bitset const &lhs, basic_bitset const &rhs) noexcept -> basic_bitset
Returns a basic_bitset containing the result of binary OR on corresponding pairs of bits of lhs and r...
Definition basic_bitset.hpp:237
constexpr basic_bitset()=default
Default constructor. Constructs a bitset with all bits set to zero.
friend constexpr auto operator==(basic_bitset const &lhs, basic_bitset const &rhs) -> bool=default
Returns true if all of the bits in lhs and rhs are equal.
constexpr auto operator[](etl::size_t pos) -> reference
Returns a reference to the bit at position pos.
Definition basic_bitset.hpp:97
constexpr auto size() const noexcept -> etl::size_t
Returns the number of bits that the bitset holds.
Definition basic_bitset.hpp:85
constexpr auto operator[](etl::size_t pos) const -> bool
Returns true if the bit at position pos is set.
Definition basic_bitset.hpp:89
static constexpr int digits
Definition numeric_limits.hpp:24
static constexpr auto max() noexcept
Definition numeric_limits.hpp:21
Function object for performing addition. Effectively calls operator+ on two instances of type T....
Definition plus.hpp:14