diff options
Diffstat (limited to 'include/util/bitset.hpp')
| -rw-r--r-- | include/util/bitset.hpp | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/include/util/bitset.hpp b/include/util/bitset.hpp index de5bd49..cd1dbd0 100644 --- a/include/util/bitset.hpp +++ b/include/util/bitset.hpp @@ -3,6 +3,7 @@ #include <array> #include <cassert> #include <cstdint> +#include <vector> constexpr int bits_ceil(int bits) { @@ -50,4 +51,48 @@ public: assert(i < flags); return bit_ref(*this, i / 8, (1 << (i % 8))); } +}; + +// FIXME: ?? +template <> +struct bitset<0> : std::vector<std::uint8_t> { + using container_type = std::vector<std::uint8_t>; + +private: + struct bit_ref { + bitset<0>& 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 < container_type::size()); + return (container_type::at(i / 8) & (1 << (i % 8))) > 0; + } + + template <class T> + bit_ref operator[](T idx) + { + unsigned int i = (unsigned int)(idx); + assert(i < container_type::size()); + return bit_ref(*this, i / 8, (1 << (i % 8))); + } };
\ No newline at end of file |
