3#ifndef TETL_BITSET_BITSET_HPP
4#define TETL_BITSET_BITSET_HPP
21template <etl::
size_t Bits>
26 constexpr bitset() noexcept = default;
35 constexpr
bitset(
unsigned long long val) noexcept
53 template <
typename CharT,
typename Traits>
58 CharT zero = CharT(
'0'),
59 CharT one = CharT(
'1')
67 for (
decltype(pos) i = 0; i < len; ++i) {
68 if (Traits::eq(str[i + pos], one)) {
71 if (Traits::eq(str[i + pos], zero)) {
83 template <
typename CharT>
87 CharT zero = CharT(
'0'),
88 CharT one = CharT(
'1')
115 _bits.unchecked_set(pos, value);
133 _bits.unchecked_reset(pos);
151 _bits.unchecked_flip(pos);
169 [[nodiscard]]
constexpr auto operator[](
size_t const pos)
const ->
bool
179 [[nodiscard]]
constexpr auto test(
size_t const pos)
const ->
bool
182 return _bits.unchecked_test(pos);
186 [[nodiscard]]
constexpr auto all() const noexcept ->
bool {
return _bits.all(); }
189 [[nodiscard]]
constexpr auto any() const noexcept ->
bool {
return _bits.any(); }
192 [[nodiscard]]
constexpr auto none() const noexcept ->
bool {
return _bits.none(); }
195 [[nodiscard]]
constexpr auto count() const noexcept ->
size_t {
return _bits.count(); }
198 [[nodiscard]]
constexpr auto size() const noexcept ->
size_t {
return _bits.size(); }
201 [[nodiscard]]
constexpr auto operator==(
bitset const& rhs)
const noexcept ->
bool {
return _bits == rhs._bits; }
207 _bits &= other._bits;
215 _bits |= other._bits;
223 _bits ^= other._bits;
236 template <
size_t Capacity,
typename CharT =
char,
typename Traits =
char_traits<CharT>>
237 requires(Capacity >= Bits)
238 [[nodiscard]]
constexpr auto to_string(CharT zero = CharT(
'0'), CharT one = CharT(
'1'))
const
242 for (
auto i{
size() - 1U}; i != 0; --i) {
243 str.push_back(
test(i) ? one : zero);
252 [[nodiscard]]
constexpr auto to_ulong() const noexcept ->
unsigned long
255 return to_unsigned_type<unsigned long>();
261 [[nodiscard]]
constexpr auto to_ullong() const noexcept ->
unsigned long long
264 return to_unsigned_type<unsigned long long>();
270 return bitset(lhs) &= rhs;
276 return bitset(lhs) |= rhs;
282 return bitset(lhs) ^= rhs;
286 template <
typename UInt>
287 [[nodiscard]]
constexpr auto to_unsigned_type() const noexcept -> UInt
292 for (UInt i{0}; i != idx; ++i) {
300 basic_bitset<Bits, etl::size_t> _bits;
#define TETL_PRECONDITION(...)
Definition check.hpp:16
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 set_bit(UInt word, UInt pos) noexcept -> UInt
Set bit at position pos.
Definition set_bit.hpp:19
Definition adjacent_find.hpp:8
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
basic_inplace_string class with fixed size capacity.
Definition basic_inplace_string.hpp:41
constexpr auto push_back(Char ch) noexcept -> void
Appends the given character ch to the end of the string.
Definition basic_inplace_string.hpp:431
The class template basic_string_view describes an object that can refer to a constant contiguous sequ...
Definition basic_string_view.hpp:34
static constexpr size_type npos
This is a special value equal to the maximum value representable by the type size_type.
Definition basic_string_view.hpp:693
etl::size_t size_type
Definition basic_string_view.hpp:43
constexpr auto size() const noexcept -> size_type
Returns the number of Char elements in the view, i.e. etl::distance(begin(), end()).
Definition basic_string_view.hpp:174
constexpr auto to_ullong() const noexcept -> unsigned long long requires(etl::numeric_limits< unsigned long long >::digits >=Bits)
Converts the contents of the bitset to an unsigned long long integer. The first bit corresponds to th...
Definition bitset.hpp:261
constexpr auto to_ulong() const noexcept -> unsigned long requires(etl::numeric_limits< unsigned long >::digits >=Bits)
Converts the contents of the bitset to an unsigned long integer. The first bit corresponds to the lea...
Definition bitset.hpp:252
constexpr auto none() const noexcept -> bool
Checks if none bits are set to true.
Definition bitset.hpp:192
constexpr auto operator==(bitset const &rhs) const noexcept -> bool
Returns true if all of the bits in *this and rhs are equal.
Definition bitset.hpp:201
constexpr auto operator^=(bitset const &other) noexcept -> bitset &
Sets the bits to the result of binary XOR on corresponding pairs of bits of *this and other.
Definition bitset.hpp:221
constexpr auto any() const noexcept -> bool
Checks if any bits are set to true.
Definition bitset.hpp:189
constexpr auto operator[](size_t const pos) const -> bool
Returns the value of the bit at the position pos. Perfoms no bounds checking.
Definition bitset.hpp:169
friend constexpr auto operator^(bitset const &lhs, bitset const &rhs) noexcept -> bitset
Performs binary XOR between two bitsets, lhs and rhs.
Definition bitset.hpp:280
basic_bitset< Bits, etl::size_t >::reference reference
Definition bitset.hpp:23
constexpr auto size() const noexcept -> size_t
Returns the number of bits that the bitset holds.
Definition bitset.hpp:198
constexpr auto count() const noexcept -> size_t
Returns the number of bits that are set to true.
Definition bitset.hpp:195
constexpr auto operator&=(bitset const &other) noexcept -> bitset &
Sets the bits to the result of binary AND on corresponding pairs of bits of *this and other.
Definition bitset.hpp:205
constexpr auto flip(size_t pos) noexcept -> bitset &
Flips the bit at the position pos.
Definition bitset.hpp:148
constexpr auto operator~() const noexcept -> bitset
Returns a temporary copy of *this with all bits flipped (binary NOT).
Definition bitset.hpp:228
constexpr auto all() const noexcept -> bool
Checks if all bits are set to true.
Definition bitset.hpp:186
constexpr auto reset() noexcept -> bitset &
Sets all bits to false.
Definition bitset.hpp:120
friend constexpr auto operator&(bitset const &lhs, bitset const &rhs) noexcept -> bitset
Performs binary AND between two bitsets, lhs and rhs.
Definition bitset.hpp:268
constexpr auto operator[](size_t const pos) -> reference
Returns a reference like proxy to the bit at the position pos. Perfoms no bounds checking.
Definition bitset.hpp:159
friend constexpr auto operator|(bitset const &lhs, bitset const &rhs) noexcept -> bitset
Performs binary OR between two bitsets, lhs and rhs.
Definition bitset.hpp:274
constexpr auto set(etl::size_t pos, bool value=true) -> bitset &
Sets the bit at the given position to the given value.
Definition bitset.hpp:112
constexpr auto flip() noexcept -> bitset &
Flips all bits (like operator~, but in-place).
Definition bitset.hpp:138
constexpr auto reset(size_t pos) noexcept -> bitset &
Sets the bit at position pos to false.
Definition bitset.hpp:130
constexpr auto to_string(CharT zero=CharT('0'), CharT one=CharT('1')) const -> basic_inplace_string< CharT, Capacity, Traits >
Converts the contents of the bitset to a string. Uses zero to represent bits with value of false and ...
Definition bitset.hpp:238
constexpr auto set() noexcept -> bitset &
Sets all bits to true.
Definition bitset.hpp:101
constexpr bitset() noexcept=default
Constructs a bitset with all bits set to zero.
constexpr auto operator|=(bitset const &other) noexcept -> bitset &
Sets the bits to the result of binary OR on corresponding pairs of bits of *this and other.
Definition bitset.hpp:213
constexpr bitset(CharT const *str, typename basic_string_view< CharT >::size_type n=basic_string_view< CharT >::npos, CharT zero=CharT('0'), CharT one=CharT('1'))
Constructs a bitset using the characters in the char const* str.
Definition bitset.hpp:84
constexpr bitset(basic_string_view< CharT, Traits > const &str, typename basic_string_view< CharT, Traits >::size_type pos=0, typename basic_string_view< CharT, Traits >::size_type n=basic_string_view< CharT, Traits >::npos, CharT zero=CharT('0'), CharT one=CharT('1'))
Constructs a bitset using the characters in the etl::basic_string_view str.
Definition bitset.hpp:54
constexpr auto test(size_t const pos) const -> bool
Returns the value of the bit at the position pos. Perfoms no bounds checking.
Definition bitset.hpp:179
Definition numeric_limits.hpp:17
static constexpr int digits
Definition numeric_limits.hpp:24