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// SPDX-FileCopyrightText: Copyright (C) 2020 Tobias Hienzsch
3
4#ifndef TETL_BITSET_BITSET_HPP
5#define TETL_BITSET_BITSET_HPP
6
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>
15
16namespace etl {
17
18/// The class template bitset represents a fixed-size sequence of Bits bits.
19/// Bitsets can be manipulated by standard logic operators.
20/// \headerfile etl/bitset.hpp
21/// \ingroup bitset
22template <etl::size_t Bits>
23struct bitset {
24 using reference = basic_bitset<Bits, etl::size_t>::reference;
25
26 /// Constructs a bitset with all bits set to zero.
27 constexpr bitset() noexcept = default;
28
29 /// Constructs a bitset, initializing the first (rightmost, least
30 /// significant) M bit positions to the corresponding bit values of val,
31 /// where M is the smaller of the number of bits in an unsigned long long
32 /// and the number of bits Bits in the bitset being constructed. If M is less
33 /// than Bits (the bitset is longer than 64 bits, for typical implementations
34 /// of unsigned long long), the remaining bit positions are initialized to
35 /// zeroes.
36 constexpr bitset(unsigned long long val) noexcept
37 : _bits(val)
38 {
39 }
40
41 /// Constructs a bitset using the characters in the
42 /// etl::basic_string_view str.
43 ///
44 /// \details An optional starting position pos and length n can be provided,
45 /// as well as characters denoting alternate values for set (one) and unset
46 /// (zero) bits. Traits::eq() is used to compare the character values. The
47 /// effective length of the initializing string is min(n, str.size() - pos).
48 ///
49 /// \param str string used to initialize the bitset
50 /// \param pos a starting offset into str
51 /// \param n number of characters to use from str
52 /// \param zero alternate character for set bits in str
53 /// \param one alternate character for unset bits in str
54 template <typename CharT, typename Traits>
55 explicit constexpr bitset(
56 basic_string_view<CharT, Traits> const& str,
57 typename basic_string_view<CharT, Traits>::size_type pos = 0,
58 typename basic_string_view<CharT, Traits>::size_type n = basic_string_view<CharT, Traits>::npos,
59 CharT zero = CharT('0'),
60 CharT one = CharT('1')
61 )
62 : bitset(0ULL)
63 {
64 auto const len = etl::min<decltype(pos)>(n, str.size() - pos);
65 TETL_PRECONDITION(len >= 0);
66 TETL_PRECONDITION(len <= size());
67
68 for (decltype(pos) i = 0; i < len; ++i) {
69 if (Traits::eq(str[i + pos], one)) {
70 set(i, true);
71 }
72 if (Traits::eq(str[i + pos], zero)) {
73 set(i, false);
74 }
75 }
76 }
77
78 /// Constructs a bitset using the characters in the char const* str.
79 ///
80 /// \param str string used to initialize the bitset
81 /// \param n number of characters to use from str
82 /// \param zero alternate character for set bits in str
83 /// \param one alternate character for unset bits in str
84 template <typename CharT>
85 explicit constexpr bitset(
86 CharT const* str,
87 typename basic_string_view<CharT>::size_type n = basic_string_view<CharT>::npos,
88 CharT zero = CharT('0'),
89 CharT one = CharT('1')
90 )
91 : bitset(
92 n == basic_string_view<CharT>::npos ? basic_string_view<CharT>(str) : basic_string_view<CharT>(str, n),
93 0,
94 n,
95 zero,
96 one
97 )
98 {
99 }
100
101 /// Sets all bits to true.
102 constexpr auto set() noexcept -> bitset&
103 {
104 _bits.set();
105 return *this;
106 }
107
108 /// Sets the bit at the given position to the given value.
109 ///
110 /// \param pos Index of the bit to be modified.
111 /// \param value The new value for the bit.
112 /// \returns *this
113 constexpr auto set(etl::size_t pos, bool value = true) -> bitset&
114 {
115 TETL_PRECONDITION(pos < size());
116 _bits.unchecked_set(pos, value);
117 return *this;
118 }
119
120 /// Sets all bits to false.
121 constexpr auto reset() noexcept -> bitset&
122 {
123 _bits.reset();
124 return *this;
125 }
126
127 /// Sets the bit at position pos to false.
128 ///
129 /// \param pos Index of the bit to be reset.
130 /// \returns *this
131 constexpr auto reset(size_t pos) noexcept -> bitset&
132 {
133 TETL_PRECONDITION(pos < size());
134 _bits.unchecked_reset(pos);
135 return *this;
136 }
137
138 /// Flips all bits (like operator~, but in-place).
139 constexpr auto flip() noexcept -> bitset&
140 {
141 _bits.flip();
142 return *this;
143 }
144
145 /// Flips the bit at the position pos.
146 ///
147 /// \param pos Index of the bit to be reset.
148 /// \returns *this
149 constexpr auto flip(size_t pos) noexcept -> bitset&
150 {
151 TETL_PRECONDITION(pos < size());
152 _bits.unchecked_flip(pos);
153 return *this;
154 }
155
156 /// Returns a reference like proxy to the bit at the position pos.
157 /// Perfoms no bounds checking.
158 ///
159 /// \param pos Index of the bit.
160 [[nodiscard]] constexpr auto operator[](size_t const pos) -> reference
161 {
162 TETL_PRECONDITION(pos < size());
163 return _bits[pos];
164 }
165
166 /// Returns the value of the bit at the position pos. Perfoms no
167 /// bounds checking.
168 ///
169 /// \param pos Index of the bit.
170 [[nodiscard]] constexpr auto operator[](size_t const pos) const -> bool
171 {
172 TETL_PRECONDITION(pos < size());
173 return _bits[pos];
174 }
175
176 /// Returns the value of the bit at the position pos. Perfoms no
177 /// bounds checking.
178 ///
179 /// \param pos Index of the bit.
180 [[nodiscard]] constexpr auto test(size_t const pos) const -> bool
181 {
182 TETL_PRECONDITION(pos < size());
183 return _bits.unchecked_test(pos);
184 }
185
186 /// Checks if all bits are set to true.
187 [[nodiscard]] constexpr auto all() const noexcept -> bool
188 {
189 return _bits.all();
190 }
191
192 /// Checks if any bits are set to true.
193 [[nodiscard]] constexpr auto any() const noexcept -> bool
194 {
195 return _bits.any();
196 }
197
198 /// Checks if none bits are set to true.
199 [[nodiscard]] constexpr auto none() const noexcept -> bool
200 {
201 return _bits.none();
202 }
203
204 /// Returns the number of bits that are set to true.
205 [[nodiscard]] constexpr auto count() const noexcept -> size_t
206 {
207 return _bits.count();
208 }
209
210 /// Returns the number of bits that the bitset holds.
211 [[nodiscard]] constexpr auto size() const noexcept -> size_t
212 {
213 return _bits.size();
214 }
215
216 /// Returns true if all of the bits in *this and rhs are equal.
217 [[nodiscard]] constexpr auto operator==(bitset const& rhs) const noexcept -> bool
218 {
219 return _bits == rhs._bits;
220 }
221
222 /// Sets the bits to the result of binary AND on corresponding pairs
223 /// of bits of *this and other.
224 constexpr auto operator&=(bitset const& other) noexcept -> bitset&
225 {
226 _bits &= other._bits;
227 return *this;
228 }
229
230 /// Sets the bits to the result of binary OR on corresponding pairs
231 /// of bits of *this and other.
232 constexpr auto operator|=(bitset const& other) noexcept -> bitset&
233 {
234 _bits |= other._bits;
235 return *this;
236 }
237
238 /// Sets the bits to the result of binary XOR on corresponding pairs
239 /// of bits of *this and other.
240 constexpr auto operator^=(bitset const& other) noexcept -> bitset&
241 {
242 _bits ^= other._bits;
243 return *this;
244 }
245
246 /// Returns a temporary copy of *this with all bits flipped (binary NOT).
247 constexpr auto operator~() const noexcept -> bitset
248 {
249 return bitset(*this).flip();
250 }
251
252 /// Converts the contents of the bitset to a string. Uses zero to
253 /// represent bits with value of false and one to represent bits with value
254 /// of true. The resulting string contains Bits characters with the first
255 /// character corresponds to the last (Bits-1th) bit and the last character
256 /// corresponding to the first bit.
257 /// \todo Currently truncates the low bits, if the string is large enough.
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
261 -> basic_inplace_string<CharT, Capacity, Traits>
262 {
263 auto str = basic_inplace_string<CharT, Capacity, Traits>{};
264 for (auto i{size() - 1U}; i != 0; --i) {
265 str.push_back(test(i) ? one : zero);
266 }
267 str.push_back(test(0) ? one : zero);
268 return str;
269 }
270
271 /// Converts the contents of the bitset to an unsigned long integer.
272 /// The first bit corresponds to the least significant digit of the number
273 /// and the last bit corresponds to the most significant digit.
274 [[nodiscard]] constexpr auto to_ulong() const noexcept -> unsigned long
275 requires(etl::numeric_limits<unsigned long>::digits >= Bits)
276 {
277 return to_unsigned_type<unsigned long>();
278 }
279
280 /// Converts the contents of the bitset to an unsigned long long
281 /// integer. The first bit corresponds to the least significant digit of the
282 /// number and the last bit corresponds to the most significant digit.
283 [[nodiscard]] constexpr auto to_ullong() const noexcept -> unsigned long long
284 requires(etl::numeric_limits<unsigned long long>::digits >= Bits)
285 {
286 return to_unsigned_type<unsigned long long>();
287 }
288
289 /// Performs binary AND between two bitsets, lhs and rhs.
290 friend constexpr auto operator&(bitset const& lhs, bitset const& rhs) noexcept -> bitset
291 {
292 return bitset(lhs) &= rhs;
293 }
294
295 /// Performs binary OR between two bitsets, lhs and rhs.
296 friend constexpr auto operator|(bitset const& lhs, bitset const& rhs) noexcept -> bitset
297 {
298 return bitset(lhs) |= rhs;
299 }
300
301 /// Performs binary XOR between two bitsets, lhs and rhs.
302 friend constexpr auto operator^(bitset const& lhs, bitset const& rhs) noexcept -> bitset
303 {
304 return bitset(lhs) ^= rhs;
305 }
306
307private:
308 template <typename UInt>
309 [[nodiscard]] constexpr auto to_unsigned_type() const noexcept -> UInt
310 {
311 constexpr auto digits = static_cast<UInt>(etl::numeric_limits<UInt>::digits);
312 auto const idx = etl::min<UInt>(static_cast<UInt>(size()), digits);
313 UInt result{};
314 for (UInt i{0}; i != idx; ++i) {
315 if (test(static_cast<etl::size_t>(i))) {
316 result = etl::set_bit(result, i);
317 }
318 }
319 return result;
320 }
321
322 basic_bitset<Bits, etl::size_t> _bits;
323};
324
325} // namespace etl
326
327#endif // TETL_BITSET_BITSET_HPP
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