4#ifndef TETL_BITSET_BASIC_BITSET_HPP
5#define TETL_BITSET_BASIC_BITSET_HPP
7#include <etl/_algorithm/all_of.hpp>
8#include <etl/_algorithm/any_of.hpp>
9#include <etl/_algorithm/fill.hpp>
10#include <etl/_algorithm/min.hpp>
11#include <etl/_algorithm/transform.hpp>
12#include <etl/_array/array.hpp>
13#include <etl/_bit/flip_bit.hpp>
14#include <etl/_bit/popcount.hpp>
15#include <etl/_bit/reset_bit.hpp>
16#include <etl/_bit/set_bit.hpp>
17#include <etl/_bit/test_bit.hpp>
18#include <etl/_concepts/unsigned_integral.hpp>
19#include <etl/_contracts/check.hpp>
20#include <etl/_cstddef/size_t.hpp>
21#include <etl/_functional/plus.hpp>
22#include <etl/_iterator/prev.hpp>
23#include <etl/_limits/numeric_limits.hpp>
24#include <etl/_memory/addressof.hpp>
25#include <etl/_numeric/transform_reduce.hpp>
31template <
etl::size_t Bits,
etl::unsigned_integral WordType =
etl::size_t>
36 *_word =
etl::set_bit(*_word, _offset, x);
42 *_word =
etl::set_bit(*_word, _offset,
static_cast<
bool>(x));
48 return etl::test_bit(*_word, _offset);
53 return not static_cast<
bool>(*
this);
58 *_word =
etl::flip_bit(*_word, _offset);
63 constexpr explicit reference(WordType& word, WordType offset)
noexcept
64 : _word{
etl::addressof(word)}
84 for (
auto i =
etl::size_t(0); i < m; ++i) {
99 TETL_PRECONDITION(pos < size());
107 TETL_PRECONDITION(pos < size());
108 return reference{_words[word_index(pos)], offset_in_word(pos)};
114 auto const allSet = [](
auto word) {
return word == ones; };
116 if constexpr (has_padding) {
117 auto const head =
etl::all_of(_words.cbegin(),
etl::prev(_words.cend()), allSet);
118 auto const tail = _words[num_words - 1] == padding_mask_inv;
119 return head
and tail;
121 return etl::all_of(_words.cbegin(), _words.cend(), allSet);
134 return etl::all_of(_words.cbegin(), _words.cend(), [](
auto word) {
return word == WordType(0); });
140 return etl::transform_reduce(_words.cbegin(), _words.cend(),
etl::size_t(0),
etl::
plus(), [](
auto word) {
141 return static_cast<
etl::size_t>(
etl::popcount(word));
149 TETL_PRECONDITION(pos < size());
150 return etl::test_bit(_words[word_index(pos)], offset_in_word(pos));
156 if constexpr (has_padding) {
157 etl::fill(_words.begin(),
etl::prev(_words.end()), ones);
158 _words[num_words - 1] = padding_mask_inv;
160 etl::fill(_words.begin(), _words.end(), ones);
170 TETL_PRECONDITION(pos < size());
171 return transform_bit(pos, [value](
auto word,
auto bit) {
return etl::set_bit(word, bit, value); });
177 etl::fill(_words.begin(), _words.end(), WordType(0));
185 TETL_PRECONDITION(pos < size());
186 return transform_bit(pos, [](
auto word,
auto bit) {
return etl::reset_bit(word, bit); });
192 etl::transform(_words.cbegin(), _words.cend(), _words.begin(), [](
auto word) {
193 return static_cast<WordType>(~word);
196 if constexpr (has_padding) {
197 _words[num_words - 1] &= padding_mask_inv;
207 TETL_PRECONDITION(pos < size());
208 return transform_bit(pos, [](
auto word,
auto bit) {
return etl::flip_bit(word, bit); });
214 etl::transform(_words.begin(), _words.end(), other._words.begin(), _words.begin(), [](
auto lhs,
auto rhs) {
215 return static_cast<WordType>(lhs & rhs);
223 etl::transform(_words.begin(), _words.end(), other._words.begin(), _words.begin(), [](
auto lhs,
auto rhs) {
224 return static_cast<WordType>(lhs | rhs);
232 etl::transform(_words.begin(), _words.end(), other._words.begin(), _words.begin(), [](
auto lhs,
auto rhs) {
233 return static_cast<WordType>(lhs ^ rhs);
261 static constexpr auto bits_per_word =
static_cast<size_t>(
etl::
numeric_limits<WordType>::digits);
262 static constexpr auto num_words = (Bits + bits_per_word - 1) / bits_per_word;
263 static constexpr auto padding = (num_words * bits_per_word) - Bits;
264 static constexpr auto has_padding = padding != 0;
265 static constexpr auto padding_mask = [] {
266 auto mask = WordType{};
267 for (
auto i{bits_per_word - padding}; i < bits_per_word; ++i) {
268 mask =
etl::set_bit(mask,
static_cast<WordType>(i));
272 static constexpr auto padding_mask_inv =
static_cast<WordType>(~padding_mask);
274 [[
nodiscard]]
static constexpr auto word_index(
etl::size_t pos) ->
etl::size_t
276 return pos / bits_per_word;
279 [[
nodiscard]]
static constexpr auto offset_in_word(
etl::size_t pos) -> WordType
281 return static_cast<WordType>(pos & (bits_per_word -
etl::size_t(1)));
291 etl::
array<WordType, num_words> _words{};
constexpr auto test_bit(UInt word, UInt pos) noexcept -> bool
Test bit at position pos.
Definition test_bit.hpp:20
Definition adjacent_find.hpp:9
A container that encapsulates fixed size arrays.
Definition array.hpp:49
Definition basic_bitset.hpp:33
constexpr auto operator=(bool x) noexcept -> reference &
Definition basic_bitset.hpp:34
constexpr operator bool() const noexcept
Definition basic_bitset.hpp:46
constexpr auto operator~() const noexcept -> bool
Definition basic_bitset.hpp:51
constexpr auto flip() noexcept -> reference &
Definition basic_bitset.hpp:56
constexpr auto operator=(reference const &x) noexcept -> reference &
Definition basic_bitset.hpp:40
Definition basic_bitset.hpp:32
constexpr auto count() const noexcept -> etl::size_t
Returns the number of bits that are set to true.
Definition basic_bitset.hpp:138
constexpr auto none() const noexcept -> bool
Checks if none of the bits are set to true.
Definition basic_bitset.hpp:132
constexpr auto set() noexcept -> basic_bitset &
Sets all bits to true.
Definition basic_bitset.hpp:154
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:230
constexpr auto any() const noexcept -> bool
Checks if any bits are set to true.
Definition basic_bitset.hpp:126
constexpr auto reset() noexcept -> basic_bitset &
Sets all bits to false.
Definition basic_bitset.hpp:175
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:221
constexpr auto unchecked_reset(etl::size_t pos) -> basic_bitset &
Sets the bit at position pos to false.
Definition basic_bitset.hpp:183
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:242
constexpr auto unchecked_test(etl::size_t pos) const -> bool
Returns true if the bit at position pos is set.
Definition basic_bitset.hpp:147
constexpr auto flip() noexcept -> basic_bitset &
Flips all bits.
Definition basic_bitset.hpp:190
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:80
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:254
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:212
constexpr auto all() const noexcept -> bool
Checks if all bits are set to true.
Definition basic_bitset.hpp:112
constexpr auto unchecked_flip(etl::size_t pos) -> basic_bitset &
Flip bit at position pos.
Definition basic_bitset.hpp:205
constexpr auto unchecked_set(etl::size_t pos, bool value=true) -> basic_bitset &
Set bit at position pos to value.
Definition basic_bitset.hpp:168
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:248
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:105
constexpr auto size() const noexcept -> etl::size_t
Returns the number of bits that the bitset holds.
Definition basic_bitset.hpp:90
constexpr auto operator[](etl::size_t pos) const -> bool
Returns true if the bit at position pos is set.
Definition basic_bitset.hpp:97
static constexpr int digits
Definition numeric_limits.hpp:982
Definition numeric_limits.hpp:18
Function object for performing addition. Effectively calls operator+ on two instances of type T....
Definition plus.hpp:15