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