4#ifndef TETL_BITSET_BITSET_HPP
5#define TETL_BITSET_BITSET_HPP
7#include <etl/_algorithm/min.hpp>
8#include <etl/_bit/bit_cast.hpp>
9#include <etl/_bit/byteswap.hpp>
10#include <etl/_bit/endian.hpp>
11#include <etl/_bit/set_bit.hpp>
12#include <etl/_bitset/basic_bitset.hpp>
13#include <etl/_contracts/check.hpp>
14#include <etl/_cstddef/size_t.hpp>
15#include <etl/_limits/numeric_limits.hpp>
16#include <etl/_string/basic_inplace_string.hpp>
17#include <etl/_string_view/basic_string_view.hpp>
25template <
etl::size_t Bits>
30 constexpr bitset()
noexcept =
default;
39 constexpr bitset(
unsigned long long val)
noexcept
57 template <
typename CharT,
typename Traits>
62 CharT zero = CharT(
'0'),
63 CharT one = CharT(
'1')
67 using size_type =
decltype(pos);
69 auto const len =
etl::min<size_type>(n, str.size() - pos);
70 auto const substr = str.substr(pos, len);
71 TETL_PRECONDITION(len >= 0);
72 TETL_PRECONDITION(len <= size());
74 for (size_type i{0}; i < len; ++i) {
75 if (Traits::eq(substr[len - i - 1], one)) {
78 if (Traits::eq(substr[len - i - 1], zero)) {
90 template <
typename CharT>
94 CharT zero = CharT(
'0'),
95 CharT one = CharT(
'1')
121 TETL_PRECONDITION(pos < size());
122 _bits.unchecked_set(pos, value);
139 TETL_PRECONDITION(pos < size());
140 _bits.unchecked_reset(pos);
157 TETL_PRECONDITION(pos < size());
158 _bits.unchecked_flip(pos);
168 TETL_PRECONDITION(pos < size());
169 return _bits.unchecked_at(pos);
178 TETL_PRECONDITION(pos < size());
179 return _bits.unchecked_at(pos);
188 TETL_PRECONDITION(pos < size());
189 return _bits.unchecked_test(pos);
213 return _bits.count();
225 return _bits == rhs._bits;
232 _bits &= other._bits;
240 _bits |= other._bits;
248 _bits ^= other._bits;
255 return bitset(*
this).flip();
264 template <size_t Capacity,
typename CharT =
char,
typename Traits = char_traits<CharT>>
265 requires(Capacity >= Bits)
266 [[nodiscard]]
constexpr auto to_string(CharT zero = CharT(
'0'), CharT one = CharT(
'1'))
const
270 for (
auto i{
size() - 1U}; i != 0; --i) {
271 str.push_back(
test(i
) ? one : zero);
273 str.push_back(
test(0
) ? one : zero);
280 [[
nodiscard]]
constexpr auto to_ulong()
const noexcept ->
unsigned long
283 return to_unsigned_type<
unsigned long>();
289 [[
nodiscard]]
constexpr auto to_ullong()
const noexcept ->
unsigned long long
292 return to_unsigned_type<
unsigned long long>();
298 return bitset(lhs) &= rhs;
304 return bitset(lhs) |= rhs;
310 return bitset(lhs) ^= rhs;
314 template <
typename UInt>
315 [[nodiscard]]
constexpr auto to_unsigned_type()
const noexcept -> UInt
317 if constexpr (
sizeof(UInt) ==
sizeof(_bits)) {
319 return etl::bit_cast<UInt>(_bits);
321 return etl::byteswap(
etl::bit_cast<UInt>(_bits));
325 auto const idx =
etl::min<UInt>(
static_cast<UInt>(
size()), digits);
327 for (UInt i{0}; i != idx; ++i) {
329 result =
etl::set_bit(result, i);
endian
Indicates the endianness of all scalar types. If all scalar types are little-endian,...
Definition endian.hpp:16
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:26
constexpr auto none() const noexcept -> bool
Checks if none bits are set to true.
Definition bitset.hpp:205
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:39
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:223
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:246
constexpr auto any() const noexcept -> bool
Checks if any bits are set to true.
Definition bitset.hpp:199
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:176
friend constexpr auto operator^(bitset const &lhs, bitset const &rhs) noexcept -> bitset
Performs binary XOR between two bitsets, lhs and rhs.
Definition bitset.hpp:308
constexpr auto size() const noexcept -> size_t
Returns the number of bits that the bitset holds.
Definition bitset.hpp:217
constexpr auto count() const noexcept -> size_t
Returns the number of bits that are set to true.
Definition bitset.hpp:211
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:230
constexpr auto flip(size_t pos) noexcept -> bitset &
Flips the bit at the position pos.
Definition bitset.hpp:155
constexpr auto operator~() const noexcept -> bitset
Returns a temporary copy of *this with all bits flipped (binary NOT).
Definition bitset.hpp:253
constexpr auto all() const noexcept -> bool
Checks if all bits are set to true.
Definition bitset.hpp:193
constexpr auto reset() noexcept -> bitset &
Sets all bits to false.
Definition bitset.hpp:127
friend constexpr auto operator&(bitset const &lhs, bitset const &rhs) noexcept -> bitset
Performs binary AND between two bitsets, lhs and rhs.
Definition bitset.hpp:296
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:166
friend constexpr auto operator|(bitset const &lhs, bitset const &rhs) noexcept -> bitset
Performs binary OR between two bitsets, lhs and rhs.
Definition bitset.hpp:302
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:119
constexpr auto flip() noexcept -> bitset &
Flips all bits (like operator~, but in-place).
Definition bitset.hpp:145
constexpr auto reset(size_t pos) noexcept -> bitset &
Sets the bit at position pos to false.
Definition bitset.hpp:137
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:266
constexpr auto set() noexcept -> bitset &
Sets all bits to true.
Definition bitset.hpp:108
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:238
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:91
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:58
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:186
static constexpr int digits
Definition numeric_limits.hpp:829
static constexpr int digits
Definition numeric_limits.hpp:982
Definition numeric_limits.hpp:18