4#ifndef TETL_BITSET_BITSET_HPP
5#define TETL_BITSET_BITSET_HPP
7#include <etl/_algorithm/min.hpp>
8#include <etl/_bit/set_bit.hpp>
9#include <etl/_bitset/basic_bitset.hpp>
10#include <etl/_contracts/check.hpp>
11#include <etl/_cstddef/size_t.hpp>
12#include <etl/_limits/numeric_limits.hpp>
13#include <etl/_string/basic_inplace_string.hpp>
14#include <etl/_string_view/basic_string_view.hpp>
22template <
etl::size_t Bits>
27 constexpr bitset()
noexcept =
default;
36 constexpr bitset(
unsigned long long val)
noexcept
54 template <
typename CharT,
typename Traits>
59 CharT zero = CharT(
'0'),
60 CharT one = CharT(
'1')
64 auto const len =
etl::min<
decltype(pos)>(n, str.size() - pos);
65 TETL_PRECONDITION(len >= 0);
66 TETL_PRECONDITION(len <= size());
68 for (
decltype(pos) i = 0; i < len; ++i) {
69 if (Traits::eq(str[i + pos], one)) {
72 if (Traits::eq(str[i + pos], zero)) {
84 template <
typename CharT>
88 CharT zero = CharT(
'0'),
89 CharT one = CharT(
'1')
115 TETL_PRECONDITION(pos < size());
116 _bits.unchecked_set(pos, value);
133 TETL_PRECONDITION(pos < size());
134 _bits.unchecked_reset(pos);
151 TETL_PRECONDITION(pos < size());
152 _bits.unchecked_flip(pos);
162 TETL_PRECONDITION(pos < size());
172 TETL_PRECONDITION(pos < size());
182 TETL_PRECONDITION(pos < size());
183 return _bits.unchecked_test(pos);
207 return _bits.count();
219 return _bits == rhs._bits;
226 _bits &= other._bits;
234 _bits |= other._bits;
242 _bits ^= other._bits;
249 return bitset(*
this).flip();
258 template <size_t Capacity,
typename CharT =
char,
typename Traits = char_traits<CharT>>
259 requires(Capacity >= Bits)
260 [[nodiscard]]
constexpr auto to_string(CharT zero = CharT(
'0'), CharT one = CharT(
'1'))
const
264 for (
auto i{
size() - 1U}; i != 0; --i) {
265 str.push_back(
test(i
) ? one : zero);
267 str.push_back(
test(0
) ? one : zero);
274 [[
nodiscard]]
constexpr auto to_ulong()
const noexcept ->
unsigned long
277 return to_unsigned_type<
unsigned long>();
283 [[
nodiscard]]
constexpr auto to_ullong()
const noexcept ->
unsigned long long
286 return to_unsigned_type<
unsigned long long>();
292 return bitset(lhs) &= rhs;
298 return bitset(lhs) |= rhs;
304 return bitset(lhs) ^= rhs;
308 template <
typename UInt>
309 [[nodiscard]]
constexpr auto to_unsigned_type()
const noexcept -> UInt
312 auto const idx =
etl::min<UInt>(
static_cast<UInt>(
size()), digits);
314 for (UInt i{0}; i != idx; ++i) {
316 result =
etl::set_bit(result, i);
Definition adjacent_find.hpp:9
Definition basic_bitset.hpp:32
basic_inplace_string class with fixed size capacity.
Definition basic_inplace_string.hpp:42
The class template basic_string_view describes an object that can refer to a constant contiguous sequ...
Definition basic_string_view.hpp:35
The class template bitset represents a fixed-size sequence of Bits bits. Bitsets can be manipulated b...
Definition bitset.hpp:23
constexpr auto none() const noexcept -> bool
Checks if none bits are set to true.
Definition bitset.hpp:199
constexpr bitset(unsigned long long val) noexcept
Constructs a bitset, initializing the first (rightmost, least significant) M bit positions to the cor...
Definition bitset.hpp:36
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:217
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:240
constexpr auto any() const noexcept -> bool
Checks if any bits are set to true.
Definition bitset.hpp:193
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:170
friend constexpr auto operator^(bitset const &lhs, bitset const &rhs) noexcept -> bitset
Performs binary XOR between two bitsets, lhs and rhs.
Definition bitset.hpp:302
constexpr auto size() const noexcept -> size_t
Returns the number of bits that the bitset holds.
Definition bitset.hpp:211
constexpr auto count() const noexcept -> size_t
Returns the number of bits that are set to true.
Definition bitset.hpp:205
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:224
constexpr auto flip(size_t pos) noexcept -> bitset &
Flips the bit at the position pos.
Definition bitset.hpp:149
constexpr auto operator~() const noexcept -> bitset
Returns a temporary copy of *this with all bits flipped (binary NOT).
Definition bitset.hpp:247
constexpr auto all() const noexcept -> bool
Checks if all bits are set to true.
Definition bitset.hpp:187
constexpr auto reset() noexcept -> bitset &
Sets all bits to false.
Definition bitset.hpp:121
friend constexpr auto operator&(bitset const &lhs, bitset const &rhs) noexcept -> bitset
Performs binary AND between two bitsets, lhs and rhs.
Definition bitset.hpp:290
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:160
friend constexpr auto operator|(bitset const &lhs, bitset const &rhs) noexcept -> bitset
Performs binary OR between two bitsets, lhs and rhs.
Definition bitset.hpp:296
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:113
constexpr auto flip() noexcept -> bitset &
Flips all bits (like operator~, but in-place).
Definition bitset.hpp:139
constexpr auto reset(size_t pos) noexcept -> bitset &
Sets the bit at position pos to false.
Definition bitset.hpp:131
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:260
constexpr auto set() noexcept -> bitset &
Sets all bits to true.
Definition bitset.hpp:102
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:232
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:85
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:55
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:180
static constexpr int digits
Definition numeric_limits.hpp:829
static constexpr int digits
Definition numeric_limits.hpp:982
Definition numeric_limits.hpp:18