Codechange: Simplify SetBitIterator

Use FindFirstBit and KillFirstBit, allowing simpler iterator equality
Add simple test
This commit is contained in:
Jonathan G Rennison
2024-01-27 17:17:27 +00:00
committed by rubidium42
parent dce7d5d9b0
commit c0b8e58404
3 changed files with 40 additions and 4 deletions

View File

@@ -294,7 +294,7 @@ struct SetBitIterator {
bool operator==(const Iterator &other) const
{
return this->bitset == other.bitset && (this->bitset == 0 || this->bitpos == other.bitpos);
return this->bitset == other.bitset;
}
bool operator!=(const Iterator &other) const { return !(*this == other); }
Tbitpos operator*() const { return this->bitpos; }
@@ -305,12 +305,14 @@ struct SetBitIterator {
Tbitpos bitpos;
void Validate()
{
while (this->bitset != 0 && (this->bitset & 1) == 0) this->Next();
if (this->bitset != 0) {
typename std::make_unsigned<Tbitset>::type unsigned_value = this->bitset;
this->bitpos = static_cast<Tbitpos>(FindFirstBit(unsigned_value));
}
}
void Next()
{
this->bitset = static_cast<Tbitset>(this->bitset >> 1);
this->bitpos++;
this->bitset = KillFirstBit(this->bitset);
}
};