Common: Optimize BitSet iterator
This commit is contained in:
parent
92bf5c88e6
commit
d36ec905b1
@ -121,22 +121,19 @@ public:
|
|||||||
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), m_bit(other.m_bit) {}
|
||||||
Iterator(IntTy val, int bit) : m_val(val), m_bit(bit) {}
|
Iterator(IntTy val) : m_val(val), m_bit(0) {}
|
||||||
Iterator& operator=(Iterator other) {
|
Iterator& operator=(Iterator other) {
|
||||||
new (this) Iterator(other);
|
new (this) Iterator(other);
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
int operator*() {
|
int operator*() {
|
||||||
return m_bit;
|
return m_bit + ComputeLsb();
|
||||||
}
|
}
|
||||||
Iterator& operator++() {
|
Iterator& operator++() {
|
||||||
if (m_val == 0) {
|
int lsb = ComputeLsb();
|
||||||
m_bit = -1;
|
m_val >>= lsb + 1;
|
||||||
} else {
|
m_bit += lsb + 1;
|
||||||
int bit = LeastSignificantSetBit(m_val);
|
m_has_lsb = false;
|
||||||
m_val &= ~(1 << bit);
|
|
||||||
m_bit = bit;
|
|
||||||
}
|
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
Iterator operator++(int _) {
|
Iterator operator++(int _) {
|
||||||
@ -145,15 +142,24 @@ public:
|
|||||||
return other;
|
return other;
|
||||||
}
|
}
|
||||||
bool operator==(Iterator other) const {
|
bool operator==(Iterator other) const {
|
||||||
return m_bit == other.m_bit;
|
return m_val == other.m_val;
|
||||||
}
|
}
|
||||||
bool operator!=(Iterator other) const {
|
bool operator!=(Iterator other) const {
|
||||||
return m_bit != other.m_bit;
|
return m_val != other.m_val;
|
||||||
}
|
}
|
||||||
|
|
||||||
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_bit;
|
||||||
|
int m_lsb = -1;
|
||||||
|
bool m_has_lsb = false;
|
||||||
};
|
};
|
||||||
|
|
||||||
BitSet() : m_val(0) {}
|
BitSet() : m_val(0) {}
|
||||||
@ -221,11 +227,10 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
Iterator begin() const {
|
Iterator begin() const {
|
||||||
Iterator it(m_val, 0);
|
return Iterator(m_val);
|
||||||
return ++it;
|
|
||||||
}
|
}
|
||||||
Iterator end() const {
|
Iterator end() const {
|
||||||
return Iterator(m_val, -1);
|
return Iterator(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
IntTy m_val;
|
IntTy m_val;
|
||||||
|
Loading…
Reference in New Issue
Block a user