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:
StringMapImpl(unsigned itemSize)44   explicit StringMapImpl(unsigned itemSize) : ItemSize(itemSize) {}
StringMapImpl(StringMapImpl && RHS)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.
LookupBucketFor(StringRef Key)63   unsigned LookupBucketFor(StringRef Key) {
64     return LookupBucketFor(Key, 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.
FindKey(StringRef Key)73   int FindKey(StringRef Key) const { return FindKey(Key, 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 
getTombstoneVal()95   static StringMapEntryBase *getTombstoneVal() {
96     return reinterpret_cast<StringMapEntryBase *>(TombstoneIntVal);
97   }
98 
getNumBuckets()99   unsigned getNumBuckets() const { return NumBuckets; }
getNumItems()100   unsigned getNumItems() const { return NumItems; }
101 
empty()102   bool empty() const { return NumItems == 0; }
size()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 
swap(StringMapImpl & Other)112   void swap(StringMapImpl &Other) {
113     std::swap(TheTable, Other.TheTable);
114     std::swap(NumBuckets, Other.NumBuckets);
115     std::swap(NumItems, Other.NumItems);
116     std::swap(NumTombstones, 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 
StringMap()133   StringMap() : StringMapImpl(static_cast<unsigned>(sizeof(MapEntryTy))) {}
134 
StringMap(unsigned InitialSize)135   explicit StringMap(unsigned InitialSize)
136       : StringMapImpl(InitialSize, static_cast<unsigned>(sizeof(MapEntryTy))) {}
137 
StringMap(AllocatorTy A)138   explicit StringMap(AllocatorTy A)
139       : StringMapImpl(static_cast<unsigned>(sizeof(MapEntryTy))), AllocTy(A) {}
140 
StringMap(unsigned InitialSize,AllocatorTy A)141   StringMap(unsigned InitialSize, AllocatorTy A)
142       : StringMapImpl(InitialSize, static_cast<unsigned>(sizeof(MapEntryTy))),
143         AllocTy(A) {}
144 
StringMap(std::initializer_list<std::pair<StringRef,ValueTy>> List)145   StringMap(std::initializer_list<std::pair<StringRef, ValueTy>> List)
146       : StringMapImpl(List.size(), static_cast<unsigned>(sizeof(MapEntryTy))) {
147     insert(List);
148   }
149 
StringMap(StringMap && RHS)150   StringMap(StringMap &&RHS)
151       : StringMapImpl(std::move(RHS)), AllocTy(std::move(RHS.getAllocator())) {}
152 
StringMap(const StringMap & RHS)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(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(RHS);
190     std::swap(getAllocator(), RHS.getAllocator());
191     return *this;
192   }
193 
~StringMap()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 
begin()219   iterator begin() { return iterator(TheTable, NumBuckets == 0); }
end()220   iterator end() { return iterator(TheTable + NumBuckets, true); }
begin()221   const_iterator begin() const {
222     return const_iterator(TheTable, NumBuckets == 0);
223   }
end()224   const_iterator end() const {
225     return const_iterator(TheTable + NumBuckets, true);
226   }
227 
keys()228   iterator_range<StringMapKeyIterator<ValueTy>> keys() const {
229     return make_range(StringMapKeyIterator<ValueTy>(begin()),
230                       StringMapKeyIterator<ValueTy>(end()));
231   }
232 
find(StringRef Key)233   iterator find(StringRef Key) { return find(Key, hash(Key)); }
234 
find(StringRef Key,uint32_t FullHashValue)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 
find(StringRef Key)242   const_iterator find(StringRef Key) const { return find(Key, hash(Key)); }
243 
find(StringRef Key,uint32_t FullHashValue)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.
lookup(StringRef Key)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.
at(StringRef Val)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.
contains(StringRef Key)273   bool contains(StringRef Key) const { return find(Key) != end(); }
274 
275   /// count - Return 1 if the element is in the map, 0 otherwise.
count(StringRef Key)276   size_type count(StringRef Key) const { return contains(Key) ? 1 : 0; }
277 
278   template <typename InputTy>
count(const StringMapEntry<InputTy> & MapEntry)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 constexpr (!std::is_same_v<ValueTy, std::nullopt_t>) {
295         if (!(KeyValue.getValue() == FindInRHS->getValue()))
296           return false;
297       }
298     }
299 
300     return true;
301   }
302 
303   bool operator!=(const StringMap &RHS) const { return !(*this == RHS); }
304 
305   /// insert - Insert the specified key/value pair into the map.  If the key
306   /// already exists in the map, return false and ignore the request, otherwise
307   /// insert it and return true.
insert(MapEntryTy * KeyValue)308   bool insert(MapEntryTy *KeyValue) {
309     unsigned BucketNo = LookupBucketFor(KeyValue->getKey());
310     StringMapEntryBase *&Bucket = TheTable[BucketNo];
311     if (Bucket && Bucket != getTombstoneVal())
312       return false; // Already exists in map.
313 
314     if (Bucket == getTombstoneVal())
315       --NumTombstones;
316     Bucket = KeyValue;
317     ++NumItems;
318     assert(NumItems + NumTombstones <= NumBuckets);
319 
320     RehashTable();
321     return true;
322   }
323 
324   /// insert - Inserts the specified key/value pair into the map if the key
325   /// isn't already in the map. The bool component of the returned pair is true
326   /// if and only if the insertion takes place, and the iterator component of
327   /// the pair points to the element with key equivalent to the key of the pair.
insert(std::pair<StringRef,ValueTy> KV)328   std::pair<iterator, bool> insert(std::pair<StringRef, ValueTy> KV) {
329     return try_emplace_with_hash(KV.first, hash(KV.first),
330                                  std::move(KV.second));
331   }
332 
insert(std::pair<StringRef,ValueTy> KV,uint32_t FullHashValue)333   std::pair<iterator, bool> insert(std::pair<StringRef, ValueTy> KV,
334                                    uint32_t FullHashValue) {
335     return try_emplace_with_hash(KV.first, FullHashValue, std::move(KV.second));
336   }
337 
338   /// Inserts elements from range [first, last). If multiple elements in the
339   /// range have keys that compare equivalent, it is unspecified which element
340   /// is inserted .
insert(InputIt First,InputIt Last)341   template <typename InputIt> void insert(InputIt First, InputIt Last) {
342     for (InputIt It = First; It != Last; ++It)
343       insert(*It);
344   }
345 
346   ///  Inserts elements from initializer list ilist. If multiple elements in
347   /// the range have keys that compare equivalent, it is unspecified which
348   /// element is inserted
insert(std::initializer_list<std::pair<StringRef,ValueTy>> List)349   void insert(std::initializer_list<std::pair<StringRef, ValueTy>> List) {
350     insert(List.begin(), List.end());
351   }
352 
353   /// Inserts an element or assigns to the current element if the key already
354   /// exists. The return type is the same as try_emplace.
355   template <typename V>
insert_or_assign(StringRef Key,V && Val)356   std::pair<iterator, bool> insert_or_assign(StringRef Key, V &&Val) {
357     auto Ret = try_emplace(Key, std::forward<V>(Val));
358     if (!Ret.second)
359       Ret.first->second = std::forward<V>(Val);
360     return Ret;
361   }
362 
363   /// Emplace a new element for the specified key into the map if the key isn't
364   /// already in the map. The bool component of the returned pair is true
365   /// if and only if the insertion takes place, and the iterator component of
366   /// the pair points to the element with key equivalent to the key of the pair.
367   template <typename... ArgsTy>
try_emplace(StringRef Key,ArgsTy &&...Args)368   std::pair<iterator, bool> try_emplace(StringRef Key, ArgsTy &&...Args) {
369     return try_emplace_with_hash(Key, hash(Key), std::forward<ArgsTy>(Args)...);
370   }
371 
372   template <typename... ArgsTy>
try_emplace_with_hash(StringRef Key,uint32_t FullHashValue,ArgsTy &&...Args)373   std::pair<iterator, bool> try_emplace_with_hash(StringRef Key,
374                                                   uint32_t FullHashValue,
375                                                   ArgsTy &&...Args) {
376     unsigned BucketNo = LookupBucketFor(Key, FullHashValue);
377     StringMapEntryBase *&Bucket = TheTable[BucketNo];
378     if (Bucket && Bucket != getTombstoneVal())
379       return std::make_pair(iterator(TheTable + BucketNo, false),
380                             false); // Already exists in map.
381 
382     if (Bucket == getTombstoneVal())
383       --NumTombstones;
384     Bucket =
385         MapEntryTy::create(Key, getAllocator(), std::forward<ArgsTy>(Args)...);
386     ++NumItems;
387     assert(NumItems + NumTombstones <= NumBuckets);
388 
389     BucketNo = RehashTable(BucketNo);
390     return std::make_pair(iterator(TheTable + BucketNo, false), true);
391   }
392 
393   // clear - Empties out the StringMap
clear()394   void clear() {
395     if (empty())
396       return;
397 
398     // Zap all values, resetting the keys back to non-present (not tombstone),
399     // which is safe because we're removing all elements.
400     for (unsigned I = 0, E = NumBuckets; I != E; ++I) {
401       StringMapEntryBase *&Bucket = TheTable[I];
402       if (Bucket && Bucket != getTombstoneVal()) {
403         static_cast<MapEntryTy *>(Bucket)->Destroy(getAllocator());
404       }
405       Bucket = nullptr;
406     }
407 
408     NumItems = 0;
409     NumTombstones = 0;
410   }
411 
412   /// remove - Remove the specified key/value pair from the map, but do not
413   /// erase it.  This aborts if the key is not in the map.
remove(MapEntryTy * KeyValue)414   void remove(MapEntryTy *KeyValue) { RemoveKey(KeyValue); }
415 
erase(iterator I)416   void erase(iterator I) {
417     MapEntryTy &V = *I;
418     remove(&V);
419     V.Destroy(getAllocator());
420   }
421 
erase(StringRef Key)422   bool erase(StringRef Key) {
423     iterator I = find(Key);
424     if (I == end())
425       return false;
426     erase(I);
427     return true;
428   }
429 };
430 
431 template <typename DerivedTy, typename ValueTy>
432 class StringMapIterBase
433     : public iterator_facade_base<DerivedTy, std::forward_iterator_tag,
434                                   ValueTy> {
435 protected:
436   StringMapEntryBase **Ptr = nullptr;
437 
438 public:
439   StringMapIterBase() = default;
440 
441   explicit StringMapIterBase(StringMapEntryBase **Bucket,
442                              bool NoAdvance = false)
Ptr(Bucket)443       : Ptr(Bucket) {
444     if (!NoAdvance)
445       AdvancePastEmptyBuckets();
446   }
447 
448   DerivedTy &operator=(const DerivedTy &Other) {
449     Ptr = Other.Ptr;
450     return static_cast<DerivedTy &>(*this);
451   }
452 
453   friend bool operator==(const DerivedTy &LHS, const DerivedTy &RHS) {
454     return LHS.Ptr == RHS.Ptr;
455   }
456 
457   DerivedTy &operator++() { // Preincrement
458     ++Ptr;
459     AdvancePastEmptyBuckets();
460     return static_cast<DerivedTy &>(*this);
461   }
462 
463   DerivedTy operator++(int) { // Post-increment
464     DerivedTy Tmp(Ptr);
465     ++*this;
466     return Tmp;
467   }
468 
469 private:
AdvancePastEmptyBuckets()470   void AdvancePastEmptyBuckets() {
471     while (*Ptr == nullptr || *Ptr == StringMapImpl::getTombstoneVal())
472       ++Ptr;
473   }
474 };
475 
476 template <typename ValueTy>
477 class StringMapConstIterator
478     : public StringMapIterBase<StringMapConstIterator<ValueTy>,
479                                const StringMapEntry<ValueTy>> {
480   using base = StringMapIterBase<StringMapConstIterator<ValueTy>,
481                                  const StringMapEntry<ValueTy>>;
482 
483 public:
484   StringMapConstIterator() = default;
485   explicit StringMapConstIterator(StringMapEntryBase **Bucket,
486                                   bool NoAdvance = false)
base(Bucket,NoAdvance)487       : base(Bucket, NoAdvance) {}
488 
489   const StringMapEntry<ValueTy> &operator*() const {
490     return *static_cast<const StringMapEntry<ValueTy> *>(*this->Ptr);
491   }
492 };
493 
494 template <typename ValueTy>
495 class StringMapIterator : public StringMapIterBase<StringMapIterator<ValueTy>,
496                                                    StringMapEntry<ValueTy>> {
497   using base =
498       StringMapIterBase<StringMapIterator<ValueTy>, StringMapEntry<ValueTy>>;
499 
500 public:
501   StringMapIterator() = default;
502   explicit StringMapIterator(StringMapEntryBase **Bucket,
503                              bool NoAdvance = false)
base(Bucket,NoAdvance)504       : base(Bucket, NoAdvance) {}
505 
506   StringMapEntry<ValueTy> &operator*() const {
507     return *static_cast<StringMapEntry<ValueTy> *>(*this->Ptr);
508   }
509 
510   operator StringMapConstIterator<ValueTy>() const {
511     return StringMapConstIterator<ValueTy>(this->Ptr, true);
512   }
513 };
514 
515 template <typename ValueTy>
516 class StringMapKeyIterator
517     : public iterator_adaptor_base<StringMapKeyIterator<ValueTy>,
518                                    StringMapConstIterator<ValueTy>,
519                                    std::forward_iterator_tag, StringRef> {
520   using base = iterator_adaptor_base<StringMapKeyIterator<ValueTy>,
521                                      StringMapConstIterator<ValueTy>,
522                                      std::forward_iterator_tag, StringRef>;
523 
524 public:
525   StringMapKeyIterator() = default;
StringMapKeyIterator(StringMapConstIterator<ValueTy> Iter)526   explicit StringMapKeyIterator(StringMapConstIterator<ValueTy> Iter)
527       : base(std::move(Iter)) {}
528 
529   StringRef operator*() const { return this->wrapped()->getKey(); }
530 };
531 
532 } // end namespace llvm
533 
534 #endif // LLVM_ADT_STRINGMAP_H
535