[ADT] Clean up SparseBitVector copying and make it moveable

llvm-svn: 345829
This commit is contained in:
Benjamin Kramer 2018-11-01 13:55:59 +00:00
parent a94ecc619b
commit 5223624e81
2 changed files with 29 additions and 26 deletions

View File

@ -261,11 +261,11 @@ class SparseBitVector {
BITWORD_SIZE = SparseBitVectorElement<ElementSize>::BITWORD_SIZE BITWORD_SIZE = SparseBitVectorElement<ElementSize>::BITWORD_SIZE
}; };
ElementList Elements;
// Pointer to our current Element. This has no visible effect on the external // Pointer to our current Element. This has no visible effect on the external
// state of a SparseBitVector, it's just used to improve performance in the // state of a SparseBitVector, it's just used to improve performance in the
// common case of testing/modifying bits with similar indices. // common case of testing/modifying bits with similar indices.
mutable ElementListIter CurrElementIter; mutable ElementListIter CurrElementIter;
ElementList Elements;
// This is like std::lower_bound, except we do linear searching from the // This is like std::lower_bound, except we do linear searching from the
// current position. // current position.
@ -441,22 +441,12 @@ class SparseBitVector {
public: public:
using iterator = SparseBitVectorIterator; using iterator = SparseBitVectorIterator;
SparseBitVector() { SparseBitVector() : Elements(), CurrElementIter(Elements.begin()) {}
CurrElementIter = Elements.begin();
}
// SparseBitVector copy ctor. SparseBitVector(const SparseBitVector &RHS)
SparseBitVector(const SparseBitVector &RHS) { : Elements(RHS.Elements), CurrElementIter(Elements.begin()) {}
ElementListConstIter ElementIter = RHS.Elements.begin(); SparseBitVector(SparseBitVector &&RHS)
while (ElementIter != RHS.Elements.end()) { : Elements(std::move(RHS.Elements)), CurrElementIter(Elements.begin()) {}
Elements.push_back(SparseBitVectorElement<ElementSize>(*ElementIter));
++ElementIter;
}
CurrElementIter = Elements.begin ();
}
~SparseBitVector() = default;
// Clear. // Clear.
void clear() { void clear() {
@ -468,16 +458,13 @@ public:
if (this == &RHS) if (this == &RHS)
return *this; return *this;
Elements.clear(); Elements = RHS.Elements;
CurrElementIter = Elements.begin();
ElementListConstIter ElementIter = RHS.Elements.begin(); return *this;
while (ElementIter != RHS.Elements.end()) { }
Elements.push_back(SparseBitVectorElement<ElementSize>(*ElementIter)); SparseBitVector &operator=(SparseBitVector &&RHS) {
++ElementIter; Elements = std::move(RHS.Elements);
} CurrElementIter = Elements.begin();
CurrElementIter = Elements.begin ();
return *this; return *this;
} }

View File

@ -36,6 +36,22 @@ TEST(SparseBitVectorTest, TrivialOperation) {
const SparseBitVector<> ConstVec = Vec; const SparseBitVector<> ConstVec = Vec;
EXPECT_TRUE(ConstVec.test(5)); EXPECT_TRUE(ConstVec.test(5));
EXPECT_FALSE(ConstVec.test(17)); EXPECT_FALSE(ConstVec.test(17));
Vec.set(1337);
EXPECT_TRUE(Vec.test(1337));
Vec = ConstVec;
EXPECT_FALSE(Vec.test(1337));
Vec.set(1337);
EXPECT_FALSE(Vec.empty());
SparseBitVector<> MovedVec(std::move(Vec));
EXPECT_TRUE(Vec.empty());
EXPECT_TRUE(MovedVec.test(5));
EXPECT_TRUE(MovedVec.test(1337));
Vec = std::move(MovedVec);
EXPECT_TRUE(MovedVec.empty());
EXPECT_FALSE(Vec.empty());
} }
TEST(SparseBitVectorTest, IntersectWith) { TEST(SparseBitVectorTest, IntersectWith) {