diff options
author | Chris Lattner <sabre@nondot.org> | 2007-02-04 00:42:41 +0000 |
---|---|---|
committer | Chris Lattner <sabre@nondot.org> | 2007-02-04 00:42:41 +0000 |
commit | 28f72279f5aaf34ba62ae00ca3b8bb94d8b91f70 (patch) | |
tree | 891c9aeb60b532fa44cc4bad67900afecccb3d60 /include | |
parent | 6734b57d1bcd743491b627979f7801f94c97a970 (diff) | |
download | llvm-28f72279f5aaf34ba62ae00ca3b8bb94d8b91f70.tar.gz llvm-28f72279f5aaf34ba62ae00ca3b8bb94d8b91f70.tar.bz2 llvm-28f72279f5aaf34ba62ae00ca3b8bb94d8b91f70.tar.xz |
add a version of insert that takes the key and value.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@33856 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'include')
-rw-r--r-- | include/llvm/ADT/DenseMap.h | 31 |
1 files changed, 23 insertions, 8 deletions
diff --git a/include/llvm/ADT/DenseMap.h b/include/llvm/ADT/DenseMap.h index 646ee40c15..5d1ad7546e 100644 --- a/include/llvm/ADT/DenseMap.h +++ b/include/llvm/ADT/DenseMap.h @@ -111,6 +111,16 @@ public: return end(); } + bool insert(const std::pair<KeyT, ValueT> &KV) { + BucketT *TheBucket; + if (LookupBucketFor(KV.first, TheBucket)) + return false; // Already in map. + + // Otherwise, insert the new element. + InsertIntoBucket(KV.first, KV.second, TheBucket); + return true; + } + bool erase(const KeyT &Val) { BucketT *TheBucket; if (!LookupBucketFor(Val, TheBucket)) @@ -129,23 +139,28 @@ public: return true; } - ValueT &operator[](const KeyT &Val) { + ValueT &operator[](const KeyT &Key) { BucketT *TheBucket; - if (LookupBucketFor(Val, TheBucket)) + if (LookupBucketFor(Key, TheBucket)) return TheBucket->second; + return InsertIntoBucket(Key, ValueT(), TheBucket)->second; + } + +private: + BucketT *InsertIntoBucket(const KeyT &Key, const ValueT &Value, + BucketT *TheBucket) { // If the load of the hash table is more than 3/4, grow it. if (NumEntries*4 >= NumBuckets*3) { this->grow(); - LookupBucketFor(Val, TheBucket); + LookupBucketFor(Key, TheBucket); } ++NumEntries; - TheBucket->first = Val; - new (&TheBucket->second) ValueT(); - return TheBucket->second; + TheBucket->first = Key; + new (&TheBucket->second) ValueT(Value); + return TheBucket; } - -private: + static unsigned getHashValue(const KeyT &Val) { return DenseMapKeyInfo<KeyT>::getHashValue(Val); } |