-
Notifications
You must be signed in to change notification settings - Fork 10
/
bitset.hpp
403 lines (344 loc) · 9.85 KB
/
bitset.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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
/**
* Copyright 2020 Tom van Dijk
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef BITSET_HPP
#define BITSET_HPP
#include <libpopcnt.h>
namespace pg
{
static inline int bsr(uint64_t x)
{
return __builtin_clzll(x) ^ 63;
}
class bitset
{
public:
class reference
{
friend class bitset;
reference(uint64_t &b, unsigned int pos) : _block(b), _mask(uint64_t(1)<<pos) { }
void operator&(); // left undefined
public:
operator bool() const { return (_block & _mask) != 0; }
bool operator~() const { return (_block & _mask) == 0; }
reference& flip() { do_flip(); return *this; }
reference& operator=(bool x) { do_assign(x); return *this; } // for b[i] = x
reference& operator=(const reference& rhs) { do_assign(rhs); return *this; } // for b[i] = b[j]
reference& operator|=(bool x) { if (x) do_set(); return *this; }
reference& operator&=(bool x) { if (!x) do_reset(); return *this; }
reference& operator^=(bool x) { if (x) do_flip(); return *this; }
reference& operator-=(bool x) { if (x) do_reset(); return *this; }
private:
uint64_t &_block;
const uint64_t _mask;
void do_set() { _block |= _mask; }
void do_reset() { _block &= ~_mask; }
void do_flip() { _block ^= _mask; }
void do_assign(bool x) { x ? do_set() : do_reset(); }
};
bitset()
{
_size = 0;
_bitssize = 0;
_allocsize = 0;
_bits = NULL;
}
bitset(size_t newsize)
{
_size = newsize;
_bitssize = (_size+63)/64;
_allocsize = _bitssize * 8;
_bits = new uint64_t[_bitssize];
std::fill(_bits, _bits+_bitssize, '\0');
}
bitset(const bitset &other)
{
_size = other._size;
_bitssize = other._bitssize;
_allocsize = _bitssize * 8;
_bits = new uint64_t[_bitssize];
std::copy(other._bits, other._bits+_bitssize, _bits);
}
~bitset()
{
if (_bits != NULL) delete[] _bits;
}
/**
* After resizing the *new* bits _can_ be undefined.
*/
void resize(size_t newsize)
{
if (_allocsize == 0) {
bitset b(newsize);
swap(b);
} else if (newsize <= _allocsize*8) {
// fits in allocated array already
_size = newsize;
_bitssize = (newsize+63)/64;
zero_unused_bits(); // only zeroes the last used block...
} else {
bitset b(newsize);
std::copy(_bits, _bits+_bitssize, b._bits);
swap(b);
}
}
private:
inline size_t num_blocks(void) const { return _bitssize; }
inline size_t block_index(size_t pos) const { return pos / 64; }
inline size_t bit_index(size_t pos) const { return pos % 64; }
inline uint64_t bit_mask(size_t pos) const { return uint64_t(1) << bit_index(pos); }
inline size_t count_extra_bits(void) const { return _size % 64; }
inline void zero_unused_bits()
{
size_t extra = count_extra_bits();
if (extra != 0) _bits[num_blocks()-1] &= ((uint64_t(1) << extra) - 1);
}
public:
__attribute__((always_inline)) bitset& reset(void)
{
std::fill(_bits, _bits+num_blocks(), '\0');
return *this;
}
__attribute__((always_inline)) bitset& set(void)
{
std::fill(_bits, _bits+num_blocks(), static_cast<uint64_t>(~0));
zero_unused_bits();
return *this;
}
inline bitset& flip(void)
{
for (size_t i=0; i<num_blocks(); i++) _bits[i] = ~_bits[i];
zero_unused_bits();
return *this;
}
inline std::size_t size(void) const
{
return _size;
}
std::size_t count(void) const
{
return popcnt(_bits, num_blocks()*8);
}
inline bool any(void) const
{
uint64_t *p = _bits;
std::size_t len = num_blocks();
while (len-- != 0) if (*p++) return true;
return false;
}
inline bool none() const
{
return !any();
}
inline bool all() const
{
if (empty()) return true;
size_t extra = count_extra_bits();
if (extra == 0) {
uint64_t *p = _bits;
std::size_t len = num_blocks();
while (len-- != 0) if (*p++ != static_cast<uint64_t>(~0)) return false;
} else {
uint64_t *p = _bits;
std::size_t len = num_blocks()-1;
while (len-- != 0) if (*p++ != static_cast<uint64_t>(~0)) return false;
const uint64_t last_mask = (uint64_t(1)<<extra)-1;
if (*p != last_mask) return false;
}
return true;
}
inline bool empty() const
{
return _size == 0;
}
bitset operator~() const
{
bitset b(*this);
b.flip();
return b;
}
inline void reset(size_t pos)
{
_bits[block_index(pos)] &= ~bit_mask(pos);
}
inline void set(size_t pos)
{
_bits[block_index(pos)] |= bit_mask(pos);
}
inline bool test(size_t pos) const
{
return (_bits[block_index(pos)] & bit_mask(pos)) != 0;
}
reference operator[](size_t pos)
{
return reference(_bits[block_index(pos)], bit_index(pos));
}
inline bool operator[](size_t pos) const
{
return test(pos);
}
bitset& operator=(const bitset &src)
{
bitset b(src);
swap(b);
return *this;
}
bitset& operator-=(const bitset& other)
{
uint64_t *p = _bits;
const uint64_t *q = other._bits;
std::size_t len = num_blocks();
while (len-- != 0) (*p++) &= ~(*q++);
return *this;
}
bitset& operator&=(const bitset& other)
{
uint64_t *p = _bits;
const uint64_t *q = other._bits;
std::size_t len = num_blocks();
while (len-- != 0) (*p++) &= (*q++);
return *this;
}
bitset& operator|=(const bitset &other)
{
uint64_t *p = _bits;
const uint64_t *q = other._bits;
std::size_t len = num_blocks();
while (len-- != 0) (*p++) |= (*q++);
return *this;
}
bitset& operator^=(const bitset &other)
{
uint64_t *p = _bits;
const uint64_t *q = other._bits;
std::size_t len = num_blocks();
while (len-- != 0) (*p++) ^= (*q++);
return *this;
}
bool operator==(const bitset &other) const
{
const uint64_t *p = _bits;
const uint64_t *q = other._bits;
std::size_t len = num_blocks();
while (len-- != 0) if ((*p++) != (*q++)) return false;
return true;
}
bool operator!=(const bitset &other) const
{
return !(*this == other);
}
inline void swap(bitset &other)
{
std::swap(_size, other._size);
std::swap(_bitssize, other._bitssize);
std::swap(_allocsize, other._allocsize);
std::swap(_bits, other._bits);
}
bool intersects(const bitset& other) const
{
const uint64_t *p = _bits;
const uint64_t *q = other._bits;
std::size_t len = num_blocks();
while (len-- != 0) if ((*p++) & (*q++)) return true;
return false;
}
size_t find_first() const
{
size_t i = 0;
while (i < num_blocks() and _bits[i] == 0) i++;
if (i == num_blocks()) return npos;
else return i*64 + __builtin_ffsll(_bits[i]) - 1;
}
size_t find_last() const
{
if (num_blocks() == 0) return npos;
size_t i = num_blocks()-1;
for (;;) {
if (_bits[i] != 0) return i*64 + bsr(_bits[i]);
if (i == 0) return npos;
i--;
}
}
size_t find_next(size_t pos) const
{
if (pos == npos or (pos+1) >= _size) return npos;
pos++;
size_t i = block_index(pos);
uint64_t m = _bits[i] & (~uint64_t(0) << bit_index(pos));
if (m) {
return i*64 + __builtin_ffsll(m) - 1;
} else {
i += 1;
while (i < num_blocks() and _bits[i] == 0) i++;
if (i == num_blocks()) return npos;
else return i*64 + __builtin_ffsll(_bits[i]) - 1;
}
}
size_t find_prev(size_t pos) const
{
if (pos == 0 or pos == npos) return npos;
size_t i = block_index(pos);
uint64_t m = _bits[i] & ~((~uint64_t(0)) << (int)bit_index(pos));
if (m) {
return i*64 + bsr(m);
} else {
if (i == 0) return npos;
i -= 1;
for (;;) {
if (_bits[i] != 0) return i*64 + bsr(_bits[i]);
if (i == 0) return npos;
i--;
}
}
}
static const size_t npos = static_cast<size_t>(-1);
protected:
uint64_t *_bits;
size_t _size, _bitssize;
size_t _allocsize;
};
inline bitset operator^(const bitset& x, const bitset& y)
{
bitset b(x);
return b ^= y;
}
inline bitset operator-(const bitset& x, const bitset& y)
{
bitset b(x);
return b -= y;
}
inline bitset operator|(const bitset& x, const bitset &y)
{
bitset b(x);
return b |= y;
}
inline bitset operator&(const bitset& x, const bitset &y)
{
bitset b(x);
return b &= y;
}
inline void swap(bitset &left, bitset &right)
{
left.swap(right);
}
}
namespace std
{
inline void swap(pg::bitset &left, pg::bitset &right)
{
left.swap(right);
}
}
#endif