tetl 0.1.0
Embedded Template Library
Loading...
Searching...
No Matches
bitset.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: BSL-1.0
2
3#ifndef TETL_BITSET_BITSET_HPP
4#define TETL_BITSET_BITSET_HPP
5
14
15namespace etl {
16
21template <etl::size_t Bits>
22struct bitset {
24
26 constexpr bitset() noexcept = default;
27
35 constexpr bitset(unsigned long long val) noexcept
36 : _bits(val)
37 {
38 }
39
53 template <typename CharT, typename Traits>
54 explicit constexpr bitset(
58 CharT zero = CharT('0'),
59 CharT one = CharT('1')
60 )
61 : bitset(0ULL)
62 {
63 auto const len = etl::min<decltype(pos)>(n, str.size() - pos);
64 TETL_PRECONDITION(len >= 0);
65 TETL_PRECONDITION(len <= size());
66
67 for (decltype(pos) i = 0; i < len; ++i) {
68 if (Traits::eq(str[i + pos], one)) {
69 set(i, true);
70 }
71 if (Traits::eq(str[i + pos], zero)) {
72 set(i, false);
73 }
74 }
75 }
76
83 template <typename CharT>
84 explicit constexpr bitset(
85 CharT const* str,
87 CharT zero = CharT('0'),
88 CharT one = CharT('1')
89 )
90 : bitset(
91 n == basic_string_view<CharT>::npos ? basic_string_view<CharT>(str) : basic_string_view<CharT>(str, n),
92 0,
93 n,
94 zero,
95 one
96 )
97 {
98 }
99
101 constexpr auto set() noexcept -> bitset&
102 {
103 _bits.set();
104 return *this;
105 }
106
112 constexpr auto set(etl::size_t pos, bool value = true) -> bitset&
113 {
114 TETL_PRECONDITION(pos < size());
115 _bits.unchecked_set(pos, value);
116 return *this;
117 }
118
120 constexpr auto reset() noexcept -> bitset&
121 {
122 _bits.reset();
123 return *this;
124 }
125
130 constexpr auto reset(size_t pos) noexcept -> bitset&
131 {
132 TETL_PRECONDITION(pos < size());
133 _bits.unchecked_reset(pos);
134 return *this;
135 }
136
138 constexpr auto flip() noexcept -> bitset&
139 {
140 _bits.flip();
141 return *this;
142 }
143
148 constexpr auto flip(size_t pos) noexcept -> bitset&
149 {
150 TETL_PRECONDITION(pos < size());
151 _bits.unchecked_flip(pos);
152 return *this;
153 }
154
159 [[nodiscard]] constexpr auto operator[](size_t const pos) -> reference
160 {
161 TETL_PRECONDITION(pos < size());
162 return _bits[pos];
163 }
164
169 [[nodiscard]] constexpr auto operator[](size_t const pos) const -> bool
170 {
171 TETL_PRECONDITION(pos < size());
172 return _bits[pos];
173 }
174
179 [[nodiscard]] constexpr auto test(size_t const pos) const -> bool
180 {
181 TETL_PRECONDITION(pos < size());
182 return _bits.unchecked_test(pos);
183 }
184
186 [[nodiscard]] constexpr auto all() const noexcept -> bool { return _bits.all(); }
187
189 [[nodiscard]] constexpr auto any() const noexcept -> bool { return _bits.any(); }
190
192 [[nodiscard]] constexpr auto none() const noexcept -> bool { return _bits.none(); }
193
195 [[nodiscard]] constexpr auto count() const noexcept -> size_t { return _bits.count(); }
196
198 [[nodiscard]] constexpr auto size() const noexcept -> size_t { return _bits.size(); }
199
201 [[nodiscard]] constexpr auto operator==(bitset const& rhs) const noexcept -> bool { return _bits == rhs._bits; }
202
205 constexpr auto operator&=(bitset const& other) noexcept -> bitset&
206 {
207 _bits &= other._bits;
208 return *this;
209 }
210
213 constexpr auto operator|=(bitset const& other) noexcept -> bitset&
214 {
215 _bits |= other._bits;
216 return *this;
217 }
218
221 constexpr auto operator^=(bitset const& other) noexcept -> bitset&
222 {
223 _bits ^= other._bits;
224 return *this;
225 }
226
228 constexpr auto operator~() const noexcept -> bitset { return bitset(*this).flip(); }
229
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
240 {
242 for (auto i{size() - 1U}; i != 0; --i) {
243 str.push_back(test(i) ? one : zero);
244 }
245 str.push_back(test(0) ? one : zero);
246 return str;
247 }
248
252 [[nodiscard]] constexpr auto to_ulong() const noexcept -> unsigned long
253 requires(etl::numeric_limits<unsigned long>::digits >= Bits)
254 {
255 return to_unsigned_type<unsigned long>();
256 }
257
261 [[nodiscard]] constexpr auto to_ullong() const noexcept -> unsigned long long
262 requires(etl::numeric_limits<unsigned long long>::digits >= Bits)
263 {
264 return to_unsigned_type<unsigned long long>();
265 }
266
268 friend constexpr auto operator&(bitset const& lhs, bitset const& rhs) noexcept -> bitset
269 {
270 return bitset(lhs) &= rhs;
271 }
272
274 friend constexpr auto operator|(bitset const& lhs, bitset const& rhs) noexcept -> bitset
275 {
276 return bitset(lhs) |= rhs;
277 }
278
280 friend constexpr auto operator^(bitset const& lhs, bitset const& rhs) noexcept -> bitset
281 {
282 return bitset(lhs) ^= rhs;
283 }
284
285private:
286 template <typename UInt>
287 [[nodiscard]] constexpr auto to_unsigned_type() const noexcept -> UInt
288 {
289 constexpr auto digits = static_cast<UInt>(etl::numeric_limits<UInt>::digits);
290 auto const idx = etl::min<UInt>(static_cast<UInt>(size()), digits);
291 UInt result{};
292 for (UInt i{0}; i != idx; ++i) {
293 if (test(static_cast<etl::size_t>(i))) {
294 result = etl::set_bit(result, i);
295 }
296 }
297 return result;
298 }
299
300 basic_bitset<Bits, etl::size_t> _bits;
301};
302
303} // namespace etl
304
305#endif // TETL_BITSET_BITSET_HPP
#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