summaryrefslogtreecommitdiff
path: root/lib/bitset.cpp
blob: bbd802de56b8f564e296d9f591ee062778ac9373 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include "bitset.h"

namespace MyGrate {
	static_assert(std::forward_iterator<BitSet::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<const std::byte> 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()};
	}
}