1 | //===- StringMap.h - String Hash table map interface ------------*- C++ -*-===// |
2 | // |
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | // See https://llvm.org/LICENSE.txt for license information. |
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | /// |
9 | /// \file |
10 | /// This file defines the StringMap class. |
11 | /// |
12 | //===----------------------------------------------------------------------===// |
13 | |
14 | #ifndef LLVM_ADT_STRINGMAP_H |
15 | #define LLVM_ADT_STRINGMAP_H |
16 | |
17 | #include "llvm/ADT/StringMapEntry.h" |
18 | #include "llvm/ADT/iterator.h" |
19 | #include "llvm/Support/AllocatorBase.h" |
20 | #include "llvm/Support/PointerLikeTypeTraits.h" |
21 | #include <initializer_list> |
22 | #include <iterator> |
23 | |
24 | namespace llvm { |
25 | |
26 | template <typename ValueTy> class StringMapConstIterator; |
27 | template <typename ValueTy> class StringMapIterator; |
28 | template <typename ValueTy> class StringMapKeyIterator; |
29 | |
30 | /// StringMapImpl - This is the base class of StringMap that is shared among |
31 | /// all of its instantiations. |
32 | class StringMapImpl { |
33 | protected: |
34 | // Array of NumBuckets pointers to entries, null pointers are holes. |
35 | // TheTable[NumBuckets] contains a sentinel value for easy iteration. Followed |
36 | // by an array of the actual hash values as unsigned integers. |
37 | StringMapEntryBase **TheTable = nullptr; |
38 | unsigned NumBuckets = 0; |
39 | unsigned NumItems = 0; |
40 | unsigned NumTombstones = 0; |
41 | unsigned ItemSize; |
42 | |
43 | protected: |
44 | explicit StringMapImpl(unsigned itemSize) : ItemSize(itemSize) {} |
45 | StringMapImpl(StringMapImpl &&RHS) |
46 | : TheTable(RHS.TheTable), NumBuckets(RHS.NumBuckets), |
47 | NumItems(RHS.NumItems), NumTombstones(RHS.NumTombstones), |
48 | ItemSize(RHS.ItemSize) { |
49 | RHS.TheTable = nullptr; |
50 | RHS.NumBuckets = 0; |
51 | RHS.NumItems = 0; |
52 | RHS.NumTombstones = 0; |
53 | } |
54 | |
55 | StringMapImpl(unsigned InitSize, unsigned ItemSize); |
56 | unsigned RehashTable(unsigned BucketNo = 0); |
57 | |
58 | /// LookupBucketFor - Look up the bucket that the specified string should end |
59 | /// up in. If it already exists as a key in the map, the Item pointer for the |
60 | /// specified bucket will be non-null. Otherwise, it will be null. In either |
61 | /// case, the FullHashValue field of the bucket will be set to the hash value |
62 | /// of the string. |
63 | unsigned LookupBucketFor(StringRef Key) { |
64 | return LookupBucketFor(Key, FullHashValue: hash(Key)); |
65 | } |
66 | |
67 | /// Overload that explicitly takes precomputed hash(Key). |
68 | unsigned LookupBucketFor(StringRef Key, uint32_t FullHashValue); |
69 | |
70 | /// FindKey - Look up the bucket that contains the specified key. If it exists |
71 | /// in the map, return the bucket number of the key. Otherwise return -1. |
72 | /// This does not modify the map. |
73 | int FindKey(StringRef Key) const { return FindKey(Key, FullHashValue: hash(Key)); } |
74 | |
75 | /// Overload that explicitly takes precomputed hash(Key). |
76 | int FindKey(StringRef Key, uint32_t FullHashValue) const; |
77 | |
78 | /// RemoveKey - Remove the specified StringMapEntry from the table, but do not |
79 | /// delete it. This aborts if the value isn't in the table. |
80 | void RemoveKey(StringMapEntryBase *V); |
81 | |
82 | /// RemoveKey - Remove the StringMapEntry for the specified key from the |
83 | /// table, returning it. If the key is not in the table, this returns null. |
84 | StringMapEntryBase *RemoveKey(StringRef Key); |
85 | |
86 | /// Allocate the table with the specified number of buckets and otherwise |
87 | /// setup the map as empty. |
88 | void init(unsigned Size); |
89 | |
90 | public: |
91 | static constexpr uintptr_t TombstoneIntVal = |
92 | static_cast<uintptr_t>(-1) |
93 | << PointerLikeTypeTraits<StringMapEntryBase *>::NumLowBitsAvailable; |
94 | |
95 | static StringMapEntryBase *getTombstoneVal() { |
96 | return reinterpret_cast<StringMapEntryBase *>(TombstoneIntVal); |
97 | } |
98 | |
99 | unsigned getNumBuckets() const { return NumBuckets; } |
100 | unsigned getNumItems() const { return NumItems; } |
101 | |
102 | bool empty() const { return NumItems == 0; } |
103 | unsigned size() const { return NumItems; } |
104 | |
105 | /// Returns the hash value that will be used for the given string. |
106 | /// This allows precomputing the value and passing it explicitly |
107 | /// to some of the functions. |
108 | /// The implementation of this function is not guaranteed to be stable |
109 | /// and may change. |
110 | static uint32_t hash(StringRef Key); |
111 | |
112 | void swap(StringMapImpl &Other) { |
113 | std::swap(a&: TheTable, b&: Other.TheTable); |
114 | std::swap(a&: NumBuckets, b&: Other.NumBuckets); |
115 | std::swap(a&: NumItems, b&: Other.NumItems); |
116 | std::swap(a&: NumTombstones, b&: Other.NumTombstones); |
117 | } |
118 | }; |
119 | |
120 | /// StringMap - This is an unconventional map that is specialized for handling |
121 | /// keys that are "strings", which are basically ranges of bytes. This does some |
122 | /// funky memory allocation and hashing things to make it extremely efficient, |
123 | /// storing the string data *after* the value in the map. |
124 | template <typename ValueTy, typename AllocatorTy = MallocAllocator> |
125 | class LLVM_ALLOCATORHOLDER_EMPTYBASE StringMap |
126 | : public StringMapImpl, |
127 | private detail::AllocatorHolder<AllocatorTy> { |
128 | using AllocTy = detail::AllocatorHolder<AllocatorTy>; |
129 | |
130 | public: |
131 | using MapEntryTy = StringMapEntry<ValueTy>; |
132 | |
133 | StringMap() : StringMapImpl(static_cast<unsigned>(sizeof(MapEntryTy))) {} |
134 | |
135 | explicit StringMap(unsigned InitialSize) |
136 | : StringMapImpl(InitialSize, static_cast<unsigned>(sizeof(MapEntryTy))) {} |
137 | |
138 | explicit StringMap(AllocatorTy A) |
139 | : StringMapImpl(static_cast<unsigned>(sizeof(MapEntryTy))), AllocTy(A) {} |
140 | |
141 | StringMap(unsigned InitialSize, AllocatorTy A) |
142 | : StringMapImpl(InitialSize, static_cast<unsigned>(sizeof(MapEntryTy))), |
143 | AllocTy(A) {} |
144 | |
145 | StringMap(std::initializer_list<std::pair<StringRef, ValueTy>> List) |
146 | : StringMapImpl(List.size(), static_cast<unsigned>(sizeof(MapEntryTy))) { |
147 | insert(List); |
148 | } |
149 | |
150 | StringMap(StringMap &&RHS) |
151 | : StringMapImpl(std::move(RHS)), AllocTy(std::move(RHS.getAllocator())) {} |
152 | |
153 | StringMap(const StringMap &RHS) |
154 | : StringMapImpl(static_cast<unsigned>(sizeof(MapEntryTy))), |
155 | AllocTy(RHS.getAllocator()) { |
156 | if (RHS.empty()) |
157 | return; |
158 | |
159 | // Allocate TheTable of the same size as RHS's TheTable, and set the |
160 | // sentinel appropriately (and NumBuckets). |
161 | init(Size: RHS.NumBuckets); |
162 | unsigned *HashTable = (unsigned *)(TheTable + NumBuckets + 1), |
163 | *RHSHashTable = (unsigned *)(RHS.TheTable + NumBuckets + 1); |
164 | |
165 | NumItems = RHS.NumItems; |
166 | NumTombstones = RHS.NumTombstones; |
167 | for (unsigned I = 0, E = NumBuckets; I != E; ++I) { |
168 | StringMapEntryBase *Bucket = RHS.TheTable[I]; |
169 | if (!Bucket || Bucket == getTombstoneVal()) { |
170 | TheTable[I] = Bucket; |
171 | continue; |
172 | } |
173 | |
174 | TheTable[I] = MapEntryTy::create( |
175 | static_cast<MapEntryTy *>(Bucket)->getKey(), getAllocator(), |
176 | static_cast<MapEntryTy *>(Bucket)->getValue()); |
177 | HashTable[I] = RHSHashTable[I]; |
178 | } |
179 | |
180 | // Note that here we've copied everything from the RHS into this object, |
181 | // tombstones included. We could, instead, have re-probed for each key to |
182 | // instantiate this new object without any tombstone buckets. The |
183 | // assumption here is that items are rarely deleted from most StringMaps, |
184 | // and so tombstones are rare, so the cost of re-probing for all inputs is |
185 | // not worthwhile. |
186 | } |
187 | |
188 | StringMap &operator=(StringMap RHS) { |
189 | StringMapImpl::swap(Other&: RHS); |
190 | std::swap(getAllocator(), RHS.getAllocator()); |
191 | return *this; |
192 | } |
193 | |
194 | ~StringMap() { |
195 | // Delete all the elements in the map, but don't reset the elements |
196 | // to default values. This is a copy of clear(), but avoids unnecessary |
197 | // work not required in the destructor. |
198 | if (!empty()) { |
199 | for (unsigned I = 0, E = NumBuckets; I != E; ++I) { |
200 | StringMapEntryBase *Bucket = TheTable[I]; |
201 | if (Bucket && Bucket != getTombstoneVal()) { |
202 | static_cast<MapEntryTy *>(Bucket)->Destroy(getAllocator()); |
203 | } |
204 | } |
205 | } |
206 | free(TheTable); |
207 | } |
208 | |
209 | using AllocTy::getAllocator; |
210 | |
211 | using key_type = const char *; |
212 | using mapped_type = ValueTy; |
213 | using value_type = StringMapEntry<ValueTy>; |
214 | using size_type = size_t; |
215 | |
216 | using const_iterator = StringMapConstIterator<ValueTy>; |
217 | using iterator = StringMapIterator<ValueTy>; |
218 | |
219 | iterator begin() { return iterator(TheTable, NumBuckets == 0); } |
220 | iterator end() { return iterator(TheTable + NumBuckets, true); } |
221 | const_iterator begin() const { |
222 | return const_iterator(TheTable, NumBuckets == 0); |
223 | } |
224 | const_iterator end() const { |
225 | return const_iterator(TheTable + NumBuckets, true); |
226 | } |
227 | |
228 | iterator_range<StringMapKeyIterator<ValueTy>> keys() const { |
229 | return make_range(StringMapKeyIterator<ValueTy>(begin()), |
230 | StringMapKeyIterator<ValueTy>(end())); |
231 | } |
232 | |
233 | iterator find(StringRef Key) { return find(Key, hash(Key)); } |
234 | |
235 | iterator find(StringRef Key, uint32_t FullHashValue) { |
236 | int Bucket = FindKey(Key, FullHashValue); |
237 | if (Bucket == -1) |
238 | return end(); |
239 | return iterator(TheTable + Bucket, true); |
240 | } |
241 | |
242 | const_iterator find(StringRef Key) const { return find(Key, hash(Key)); } |
243 | |
244 | const_iterator find(StringRef Key, uint32_t FullHashValue) const { |
245 | int Bucket = FindKey(Key, FullHashValue); |
246 | if (Bucket == -1) |
247 | return end(); |
248 | return const_iterator(TheTable + Bucket, true); |
249 | } |
250 | |
251 | /// lookup - Return the entry for the specified key, or a default |
252 | /// constructed value if no such entry exists. |
253 | ValueTy lookup(StringRef Key) const { |
254 | const_iterator Iter = find(Key); |
255 | if (Iter != end()) |
256 | return Iter->second; |
257 | return ValueTy(); |
258 | } |
259 | |
260 | /// at - Return the entry for the specified key, or abort if no such |
261 | /// entry exists. |
262 | const ValueTy &at(StringRef Val) const { |
263 | auto Iter = this->find(std::move(Val)); |
264 | assert(Iter != this->end() && "StringMap::at failed due to a missing key" ); |
265 | return Iter->second; |
266 | } |
267 | |
268 | /// Lookup the ValueTy for the \p Key, or create a default constructed value |
269 | /// if the key is not in the map. |
270 | ValueTy &operator[](StringRef Key) { return try_emplace(Key).first->second; } |
271 | |
272 | /// contains - Return true if the element is in the map, false otherwise. |
273 | bool contains(StringRef Key) const { return find(Key) != end(); } |
274 | |
275 | /// count - Return 1 if the element is in the map, 0 otherwise. |
276 | size_type count(StringRef Key) const { return contains(Key) ? 1 : 0; } |
277 | |
278 | template <typename InputTy> |
279 | size_type count(const StringMapEntry<InputTy> &MapEntry) const { |
280 | return count(MapEntry.getKey()); |
281 | } |
282 | |
283 | /// equal - check whether both of the containers are equal. |
284 | bool operator==(const StringMap &RHS) const { |
285 | if (size() != RHS.size()) |
286 | return false; |
287 | |
288 | for (const auto &KeyValue : *this) { |
289 | auto FindInRHS = RHS.find(KeyValue.getKey()); |
290 | |
291 | if (FindInRHS == RHS.end()) |
292 | return false; |
293 | |
294 | if (!(KeyValue.getValue() == FindInRHS->getValue())) |
295 | return false; |
296 | } |
297 | |
298 | return true; |
299 | } |
300 | |
301 | bool operator!=(const StringMap &RHS) const { return !(*this == RHS); } |
302 | |
303 | /// insert - Insert the specified key/value pair into the map. If the key |
304 | /// already exists in the map, return false and ignore the request, otherwise |
305 | /// insert it and return true. |
306 | bool insert(MapEntryTy *KeyValue) { |
307 | unsigned BucketNo = LookupBucketFor(KeyValue->getKey()); |
308 | StringMapEntryBase *&Bucket = TheTable[BucketNo]; |
309 | if (Bucket && Bucket != getTombstoneVal()) |
310 | return false; // Already exists in map. |
311 | |
312 | if (Bucket == getTombstoneVal()) |
313 | --NumTombstones; |
314 | Bucket = KeyValue; |
315 | ++NumItems; |
316 | assert(NumItems + NumTombstones <= NumBuckets); |
317 | |
318 | RehashTable(); |
319 | return true; |
320 | } |
321 | |
322 | /// insert - Inserts the specified key/value pair into the map if the key |
323 | /// isn't already in the map. The bool component of the returned pair is true |
324 | /// if and only if the insertion takes place, and the iterator component of |
325 | /// the pair points to the element with key equivalent to the key of the pair. |
326 | std::pair<iterator, bool> insert(std::pair<StringRef, ValueTy> KV) { |
327 | return try_emplace_with_hash(KV.first, hash(Key: KV.first), |
328 | std::move(KV.second)); |
329 | } |
330 | |
331 | std::pair<iterator, bool> insert(std::pair<StringRef, ValueTy> KV, |
332 | uint32_t FullHashValue) { |
333 | return try_emplace_with_hash(KV.first, FullHashValue, std::move(KV.second)); |
334 | } |
335 | |
336 | /// Inserts elements from range [first, last). If multiple elements in the |
337 | /// range have keys that compare equivalent, it is unspecified which element |
338 | /// is inserted . |
339 | template <typename InputIt> void insert(InputIt First, InputIt Last) { |
340 | for (InputIt It = First; It != Last; ++It) |
341 | insert(*It); |
342 | } |
343 | |
344 | /// Inserts elements from initializer list ilist. If multiple elements in |
345 | /// the range have keys that compare equivalent, it is unspecified which |
346 | /// element is inserted |
347 | void insert(std::initializer_list<std::pair<StringRef, ValueTy>> List) { |
348 | insert(List.begin(), List.end()); |
349 | } |
350 | |
351 | /// Inserts an element or assigns to the current element if the key already |
352 | /// exists. The return type is the same as try_emplace. |
353 | template <typename V> |
354 | std::pair<iterator, bool> insert_or_assign(StringRef Key, V &&Val) { |
355 | auto Ret = try_emplace(Key, std::forward<V>(Val)); |
356 | if (!Ret.second) |
357 | Ret.first->second = std::forward<V>(Val); |
358 | return Ret; |
359 | } |
360 | |
361 | /// Emplace a new element for the specified key into the map if the key isn't |
362 | /// already in the map. The bool component of the returned pair is true |
363 | /// if and only if the insertion takes place, and the iterator component of |
364 | /// the pair points to the element with key equivalent to the key of the pair. |
365 | template <typename... ArgsTy> |
366 | std::pair<iterator, bool> try_emplace(StringRef Key, ArgsTy &&...Args) { |
367 | return try_emplace_with_hash(Key, hash(Key), std::forward<ArgsTy>(Args)...); |
368 | } |
369 | |
370 | template <typename... ArgsTy> |
371 | std::pair<iterator, bool> try_emplace_with_hash(StringRef Key, |
372 | uint32_t FullHashValue, |
373 | ArgsTy &&...Args) { |
374 | unsigned BucketNo = LookupBucketFor(Key, FullHashValue); |
375 | StringMapEntryBase *&Bucket = TheTable[BucketNo]; |
376 | if (Bucket && Bucket != getTombstoneVal()) |
377 | return std::make_pair(iterator(TheTable + BucketNo, false), |
378 | false); // Already exists in map. |
379 | |
380 | if (Bucket == getTombstoneVal()) |
381 | --NumTombstones; |
382 | Bucket = |
383 | MapEntryTy::create(Key, getAllocator(), std::forward<ArgsTy>(Args)...); |
384 | ++NumItems; |
385 | assert(NumItems + NumTombstones <= NumBuckets); |
386 | |
387 | BucketNo = RehashTable(BucketNo); |
388 | return std::make_pair(iterator(TheTable + BucketNo, false), true); |
389 | } |
390 | |
391 | // clear - Empties out the StringMap |
392 | void clear() { |
393 | if (empty()) |
394 | return; |
395 | |
396 | // Zap all values, resetting the keys back to non-present (not tombstone), |
397 | // which is safe because we're removing all elements. |
398 | for (unsigned I = 0, E = NumBuckets; I != E; ++I) { |
399 | StringMapEntryBase *&Bucket = TheTable[I]; |
400 | if (Bucket && Bucket != getTombstoneVal()) { |
401 | static_cast<MapEntryTy *>(Bucket)->Destroy(getAllocator()); |
402 | } |
403 | Bucket = nullptr; |
404 | } |
405 | |
406 | NumItems = 0; |
407 | NumTombstones = 0; |
408 | } |
409 | |
410 | /// remove - Remove the specified key/value pair from the map, but do not |
411 | /// erase it. This aborts if the key is not in the map. |
412 | void remove(MapEntryTy *KeyValue) { RemoveKey(KeyValue); } |
413 | |
414 | void erase(iterator I) { |
415 | MapEntryTy &V = *I; |
416 | remove(KeyValue: &V); |
417 | V.Destroy(getAllocator()); |
418 | } |
419 | |
420 | bool erase(StringRef Key) { |
421 | iterator I = find(Key); |
422 | if (I == end()) |
423 | return false; |
424 | erase(I); |
425 | return true; |
426 | } |
427 | }; |
428 | |
429 | template <typename DerivedTy, typename ValueTy> |
430 | class StringMapIterBase |
431 | : public iterator_facade_base<DerivedTy, std::forward_iterator_tag, |
432 | ValueTy> { |
433 | protected: |
434 | StringMapEntryBase **Ptr = nullptr; |
435 | |
436 | public: |
437 | StringMapIterBase() = default; |
438 | |
439 | explicit StringMapIterBase(StringMapEntryBase **Bucket, |
440 | bool NoAdvance = false) |
441 | : Ptr(Bucket) { |
442 | if (!NoAdvance) |
443 | AdvancePastEmptyBuckets(); |
444 | } |
445 | |
446 | DerivedTy &operator=(const DerivedTy &Other) { |
447 | Ptr = Other.Ptr; |
448 | return static_cast<DerivedTy &>(*this); |
449 | } |
450 | |
451 | friend bool operator==(const DerivedTy &LHS, const DerivedTy &RHS) { |
452 | return LHS.Ptr == RHS.Ptr; |
453 | } |
454 | |
455 | DerivedTy &operator++() { // Preincrement |
456 | ++Ptr; |
457 | AdvancePastEmptyBuckets(); |
458 | return static_cast<DerivedTy &>(*this); |
459 | } |
460 | |
461 | DerivedTy operator++(int) { // Post-increment |
462 | DerivedTy Tmp(Ptr); |
463 | ++*this; |
464 | return Tmp; |
465 | } |
466 | |
467 | private: |
468 | void AdvancePastEmptyBuckets() { |
469 | while (*Ptr == nullptr || *Ptr == StringMapImpl::getTombstoneVal()) |
470 | ++Ptr; |
471 | } |
472 | }; |
473 | |
474 | template <typename ValueTy> |
475 | class StringMapConstIterator |
476 | : public StringMapIterBase<StringMapConstIterator<ValueTy>, |
477 | const StringMapEntry<ValueTy>> { |
478 | using base = StringMapIterBase<StringMapConstIterator<ValueTy>, |
479 | const StringMapEntry<ValueTy>>; |
480 | |
481 | public: |
482 | StringMapConstIterator() = default; |
483 | explicit StringMapConstIterator(StringMapEntryBase **Bucket, |
484 | bool NoAdvance = false) |
485 | : base(Bucket, NoAdvance) {} |
486 | |
487 | const StringMapEntry<ValueTy> &operator*() const { |
488 | return *static_cast<const StringMapEntry<ValueTy> *>(*this->Ptr); |
489 | } |
490 | }; |
491 | |
492 | template <typename ValueTy> |
493 | class StringMapIterator : public StringMapIterBase<StringMapIterator<ValueTy>, |
494 | StringMapEntry<ValueTy>> { |
495 | using base = |
496 | StringMapIterBase<StringMapIterator<ValueTy>, StringMapEntry<ValueTy>>; |
497 | |
498 | public: |
499 | StringMapIterator() = default; |
500 | explicit StringMapIterator(StringMapEntryBase **Bucket, |
501 | bool NoAdvance = false) |
502 | : base(Bucket, NoAdvance) {} |
503 | |
504 | StringMapEntry<ValueTy> &operator*() const { |
505 | return *static_cast<StringMapEntry<ValueTy> *>(*this->Ptr); |
506 | } |
507 | |
508 | operator StringMapConstIterator<ValueTy>() const { |
509 | return StringMapConstIterator<ValueTy>(this->Ptr, true); |
510 | } |
511 | }; |
512 | |
513 | template <typename ValueTy> |
514 | class StringMapKeyIterator |
515 | : public iterator_adaptor_base<StringMapKeyIterator<ValueTy>, |
516 | StringMapConstIterator<ValueTy>, |
517 | std::forward_iterator_tag, StringRef> { |
518 | using base = iterator_adaptor_base<StringMapKeyIterator<ValueTy>, |
519 | StringMapConstIterator<ValueTy>, |
520 | std::forward_iterator_tag, StringRef>; |
521 | |
522 | public: |
523 | StringMapKeyIterator() = default; |
524 | explicit StringMapKeyIterator(StringMapConstIterator<ValueTy> Iter) |
525 | : base(std::move(Iter)) {} |
526 | |
527 | StringRef operator*() const { return this->wrapped()->getKey(); } |
528 | }; |
529 | |
530 | } // end namespace llvm |
531 | |
532 | #endif // LLVM_ADT_STRINGMAP_H |
533 | |