DenseMap: Factor destruction into a common helper method.
authorBenjamin Kramer <benny.kra@googlemail.com>
Sun, 27 May 2012 17:38:18 +0000 (17:38 +0000)
committerBenjamin Kramer <benny.kra@googlemail.com>
Sun, 27 May 2012 17:38:18 +0000 (17:38 +0000)
llvm-svn: 157538

llvm/include/llvm/ADT/DenseMap.h

index f7ae346..4b0df5d 100644 (file)
@@ -62,20 +62,9 @@ public:
     init(NextPowerOf2(std::distance(I, E)));
     insert(I, E);
   }
-  
+
   ~DenseMap() {
-    const KeyT EmptyKey = getEmptyKey(), TombstoneKey = getTombstoneKey();
-    for (BucketT *P = Buckets, *E = Buckets+NumBuckets; P != E; ++P) {
-      if (!KeyInfoT::isEqual(P->first, EmptyKey) &&
-          !KeyInfoT::isEqual(P->first, TombstoneKey))
-        P->second.~ValueT();
-      P->first.~KeyT();
-    }
-#ifndef NDEBUG
-    if (NumBuckets)
-      memset((void*)Buckets, 0x5a, sizeof(BucketT)*NumBuckets);
-#endif
-    operator delete(Buckets);
+    DestroyAll();
   }
 
   typedef DenseMapIterator<KeyT, ValueT, KeyInfoT> iterator;
@@ -254,28 +243,28 @@ public:
   const void *getPointerIntoBucketsArray() const { return Buckets; }
 
 private:
-  void CopyFrom(const DenseMap& other) {
-    if (NumBuckets != 0 &&
-        (!isPodLike<KeyT>::value || !isPodLike<ValueT>::value)) {
-      const KeyT EmptyKey = getEmptyKey(), TombstoneKey = getTombstoneKey();
-      for (BucketT *P = Buckets, *E = Buckets+NumBuckets; P != E; ++P) {
-        if (!KeyInfoT::isEqual(P->first, EmptyKey) &&
-            !KeyInfoT::isEqual(P->first, TombstoneKey))
-          P->second.~ValueT();
-        P->first.~KeyT();
-      }
+  void DestroyAll() {
+    const KeyT EmptyKey = getEmptyKey(), TombstoneKey = getTombstoneKey();
+    for (BucketT *P = Buckets, *E = Buckets+NumBuckets; P != E; ++P) {
+      if (!KeyInfoT::isEqual(P->first, EmptyKey) &&
+          !KeyInfoT::isEqual(P->first, TombstoneKey))
+        P->second.~ValueT();
+      P->first.~KeyT();
     }
 
-    NumEntries = other.NumEntries;
-    NumTombstones = other.NumTombstones;
-
     if (NumBuckets) {
 #ifndef NDEBUG
       memset((void*)Buckets, 0x5a, sizeof(BucketT)*NumBuckets);
 #endif
       operator delete(Buckets);
     }
+  }
 
+  void CopyFrom(const DenseMap& other) {
+    DestroyAll();
+
+    NumEntries = other.NumEntries;
+    NumTombstones = other.NumTombstones;
     NumBuckets = other.NumBuckets;
 
     if (NumBuckets == 0) {