DenseMap.h revision 98153ecbc1b29c576ff43a7718c3f657f81b8aac
1//===- llvm/ADT/DenseMap.h - Dense probed hash table ------------*- C++ -*-===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file was developed by Chris Lattner and is distributed under 6// the University of Illinois Open Source License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9// 10// This file defines the DenseMap class. 11// 12//===----------------------------------------------------------------------===// 13 14#ifndef LLVM_ADT_DENSEMAP_H 15#define LLVM_ADT_DENSEMAP_H 16 17#include "llvm/Support/DataTypes.h" 18#include "llvm/Support/MathExtras.h" 19#include <cassert> 20#include <utility> 21 22namespace llvm { 23 24template<typename T> 25struct DenseMapKeyInfo { 26 //static inline T getEmptyKey(); 27 //static inline T getTombstoneKey(); 28 //static unsigned getHashValue(const T &Val); 29 //static bool isPod() 30}; 31 32// Provide DenseMapKeyInfo for all pointers. 33template<typename T> 34struct DenseMapKeyInfo<T*> { 35 static inline T* getEmptyKey() { return reinterpret_cast<T*>(-1); } 36 static inline T* getTombstoneKey() { return reinterpret_cast<T*>(-2); } 37 static unsigned getHashValue(const T *PtrVal) { 38 return (unsigned(uintptr_t(PtrVal)) >> 4) ^ 39 (unsigned(uintptr_t(PtrVal)) >> 9); 40 } 41 static bool isPod() { return true; } 42}; 43 44template<typename KeyT, typename ValueT, 45 typename KeyInfoT = DenseMapKeyInfo<KeyT> > 46class DenseMapIterator; 47template<typename KeyT, typename ValueT, 48 typename KeyInfoT = DenseMapKeyInfo<KeyT> > 49class DenseMapConstIterator; 50 51template<typename KeyT, typename ValueT, 52 typename KeyInfoT = DenseMapKeyInfo<KeyT> > 53class DenseMap { 54 typedef std::pair<KeyT, ValueT> BucketT; 55 unsigned NumBuckets; 56 BucketT *Buckets; 57 58 unsigned NumEntries; 59 unsigned NumTombstones; 60public: 61 DenseMap(const DenseMap& other) { 62 NumBuckets = 0; 63 CopyFrom(other); 64 } 65 66 explicit DenseMap(unsigned NumInitBuckets = 64) { 67 init(NumInitBuckets); 68 } 69 70 ~DenseMap() { 71 const KeyT EmptyKey = getEmptyKey(), TombstoneKey = getTombstoneKey(); 72 for (BucketT *P = Buckets, *E = Buckets+NumBuckets; P != E; ++P) { 73 if (P->first != EmptyKey && P->first != TombstoneKey) 74 P->second.~ValueT(); 75 P->first.~KeyT(); 76 } 77 delete[] reinterpret_cast<char*>(Buckets); 78 } 79 80 typedef DenseMapIterator<KeyT, ValueT, KeyInfoT> iterator; 81 typedef DenseMapConstIterator<KeyT, ValueT, KeyInfoT> const_iterator; 82 inline iterator begin() { 83 return iterator(Buckets, Buckets+NumBuckets); 84 } 85 inline iterator end() { 86 return iterator(Buckets+NumBuckets, Buckets+NumBuckets); 87 } 88 inline const_iterator begin() const { 89 return const_iterator(Buckets, Buckets+NumBuckets); 90 } 91 inline const_iterator end() const { 92 return const_iterator(Buckets+NumBuckets, Buckets+NumBuckets); 93 } 94 95 bool empty() const { return NumEntries == 0; } 96 unsigned size() const { return NumEntries; } 97 98 void clear() { 99 // If the capacity of the array is huge, and the # elements used is small, 100 // shrink the array. 101 if (NumEntries * 4 < NumBuckets && NumBuckets > 64) { 102 shrink_and_clear(); 103 return; 104 } 105 106 const KeyT EmptyKey = getEmptyKey(), TombstoneKey = getTombstoneKey(); 107 for (BucketT *P = Buckets, *E = Buckets+NumBuckets; P != E; ++P) { 108 if (P->first != EmptyKey) { 109 if (P->first != TombstoneKey) { 110 P->second.~ValueT(); 111 --NumEntries; 112 } 113 P->first = EmptyKey; 114 } 115 } 116 assert(NumEntries == 0 && "Node count imbalance!"); 117 NumTombstones = 0; 118 } 119 120 /// count - Return true if the specified key is in the map. 121 bool count(const KeyT &Val) const { 122 BucketT *TheBucket; 123 return LookupBucketFor(Val, TheBucket); 124 } 125 126 iterator find(const KeyT &Val) { 127 BucketT *TheBucket; 128 if (LookupBucketFor(Val, TheBucket)) 129 return iterator(TheBucket, Buckets+NumBuckets); 130 return end(); 131 } 132 const_iterator find(const KeyT &Val) const { 133 BucketT *TheBucket; 134 if (LookupBucketFor(Val, TheBucket)) 135 return const_iterator(TheBucket, Buckets+NumBuckets); 136 return end(); 137 } 138 139 bool insert(const std::pair<KeyT, ValueT> &KV) { 140 BucketT *TheBucket; 141 if (LookupBucketFor(KV.first, TheBucket)) 142 return false; // Already in map. 143 144 // Otherwise, insert the new element. 145 InsertIntoBucket(KV.first, KV.second, TheBucket); 146 return true; 147 } 148 149 bool erase(const KeyT &Val) { 150 BucketT *TheBucket; 151 if (!LookupBucketFor(Val, TheBucket)) 152 return false; // not in map. 153 154 TheBucket->second.~ValueT(); 155 TheBucket->first = getTombstoneKey(); 156 --NumEntries; 157 ++NumTombstones; 158 return true; 159 } 160 bool erase(iterator I) { 161 BucketT *TheBucket = &*I; 162 TheBucket->second.~ValueT(); 163 TheBucket->first = getTombstoneKey(); 164 --NumEntries; 165 ++NumTombstones; 166 return true; 167 } 168 169 ValueT &operator[](const KeyT &Key) { 170 BucketT *TheBucket; 171 if (LookupBucketFor(Key, TheBucket)) 172 return TheBucket->second; 173 174 return InsertIntoBucket(Key, ValueT(), TheBucket)->second; 175 } 176 177 DenseMap& operator=(const DenseMap& other) { 178 CopyFrom(other); 179 return *this; 180 } 181 182private: 183 void CopyFrom(const DenseMap& other) { 184 if (NumBuckets != 0 && !KeyInfoT::isPod()) { 185 const KeyT EmptyKey = getEmptyKey(), TombstoneKey = getTombstoneKey(); 186 for (BucketT *P = Buckets, *E = Buckets+NumBuckets; P != E; ++P) { 187 if (P->first != EmptyKey && P->first != TombstoneKey) 188 P->second.~ValueT(); 189 P->first.~KeyT(); 190 } 191 } 192 193 NumEntries = other.NumEntries; 194 NumTombstones = other.NumTombstones; 195 196 if (NumBuckets) 197 delete[] reinterpret_cast<char*>(Buckets); 198 Buckets = reinterpret_cast<BucketT*>(new char[sizeof(BucketT) * 199 other.NumBuckets]); 200 201 if (KeyInfoT::isPod()) 202 memcpy(Buckets, other.Buckets, other.NumBuckets * sizeof(BucketT)); 203 else 204 for (size_t i = 0; i < other.NumBuckets; ++i) { 205 new (Buckets[i].first) KeyT(other.Buckets[i].first); 206 if (Buckets[i].first != getEmptyKey() && 207 Buckets[i].first != getTombstoneKey()) 208 new (Buckets[i].second) ValueT(other.Buckets[i].second); 209 } 210 NumBuckets = other.NumBuckets; 211 } 212 213 BucketT *InsertIntoBucket(const KeyT &Key, const ValueT &Value, 214 BucketT *TheBucket) { 215 // If the load of the hash table is more than 3/4, or if fewer than 1/8 of 216 // the buckets are empty (meaning that many are filled with tombstones), 217 // grow the table. 218 // 219 // The later case is tricky. For example, if we had one empty bucket with 220 // tons of tombstones, failing lookups (e.g. for insertion) would have to 221 // probe almost the entire table until it found the empty bucket. If the 222 // table completely filled with tombstones, no lookup would ever succeed, 223 // causing infinite loops in lookup. 224 if (NumEntries*4 >= NumBuckets*3 || 225 NumBuckets-(NumEntries+NumTombstones) < NumBuckets/8) { 226 this->grow(); 227 LookupBucketFor(Key, TheBucket); 228 } 229 ++NumEntries; 230 231 // If we are writing over a tombstone, remember this. 232 if (TheBucket->first != getEmptyKey()) 233 --NumTombstones; 234 235 TheBucket->first = Key; 236 new (&TheBucket->second) ValueT(Value); 237 return TheBucket; 238 } 239 240 static unsigned getHashValue(const KeyT &Val) { 241 return KeyInfoT::getHashValue(Val); 242 } 243 static const KeyT getEmptyKey() { 244 return KeyInfoT::getEmptyKey(); 245 } 246 static const KeyT getTombstoneKey() { 247 return KeyInfoT::getTombstoneKey(); 248 } 249 250 /// LookupBucketFor - Lookup the appropriate bucket for Val, returning it in 251 /// FoundBucket. If the bucket contains the key and a value, this returns 252 /// true, otherwise it returns a bucket with an empty marker or tombstone and 253 /// returns false. 254 bool LookupBucketFor(const KeyT &Val, BucketT *&FoundBucket) const { 255 unsigned BucketNo = getHashValue(Val); 256 unsigned ProbeAmt = 1; 257 BucketT *BucketsPtr = Buckets; 258 259 // FoundTombstone - Keep track of whether we find a tombstone while probing. 260 BucketT *FoundTombstone = 0; 261 const KeyT EmptyKey = getEmptyKey(); 262 const KeyT TombstoneKey = getTombstoneKey(); 263 assert(Val != EmptyKey && Val != TombstoneKey && 264 "Empty/Tombstone value shouldn't be inserted into map!"); 265 266 while (1) { 267 BucketT *ThisBucket = BucketsPtr + (BucketNo & (NumBuckets-1)); 268 // Found Val's bucket? If so, return it. 269 if (ThisBucket->first == Val) { 270 FoundBucket = ThisBucket; 271 return true; 272 } 273 274 // If we found an empty bucket, the key doesn't exist in the set. 275 // Insert it and return the default value. 276 if (ThisBucket->first == EmptyKey) { 277 // If we've already seen a tombstone while probing, fill it in instead 278 // of the empty bucket we eventually probed to. 279 if (FoundTombstone) ThisBucket = FoundTombstone; 280 FoundBucket = FoundTombstone ? FoundTombstone : ThisBucket; 281 return false; 282 } 283 284 // If this is a tombstone, remember it. If Val ends up not in the map, we 285 // prefer to return it than something that would require more probing. 286 if (ThisBucket->first == TombstoneKey && !FoundTombstone) 287 FoundTombstone = ThisBucket; // Remember the first tombstone found. 288 289 // Otherwise, it's a hash collision or a tombstone, continue quadratic 290 // probing. 291 BucketNo += ProbeAmt++; 292 } 293 } 294 295 void init(unsigned InitBuckets) { 296 NumEntries = 0; 297 NumTombstones = 0; 298 NumBuckets = InitBuckets; 299 assert(InitBuckets && (InitBuckets & InitBuckets-1) == 0 && 300 "# initial buckets must be a power of two!"); 301 Buckets = reinterpret_cast<BucketT*>(new char[sizeof(BucketT)*InitBuckets]); 302 // Initialize all the keys to EmptyKey. 303 const KeyT EmptyKey = getEmptyKey(); 304 for (unsigned i = 0; i != InitBuckets; ++i) 305 new (&Buckets[i].first) KeyT(EmptyKey); 306 } 307 308 void grow() { 309 unsigned OldNumBuckets = NumBuckets; 310 BucketT *OldBuckets = Buckets; 311 312 // Double the number of buckets. 313 NumBuckets <<= 1; 314 NumTombstones = 0; 315 Buckets = reinterpret_cast<BucketT*>(new char[sizeof(BucketT)*NumBuckets]); 316 317 // Initialize all the keys to EmptyKey. 318 const KeyT EmptyKey = getEmptyKey(); 319 for (unsigned i = 0, e = NumBuckets; i != e; ++i) 320 new (&Buckets[i].first) KeyT(EmptyKey); 321 322 // Insert all the old elements. 323 const KeyT TombstoneKey = getTombstoneKey(); 324 for (BucketT *B = OldBuckets, *E = OldBuckets+OldNumBuckets; B != E; ++B) { 325 if (B->first != EmptyKey && B->first != TombstoneKey) { 326 // Insert the key/value into the new table. 327 BucketT *DestBucket; 328 bool FoundVal = LookupBucketFor(B->first, DestBucket); 329 FoundVal = FoundVal; // silence warning. 330 assert(!FoundVal && "Key already in new map?"); 331 DestBucket->first = B->first; 332 new (&DestBucket->second) ValueT(B->second); 333 334 // Free the value. 335 B->second.~ValueT(); 336 } 337 B->first.~KeyT(); 338 } 339 340 // Free the old table. 341 delete[] reinterpret_cast<char*>(OldBuckets); 342 } 343 344 void shrink_and_clear() { 345 unsigned OldNumBuckets = NumBuckets; 346 BucketT *OldBuckets = Buckets; 347 348 // Reduce the number of buckets. 349 NumBuckets = NumEntries > 32 ? 1 << (Log2_32_Ceil(NumEntries) + 1) 350 : 64; 351 NumTombstones = 0; 352 Buckets = reinterpret_cast<BucketT*>(new char[sizeof(BucketT)*NumBuckets]); 353 354 // Initialize all the keys to EmptyKey. 355 const KeyT EmptyKey = getEmptyKey(); 356 for (unsigned i = 0, e = NumBuckets; i != e; ++i) 357 new (&Buckets[i].first) KeyT(EmptyKey); 358 359 // Free the old buckets. 360 const KeyT TombstoneKey = getTombstoneKey(); 361 for (BucketT *B = OldBuckets, *E = OldBuckets+OldNumBuckets; B != E; ++B) { 362 if (B->first != EmptyKey && B->first != TombstoneKey) { 363 // Free the value. 364 B->second.~ValueT(); 365 } 366 B->first.~KeyT(); 367 } 368 369 // Free the old table. 370 delete[] reinterpret_cast<char*>(OldBuckets); 371 372 NumEntries = 0; 373 } 374}; 375 376template<typename KeyT, typename ValueT, typename KeyInfoT> 377class DenseMapIterator { 378 typedef std::pair<KeyT, ValueT> BucketT; 379protected: 380 const BucketT *Ptr, *End; 381public: 382 DenseMapIterator(const BucketT *Pos, const BucketT *E) : Ptr(Pos), End(E) { 383 AdvancePastEmptyBuckets(); 384 } 385 386 std::pair<KeyT, ValueT> &operator*() const { 387 return *const_cast<BucketT*>(Ptr); 388 } 389 std::pair<KeyT, ValueT> *operator->() const { 390 return const_cast<BucketT*>(Ptr); 391 } 392 393 bool operator==(const DenseMapIterator &RHS) const { 394 return Ptr == RHS.Ptr; 395 } 396 bool operator!=(const DenseMapIterator &RHS) const { 397 return Ptr != RHS.Ptr; 398 } 399 400 inline DenseMapIterator& operator++() { // Preincrement 401 ++Ptr; 402 AdvancePastEmptyBuckets(); 403 return *this; 404 } 405 DenseMapIterator operator++(int) { // Postincrement 406 DenseMapIterator tmp = *this; ++*this; return tmp; 407 } 408 409private: 410 void AdvancePastEmptyBuckets() { 411 const KeyT Empty = KeyInfoT::getEmptyKey(); 412 const KeyT Tombstone = KeyInfoT::getTombstoneKey(); 413 414 while (Ptr != End && (Ptr->first == Empty || Ptr->first == Tombstone)) 415 ++Ptr; 416 } 417}; 418 419template<typename KeyT, typename ValueT, typename KeyInfoT> 420class DenseMapConstIterator : public DenseMapIterator<KeyT, ValueT, KeyInfoT> { 421public: 422 DenseMapConstIterator(const std::pair<KeyT, ValueT> *Pos, 423 const std::pair<KeyT, ValueT> *E) 424 : DenseMapIterator<KeyT, ValueT, KeyInfoT>(Pos, E) { 425 } 426 const std::pair<KeyT, ValueT> &operator*() const { 427 return *this->Ptr; 428 } 429 const std::pair<KeyT, ValueT> *operator->() const { 430 return this->Ptr; 431 } 432}; 433 434} // end namespace llvm 435 436#endif 437