-
Notifications
You must be signed in to change notification settings - Fork 0
/
boolarray.h
493 lines (438 loc) · 13.4 KB
/
boolarray.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
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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
/**
* This code is released under the
* Apache License Version 2.0 http://www.apache.org/licenses/.
*
* (c) Daniel Lemire, http://lemire.me/en/
*/
#ifndef BOOLARRAY_H
#define BOOLARRAY_H
#include <iso646.h> // mostly for Microsoft compilers
#include <stdarg.h>
#include <cassert>
#include <iostream>
#include <vector>
#include <stdexcept>
#include <sstream>
// uncomment this for debugging
//#define EWAHASSERT
/**
* A dynamic bitset implementation. (without compression).
*/
template <class uword = uint32_t> class BoolArray {
public:
BoolArray(const size_t n, const uword initval = 0)
: buffer(n / wordinbits + (n % wordinbits == 0 ? 0 : 1), initval),
sizeinbits(n) {}
BoolArray() : buffer(), sizeinbits(0) {}
BoolArray(const BoolArray &ba)
: buffer(ba.buffer), sizeinbits(ba.sizeinbits) {}
static BoolArray bitmapOf(size_t n, ...) {
BoolArray ans;
va_list vl;
va_start(vl, n);
for (size_t i = 0; i < n; i++) {
ans.set(static_cast<size_t>(va_arg(vl, int)));
}
va_end(vl);
return ans;
}
size_t sizeInBytes() const { return buffer.size() * sizeof(uword); }
void read(std::istream &in) {
sizeinbits = 0;
in.read(reinterpret_cast<char *>(&sizeinbits), sizeof(sizeinbits));
buffer.resize(sizeinbits / wordinbits +
(sizeinbits % wordinbits == 0 ? 0 : 1));
if (buffer.size() == 0)
return;
in.read(reinterpret_cast<char *>(&buffer[0]),
static_cast<std::streamsize>(buffer.size() * sizeof(uword)));
}
void readBuffer(std::istream &in, const size_t size) {
buffer.resize(size);
sizeinbits = size * sizeof(uword) * 8;
if (buffer.empty())
return;
in.read(reinterpret_cast<char *>(&buffer[0]),
buffer.size() * sizeof(uword));
}
void setSizeInBits(const size_t sizeib) { sizeinbits = sizeib; }
void write(std::ostream &out) { write(out, sizeinbits); }
void write(std::ostream &out, const size_t numberofbits) const {
const size_t size =
numberofbits / wordinbits + (numberofbits % wordinbits == 0 ? 0 : 1);
out.write(reinterpret_cast<const char *>(&numberofbits),
sizeof(numberofbits));
if (numberofbits == 0)
return;
out.write(reinterpret_cast<const char *>(&buffer[0]),
static_cast<std::streamsize>(size * sizeof(uword)));
}
void writeBuffer(std::ostream &out, const size_t numberofbits) const {
const size_t size =
numberofbits / wordinbits + (numberofbits % wordinbits == 0 ? 0 : 1);
if (size == 0)
return;
#ifdef EWAHASSERT
assert(buffer.size() >= size);
#endif
out.write(reinterpret_cast<const char *>(&buffer[0]), size * sizeof(uword));
}
size_t sizeOnDisk() const {
size_t size =
sizeinbits / wordinbits + (sizeinbits % wordinbits == 0 ? 0 : 1);
return sizeof(sizeinbits) + size * sizeof(uword);
}
BoolArray &operator=(const BoolArray &x) {
this->buffer = x.buffer;
this->sizeinbits = x.sizeinbits;
return *this;
}
bool operator==(const BoolArray &x) const {
if (sizeinbits != x.sizeinbits)
return false;
for (size_t k = 0; k < buffer.size(); ++k)
if (buffer[k] != x.buffer[k])
return false;
return true;
}
bool operator!=(const BoolArray &x) const { return !operator==(x); }
void setWord(const size_t pos, const uword val) {
#ifdef EWAHASSERT
assert(pos < buffer.size());
#endif
buffer[pos] = val;
}
void addWord(const uword val) {
if (sizeinbits % wordinbits != 0)
throw std::invalid_argument("you probably didn't want to do this");
sizeinbits += wordinbits;
buffer.push_back(val);
}
uword getWord(const size_t pos) const {
#ifdef EWAHASSERT
assert(pos < buffer.size());
#endif
return buffer[pos];
}
/**
* set to true (whether it was already set to true or not)
*
* This is an expensive (random access) API, you really ought to
* prepare a new word and then append it.
*/
void set(const size_t pos) {
if (pos >= sizeinbits)
padWithZeroes(pos + 1);
buffer[pos / wordinbits] |= (static_cast<uword>(1) << (pos % wordinbits));
}
/**
* set to false (whether it was already set to false or not)
*
* This is an expensive (random access) API, you really ought to
* prepare a new word and then append it.
*/
void unset(const size_t pos) {
if (pos < sizeinbits)
buffer[pos / wordinbits] |=
~(static_cast<uword>(1) << (pos % wordinbits));
}
/**
* true of false? (set or unset)
*/
bool get(const size_t pos) const {
#ifdef EWAHASSERT
assert(pos / wordinbits < buffer.size());
#endif
return (buffer[pos / wordinbits] &
(static_cast<uword>(1) << (pos % wordinbits))) != 0;
}
/**
* set all bits to 0
*/
void reset() {
if (buffer.size() > 0)
memset(&buffer[0], 0, sizeof(uword) * buffer.size());
sizeinbits = 0;
}
size_t sizeInBits() const { return sizeinbits; }
~BoolArray() {}
/**
* Computes the logical and and writes to the provided BoolArray (out).
* The current bitmaps is unchanged.
*/
void logicaland(const BoolArray &ba, BoolArray &out) const {
if (ba.buffer.size() < buffer.size())
out.setToSize(ba);
else
out.setToSize(*this);
for (size_t i = 0; i < out.buffer.size(); ++i)
out.buffer[i] = buffer[i] & ba.buffer[i];
}
/**
* Computes the logical and and return the result.
* The current bitmaps is unchanged.
*/
BoolArray logicaland(const BoolArray &a) const {
BoolArray answer;
logicaland(a, answer);
return answer;
}
void inplace_logicaland(const BoolArray &ba) {
if (ba.buffer.size() < buffer.size())
setToSize(ba);
for (size_t i = 0; i < buffer.size(); ++i)
buffer[i] = buffer[i] & ba.buffer[i];
}
/**
* Computes the logical andnot and writes to the provided BoolArray (out).
* The current bitmaps is unchanged.
*/
void logicalandnot(const BoolArray &ba, BoolArray &out) const {
out.setToSize(*this);
size_t upto = out.buffer.size() < ba.buffer.size() ? out.buffer.size()
: ba.buffer.size();
for (size_t i = 0; i < upto; ++i)
out.buffer[i] = buffer[i] & (~ba.buffer[i]);
for (size_t i = upto; i < out.buffer.size(); ++i)
out.buffer[i] = buffer[i];
out.clearBogusBits();
}
/**
* Computes the logical andnot and return the result.
* The current bitmaps is unchanged.
*/
BoolArray logicalandnot(const BoolArray &a) const {
BoolArray answer;
logicalandnot(a, answer);
return answer;
}
void inplace_logicalandnot(const BoolArray &ba) {
size_t upto =
buffer.size() < ba.buffer.size() ? buffer.size() : ba.buffer.size();
for (size_t i = 0; i < upto; ++i)
buffer[i] = buffer[i] & (~ba.buffer[i]);
clearBogusBits();
}
/**
* Computes the logical or and writes to the provided BoolArray (out).
* The current bitmaps is unchanged.
*/
void logicalor(const BoolArray &ba, BoolArray &out) const {
const BoolArray *smallest;
const BoolArray *largest;
if (ba.buffer.size() > buffer.size()) {
smallest = this;
largest = &ba;
out.setToSize(ba);
} else {
smallest = &ba;
largest = this;
out.setToSize(*this);
}
for (size_t i = 0; i < smallest->buffer.size(); ++i)
out.buffer[i] = buffer[i] | ba.buffer[i];
for (size_t i = smallest->buffer.size(); i < largest->buffer.size(); ++i)
out.buffer[i] = largest->buffer[i];
}
/**
* Computes the logical or and return the result.
* The current bitmaps is unchanged.
*/
BoolArray logicalor(const BoolArray &a) const {
BoolArray answer;
logicalor(a, answer);
return answer;
}
void inplace_logicalor(const BoolArray &ba) { logicalor(ba, *this); }
/**
* Computes the logical xor and writes to the provided BoolArray (out).
* The current bitmaps is unchanged.
*/
void logicalxor(const BoolArray &ba, BoolArray &out) const {
const BoolArray *smallest;
const BoolArray *largest;
if (ba.buffer.size() > buffer.size()) {
smallest = this;
largest = &ba;
out.setToSize(ba);
} else {
smallest = &ba;
largest = this;
out.setToSize(*this);
}
for (size_t i = 0; i < smallest->buffer.size(); ++i)
out.buffer[i] = buffer[i] ^ ba.buffer[i];
for (size_t i = smallest->buffer.size(); i < largest->buffer.size(); ++i)
out.buffer[i] = largest->buffer[i];
}
/**
* Computes the logical xor and return the result.
* The current bitmaps is unchanged.
*/
BoolArray logicalxor(const BoolArray &a) const {
BoolArray answer;
logicalxor(a, answer);
return answer;
}
void inplace_logicalxor(const BoolArray &ba) { logicalxor(ba, *this); }
/**
* Computes the logical not and writes to the provided BoolArray (out).
* The current bitmaps is unchanged.
*/
void logicalnot(BoolArray &out) const {
out.setToSize(*this);
for (size_t i = 0; i < buffer.size(); ++i)
out.buffer[i] = ~buffer[i];
out.clearBogusBits();
}
/**
* Computes the logical not and return the result.
* The current bitmaps is unchanged.
*/
BoolArray logicalandnot() const {
BoolArray answer;
logicalnot(answer);
return answer;
}
void inplace_logicalnot() {
for (size_t i = 0; i < buffer.size(); ++i)
buffer[i] = ~buffer[i];
clearBogusBits();
}
/**
* Returns the number of bits set to the value 1.
* The running time complexity is proportional to the
* size of the bitmap.
*
* This is sometimes called the cardinality.
*/
size_t numberOfOnes() const {
size_t count = 0;
for (size_t i = 0; i < buffer.size(); ++i) {
count += countOnes(buffer[i]);
}
return count;
}
inline void printout(std::ostream &o = std::cout) {
for (size_t k = 0; k < sizeinbits; ++k)
o << get(k) << " ";
o << std::endl;
}
/**
* Make sure the two bitmaps have the same size (padding with zeroes
* if necessary). It has constant running time complexity.
*/
void makeSameSize(BoolArray &a) {
if (a.sizeinbits < sizeinbits)
a.padWithZeroes(sizeinbits);
else if (sizeinbits < a.sizeinbits)
padWithZeroes(a.sizeinbits);
}
/**
* Make sure the current bitmap has the size of the provided bitmap.
*/
void setToSize(const BoolArray &a) {
sizeinbits = a.sizeinbits;
buffer.resize(a.buffer.size());
}
/**
* make sure the size of the array is totalbits bits by padding with zeroes.
* returns the number of words added (storage cost increase)
*/
size_t padWithZeroes(const size_t totalbits) {
size_t currentwordsize = (sizeinbits + wordinbits - 1) / wordinbits;
size_t neededwordsize = (totalbits + wordinbits - 1) / wordinbits;
#ifdef EWAHASSERT
assert(neededwordsize >= currentwordsize);
#endif
buffer.resize(neededwordsize);
sizeinbits = totalbits;
return static_cast<size_t>(neededwordsize - currentwordsize);
}
void append(const BoolArray &a);
enum { wordinbits = sizeof(uword) * 8 };
std::vector<size_t> toArray() const {
std::vector<size_t> ans;
for (size_t k = 0; k < buffer.size(); ++k) {
uword myword = buffer[k];
while (myword != 0) {
uint32_t ntz = numberOfTrailingZeros(myword);
ans.push_back(sizeof(uword) * 8 * k + ntz);
myword ^= (static_cast<uword>(1) << ntz);
}
}
return ans;
}
/**
* Transform into a string that presents a list of set bits.
* The running time is linear in the size of the bitmap.
*/
operator std::string() const {
std::stringstream ss;
ss << *this;
return ss.str();
}
friend std::ostream &operator<<(std::ostream &out, const BoolArray &a) {
std::vector<size_t> v = a.toArray();
out << "{";
for (std::vector<size_t>::const_iterator i = v.begin(); i != v.end();) {
out << *i;
++i;
if (i != v.end())
out << ",";
}
out << "}";
return out;
return (out << static_cast<std::string>(a));
}
private:
void clearBogusBits() {
if ((sizeinbits % wordinbits) != 0) {
const uword maskbogus =
(static_cast<uword>(1) << (sizeinbits % wordinbits)) - 1;
buffer[buffer.size() - 1] &= maskbogus;
}
}
std::vector<uword> buffer;
size_t sizeinbits;
};
/**
* computes the logical or (union) between "n" bitmaps (referenced by a
* pointer).
* The answer gets written out in container. This might be faster than calling
* logicalor n-1 times.
*/
template <class uword>
void fast_logicalor_tocontainer(size_t n, const BoolArray<uword> **inputs,
BoolArray<uword> &container) {
if (n == 0) {
container.reset();
return;
}
container = *inputs[0];
for (size_t i = 0; i < n; i++) {
container.inplace_logicalor(*inputs[i]);
}
}
/**
* computes the logical or (union) between "n" bitmaps (referenced by a
* pointer).
* Returns the answer. This might be faster than calling
* logicalor n-1 times.
*/
template <class uword>
BoolArray<uword> fast_logicalor(size_t n, const BoolArray<uword> **inputs) {
BoolArray<uword> answer;
fast_logicalor_tocontainer(n, inputs, answer);
return answer;
}
template <class uword> void BoolArray<uword>::append(const BoolArray &a) {
if (sizeinbits % wordinbits == 0) {
buffer.insert(buffer.end(), a.buffer.begin(), a.buffer.end());
} else {
throw std::invalid_argument(
"Cannot append if parent does not meet boundary");
}
sizeinbits += a.sizeinbits;
}
#endif