Unconditionally use FindFirstBit in SetBitIterator
Add simple tests
This commit is contained in:
@@ -322,11 +322,7 @@ struct SetBitIterator {
|
|||||||
|
|
||||||
bool operator==(const Iterator &other) const
|
bool operator==(const Iterator &other) const
|
||||||
{
|
{
|
||||||
#ifdef WITH_BITMATH_BUILTINS
|
|
||||||
return this->bitset == other.bitset;
|
return this->bitset == other.bitset;
|
||||||
#else
|
|
||||||
return this->bitset == other.bitset && (this->bitset == 0 || this->bitpos == other.bitpos);
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
bool operator!=(const Iterator &other) const { return !(*this == other); }
|
bool operator!=(const Iterator &other) const { return !(*this == other); }
|
||||||
Tbitpos operator*() const { return this->bitpos; }
|
Tbitpos operator*() const { return this->bitpos; }
|
||||||
@@ -337,29 +333,14 @@ struct SetBitIterator {
|
|||||||
Tbitpos bitpos;
|
Tbitpos bitpos;
|
||||||
void Validate()
|
void Validate()
|
||||||
{
|
{
|
||||||
#ifdef WITH_BITMATH_BUILTINS
|
|
||||||
if (this->bitset != 0) {
|
if (this->bitset != 0) {
|
||||||
typename std::make_unsigned<Tbitset>::type unsigned_value = this->bitset;
|
typename std::make_unsigned<Tbitset>::type unsigned_value = this->bitset;
|
||||||
if (sizeof(Tbitset) <= sizeof(unsigned int)) {
|
this->bitpos = static_cast<Tbitpos>(FindFirstBit(unsigned_value));
|
||||||
bitpos = static_cast<Tbitpos>(__builtin_ctz(unsigned_value));
|
|
||||||
} else if (sizeof(Tbitset) == sizeof(unsigned long)) {
|
|
||||||
bitpos = static_cast<Tbitpos>(__builtin_ctzl(unsigned_value));
|
|
||||||
} else {
|
|
||||||
bitpos = static_cast<Tbitpos>(__builtin_ctzll(unsigned_value));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
#else
|
|
||||||
while (this->bitset != 0 && (this->bitset & 1) == 0) this->Next();
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
void Next()
|
void Next()
|
||||||
{
|
{
|
||||||
#ifdef WITH_BITMATH_BUILTINS
|
this->bitset = KillFirstBit(this->bitset);
|
||||||
this->bitset = static_cast<Tbitset>(this->bitset ^ (this->bitset & -this->bitset));
|
|
||||||
#else
|
|
||||||
this->bitset = static_cast<Tbitset>(this->bitset >> 1);
|
|
||||||
this->bitpos++;
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@@ -51,3 +51,22 @@ TEST_CASE("FindLastBit tests")
|
|||||||
CHECK(FindLastBit(0x42U) == FindLastBit(0x40U));
|
CHECK(FindLastBit(0x42U) == FindLastBit(0x40U));
|
||||||
CHECK(FindLastBit(0xAAAAU) == FindLastBit(0x8000U));
|
CHECK(FindLastBit(0xAAAAU) == FindLastBit(0x8000U));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_CASE("SetBitIterator tests")
|
||||||
|
{
|
||||||
|
auto test_case = [&](auto input, std::initializer_list<uint> expected) {
|
||||||
|
auto iter = expected.begin();
|
||||||
|
for (auto bit : SetBitIterator(input)) {
|
||||||
|
if (iter == expected.end()) return false;
|
||||||
|
if (bit != *iter) return false;
|
||||||
|
++iter;
|
||||||
|
}
|
||||||
|
return iter == expected.end();
|
||||||
|
};
|
||||||
|
CHECK(test_case(0, {}));
|
||||||
|
CHECK(test_case(1, { 0 }));
|
||||||
|
CHECK(test_case(42, { 1, 3, 5 }));
|
||||||
|
CHECK(test_case(0x8080FFFFU, { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 23, 31 }));
|
||||||
|
CHECK(test_case(INT32_MIN, { 31 }));
|
||||||
|
CHECK(test_case(INT64_MIN, { 63 }));
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user