[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
};
ElementList Elements;
// 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
// common case of testing/modifying bits with similar indices.
mutable ElementListIter CurrElementIter;
ElementList Elements;
// This is like std::lower_bound, except we do linear searching from the
// current position.
@ -441,22 +441,12 @@ class SparseBitVector {
public:
using iterator = SparseBitVectorIterator;
SparseBitVector() {
CurrElementIter = Elements.begin();
}
SparseBitVector() : Elements(), CurrElementIter(Elements.begin()) {}
// SparseBitVector copy ctor.
SparseBitVector(const SparseBitVector &RHS) {
ElementListConstIter ElementIter = RHS.Elements.begin();
while (ElementIter != RHS.Elements.end()) {
Elements.push_back(SparseBitVectorElement<ElementSize>(*ElementIter));
++ElementIter;
}
CurrElementIter = Elements.begin ();
}
~SparseBitVector() = default;
SparseBitVector(const SparseBitVector &RHS)
: Elements(RHS.Elements), CurrElementIter(Elements.begin()) {}
SparseBitVector(SparseBitVector &&RHS)
: Elements(std::move(RHS.Elements)), CurrElementIter(Elements.begin()) {}
// Clear.
void clear() {
@ -468,16 +458,13 @@ public:
if (this == &RHS)
return *this;
Elements.clear();
ElementListConstIter ElementIter = RHS.Elements.begin();
while (ElementIter != RHS.Elements.end()) {
Elements.push_back(SparseBitVectorElement<ElementSize>(*ElementIter));
++ElementIter;
}
CurrElementIter = Elements.begin ();
Elements = RHS.Elements;
CurrElementIter = Elements.begin();
return *this;
}
SparseBitVector &operator=(SparseBitVector &&RHS) {
Elements = std::move(RHS.Elements);
CurrElementIter = Elements.begin();
return *this;
}

View File

@ -36,6 +36,22 @@ TEST(SparseBitVectorTest, TrivialOperation) {
const SparseBitVector<> ConstVec = Vec;
EXPECT_TRUE(ConstVec.test(5));
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) {