llvm.org GIT mirror llvm / 67280e1
Add a copy constructor and an assignment operator to DenseMap. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@41114 91177308-0d34-0410-b5e6-96231b3b80d8 Owen Anderson 12 years ago
1 changed file(s) with 32 addition(s) and 1 deletion(s). Raw diff Collapse all Expand all
5656
5757 unsigned NumEntries;
5858 unsigned NumTombstones;
59 DenseMap(const DenseMap &); // not implemented.
6059 public:
60 DenseMap(const DenseMap& other) {
61 NumBuckets = 0;
62 CopyFrom(other);
63 }
64
6165 explicit DenseMap(unsigned NumInitBuckets = 64) {
6266 init(NumInitBuckets);
6367 }
68
6469 ~DenseMap() {
6570 const KeyT EmptyKey = getEmptyKey(), TombstoneKey = getTombstoneKey();
6671 for (BucketT *P = Buckets, *E = Buckets+NumBuckets; P != E; ++P) {
168173 return InsertIntoBucket(Key, ValueT(), TheBucket)->second;
169174 }
170175
176 DenseMap& operator=(const DenseMap& other) {
177 CopyFrom(other);
178 }
179
171180 private:
181 void CopyFrom(const DenseMap& other) {
182 if (NumEntries != 0) {
183 const KeyT EmptyKey = getEmptyKey(), TombstoneKey = getTombstoneKey();
184 for (BucketT *P = Buckets, *E = Buckets+NumBuckets; P != E; ++P) {
185 if (P->first != EmptyKey && P->first != TombstoneKey)
186 P->second.~ValueT();
187 P->first.~KeyT();
188 }
189 }
190
191 NumEntries = other.NumEntries;
192 NumTombstones = other.NumTombstones;
193
194 if (NumBuckets)
195 delete[] reinterpret_cast(Buckets);
196 Buckets = reinterpret_cast(new char[sizeof(BucketT) *
197 other.NumBuckets]);
198 memcpy(Buckets, other.Buckets, other.NumBuckets * sizeof(BucketT));
199
200 NumBuckets = other.NumBuckets;
201 }
202
172203 BucketT *InsertIntoBucket(const KeyT &Key, const ValueT &Value,
173204 BucketT *TheBucket) {
174205 // If the load of the hash table is more than 3/4, or if fewer than 1/8 of