[llvm-commits] [llvm] r41114 - /llvm/trunk/include/llvm/ADT/DenseMap.h

Owen Anderson resistor at mac.com
Wed Aug 15 17:18:33 PDT 2007


Author: resistor
Date: Wed Aug 15 19:18:32 2007
New Revision: 41114

URL: http://llvm.org/viewvc/llvm-project?rev=41114&view=rev
Log:
Add a copy constructor and an assignment operator to DenseMap.

Modified:
    llvm/trunk/include/llvm/ADT/DenseMap.h

Modified: llvm/trunk/include/llvm/ADT/DenseMap.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ADT/DenseMap.h?rev=41114&r1=41113&r2=41114&view=diff

==============================================================================
--- llvm/trunk/include/llvm/ADT/DenseMap.h (original)
+++ llvm/trunk/include/llvm/ADT/DenseMap.h Wed Aug 15 19:18:32 2007
@@ -57,11 +57,16 @@
   
   unsigned NumEntries;
   unsigned NumTombstones;
-  DenseMap(const DenseMap &); // not implemented.
 public:
+  DenseMap(const DenseMap& other) {
+    NumBuckets = 0;
+    CopyFrom(other);
+  }
+  
   explicit DenseMap(unsigned NumInitBuckets = 64) {
     init(NumInitBuckets);
   }
+  
   ~DenseMap() {
     const KeyT EmptyKey = getEmptyKey(), TombstoneKey = getTombstoneKey();
     for (BucketT *P = Buckets, *E = Buckets+NumBuckets; P != E; ++P) {
@@ -169,7 +174,33 @@
     return InsertIntoBucket(Key, ValueT(), TheBucket)->second;
   }
   
+  DenseMap& operator=(const DenseMap& other) {
+    CopyFrom(other);
+  }
+  
 private:
+  void CopyFrom(const DenseMap& other) {
+    if (NumEntries != 0) {
+      const KeyT EmptyKey = getEmptyKey(), TombstoneKey = getTombstoneKey();
+      for (BucketT *P = Buckets, *E = Buckets+NumBuckets; P != E; ++P) {
+        if (P->first != EmptyKey && P->first != TombstoneKey)
+          P->second.~ValueT();
+        P->first.~KeyT();
+      }
+    }
+    
+    NumEntries = other.NumEntries;
+    NumTombstones = other.NumTombstones;
+    
+    if (NumBuckets)
+      delete[] reinterpret_cast<char*>(Buckets);
+    Buckets = reinterpret_cast<BucketT*>(new char[sizeof(BucketT) *
+                                                  other.NumBuckets]);
+    memcpy(Buckets, other.Buckets, other.NumBuckets * sizeof(BucketT));
+    
+    NumBuckets = other.NumBuckets;
+  }
+  
   BucketT *InsertIntoBucket(const KeyT &Key, const ValueT &Value,
                             BucketT *TheBucket) {
     // If the load of the hash table is more than 3/4, or if fewer than 1/8 of





More information about the llvm-commits mailing list