-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbit_lens.h
225 lines (181 loc) · 6.73 KB
/
bit_lens.h
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#pragma once
#include <climits>
#include <cstddef>
#include <iterator>
#include <type_traits>
#include <utility>
namespace bit_lens {
template <class Word> struct WordSize {
static constexpr size_t value = sizeof(Word) * CHAR_BIT;
};
/*
* A reference for a single bit
*/
template <class Word> class BitReference {
private:
size_t offset;
Word &value;
public:
constexpr BitReference(Word &v, size_t o) noexcept : offset(o), value(v) {
static_assert(std::is_integral<Word>::value && std::is_unsigned<Word>::value,
"only unsigned integer value types are supported");
}
constexpr operator bool() const noexcept { return (value >> offset) & Word(1); }
BitReference &operator=(bool v) noexcept {
value = (value & ~(Word(1) << offset)) | (Word(v) << offset);
return *this;
}
BitReference &operator=(const BitReference &v) noexcept { return *this = bool(v); }
};
/**
* Swaps only the referenced bit
*/
template <class Word> void swap(BitReference<Word> &a, BitReference<Word> &b) {
bool tmp = a;
a = b;
b = tmp;
}
template <class ContainerIterator> class BitIterator {
private:
using Word = typename std::remove_reference<decltype(*std::declval<ContainerIterator>())>::type;
static constexpr auto WORD_SIZE = WordSize<Word>::value;
ContainerIterator iterator;
size_t index;
public:
using iterator_category = std::random_access_iterator_tag;
using value_type = BitReference<Word>;
using difference_type = size_t;
using reference = value_type;
constexpr BitIterator(ContainerIterator i, size_t o) noexcept : iterator(i), index(o) {}
BitIterator &operator++(int) noexcept {
BitIterator copy(*this);
index++;
return copy;
}
BitIterator operator++() noexcept {
++index;
return *this;
}
BitIterator &operator--(int) noexcept {
BitIterator copy(*this);
index--;
return copy;
}
BitIterator operator--() noexcept {
--index;
return *this;
}
BitIterator operator+(int n) const noexcept { return BitIterator(iterator, index + n); }
BitIterator operator-(int n) const noexcept { return BitIterator(iterator, index - n); }
size_t operator-(const BitIterator &other) const noexcept { return index - other.index; }
constexpr bool operator!=(const BitIterator &other) const {
return iterator != other.iterator || index != other.index;
}
constexpr bool operator==(const BitIterator &other) const {
return iterator == other.iterator && index == other.index;
}
constexpr BitReference<Word> operator*() const noexcept {
return BitReference<Word>(*(iterator + index / WORD_SIZE), index % WORD_SIZE);
}
};
template <class Container> class BitLens {
private:
Container &container;
public:
using Word = typename std::remove_reference<decltype(std::declval<Container>()[0])>::type;
static constexpr auto WORD_SIZE = WordSize<Word>::value;
constexpr BitLens(Container &c) noexcept : container(c) {}
/**
* returns the number of bits that fit in the container
*/
size_t size() const noexcept(noexcept(container.size())) {
return container.size() * WORD_SIZE;
}
/**
* resizes the container to hold at least `N` bits.
* Note that the actual size will be a multiple of the bits in a word.
*/
void resize(size_t N, bool v = false) noexcept(noexcept(container.resize(0, 0))) {
Word offset = N % WORD_SIZE == 0 ? 0 : 1;
container.resize(N / WORD_SIZE + offset, v ? ~0 : 0);
}
[[deprecated("legacy API: use `resize(i,v)` insted")]] void resizeToHold(size_t N,
bool v = false) {
Word offset = N % WORD_SIZE == 0 ? 0 : 1;
container.resize(N / WORD_SIZE + offset, v ? ~0 : 0);
}
/**
* returns a BitReference to the bit at position `i`.
*/
constexpr BitReference<Word> operator[](size_t i) noexcept(noexcept(container[0])) {
Word offset = i % WORD_SIZE;
return BitReference<Word>(container[i / WORD_SIZE], offset);
}
/**
* returns a BitReference to the bit at position `i`.
*/
constexpr BitReference<const Word> operator[](size_t i) const noexcept(noexcept(container[0])) {
Word offset = i % WORD_SIZE;
return BitReference<const Word>(container[i / WORD_SIZE], offset);
}
[[deprecated("legacy API: use `[i]` insted")]] bool get(size_t i) const
noexcept(noexcept(std::declval<BitLens<Container>>()[i])) {
return (*this)[i];
}
[[deprecated("legacy API: use `[i] = v` insted")]] void set(size_t i, bool v) noexcept(
noexcept(std::declval<BitLens<Container>>()[i])) {
(*this)[i] = v;
}
constexpr auto begin() noexcept(noexcept(container.begin())) {
return BitIterator(container.begin(), 0);
}
constexpr auto end() noexcept(noexcept(container.begin())) {
return BitIterator(container.begin(), size());
}
constexpr auto begin() const noexcept(noexcept(container.begin())) {
return BitIterator(container.begin(), 0);
}
constexpr auto end() const noexcept(noexcept(container.begin())) {
return BitIterator(container.begin(), size());
}
template <class F>[[deprecated("legacy API: use iterators instead")]] void forEach(F &&f) {
size_t N = size();
for (size_t idx = 0; idx != N; ++idx) {
f((*this)[idx], idx);
}
}
/**
* get the underlying data container
*/
Container &data() noexcept { return container; }
/**
* get the underlying data container
*/
const Container &data() const noexcept { return container; }
};
/**
* Legacy API
* @deprecated Use BitLens instead
*/
template <class BitContainer> using Lens = BitLens<BitContainer>;
/**
* creates a bitlens that owns its data
*/
template <class C> class BitContainer : public BitLens<C> {
private:
C dataContainer;
public:
constexpr BitContainer() noexcept(noexcept(C())) : BitLens<C>(dataContainer) {}
constexpr BitContainer(const BitContainer &other) noexcept(
noexcept(C(std::declval<const C &>())))
: dataContainer(other.data()), BitLens<C>(dataContainer) {}
constexpr BitContainer(BitContainer &&other) noexcept(noexcept(C(std::declval<C &&>())))
: dataContainer(std::move(other.data())), BitLens<C>(dataContainer) {}
template <typename... Args> BitContainer(Args &&... args)
: dataContainer(std::forward<Args...>(args...)), BitLens<C>(dataContainer) {}
};
} // namespace bit_lens
namespace std {
template <class T> struct iterator_traits<bit_lens::BitIterator<T>>
: public bit_lens::BitIterator<T> {};
} // namespace std