llvm.org GIT mirror llvm / 2b94fa7
StringMap: Move assignment and move construction. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@208361 91177308-0d34-0410-b5e6-96231b3b80d8 David Blaikie 5 years ago
2 changed file(s) with 80 addition(s) and 8 deletion(s). Raw diff Collapse all Expand all
5252 : TheTable(nullptr),
5353 // Initialize the map with zero buckets to allocation.
5454 NumBuckets(0), NumItems(0), NumTombstones(0), ItemSize(itemSize) {}
55 StringMapImpl(StringMapImpl &&RHS) : TheTable(RHS.TheTable), NumBuckets(RHS.NumBuckets), NumItems(RHS.NumItems), NumTombstones(RHS.NumTombstones), ItemSize(RHS.ItemSize) {
56 RHS.TheTable = nullptr;
57 RHS.NumBuckets = 0;
58 RHS.NumItems = 0;
59 RHS.NumTombstones = 0;
60 }
5561
5662 StringMapImpl(unsigned InitSize, unsigned ItemSize);
5763 void RehashTable();
232238 Allocator(A) {}
233239
234240 StringMap(StringMap &&RHS)
235 : StringMapImpl(static_cast(sizeof(MapEntryTy))) {
236 assert(RHS.empty());
237 }
238 StringMap &operator=(StringMap &&RHS) {
239 assert(RHS.empty());
240 clear();
241 : StringMapImpl(std::move(RHS)), Allocator(std::move(RHS.Allocator)) {}
242
243 StringMap &operator=(StringMap RHS) {
244 StringMapImpl::swap(RHS);
245 std::swap(Allocator, RHS.Allocator);
241246 return *this;
242247 }
243 StringMap(const StringMap &RHS) LLVM_DELETED_FUNCTION;
244 void operator=(const StringMap &RHS) LLVM_DELETED_FUNCTION;
248
249 // FIXME: Implement copy operations if/when they're needed.
245250
246251 AllocatorTy &getAllocator() { return Allocator; }
247252 const AllocatorTy &getAllocator() const { return Allocator; }
233233 StringMapEntry::Create(Key.begin(), Key.end(), MoveOnly(42))->Destroy();
234234 }
235235
236 TEST_F(StringMapTest, MoveConstruct) {
237 StringMap A;
238 A.GetOrCreateValue("x", 42);
239 StringMap B = std::move(A);
240 ASSERT_EQ(A.size(), 0u);
241 ASSERT_EQ(B.size(), 1u);
242 ASSERT_EQ(B["x"], 42);
243 ASSERT_EQ(B.count("y"), 0u);
244 }
245
246 TEST_F(StringMapTest, MoveAssignment) {
247 StringMap A;
248 A["x"] = 42;
249 StringMap B;
250 B["y"] = 117;
251 A = std::move(B);
252 ASSERT_EQ(A.size(), 1u);
253 ASSERT_EQ(B.size(), 0u);
254 ASSERT_EQ(A["y"], 117);
255 ASSERT_EQ(B.count("x"), 0u);
256 }
257
258 struct Countable {
259 int &InstanceCount;
260 int Number;
261 Countable(int Number, int &InstanceCount) :InstanceCount(InstanceCount), Number(Number) {
262 ++InstanceCount;
263 }
264 Countable(Countable &&C) : InstanceCount(C.InstanceCount), Number(C.Number) {
265 ++InstanceCount;
266 C.Number = -1;
267 }
268 Countable(const Countable &C) : InstanceCount(C.InstanceCount), Number(C.Number) {
269 ++InstanceCount;
270 }
271 Countable &operator=(Countable C) {
272 Number = C.Number;
273 return *this;
274 }
275 ~Countable() {
276 --InstanceCount;
277 }
278
279 };
280
281 TEST_F(StringMapTest, MoveDtor) {
282 int InstanceCount = 0;
283 StringMap A;
284 A.GetOrCreateValue("x", Countable(42, InstanceCount));
285 ASSERT_EQ(InstanceCount, 1);
286 auto I = A.find("x");
287 ASSERT_NE(I, A.end());
288 ASSERT_EQ(I->second.Number, 42);
289
290 StringMap B;
291 B = std::move(A);
292 ASSERT_EQ(InstanceCount, 1);
293 ASSERT_TRUE(A.empty());
294 I = B.find("x");
295 ASSERT_NE(I, B.end());
296 ASSERT_EQ(I->second.Number, 42);
297
298 B = StringMap();
299 ASSERT_EQ(InstanceCount, 0);
300 ASSERT_TRUE(B.empty());
301 }
302
236303 } // end anonymous namespace