21 BoolArray(
const size_t n,
const uword initval = 0)
22 : buffer(n / wordinbits + (n % wordinbits == 0 ? 0 : 1), initval),
28 : buffer(ba.buffer), sizeinbits(ba.sizeinbits) {}
29 static BoolArray bitmapOf(
size_t n, ...) {
33 for (
size_t i = 0; i < n; i++) {
34 ans.
set(
static_cast<size_t>(va_arg(vl,
int)));
39 size_t sizeInBytes()
const {
return buffer.size() *
sizeof(uword); }
41 void read(std::istream &in) {
43 in.read(
reinterpret_cast<char *
>(&sizeinbits),
sizeof(sizeinbits));
44 buffer.resize(sizeinbits / wordinbits +
45 (sizeinbits % wordinbits == 0 ? 0 : 1));
46 if (buffer.size() == 0)
48 in.read(
reinterpret_cast<char *
>(&buffer[0]),
49 static_cast<std::streamsize
>(buffer.size() *
sizeof(uword)));
52 void readBuffer(std::istream &in,
const size_t size) {
54 sizeinbits = size *
sizeof(uword) * 8;
57 in.read(
reinterpret_cast<char *
>(&buffer[0]),
58 buffer.size() *
sizeof(uword));
61 void setSizeInBits(
const size_t sizeib) { sizeinbits = sizeib; }
63 void write(std::ostream &out) { write(out, sizeinbits); }
65 void write(std::ostream &out,
const size_t numberofbits)
const {
67 numberofbits / wordinbits + (numberofbits % wordinbits == 0 ? 0 : 1);
68 out.write(
reinterpret_cast<const char *
>(&numberofbits),
69 sizeof(numberofbits));
70 if (numberofbits == 0)
72 out.write(
reinterpret_cast<const char *
>(&buffer[0]),
73 static_cast<std::streamsize
>(size *
sizeof(uword)));
76 void writeBuffer(std::ostream &out,
const size_t numberofbits)
const {
78 numberofbits / wordinbits + (numberofbits % wordinbits == 0 ? 0 : 1);
82 assert(buffer.size() >= size);
84 out.write(
reinterpret_cast<const char *
>(&buffer[0]), size *
sizeof(uword));
87 size_t sizeOnDisk()
const {
89 sizeinbits / wordinbits + (sizeinbits % wordinbits == 0 ? 0 : 1);
90 return sizeof(sizeinbits) + size *
sizeof(uword);
94 this->buffer = x.buffer;
95 this->sizeinbits = x.sizeinbits;
99 bool operator==(
const BoolArray &x)
const {
100 if (sizeinbits != x.sizeinbits)
102 for (
size_t k = 0; k < buffer.size(); ++k)
103 if (buffer[k] != x.buffer[k])
108 bool operator!=(
const BoolArray &x)
const {
return !operator==(x); }
110 void setWord(
const size_t pos,
const uword val) {
112 assert(pos < buffer.size());
117 void addWord(
const uword val) {
118 if (sizeinbits % wordinbits != 0)
119 throw std::invalid_argument(
"you probably didn't want to do this");
120 sizeinbits += wordinbits;
121 buffer.push_back(val);
124 uword getWord(
const size_t pos)
const {
126 assert(pos < buffer.size());
134 void set(
const size_t pos) {
135 if (pos >= sizeinbits)
137 buffer[pos / wordinbits] |=
138 static_cast<uword
>((
static_cast<uword
>(1) << (pos % wordinbits)));
146 if (pos < sizeinbits)
147 buffer[pos / wordinbits] &=
148 ~(
static_cast<uword
>(1) << (pos % wordinbits));
154 bool get(
const size_t pos)
const {
156 assert(pos / wordinbits < buffer.size());
158 return (buffer[pos / wordinbits] &
159 (
static_cast<uword
>(1) << (pos % wordinbits))) != 0;
166 if (buffer.size() > 0)
167 memset(&buffer[0], 0,
sizeof(uword) * buffer.size());
171 size_t sizeInBits()
const {
return sizeinbits; }
180 if (ba.buffer.size() < buffer.size())
184 for (
size_t i = 0; i < out.buffer.size(); ++i)
185 out.buffer[i] = buffer[i] & ba.buffer[i];
198 void inplace_logicaland(
const BoolArray &ba) {
199 if (ba.buffer.size() < buffer.size())
201 for (
size_t i = 0; i < buffer.size(); ++i)
202 buffer[i] = buffer[i] & ba.buffer[i];
211 size_t upto = out.buffer.size() < ba.buffer.size() ? out.buffer.size()
213 for (
size_t i = 0; i < upto; ++i)
214 out.buffer[i] =
static_cast<uword
>(buffer[i] & (~ba.buffer[i]));
215 for (
size_t i = upto; i < out.buffer.size(); ++i)
216 out.buffer[i] = buffer[i];
217 out.clearBogusBits();
230 void inplace_logicalandnot(
const BoolArray &ba) {
232 buffer.size() < ba.buffer.size() ? buffer.size() : ba.buffer.size();
233 for (
size_t i = 0; i < upto; ++i)
234 buffer[i] = buffer[i] & (~ba.buffer[i]);
245 if (ba.buffer.size() > buffer.size()) {
254 for (
size_t i = 0; i < smallest->buffer.size(); ++i)
255 out.buffer[i] = buffer[i] | ba.buffer[i];
256 for (
size_t i = smallest->buffer.size(); i < largest->buffer.size(); ++i)
257 out.buffer[i] = largest->buffer[i];
279 if (ba.buffer.size() > buffer.size()) {
288 for (
size_t i = 0; i < smallest->buffer.size(); ++i)
289 out.buffer[i] = buffer[i] ^ ba.buffer[i];
290 for (
size_t i = smallest->buffer.size(); i < largest->buffer.size(); ++i)
291 out.buffer[i] = largest->buffer[i];
312 for (
size_t i = 0; i < buffer.size(); ++i)
313 out.buffer[i] = ~buffer[i];
314 out.clearBogusBits();
327 void inplace_logicalnot() {
328 for (
size_t i = 0; i < buffer.size(); ++i)
329 buffer[i] = ~buffer[i];
342 for (
size_t i = 0; i < buffer.size(); ++i) {
343 count += countOnes(buffer[i]);
348 inline void printout(std::ostream &o = std::cout) {
349 for (
size_t k = 0; k < sizeinbits; ++k)
359 if (a.sizeinbits < sizeinbits)
361 else if (sizeinbits < a.sizeinbits)
368 sizeinbits = a.sizeinbits;
369 buffer.resize(a.buffer.size());
377 size_t currentwordsize = (sizeinbits + wordinbits - 1) / wordinbits;
378 size_t neededwordsize = (totalbits + wordinbits - 1) / wordinbits;
380 assert(neededwordsize >= currentwordsize);
382 buffer.resize(neededwordsize);
383 sizeinbits = totalbits;
384 return static_cast<size_t>(neededwordsize - currentwordsize);
389 enum { wordinbits =
sizeof(uword) * 8 };
391 std::vector<size_t> toArray()
const {
392 std::vector<size_t> ans;
393 for (
size_t k = 0; k < buffer.size(); ++k) {
394 uword myword = buffer[k];
395 while (myword != 0) {
396 uint32_t ntz = numberOfTrailingZeros(myword);
397 ans.push_back(
sizeof(uword) * 8 * k + ntz);
398 myword ^= (
static_cast<uword
>(1) << ntz);
408 operator std::string()
const {
409 std::stringstream ss;
414 friend std::ostream &operator<<(std::ostream &out,
const BoolArray &a) {
415 std::vector<size_t> v = a.toArray();
417 for (std::vector<size_t>::const_iterator i = v.begin(); i != v.end();) {
426 return (out <<
static_cast<std::string
>(a));
430 void clearBogusBits() {
431 if ((sizeinbits % wordinbits) != 0) {
432 const uword maskbogus =
static_cast<uword
>(
433 (
static_cast<uword
>(1) << (sizeinbits % wordinbits)) - 1);
434 buffer[buffer.size() - 1] &= maskbogus;
438 std::vector<uword> buffer;
448template <
class uword>
449void fast_logicalor_tocontainer(
size_t n,
const BoolArray<uword> **inputs,
450 BoolArray<uword> &container) {
455 container = *inputs[0];
456 for (
size_t i = 0; i < n; i++) {
457 container.inplace_logicalor(*inputs[i]);
467template <
class uword>
468BoolArray<uword> fast_logicalor(
size_t n,
const BoolArray<uword> **inputs) {
469 BoolArray<uword> answer;
470 fast_logicalor_tocontainer(n, inputs, answer);
474template <
class uword>
void BoolArray<uword>::append(
const BoolArray &a) {
475 if (sizeinbits % wordinbits == 0) {
476 buffer.insert(buffer.end(), a.buffer.begin(), a.buffer.end());
478 throw std::invalid_argument(
479 "Cannot append if parent does not meet boundary");
481 sizeinbits += a.sizeinbits;
A dynamic bitset implementation.
BoolArray logicaland(const BoolArray &a) const
Computes the logical and and return the result.
bool get(const size_t pos) const
true of false? (set or unset)
void logicalnot(BoolArray &out) const
Computes the logical not and writes to the provided BoolArray (out).
void unset(const size_t pos)
set to false (whether it was already set to false or not)
size_t padWithZeroes(const size_t totalbits)
make sure the size of the array is totalbits bits by padding with zeroes.
BoolArray logicalxor(const BoolArray &a) const
Computes the logical xor and return the result.
BoolArray logicalor(const BoolArray &a) const
Computes the logical or and return the result.
void logicalxor(const BoolArray &ba, BoolArray &out) const
Computes the logical xor and writes to the provided BoolArray (out).
void logicalor(const BoolArray &ba, BoolArray &out) const
Computes the logical or and writes to the provided BoolArray (out).
void logicaland(const BoolArray &ba, BoolArray &out) const
Computes the logical and and writes to the provided BoolArray (out).
size_t numberOfOnes() const
Returns the number of bits set to the value 1.
void setToSize(const BoolArray &a)
Make sure the current bitmap has the size of the provided bitmap.
BoolArray logicalandnot(const BoolArray &a) const
Computes the logical andnot and return the result.
void set(const size_t pos)
set to true (whether it was already set to true or not)
void makeSameSize(BoolArray &a)
Make sure the two bitmaps have the same size (padding with zeroes if necessary).
void reset()
set all bits to 0
BoolArray logicalandnot() const
Computes the logical not and return the result.
void logicalandnot(const BoolArray &ba, BoolArray &out) const
Computes the logical andnot and writes to the provided BoolArray (out).