StringMap: Move assignment and move construction.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@208361 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
David Blaikie 2014-05-08 21:52:29 +00:00
parent 06eb84e21a
commit 2b94fa799b
2 changed files with 80 additions and 8 deletions

View File

@ -53,6 +53,12 @@ protected:
: TheTable(nullptr),
// Initialize the map with zero buckets to allocation.
NumBuckets(0), NumItems(0), NumTombstones(0), ItemSize(itemSize) {}
StringMapImpl(StringMapImpl &&RHS) : TheTable(RHS.TheTable), NumBuckets(RHS.NumBuckets), NumItems(RHS.NumItems), NumTombstones(RHS.NumTombstones), ItemSize(RHS.ItemSize) {
RHS.TheTable = nullptr;
RHS.NumBuckets = 0;
RHS.NumItems = 0;
RHS.NumTombstones = 0;
}
StringMapImpl(unsigned InitSize, unsigned ItemSize);
void RehashTable();
@ -233,16 +239,15 @@ public:
Allocator(A) {}
StringMap(StringMap &&RHS)
: StringMapImpl(static_cast<unsigned>(sizeof(MapEntryTy))) {
assert(RHS.empty());
}
StringMap &operator=(StringMap &&RHS) {
assert(RHS.empty());
clear();
: StringMapImpl(std::move(RHS)), Allocator(std::move(RHS.Allocator)) {}
StringMap &operator=(StringMap RHS) {
StringMapImpl::swap(RHS);
std::swap(Allocator, RHS.Allocator);
return *this;
}
StringMap(const StringMap &RHS) LLVM_DELETED_FUNCTION;
void operator=(const StringMap &RHS) LLVM_DELETED_FUNCTION;
// FIXME: Implement copy operations if/when they're needed.
AllocatorTy &getAllocator() { return Allocator; }
const AllocatorTy &getAllocator() const { return Allocator; }

View File

@ -234,4 +234,71 @@ TEST_F(StringMapTest, MoveOnlyKey) {
StringMapEntry<MoveOnly>::Create(Key.begin(), Key.end(), MoveOnly(42))->Destroy();
}
TEST_F(StringMapTest, MoveConstruct) {
StringMap<int> A;
A.GetOrCreateValue("x", 42);
StringMap<int> B = std::move(A);
ASSERT_EQ(A.size(), 0u);
ASSERT_EQ(B.size(), 1u);
ASSERT_EQ(B["x"], 42);
ASSERT_EQ(B.count("y"), 0u);
}
TEST_F(StringMapTest, MoveAssignment) {
StringMap<int> A;
A["x"] = 42;
StringMap<int> B;
B["y"] = 117;
A = std::move(B);
ASSERT_EQ(A.size(), 1u);
ASSERT_EQ(B.size(), 0u);
ASSERT_EQ(A["y"], 117);
ASSERT_EQ(B.count("x"), 0u);
}
struct Countable {
int &InstanceCount;
int Number;
Countable(int Number, int &InstanceCount) :InstanceCount(InstanceCount), Number(Number) {
++InstanceCount;
}
Countable(Countable &&C) : InstanceCount(C.InstanceCount), Number(C.Number) {
++InstanceCount;
C.Number = -1;
}
Countable(const Countable &C) : InstanceCount(C.InstanceCount), Number(C.Number) {
++InstanceCount;
}
Countable &operator=(Countable C) {
Number = C.Number;
return *this;
}
~Countable() {
--InstanceCount;
}
};
TEST_F(StringMapTest, MoveDtor) {
int InstanceCount = 0;
StringMap<Countable> A;
A.GetOrCreateValue("x", Countable(42, InstanceCount));
ASSERT_EQ(InstanceCount, 1);
auto I = A.find("x");
ASSERT_NE(I, A.end());
ASSERT_EQ(I->second.Number, 42);
StringMap<Countable> B;
B = std::move(A);
ASSERT_EQ(InstanceCount, 1);
ASSERT_TRUE(A.empty());
I = B.find("x");
ASSERT_NE(I, B.end());
ASSERT_EQ(I->second.Number, 42);
B = StringMap<Countable>();
ASSERT_EQ(InstanceCount, 0);
ASSERT_TRUE(B.empty());
}
} // end anonymous namespace