From fcdca58617caf6a8c034a91588d6abb399be6b57 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Tue, 18 May 2021 00:06:37 +0100 Subject: Initial commit, still lots to do! --- lib/bitset.cpp | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 lib/bitset.cpp (limited to 'lib/bitset.cpp') diff --git a/lib/bitset.cpp b/lib/bitset.cpp new file mode 100644 index 0000000..bbd802d --- /dev/null +++ b/lib/bitset.cpp @@ -0,0 +1,66 @@ +#include "bitset.h" + +namespace MyGrate { + static_assert(std::forward_iterator); + + BitSet::Iterator::Iterator(const std::byte * bb) : b {bb} { } + + bool + BitSet::Iterator::operator*() const + { + return (*b & m) != std::byte {}; + } + + bool + BitSet::Iterator::operator==(const BitSet::Iterator & o) const + { + return b == o.b && m == o.m; + } + + bool + BitSet::Iterator::operator!=(const BitSet::Iterator & o) const + { + return b != o.b || m != o.m; + } + + BitSet::Iterator & + BitSet::Iterator::operator++() + { + if (m == std::byte {128}) { + b++; + m = std::byte {1}; + } + else { + m <<= 1; + } + return *this; + } + + BitSet::Iterator + BitSet::Iterator::operator++(int) + { + auto pre {*this}; + ++*this; + return pre; + } + + BitSet::BitSet(const std::span bs) : bytes {bs} { } + + std::size_t + BitSet::size() const + { + return bytes.size() * 8; + } + + BitSet::Iterator + BitSet::begin() const + { + return BitSet::Iterator {bytes.data()}; + } + + BitSet::Iterator + BitSet::end() const + { + return BitSet::Iterator {bytes.data() + bytes.size()}; + } +} -- cgit v1.2.3