6TEST(BitVectorTest, InitToZero) {
9 for (uint_fast64_t i = 0; i < 32; ++i) {
10 ASSERT_FALSE(vector.
get(i));
13 ASSERT_TRUE(vector.
empty());
14 ASSERT_FALSE(vector.
full());
17TEST(BitVectorTest, InitToOne) {
20 for (uint_fast64_t i = 0; i < 32; ++i) {
21 ASSERT_TRUE(vector.
get(i));
23 ASSERT_FALSE(vector.
empty());
24 ASSERT_TRUE(vector.
full());
27TEST(BitVectorTest, InitFromIterator) {
28 std::vector<uint_fast64_t> valueVector = {0, 4, 10};
31 ASSERT_EQ(32ul, vector.
size());
33 for (uint_fast64_t i = 0; i < 32; ++i) {
34 if (i == 0 || i == 4 || i == 10) {
35 ASSERT_TRUE(vector.
get(i));
37 ASSERT_FALSE(vector.
get(i));
42TEST(BitVectorTest, InitFromIntVector) {
43 std::vector<uint_fast64_t> valueVector = {0, 4, 10};
46 ASSERT_EQ(32ul, vector.
size());
48 for (uint_fast64_t i = 0; i < 32; ++i) {
49 if (i == 0 || i == 4 || i == 10) {
50 ASSERT_TRUE(vector.
get(i));
52 ASSERT_FALSE(vector.
get(i));
57TEST(BitVectorTest, GetSet) {
60 for (uint_fast64_t i = 0; i < 32; ++i) {
61 vector.
set(i, i % 2 == 0);
64 for (uint_fast64_t i = 0; i < 32; ++i) {
65 ASSERT_EQ(i % 2 == 0, vector.
get(i));
69TEST(BitVectorTest, GetAsInt) {
77 EXPECT_EQ(3ul, vector.
getAsInt(0, 64));
78 EXPECT_EQ(1ul, vector.
getAsInt(62, 1));
79 EXPECT_EQ(3ul, vector.
getAsInt(62, 2));
80 EXPECT_EQ(7ul, vector.
getAsInt(62, 3));
81 EXPECT_EQ(15ul, vector.
getAsInt(62, 4));
83 vector.
set(64,
false);
85 EXPECT_EQ(1ul, vector.
getAsInt(62, 1));
86 EXPECT_EQ(3ul, vector.
getAsInt(62, 2));
87 EXPECT_EQ(6ul, vector.
getAsInt(62, 3));
88 EXPECT_EQ(13ul, vector.
getAsInt(62, 4));
91 vector.
set(62,
false);
92 EXPECT_EQ(2ul, vector.
getAsInt(61, 2));
95TEST(BitVectorTest, SetFromInt) {
100 EXPECT_TRUE(vector.
get(62));
101 EXPECT_FALSE(vector.
get(63));
102 EXPECT_FALSE(vector.
get(64));
103 EXPECT_FALSE(vector.
get(65));
107 EXPECT_TRUE(vector.
get(61));
108 EXPECT_FALSE(vector.
get(62));
109 EXPECT_FALSE(vector.
get(63));
113 EXPECT_TRUE(vector.
get(61));
114 EXPECT_FALSE(vector.
get(62));
115 EXPECT_TRUE(vector.
get(63));
120 EXPECT_TRUE(vector.
get(62));
121 EXPECT_TRUE(vector.
get(63));
122 EXPECT_TRUE(vector.
get(64));
123 EXPECT_TRUE(vector.
get(65));
128TEST(BitVectorTest, GetSetInt) {
132 EXPECT_EQ(2ul, vector.
getAsInt(63, 3));
135TEST(BitVectorDeathTest, GetSetAssertion) {
140 EXPECT_EXIT(vector.
get(32), ::testing::ExitedWithCode(0),
".*");
141 EXPECT_EXIT(vector.
set(32), ::testing::ExitedWithCode(0),
".*");
143 EXPECT_DEATH_IF_SUPPORTED(vector.
get(32),
"");
144 EXPECT_DEATH_IF_SUPPORTED(vector.
set(32),
"");
147 std::cerr <<
"WARNING: Not testing GetSetAssertions, as they are disabled in release mode.\n";
155 for (uint_fast64_t i = 0; i < 32; ++i) {
161 ASSERT_EQ(70ul, vector.
size());
164 for (uint_fast64_t i = 0; i < 32; ++i) {
165 ASSERT_TRUE(vector.
get(i));
168 for (uint_fast64_t i = 32; i < 70; ++i) {
170 ASSERT_NO_THROW(result = vector.
get(i));
171 ASSERT_FALSE(result);
176 ASSERT_EQ(72ul, vector.
size());
179 for (uint_fast64_t i = 0; i < 32; ++i) {
180 ASSERT_TRUE(vector.
get(i));
182 for (uint_fast64_t i = 32; i < 70; ++i) {
184 ASSERT_NO_THROW(result = vector.
get(i));
185 ASSERT_FALSE(result);
187 for (uint_fast64_t i = 70; i < 72; ++i) {
188 ASSERT_TRUE(vector.
get(i));
192 ASSERT_EQ(16ul, vector.
size());
195 for (uint_fast64_t i = 0; i < 16; ++i) {
196 ASSERT_TRUE(vector.
get(i));
200 ASSERT_EQ(65ul, vector.
size());
201 ASSERT_TRUE(vector.
full());
204TEST(BitVectorTest, OperatorAnd) {
208 for (
int i = 0; i < 32; ++i) {
209 vector1.
set(i, i % 2 == 0);
210 vector2.
set(i, i % 2 == 1);
216 for (uint_fast64_t i = 0; i < 31; ++i) {
217 ASSERT_FALSE(andResult.
get(i));
219 ASSERT_TRUE(andResult.
get(31));
222TEST(BitVectorTest, OperatorAndEqual) {
226 for (
int i = 0; i < 32; ++i) {
227 vector1.
set(i, i % 2 == 0);
228 vector2.
set(i, i % 2 == 1);
235 for (uint_fast64_t i = 0; i < 31; ++i) {
236 ASSERT_FALSE(vector1.
get(i));
238 ASSERT_TRUE(vector1.
get(31));
241TEST(BitVectorTest, OperatorOr) {
245 for (uint_fast64_t i = 0; i < 32; ++i) {
246 vector1.
set(i, i % 2 == 0);
247 vector2.
set(i, i % 2 == 1);
249 vector1.
set(31,
false);
250 vector2.
set(31,
false);
254 for (uint_fast64_t i = 0; i < 31; ++i) {
255 ASSERT_TRUE(orResult.
get(i));
257 ASSERT_FALSE(orResult.
get(31));
260TEST(BitVectorTest, OperatorOrEqual) {
264 for (uint_fast64_t i = 0; i < 32; ++i) {
265 vector1.
set(i, i % 2 == 0);
266 vector2.
set(i, i % 2 == 1);
268 vector1.
set(31,
false);
269 vector2.
set(31,
false);
273 for (uint_fast64_t i = 0; i < 31; ++i) {
274 ASSERT_TRUE(vector1.
get(i));
276 ASSERT_FALSE(vector1.
get(31));
279TEST(BitVectorTest, OperatorXor) {
283 for (uint_fast64_t i = 0; i < 32; ++i) {
285 vector2.
set(i, i % 2 == 1);
292 for (uint_fast64_t i = 0; i < 32; ++i) {
293 ASSERT_EQ(vector3.
get(i), vector4.
get(i));
294 ASSERT_FALSE(vector5.
get(i));
298TEST(BitVectorTest, OperatorModulo) {
302 for (uint_fast64_t i = 0; i < 15; ++i) {
303 vector2.
set(i, i % 2 == 0);
312 ASSERT_EQ(8ul, moduloResult.
size());
315 for (uint_fast64_t i = 0; i < 8; ++i) {
316 if (i == 1 || i == 3) {
317 ASSERT_TRUE(moduloResult.
get(i));
319 ASSERT_FALSE(moduloResult.
get(i));
324TEST(BitVectorTest, OperatorNot) {
328 for (uint_fast64_t i = 0; i < 32; ++i) {
329 vector1.
set(i, i % 2 == 0);
330 vector2.
set(i, i % 2 == 1);
335 for (uint_fast64_t i = 0; i < 32; ++i) {
336 ASSERT_EQ(vector1.
get(i), notResult.
get(i));
340TEST(BitVectorTest, Complement) {
344 for (uint_fast64_t i = 0; i < 32; ++i) {
345 vector1.
set(i, i % 2 == 0);
346 vector2.
set(i, i % 2 == 1);
351 for (uint_fast64_t i = 0; i < 32; ++i) {
352 ASSERT_EQ(vector1.
get(i), vector2.
get(i));
356TEST(BitVectorTest, Increment) {
362 vector2.
set(0,
true);
363 EXPECT_EQ(vector1, vector2);
368 vector2.
set(1,
true);
369 EXPECT_EQ(vector1, vector2);
373 vector2.
set(0,
true);
374 EXPECT_EQ(vector1, vector2);
377 for (uint_fast64_t i = 0; i < 66; ++i) {
378 vector1.
set(i,
true);
383 vector2.
set(66,
true);
384 EXPECT_EQ(vector1, vector2);
388 vector2.
set(0,
true);
389 EXPECT_EQ(vector1, vector2);
393 EXPECT_TRUE(vector1.
full());
395 EXPECT_TRUE(vector1.
empty());
400 std::vector<uint64_t> inversePermutation = {0, 1, 3, 2, 4, 6, 5, 8, 7};
403 EXPECT_TRUE(vector2.
get(2));
404 EXPECT_TRUE(vector2.
get(6));
407TEST(BitVectorTest, permuteGrouped) {
409 std::vector<uint64_t> inversePermutation = {1, 0, 2};
410 std::vector<uint64_t> groupIndices = {0, 3, 5, 6};
413 EXPECT_EQ(expected, permuted);
420 for (uint_fast64_t i = 0; i < 32; ++i) {
421 vector1.
set(i, i % 2 == 0);
423 vector2.
set(31,
false);
424 vector2.
set(30,
false);
428 for (uint_fast64_t i = 0; i < 30; ++i) {
429 ASSERT_TRUE(impliesResult.
get(i));
431 ASSERT_FALSE(impliesResult.
get(30));
432 ASSERT_TRUE(impliesResult.
get(31));
439 for (uint_fast64_t i = 0; i < 32; ++i) {
440 vector1.
set(i, i % 2 == 0);
445 vector2.
set(16,
false);
454 for (uint_fast64_t i = 0; i < 32; ++i) {
455 vector1.
set(i, i % 2 == 0);
456 vector2.
set(i, i % 2 == 1);
461 vector2.
set(16,
true);
469 ASSERT_TRUE(vector.
empty());
471 vector.
set(17,
true);
473 ASSERT_FALSE(vector.
empty());
475 vector.
set(17,
false);
476 vector.
set(18,
false);
478 ASSERT_TRUE(vector.
empty());
484 ASSERT_TRUE(vector.
full());
486 vector.
set(17,
false);
488 ASSERT_FALSE(vector.
full());
490 vector.
set(17,
true);
491 vector.
set(18,
true);
493 ASSERT_TRUE(vector.
full());
496TEST(BitVectorTest, NumberOfSetBits) {
499 for (uint_fast64_t i = 0; i < 32; ++i) {
500 vector.
set(i, i % 2 == 0);
506TEST(BitVectorTest, NumberOfSetBitsBeforeIndex) {
509 for (uint_fast64_t i = 0; i < 32; ++i) {
510 vector.
set(i, i % 2 == 0);
519 ASSERT_TRUE(vector.
begin() == vector.
end());
523 ASSERT_FALSE(vector.
begin() == vector.
end());
525 vector.
set(17,
false);
527 ASSERT_TRUE(vector.
begin() == vector.
end());
530TEST(BitVectorTest, NextSetIndex) {
543TEST(BitVectorTest, NextUnsetIndex) {
558TEST(BitVectorTest, SequenceBefore) {
565 auto vector_compl = ~vector;
567 for (uint64_t i = 0; i <= 65; ++i) {
571 }
else if (i <= 17) {
573 }
else if (i <= 64) {
579 ASSERT_EQ(expected, vector_compl.getStartOfOneSequenceBefore(i)) <<
" input index is i=" << i;
586 for (uint64_t i = 0; i < 32; ++i) {
587 vector.
set(i, i % 2 == 0);
591 for (
auto bit : vector) {
598TEST(BitVectorTest, ReverseIterator) {
602 for (; i < vector.
size(); i += 3) {
606 for (
auto bitIt = vector.
rbegin(); bitIt != vector.
rend(); ++bitIt) {
608 ASSERT_EQ(i, *bitIt);
613TEST(BitVectorTest, CompareAndSwap) {
615 vector.
setFromInt(0, 64, 2377830234574424100);
616 vector.
setFromInt(64, 64, 1152921504607379586);
620 ASSERT_FALSE(result);
631 ASSERT_EQ(129ul, vector1.size());
632 ASSERT_TRUE(vector1.get(3));
633 ASSERT_TRUE(vector1.get(5));
634 ASSERT_TRUE(vector1.get(10 + 64));
635 ASSERT_TRUE(vector1.get(12 + 64));
636 ASSERT_EQ(4ul, vector1.getNumberOfSetBits());
642 ASSERT_EQ(64ul, vector1.size());
643 ASSERT_EQ(2ul, vector1.getNumberOfSetBits());
646 ASSERT_EQ(128ul, vector2.size());
647 ASSERT_EQ(2ul, vector2.getNumberOfSetBits());
650TEST(BitVectorTest, Assignment) {
654 ASSERT_TRUE(v1.get(9999));
TEST(BitVectorTest, InitToZero)
PositionIteratorType Iterator
A bit vector that is internally represented as a vector of 64-bit values.
void complement()
Negates all bits in the bit vector.
bool compareAndSwap(uint_fast64_t start1, uint_fast64_t start2, uint_fast64_t length)
Compare two intervals [start1, start1+length] and [start2, start2+length] and swap them if the second...
const_reverse_iterator rbegin() const
Returns a reverse iterator to the indices of the set bits in the bit vector.
bool isDisjointFrom(BitVector const &other) const
Checks whether none of the bits that are set in the current bit vector are also set in the given bit ...
bool full() const
Retrieves whether all bits are set in this bit vector.
const_reverse_iterator rend() const
Returns a reverse iterator pointing at the element past the front of the bit vector.
const_iterator end() const
Returns an iterator pointing at the element past the back of the bit vector.
uint_fast64_t getNextSetIndex(uint_fast64_t startingIndex) const
Retrieves the index of the bit that is the next bit set to true in the bit vector.
void setFromInt(uint_fast64_t bitIndex, uint_fast64_t numberOfBits, uint64_t value)
Sets the selected number of lowermost bits of the provided value at the given bit index.
bool empty() const
Retrieves whether no bits are set to true in this bit vector.
void clear()
Removes all set bits from the bit vector.
bool isSubsetOf(BitVector const &other) const
Checks whether all bits that are set in the current bit vector are also set in the given bit vector.
BitVector implies(BitVector const &other) const
Performs a logical "implies" with the given bit vector.
void resize(uint_fast64_t newLength, bool init=false)
Resizes the bit vector to hold the given new number of bits.
BitVector permute(std::vector< uint64_t > const &inversePermutation) const
Apply a permutation of entries.
void increment()
Increments the (unsigned) number represented by this BitVector by one.
const_iterator begin() const
Returns an iterator to the indices of the set bits in the bit vector.
uint64_t getStartOfZeroSequenceBefore(uint64_t endIndex) const
Retrieves the smallest index i such that all bits in the range [i,endIndex) are 0.
void set(uint_fast64_t index, bool value=true)
Sets the given truth value at the given index.
size_t size() const
Retrieves the number of bits this bit vector can store.
uint_fast64_t getNextUnsetIndex(uint_fast64_t startingIndex) const
Retrieves the index of the bit that is the next bit set to false in the bit vector.
uint_fast64_t getNumberOfSetBits() const
Returns the number of bits that are set to true in this bit vector.
uint_fast64_t getAsInt(uint_fast64_t bitIndex, uint_fast64_t numberOfBits) const
Retrieves the content of the current bit vector at the given index for the given number of bits as an...
void expandSize(bool init=false)
bool get(uint_fast64_t index) const
Retrieves the truth value of the bit at the given index and performs a bound check.
BitVector permuteGroupedVector(std::vector< uint64_t > const &inversePermutation, std::vector< uint64_t > const &rowGroupIndices) const
Apply a permutation of entries assuming a grouped vector.
uint_fast64_t getNumberOfSetBitsBeforeIndex(uint_fast64_t index) const
Retrieves the number of bits set in this bit vector with an index strictly smaller than the given one...
void concat(BitVector const &extension)
Concatenate this bitvector with another bitvector.