summaryrefslogtreecommitdiff
path: root/include/util
diff options
context:
space:
mode:
Diffstat (limited to 'include/util')
-rw-r--r--include/util/bitset.hpp71
1 files changed, 38 insertions, 33 deletions
diff --git a/include/util/bitset.hpp b/include/util/bitset.hpp
index 8798a4a..de5bd49 100644
--- a/include/util/bitset.hpp
+++ b/include/util/bitset.hpp
@@ -4,45 +4,50 @@
#include <cassert>
#include <cstdint>
-constexpr int bits_ceil(int bits) {
- return bits / 8 + (bits % 8 > 0 ? 1 : 0);
+constexpr int bits_ceil(int bits)
+{
+ return bits / 8 + (bits % 8 > 0 ? 1 : 0);
}
template <unsigned int flags, auto size = bits_ceil(flags)>
struct bitset : std::array<std::uint8_t, size> {
- using array_type = std::array<std::uint8_t, size>;
+ using array_type = std::array<std::uint8_t, size>;
private:
- struct bit_ref {
- bitset<flags> &a;
- int referenced_byte;
- uint8_t referenced_bit_mask;
-
- bool operator=(bool b) {
- if (b)
- a.at(referenced_byte) |= referenced_bit_mask;
- else
- a.at(referenced_byte) &= ~referenced_bit_mask;
- return b;
- }
-
- constexpr operator bool() const {
- return (a.at(referenced_byte) & referenced_bit_mask) > 0;
- }
- };
+ struct bit_ref {
+ bitset<flags>& a;
+ int referenced_byte;
+ uint8_t referenced_bit_mask;
+
+ bool operator=(bool b)
+ {
+ if (b)
+ a.at(referenced_byte) |= referenced_bit_mask;
+ else
+ a.at(referenced_byte) &= ~referenced_bit_mask;
+ return b;
+ }
+
+ constexpr operator bool() const
+ {
+ return (a.at(referenced_byte) & referenced_bit_mask) > 0;
+ }
+ };
public:
- template<class T>
- constexpr bool operator [](T idx) const {
- unsigned int i = (unsigned int)(idx);
- assert(i < flags);
- return (array_type::at(i/8) & (1 << (i%8))) > 0;
- }
-
- template<class T>
- bit_ref operator[](T idx) {
- unsigned int i = (unsigned int)(idx);
- assert(i < flags);
- return bit_ref(*this, i/8, (1 << (i%8)));
- }
+ template <class T>
+ constexpr bool operator[](T idx) const
+ {
+ unsigned int i = (unsigned int)(idx);
+ assert(i < flags);
+ return (array_type::at(i / 8) & (1 << (i % 8))) > 0;
+ }
+
+ template <class T>
+ bit_ref operator[](T idx)
+ {
+ unsigned int i = (unsigned int)(idx);
+ assert(i < flags);
+ return bit_ref(*this, i / 8, (1 << (i % 8)));
+ }
}; \ No newline at end of file