-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbit_mask.hpp
318 lines (272 loc) · 5.08 KB
/
bit_mask.hpp
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
// This file is covered by the LICENSE file in the root of this project.
#pragma once
#include <cassert>
#include <cstddef>
#include <string>
#include <type_traits>
class Bit_mask
{
public:
using Mask = unsigned long;
using Length = std::size_t;
public:
explicit Bit_mask(std::size_t length, Mask mask = 0ul) : mask_(mask), length_(length)
{
assert(length <= 8 * sizeof(Mask));
mask_visualization();
}
template<class Container, typename = typename std::enable_if<
std::is_same<typename Container::value_type, bool>::value>::type>
explicit Bit_mask(const Container& mask) : Bit_mask(mask.size())
{
Mask nth_bit = 1ul;
for (auto f : mask)
{
if (f)
mask_ |= nth_bit;
nth_bit <<= 1;
}
mask_visualization();
}
explicit Bit_mask(const std::string& mask) : Bit_mask(mask.length(), std::stoul(mask, 0, 2))
{}
Bit_mask(const Bit_mask& other) : Bit_mask(other.length_, other.mask_)
{}
Bit_mask& operator=(const Bit_mask& other)
{
assert(length_ == other.length_);
mask_ = other.mask_;
mask_visualization();
return *this;
};
operator Mask() const
{
return mask_;
}
bool operator[](Length i) const
{
assert(i < length_);
return mask_ & ith_bit(i);
}
bool all() const
{
return mask_ == all_bits(length_);
}
bool any() const
{
return mask_ != 0;
}
bool none() const
{
return !any();
}
Bit_mask& set(Length i)
{
assert(i < length_);
mask_ |= ith_bit(i);
mask_visualization();
return *this;
}
Bit_mask& set()
{
mask_ = all_bits(length_);
mask_visualization();
return *this;
}
Bit_mask& reset(Length i)
{
assert(i < length_);
mask_ &= ~ith_bit(i);
mask_visualization();
return *this;
}
Bit_mask& reset()
{
mask_ = 0;
mask_visualization();
return *this;
}
Bit_mask& flip(Length i)
{
assert(i < length_);
mask_ ^= ith_bit(i);
mask_visualization();
return *this;
}
Bit_mask& flip()
{
mask_ ^= all_bits(length_);
mask_visualization();
return *this;
}
Bit_mask with_set(Length i) const
{
assert(i < length_);
auto tmp(*this);
tmp.mask_ |= ith_bit(i);
return tmp;
}
Bit_mask with_reset(Length i) const
{
assert(i < length_);
auto tmp(*this);
tmp.mask_ &= ~ith_bit(i);
return tmp;
}
Bit_mask with_flipped(Length i) const
{
assert(i < length_);
auto tmp(*this);
tmp.mask_ ^= ith_bit(i);
return tmp;
}
#ifdef __GNUG__
Length count() const
{
return static_cast<Length>(__builtin_popcountl(mask_));
}
#else
Length count() const
{
Length n = 0;
auto mask = mask_;
while (mask != 0)
{
mask &= mask - 1; // Reset the right-most bit
++n;
}
return n;
}
#endif
Bit_mask& xor_at_pos(Length pos, const Bit_mask& other)
{
assert(pos + other.length_ <= length_);
mask_ ^= (other.mask_ << pos);
mask_visualization();
return *this;
}
void truncate(Length new_length)
{
length_ = new_length;
mask_ &= all_bits(new_length);
mask_visualization();
}
Length length() const
{
return length_;
}
Mask size() const
{
return 1ul << length_;
}
std::string to_string() const
{
std::string s(length_, '0');
for (Length i = 0; i < length_; ++i)
if ((*this)[i])
s[length_ - i - 1] = '1';
return s;
}
Bit_mask operator<<(Length shift) const
{
auto tmp(*this);
tmp.mask_ <<= shift;
return tmp;
}
Bit_mask& operator<<=(Length shift)
{
mask_ <<= shift;
mask_visualization();
return *this;
}
Bit_mask operator>>(Length shift) const
{
auto tmp(*this);
tmp.mask_ >>= shift;
return tmp;
}
Bit_mask& operator>>=(Length shift)
{
mask_ >>= shift;
mask_visualization();
return *this;
}
Bit_mask operator&(const Bit_mask& other) const
{
assert(length_ == other.length_);
auto tmp(*this);
tmp.mask_ &= other.mask_;
return tmp;
}
Bit_mask operator|(const Bit_mask& other) const
{
assert(length_ == other.length_);
auto tmp(*this);
tmp.mask_ |= other.mask_;
return tmp;
}
Bit_mask operator^(const Bit_mask& other) const
{
assert(length_ == other.length_);
auto tmp(*this);
tmp.mask_ ^= other.mask_;
return tmp;
}
Bit_mask& operator++()
{
++mask_;
mask_visualization();
return *this;
}
Bit_mask& operator--()
{
--mask_;
mask_visualization();
return *this;
}
bool operator==(const Bit_mask& other) const
{
assert(length_ == other.length_);
return mask_ == other.mask_;
}
bool operator!=(const Bit_mask& other) const
{
return !(*this == other);
}
bool operator<(const Bit_mask& other) const
{
assert(length_ == other.length_);
return mask_ < other.mask_;
}
bool operator<=(const Bit_mask& other) const
{
assert(length_ == other.length_);
return mask_ <= other.mask_;
}
private:
static Mask ith_bit(Length i)
{
return 1ul << i;
}
static Mask all_bits(Length length)
{
if (length == 8 * sizeof(Mask))
return static_cast<Mask>(-1);
else
return ith_bit(length) - 1ul;
}
void mask_visualization()
{
#ifdef _DEBUG
mask_str_[length_] = 0;
for (Length i = 0; i < length_; ++i)
mask_str_[length_ - i - 1] = (*this)[i] ? '1' : '0';
#endif
}
private:
Mask mask_ = 0;
Length length_ = 0;
#ifdef _DEBUG
char mask_str_[8 * sizeof(Mask) + 1];
#endif
};