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