Line data Source code
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 27543 : 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 27453 : ~StringMapImpl() { free(TheTable); }
57 : unsigned RehashTable(unsigned BucketNo = 0);
58 :
59 : /// LookupBucketFor - Look up the bucket that the specified string should end
60 : /// up in. If it already exists as a key in the map, the Item pointer for the
61 : /// specified bucket will be non-null. Otherwise, it will be null. In either
62 : /// case, the FullHashValue field of the bucket will be set to the hash value
63 : /// of the string.
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);
70 :
71 : /// FindKey - Look up the bucket that contains the specified key. If it exists
72 : /// in the map, return the bucket number of the key. Otherwise return -1.
73 : /// This does not modify the map.
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;
78 :
79 : /// RemoveKey - Remove the specified StringMapEntry from the table, but do not
80 : /// delete it. This aborts if the value isn't in the table.
81 : void RemoveKey(StringMapEntryBase *V);
82 :
83 : /// RemoveKey - Remove the StringMapEntry for the specified key from the
84 : /// table, returning it. If the key is not in the table, this returns null.
85 : StringMapEntryBase *RemoveKey(StringRef Key);
86 :
87 : /// Allocate the table with the specified number of buckets and otherwise
88 : /// setup the map as empty.
89 : void init(unsigned Size);
90 :
91 : public:
92 : static constexpr uintptr_t TombstoneIntVal =
93 : static_cast<uintptr_t>(-1)
94 : << PointerLikeTypeTraits<StringMapEntryBase *>::NumLowBitsAvailable;
95 :
96 282578 : static StringMapEntryBase *getTombstoneVal() {
97 282578 : return reinterpret_cast<StringMapEntryBase *>(TombstoneIntVal);
98 : }
99 :
100 : unsigned getNumBuckets() const { return NumBuckets; }
101 : unsigned getNumItems() const { return NumItems; }
102 :
103 28458 : bool empty() const { return NumItems == 0; }
104 : unsigned size() const { return NumItems; }
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 :
113 : void swap(StringMapImpl &Other) {
114 : std::swap(TheTable, Other.TheTable);
115 : std::swap(NumBuckets, Other.NumBuckets);
116 : std::swap(NumItems, Other.NumItems);
117 : std::swap(NumTombstones, Other.NumTombstones);
118 : }
119 : };
120 :
121 : /// StringMap - This is an unconventional map that is specialized for handling
122 : /// keys that are "strings", which are basically ranges of bytes. This does some
123 : /// funky memory allocation and hashing things to make it extremely efficient,
124 : /// storing the string data *after* the value in the map.
125 : template <typename ValueTy, typename AllocatorTy = MallocAllocator>
126 : class LLVM_ALLOCATORHOLDER_EMPTYBASE StringMap
127 : : public StringMapImpl,
128 : private detail::AllocatorHolder<AllocatorTy> {
129 : using AllocTy = detail::AllocatorHolder<AllocatorTy>;
130 :
131 : public:
132 : using MapEntryTy = StringMapEntry<ValueTy>;
133 :
134 27543 : StringMap() : StringMapImpl(static_cast<unsigned>(sizeof(MapEntryTy))) {}
135 :
136 : explicit StringMap(unsigned InitialSize)
137 : : StringMapImpl(InitialSize, static_cast<unsigned>(sizeof(MapEntryTy))) {}
138 :
139 : explicit StringMap(AllocatorTy A)
140 : : StringMapImpl(static_cast<unsigned>(sizeof(MapEntryTy))), AllocTy(A) {}
141 :
142 : StringMap(unsigned InitialSize, AllocatorTy A)
143 : : StringMapImpl(InitialSize, static_cast<unsigned>(sizeof(MapEntryTy))),
144 : AllocTy(A) {}
145 :
146 : StringMap(std::initializer_list<std::pair<StringRef, ValueTy>> List)
147 : : StringMapImpl(List.size(), static_cast<unsigned>(sizeof(MapEntryTy))) {
148 : insert(List);
149 : }
150 :
151 : StringMap(StringMap &&RHS)
152 : : StringMapImpl(std::move(RHS)), AllocTy(std::move(RHS.getAllocator())) {}
153 :
154 : StringMap(const StringMap &RHS)
155 : : StringMapImpl(static_cast<unsigned>(sizeof(MapEntryTy))),
156 : AllocTy(RHS.getAllocator()) {
157 : if (RHS.empty())
158 : return;
159 :
160 : // Allocate TheTable of the same size as RHS's TheTable, and set the
161 : // sentinel appropriately (and NumBuckets).
162 : init(RHS.NumBuckets);
163 : unsigned *HashTable = (unsigned *)(TheTable + NumBuckets + 1),
164 : *RHSHashTable = (unsigned *)(RHS.TheTable + NumBuckets + 1);
165 :
166 : NumItems = RHS.NumItems;
167 : NumTombstones = RHS.NumTombstones;
168 : for (unsigned I = 0, E = NumBuckets; I != E; ++I) {
169 : StringMapEntryBase *Bucket = RHS.TheTable[I];
170 : if (!Bucket || Bucket == getTombstoneVal()) {
171 : TheTable[I] = Bucket;
172 : continue;
173 : }
174 :
175 : TheTable[I] = MapEntryTy::create(
176 : static_cast<MapEntryTy *>(Bucket)->getKey(), getAllocator(),
177 : static_cast<MapEntryTy *>(Bucket)->getValue());
178 : HashTable[I] = RHSHashTable[I];
179 : }
180 :
181 : // Note that here we've copied everything from the RHS into this object,
182 : // tombstones included. We could, instead, have re-probed for each key to
183 : // instantiate this new object without any tombstone buckets. The
184 : // assumption here is that items are rarely deleted from most StringMaps,
185 : // and so tombstones are rare, so the cost of re-probing for all inputs is
186 : // not worthwhile.
187 : }
188 :
189 : StringMap &operator=(StringMap RHS) {
190 : StringMapImpl::swap(RHS);
191 : std::swap(getAllocator(), RHS.getAllocator());
192 : return *this;
193 : }
194 :
195 27453 : ~StringMap() {
196 : // Delete all the elements in the map, but don't reset the elements
197 : // to default values. This is a copy of clear(), but avoids unnecessary
198 : // work not required in the destructor.
199 27453 : if (!empty()) {
200 188654 : for (unsigned I = 0, E = NumBuckets; I != E; ++I) {
201 183728 : StringMapEntryBase *Bucket = TheTable[I];
202 183728 : if (Bucket && Bucket != getTombstoneVal()) {
203 68211 : static_cast<MapEntryTy *>(Bucket)->Destroy(getAllocator());
204 : }
205 : }
206 : }
207 27453 : }
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 3786 : iterator begin() { return iterator(TheTable, NumBuckets == 0); }
220 18835 : iterator end() { return iterator(TheTable + NumBuckets, true); }
221 1005 : const_iterator begin() const {
222 1005 : return const_iterator(TheTable, NumBuckets == 0);
223 : }
224 1005 : const_iterator end() const {
225 1005 : 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 10242 : iterator find(StringRef Key) { return find(Key, hash(Key)); }
234 :
235 10242 : iterator find(StringRef Key, uint32_t FullHashValue) {
236 10242 : int Bucket = FindKey(Key, FullHashValue);
237 10242 : if (Bucket == -1)
238 4852 : return end();
239 5390 : 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 11725 : 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 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.
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.
328 1060 : std::pair<iterator, bool> insert(std::pair<StringRef, ValueTy> KV) {
329 1060 : return try_emplace_with_hash(KV.first, hash(KV.first),
330 2120 : std::move(KV.second));
331 : }
332 :
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 .
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
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>
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>
368 77913 : std::pair<iterator, bool> try_emplace(StringRef Key, ArgsTy &&...Args) {
369 77913 : return try_emplace_with_hash(Key, hash(Key), std::forward<ArgsTy>(Args)...);
370 : }
371 :
372 : template <typename... ArgsTy>
373 78973 : std::pair<iterator, bool> try_emplace_with_hash(StringRef Key,
374 : uint32_t FullHashValue,
375 : ArgsTy &&...Args) {
376 78973 : unsigned BucketNo = LookupBucketFor(Key, FullHashValue);
377 78973 : StringMapEntryBase *&Bucket = TheTable[BucketNo];
378 78973 : if (Bucket && Bucket != getTombstoneVal())
379 9657 : return std::make_pair(iterator(TheTable + BucketNo, false),
380 19314 : false); // Already exists in map.
381 :
382 69316 : if (Bucket == getTombstoneVal())
383 604 : --NumTombstones;
384 69316 : Bucket =
385 69316 : MapEntryTy::create(Key, getAllocator(), std::forward<ArgsTy>(Args)...);
386 69316 : ++NumItems;
387 69316 : assert(NumItems + NumTombstones <= NumBuckets);
388 :
389 69316 : BucketNo = RehashTable(BucketNo);
390 69316 : return std::make_pair(iterator(TheTable + BucketNo, false), true);
391 : }
392 :
393 : // clear - Empties out the StringMap
394 0 : void clear() {
395 0 : if (empty())
396 0 : 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 0 : for (unsigned I = 0, E = NumBuckets; I != E; ++I) {
401 0 : StringMapEntryBase *&Bucket = TheTable[I];
402 0 : if (Bucket && Bucket != getTombstoneVal()) {
403 0 : static_cast<MapEntryTy *>(Bucket)->Destroy(getAllocator());
404 : }
405 0 : Bucket = nullptr;
406 : }
407 :
408 0 : NumItems = 0;
409 0 : 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.
414 1005 : void remove(MapEntryTy *KeyValue) { RemoveKey(KeyValue); }
415 :
416 1005 : void erase(iterator I) {
417 1005 : MapEntryTy &V = *I;
418 1005 : remove(&V);
419 1005 : V.Destroy(getAllocator());
420 1005 : }
421 :
422 1005 : bool erase(StringRef Key) {
423 1005 : iterator I = find(Key);
424 1005 : if (I == end())
425 0 : return false;
426 1005 : erase(I);
427 1005 : 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 108994 : explicit StringMapIterBase(StringMapEntryBase **Bucket,
442 : bool NoAdvance = false)
443 108994 : : Ptr(Bucket) {
444 108994 : if (!NoAdvance)
445 83431 : AdvancePastEmptyBuckets();
446 108994 : }
447 :
448 : DerivedTy &operator=(const DerivedTy &Other) {
449 : Ptr = Other.Ptr;
450 : return static_cast<DerivedTy &>(*this);
451 : }
452 :
453 66347 : friend bool operator==(const DerivedTy &LHS, const DerivedTy &RHS) {
454 66347 : return LHS.Ptr == RHS.Ptr;
455 : }
456 :
457 51359 : DerivedTy &operator++() { // Preincrement
458 51359 : ++Ptr;
459 51359 : AdvancePastEmptyBuckets();
460 51359 : 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:
470 134790 : void AdvancePastEmptyBuckets() {
471 230407 : while (*Ptr == nullptr || *Ptr == StringMapImpl::getTombstoneVal())
472 95617 : ++Ptr;
473 134790 : }
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 2010 : explicit StringMapConstIterator(StringMapEntryBase **Bucket,
486 : bool NoAdvance = false)
487 2010 : : base(Bucket, NoAdvance) {}
488 :
489 2192 : const StringMapEntry<ValueTy> &operator*() const {
490 2192 : 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 106984 : explicit StringMapIterator(StringMapEntryBase **Bucket,
503 : bool NoAdvance = false)
504 106984 : : base(Bucket, NoAdvance) {}
505 :
506 74040 : StringMapEntry<ValueTy> &operator*() const {
507 74040 : 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;
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
|