mirror of
https://github.com/Lime3DS/Lime3DS.git
synced 2024-11-30 13:24:16 +01:00
Common: Simplify and optimize BitSet iterator
Instead of doing complex machinations to keep track of the current bit index, just unset the lowest bit on each iteration, greatly simplifying the code.
This commit is contained in:
parent
176ed77506
commit
a5aa5e2b2d
@ -120,20 +120,15 @@ public:
|
|||||||
// A STL-like iterator is required to be able to use range-based for loops.
|
// A STL-like iterator is required to be able to use range-based for loops.
|
||||||
class Iterator {
|
class Iterator {
|
||||||
public:
|
public:
|
||||||
Iterator(const Iterator& other) : m_val(other.m_val), m_bit(other.m_bit) {}
|
Iterator(const Iterator& other) : m_val(other.m_val) {}
|
||||||
Iterator(IntTy val) : m_val(val), m_bit(0) {}
|
Iterator(IntTy val) : m_val(val) {}
|
||||||
Iterator& operator=(Iterator other) {
|
|
||||||
new (this) Iterator(other);
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
int operator*() {
|
int operator*() {
|
||||||
return m_bit + ComputeLsb();
|
// This will never be called when m_val == 0, because that would be the end() iterator
|
||||||
|
return LeastSignificantSetBit(m_val);
|
||||||
}
|
}
|
||||||
Iterator& operator++() {
|
Iterator& operator++() {
|
||||||
int lsb = ComputeLsb();
|
// Unset least significant set bit
|
||||||
m_val >>= lsb + 1;
|
m_val &= m_val - IntTy(1);
|
||||||
m_bit += lsb + 1;
|
|
||||||
m_has_lsb = false;
|
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
Iterator operator++(int _) {
|
Iterator operator++(int _) {
|
||||||
@ -149,17 +144,7 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
int ComputeLsb() {
|
|
||||||
if (!m_has_lsb) {
|
|
||||||
m_lsb = LeastSignificantSetBit(m_val);
|
|
||||||
m_has_lsb = true;
|
|
||||||
}
|
|
||||||
return m_lsb;
|
|
||||||
}
|
|
||||||
IntTy m_val;
|
IntTy m_val;
|
||||||
int m_bit;
|
|
||||||
int m_lsb = -1;
|
|
||||||
bool m_has_lsb = false;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
BitSet() : m_val(0) {}
|
BitSet() : m_val(0) {}
|
||||||
|
Loading…
Reference in New Issue
Block a user