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