|
17 | 17 | #include "llvm/ADT/StringMapEntry.h"
|
18 | 18 | #include "llvm/ADT/iterator.h"
|
19 | 19 | #include "llvm/Support/AllocatorBase.h"
|
| 20 | +#include "llvm/Support/DJB.h" |
20 | 21 | #include "llvm/Support/PointerLikeTypeTraits.h"
|
21 | 22 | #include <initializer_list>
|
22 | 23 | #include <iterator>
|
@@ -60,12 +61,20 @@ class StringMapImpl {
|
60 | 61 | /// specified bucket will be non-null. Otherwise, it will be null. In either
|
61 | 62 | /// case, the FullHashValue field of the bucket will be set to the hash value
|
62 | 63 | /// of the string.
|
63 |
| - unsigned LookupBucketFor(StringRef Key); |
| 64 | + unsigned LookupBucketFor(StringRef Key) { |
| 65 | + return LookupBucketFor(Key, hash(Key)); |
| 66 | + } |
| 67 | + |
| 68 | + /// Overload that explicitly takes precomputed hash(Key). |
| 69 | + unsigned LookupBucketFor(StringRef Key, uint32_t FullHashValue); |
64 | 70 |
|
65 | 71 | /// FindKey - Look up the bucket that contains the specified key. If it exists
|
66 | 72 | /// in the map, return the bucket number of the key. Otherwise return -1.
|
67 | 73 | /// This does not modify the map.
|
68 |
| - int FindKey(StringRef Key) const; |
| 74 | + int FindKey(StringRef Key) const { return FindKey(Key, hash(Key)); } |
| 75 | + |
| 76 | + /// Overload that explicitly takes precomputed hash(Key). |
| 77 | + int FindKey(StringRef Key, uint32_t FullHashValue) const; |
69 | 78 |
|
70 | 79 | /// RemoveKey - Remove the specified StringMapEntry from the table, but do not
|
71 | 80 | /// delete it. This aborts if the value isn't in the table.
|
@@ -94,6 +103,13 @@ class StringMapImpl {
|
94 | 103 | bool empty() const { return NumItems == 0; }
|
95 | 104 | unsigned size() const { return NumItems; }
|
96 | 105 |
|
| 106 | + /// Returns the hash value that will be used for the given string. |
| 107 | + /// This allows precomputing the value and passing it explicitly |
| 108 | + /// to some of the functions. |
| 109 | + /// The implementation of this function is not guaranteed to be stable |
| 110 | + /// and may change. |
| 111 | + static uint32_t hash(StringRef Key); |
| 112 | + |
97 | 113 | void swap(StringMapImpl &Other) {
|
98 | 114 | std::swap(TheTable, Other.TheTable);
|
99 | 115 | std::swap(NumBuckets, Other.NumBuckets);
|
@@ -215,15 +231,19 @@ class LLVM_ALLOCATORHOLDER_EMPTYBASE StringMap
|
215 | 231 | StringMapKeyIterator<ValueTy>(end()));
|
216 | 232 | }
|
217 | 233 |
|
218 |
| - iterator find(StringRef Key) { |
219 |
| - int Bucket = FindKey(Key); |
| 234 | + iterator find(StringRef Key) { return find(Key, hash(Key)); } |
| 235 | + |
| 236 | + iterator find(StringRef Key, uint32_t FullHashValue) { |
| 237 | + int Bucket = FindKey(Key, FullHashValue); |
220 | 238 | if (Bucket == -1)
|
221 | 239 | return end();
|
222 | 240 | return iterator(TheTable + Bucket, true);
|
223 | 241 | }
|
224 | 242 |
|
225 |
| - const_iterator find(StringRef Key) const { |
226 |
| - int Bucket = FindKey(Key); |
| 243 | + const_iterator find(StringRef Key) const { return find(Key, hash(Key)); } |
| 244 | + |
| 245 | + const_iterator find(StringRef Key, uint32_t FullHashValue) const { |
| 246 | + int Bucket = FindKey(Key, FullHashValue); |
227 | 247 | if (Bucket == -1)
|
228 | 248 | return end();
|
229 | 249 | return const_iterator(TheTable + Bucket, true);
|
@@ -305,7 +325,13 @@ class LLVM_ALLOCATORHOLDER_EMPTYBASE StringMap
|
305 | 325 | /// if and only if the insertion takes place, and the iterator component of
|
306 | 326 | /// the pair points to the element with key equivalent to the key of the pair.
|
307 | 327 | std::pair<iterator, bool> insert(std::pair<StringRef, ValueTy> KV) {
|
308 |
| - return try_emplace(KV.first, std::move(KV.second)); |
| 328 | + return try_emplace_with_hash(KV.first, hash(KV.first), |
| 329 | + std::move(KV.second)); |
| 330 | + } |
| 331 | + |
| 332 | + std::pair<iterator, bool> insert(std::pair<StringRef, ValueTy> KV, |
| 333 | + uint32_t FullHashValue) { |
| 334 | + return try_emplace_with_hash(KV.first, FullHashValue, std::move(KV.second)); |
309 | 335 | }
|
310 | 336 |
|
311 | 337 | /// Inserts elements from range [first, last). If multiple elements in the
|
@@ -339,7 +365,14 @@ class LLVM_ALLOCATORHOLDER_EMPTYBASE StringMap
|
339 | 365 | /// the pair points to the element with key equivalent to the key of the pair.
|
340 | 366 | template <typename... ArgsTy>
|
341 | 367 | std::pair<iterator, bool> try_emplace(StringRef Key, ArgsTy &&...Args) {
|
342 |
| - unsigned BucketNo = LookupBucketFor(Key); |
| 368 | + return try_emplace_with_hash(Key, hash(Key), std::forward<ArgsTy>(Args)...); |
| 369 | + } |
| 370 | + |
| 371 | + template <typename... ArgsTy> |
| 372 | + std::pair<iterator, bool> try_emplace_with_hash(StringRef Key, |
| 373 | + uint32_t FullHashValue, |
| 374 | + ArgsTy &&...Args) { |
| 375 | + unsigned BucketNo = LookupBucketFor(Key, FullHashValue); |
343 | 376 | StringMapEntryBase *&Bucket = TheTable[BucketNo];
|
344 | 377 | if (Bucket && Bucket != getTombstoneVal())
|
345 | 378 | return std::make_pair(iterator(TheTable + BucketNo, false),
|
|
0 commit comments