Unconditionally use FindFirstBit in SetBitIterator

Add simple tests
This commit is contained in:
Jonathan G Rennison
2024-01-25 19:03:32 +00:00
parent 5404be172c
commit c26ca5369e
2 changed files with 21 additions and 21 deletions

View File

@@ -322,11 +322,7 @@ struct SetBitIterator {
bool operator==(const Iterator &other) const
{
#ifdef WITH_BITMATH_BUILTINS
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); }
Tbitpos operator*() const { return this->bitpos; }
@@ -337,29 +333,14 @@ struct SetBitIterator {
Tbitpos bitpos;
void Validate()
{
#ifdef WITH_BITMATH_BUILTINS
if (this->bitset != 0) {
typename std::make_unsigned<Tbitset>::type unsigned_value = this->bitset;
if (sizeof(Tbitset) <= sizeof(unsigned int)) {
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));
}
this->bitpos = static_cast<Tbitpos>(FindFirstBit(unsigned_value));
}
#else
while (this->bitset != 0 && (this->bitset & 1) == 0) this->Next();
#endif
}
void Next()
{
#ifdef WITH_BITMATH_BUILTINS
this->bitset = static_cast<Tbitset>(this->bitset ^ (this->bitset & -this->bitset));
#else
this->bitset = static_cast<Tbitset>(this->bitset >> 1);
this->bitpos++;
#endif
this->bitset = KillFirstBit(this->bitset);
}
};