6TEST(BitVectorTest, InitToZero) {
9 for (uint64_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 (uint64_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<uint64_t> valueVector = {0, 4, 10};
31 ASSERT_EQ(32ul, vector.
size());
33 for (uint64_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<uint64_t> valueVector = {0, 4, 10};
46 ASSERT_EQ(32ul, vector.
size());
48 for (uint64_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 (uint64_t i = 0; i < 32; ++i) {
61 vector.
set(i, i % 2 == 0);
64 for (uint64_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) {
139 EXPECT_DEATH_IF_SUPPORTED(vector.
get(32),
"");
140 EXPECT_DEATH_IF_SUPPORTED(vector.
set(32),
"");
142 std::cerr <<
"WARNING: Not testing GetSetAssertions, as they are disabled in release mode.\n";
150 for (uint64_t i = 0; i < 32; ++i) {
156 ASSERT_EQ(70ul, vector.
size());
159 for (uint64_t i = 0; i < 32; ++i) {
160 ASSERT_TRUE(vector.
get(i));
163 for (uint64_t i = 32; i < 70; ++i) {
165 ASSERT_NO_THROW(result = vector.
get(i));
166 ASSERT_FALSE(result);
171 std::cout << vector << std::endl;
172 ASSERT_EQ(72ul, vector.
size());
175 for (uint64_t i = 0; i < 32; ++i) {
176 ASSERT_TRUE(vector.
get(i));
178 for (uint64_t i = 32; i < 70; ++i) {
180 ASSERT_NO_THROW(result = vector.
get(i));
181 ASSERT_FALSE(result);
183 for (uint64_t i = 70; i < 72; ++i) {
184 ASSERT_TRUE(vector.
get(i));
188 ASSERT_EQ(16ul, vector.
size());
191 for (uint64_t i = 0; i < 16; ++i) {
192 ASSERT_TRUE(vector.
get(i));
196 ASSERT_EQ(65ul, vector.
size());
197 ASSERT_TRUE(vector.
full());
200TEST(BitVectorTest, OperatorAnd) {
204 for (
int i = 0; i < 32; ++i) {
205 vector1.
set(i, i % 2 == 0);
206 vector2.
set(i, i % 2 == 1);
212 for (uint64_t i = 0; i < 31; ++i) {
213 ASSERT_FALSE(andResult.
get(i));
215 ASSERT_TRUE(andResult.
get(31));
218TEST(BitVectorTest, OperatorAndEqual) {
222 for (
int i = 0; i < 32; ++i) {
223 vector1.
set(i, i % 2 == 0);
224 vector2.
set(i, i % 2 == 1);
231 for (uint64_t i = 0; i < 31; ++i) {
232 ASSERT_FALSE(vector1.
get(i));
234 ASSERT_TRUE(vector1.
get(31));
237TEST(BitVectorTest, OperatorOr) {
241 for (uint64_t i = 0; i < 32; ++i) {
242 vector1.
set(i, i % 2 == 0);
243 vector2.
set(i, i % 2 == 1);
245 vector1.
set(31,
false);
246 vector2.
set(31,
false);
250 for (uint64_t i = 0; i < 31; ++i) {
251 ASSERT_TRUE(orResult.
get(i));
253 ASSERT_FALSE(orResult.
get(31));
256TEST(BitVectorTest, OperatorOrEqual) {
260 for (uint64_t i = 0; i < 32; ++i) {
261 vector1.
set(i, i % 2 == 0);
262 vector2.
set(i, i % 2 == 1);
264 vector1.
set(31,
false);
265 vector2.
set(31,
false);
269 for (uint64_t i = 0; i < 31; ++i) {
270 ASSERT_TRUE(vector1.
get(i));
272 ASSERT_FALSE(vector1.
get(31));
275TEST(BitVectorTest, OperatorXor) {
279 for (uint64_t i = 0; i < 32; ++i) {
281 vector2.
set(i, i % 2 == 1);
288 for (uint64_t i = 0; i < 32; ++i) {
289 ASSERT_EQ(vector3.
get(i), vector4.
get(i));
290 ASSERT_FALSE(vector5.
get(i));
294TEST(BitVectorTest, OperatorModulo) {
298 for (uint64_t i = 0; i < 15; ++i) {
299 vector2.
set(i, i % 2 == 0);
308 ASSERT_EQ(8ul, moduloResult.
size());
311 for (uint64_t i = 0; i < 8; ++i) {
312 if (i == 1 || i == 3) {
313 ASSERT_TRUE(moduloResult.
get(i));
315 ASSERT_FALSE(moduloResult.
get(i));
320TEST(BitVectorTest, OperatorNot) {
324 for (uint64_t i = 0; i < 32; ++i) {
325 vector1.
set(i, i % 2 == 0);
326 vector2.
set(i, i % 2 == 1);
331 for (uint64_t i = 0; i < 32; ++i) {
332 ASSERT_EQ(vector1.
get(i), notResult.
get(i));
336TEST(BitVectorTest, Complement) {
340 for (uint64_t i = 0; i < 32; ++i) {
341 vector1.
set(i, i % 2 == 0);
342 vector2.
set(i, i % 2 == 1);
347 for (uint64_t i = 0; i < 32; ++i) {
348 ASSERT_EQ(vector1.
get(i), vector2.
get(i));
352TEST(BitVectorTest, Increment) {
358 vector2.
set(0,
true);
359 EXPECT_EQ(vector1, vector2);
364 vector2.
set(1,
true);
365 EXPECT_EQ(vector1, vector2);
369 vector2.
set(0,
true);
370 EXPECT_EQ(vector1, vector2);
373 for (uint64_t i = 0; i < 66; ++i) {
374 vector1.
set(i,
true);
379 vector2.
set(66,
true);
380 EXPECT_EQ(vector1, vector2);
384 vector2.
set(0,
true);
385 EXPECT_EQ(vector1, vector2);
389 EXPECT_TRUE(vector1.
full());
391 EXPECT_TRUE(vector1.
empty());
396 std::vector<uint64_t> inversePermutation = {0, 1, 3, 2, 4, 6, 5, 8, 7};
399 EXPECT_TRUE(vector2.
get(2));
400 EXPECT_TRUE(vector2.
get(6));
403TEST(BitVectorTest, permuteGrouped) {
405 std::vector<uint64_t> inversePermutation = {1, 0, 2};
406 std::vector<uint64_t> groupIndices = {0, 3, 5, 6};
409 EXPECT_EQ(expected, permuted);
416 for (uint64_t i = 0; i < 32; ++i) {
417 vector1.
set(i, i % 2 == 0);
419 vector2.
set(31,
false);
420 vector2.
set(30,
false);
424 for (uint64_t i = 0; i < 30; ++i) {
425 ASSERT_TRUE(impliesResult.
get(i));
427 ASSERT_FALSE(impliesResult.
get(30));
428 ASSERT_TRUE(impliesResult.
get(31));
435 for (uint64_t i = 0; i < 32; ++i) {
436 vector1.
set(i, i % 2 == 0);
441 vector2.
set(16,
false);
450 for (uint64_t i = 0; i < 32; ++i) {
451 vector1.
set(i, i % 2 == 0);
452 vector2.
set(i, i % 2 == 1);
457 vector2.
set(16,
true);
465 ASSERT_TRUE(vector.
empty());
467 vector.
set(17,
true);
469 ASSERT_FALSE(vector.
empty());
471 vector.
set(17,
false);
472 vector.
set(18,
false);
474 ASSERT_TRUE(vector.
empty());
480 ASSERT_TRUE(vector.
full());
482 vector.
set(17,
false);
484 ASSERT_FALSE(vector.
full());
486 vector.
set(17,
true);
487 vector.
set(18,
true);
489 ASSERT_TRUE(vector.
full());
492TEST(BitVectorTest, NumberOfSetBits) {
495 for (uint64_t i = 0; i < 32; ++i) {
496 vector.
set(i, i % 2 == 0);
502TEST(BitVectorTest, NumberOfSetBitsBeforeIndex) {
505 for (uint64_t i = 0; i < 32; ++i) {
506 vector.
set(i, i % 2 == 0);
515 ASSERT_TRUE(vector.
begin() == vector.
end());
519 ASSERT_FALSE(vector.
begin() == vector.
end());
521 vector.
set(17,
false);
523 ASSERT_TRUE(vector.
begin() == vector.
end());
526TEST(BitVectorTest, NextSetIndex) {
539TEST(BitVectorTest, NextUnsetIndex) {
554TEST(BitVectorTest, SequenceBefore) {
561 auto vector_compl = ~vector;
563 for (uint64_t i = 0; i <= 65; ++i) {
567 }
else if (i <= 17) {
569 }
else if (i <= 64) {
575 ASSERT_EQ(expected, vector_compl.getStartOfOneSequenceBefore(i)) <<
" input index is i=" << i;
582 for (uint64_t i = 0; i < 32; ++i) {
583 vector.
set(i, i % 2 == 0);
587 for (
auto bit : vector) {
594TEST(BitVectorTest, ReverseIterator) {
598 for (; i < vector.
size(); i += 3) {
602 for (
auto bitIt = vector.
rbegin(); bitIt != vector.
rend(); ++bitIt) {
604 ASSERT_EQ(i, *bitIt);
609TEST(BitVectorTest, CompareAndSwap) {
611 vector.
setFromInt(0, 64, 2377830234574424100);
612 vector.
setFromInt(64, 64, 1152921504607379586);
616 ASSERT_FALSE(result);
627 ASSERT_EQ(129ul, vector1.size());
628 ASSERT_TRUE(vector1.get(3));
629 ASSERT_TRUE(vector1.get(5));
630 ASSERT_TRUE(vector1.get(10 + 64));
631 ASSERT_TRUE(vector1.get(12 + 64));
632 ASSERT_EQ(4ul, vector1.getNumberOfSetBits());
638 ASSERT_EQ(64ul, vector1.size());
639 ASSERT_EQ(2ul, vector1.getNumberOfSetBits());
642 ASSERT_EQ(128ul, vector2.size());
643 ASSERT_EQ(2ul, vector2.getNumberOfSetBits());
646TEST(BitVectorTest, Assignment) {
650 ASSERT_TRUE(v1.get(9999));
653TEST(BitVectorTest, ZeroSized) {
655 EXPECT_EQ(0, v.
size());
656 EXPECT_TRUE(v.
empty());
657 EXPECT_TRUE(v.
full());
663 for (
auto const& entry : v) {
664 FAIL() <<
"Should not iterate over an empty bit vector.";
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.
const_reverse_iterator rbegin() const
Returns a reverse iterator to the indices of the set bits in the bit vector.
uint64_t getNextSetIndex(uint64_t startingIndex) const
Retrieves the index of the bit that is the next bit set to true 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.
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.
uint64_t getNumberOfSetBits() const
Returns the number of bits that are set to true in this bit vector.
uint64_t getNextUnsetIndex(uint64_t startingIndex) const
Retrieves the index of the bit that is the next bit set to false in the bit vector.
bool compareAndSwap(uint64_t start1, uint64_t start2, uint64_t length)
Compare two intervals [start1, start1+length] and [start2, start2+length] and swap them if the second...
void setFromInt(uint64_t bitIndex, uint64_t numberOfBits, uint64_t value)
Sets the selected number of lowermost bits of the provided value at the given bit index.
BitVector permute(std::vector< uint64_t > const &inversePermutation) const
Apply a permutation of entries.
void set(uint64_t index, bool value=true)
Sets the given truth value at the given index.
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.
uint64_t getAsInt(uint64_t bitIndex, uint64_t numberOfBits) const
Retrieves the content of the current bit vector at the given index for the given number of bits as an...
size_t size() const
Retrieves the number of bits this bit vector can store.
void resize(uint64_t newLength, bool init=false)
Resizes the bit vector to hold the given new number of bits.
void expandSize(bool init=false)
bool get(uint64_t index) const
Retrieves the truth value of the bit at the given index and performs a bound check.
uint64_t getNumberOfSetBitsBeforeIndex(uint64_t index) const
Retrieves the number of bits set in this bit vector with an index strictly smaller than the given one...
BitVector permuteGroupedVector(std::vector< uint64_t > const &inversePermutation, std::vector< uint64_t > const &rowGroupIndices) const
Apply a permutation of entries assuming a grouped vector.
void concat(BitVector const &extension)
Concatenate this bitvector with another bitvector.