1 //===- llvm/ADT/DenseMap.h - Dense probed hash table ------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the DenseMap class.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_ADT_DENSEMAP_H
15 #define LLVM_ADT_DENSEMAP_H
17 #include "llvm/Support/PointerLikeTypeTraits.h"
18 #include "llvm/Support/MathExtras.h"
27 //static inline T getEmptyKey();
28 //static inline T getTombstoneKey();
29 //static unsigned getHashValue(const T &Val);
30 //static bool isEqual(const T &LHS, const T &RHS);
34 // Provide DenseMapInfo for all pointers.
36 struct DenseMapInfo<T*> {
37 static inline T* getEmptyKey() {
39 Val <<= PointerLikeTypeTraits<T*>::NumLowBitsAvailable;
40 return reinterpret_cast<T*>(Val);
42 static inline T* getTombstoneKey() {
44 Val <<= PointerLikeTypeTraits<T*>::NumLowBitsAvailable;
45 return reinterpret_cast<T*>(Val);
47 static unsigned getHashValue(const T *PtrVal) {
48 return (unsigned((uintptr_t)PtrVal) >> 4) ^
49 (unsigned((uintptr_t)PtrVal) >> 9);
51 static bool isEqual(const T *LHS, const T *RHS) { return LHS == RHS; }
52 static bool isPod() { return true; }
55 // Provide DenseMapInfo for chars.
56 template<> struct DenseMapInfo<char> {
57 static inline char getEmptyKey() { return ~0; }
58 static inline char getTombstoneKey() { return ~0 - 1; }
59 static unsigned getHashValue(const char& Val) { return Val * 37; }
60 static bool isPod() { return true; }
61 static bool isEqual(const char &LHS, const char &RHS) {
66 // Provide DenseMapInfo for unsigned ints.
67 template<> struct DenseMapInfo<unsigned> {
68 static inline unsigned getEmptyKey() { return ~0; }
69 static inline unsigned getTombstoneKey() { return ~0 - 1; }
70 static unsigned getHashValue(const unsigned& Val) { return Val * 37; }
71 static bool isPod() { return true; }
72 static bool isEqual(const unsigned& LHS, const unsigned& RHS) {
77 // Provide DenseMapInfo for unsigned longs.
78 template<> struct DenseMapInfo<unsigned long> {
79 static inline unsigned long getEmptyKey() { return ~0L; }
80 static inline unsigned long getTombstoneKey() { return ~0L - 1L; }
81 static unsigned getHashValue(const unsigned long& Val) {
82 return (unsigned)(Val * 37L);
84 static bool isPod() { return true; }
85 static bool isEqual(const unsigned long& LHS, const unsigned long& RHS) {
90 // Provide DenseMapInfo for all pairs whose members have info.
91 template<typename T, typename U>
92 struct DenseMapInfo<std::pair<T, U> > {
93 typedef std::pair<T, U> Pair;
94 typedef DenseMapInfo<T> FirstInfo;
95 typedef DenseMapInfo<U> SecondInfo;
97 static inline Pair getEmptyKey() {
98 return std::make_pair(FirstInfo::getEmptyKey(),
99 SecondInfo::getEmptyKey());
101 static inline Pair getTombstoneKey() {
102 return std::make_pair(FirstInfo::getTombstoneKey(),
103 SecondInfo::getEmptyKey());
105 static unsigned getHashValue(const Pair& PairVal) {
106 uint64_t key = (uint64_t)FirstInfo::getHashValue(PairVal.first) << 32
107 | (uint64_t)SecondInfo::getHashValue(PairVal.second);
116 return (unsigned)key;
118 static bool isEqual(const Pair& LHS, const Pair& RHS) { return LHS == RHS; }
119 static bool isPod() { return FirstInfo::isPod() && SecondInfo::isPod(); }
122 template<typename KeyT, typename ValueT,
123 typename KeyInfoT = DenseMapInfo<KeyT>,
124 typename ValueInfoT = DenseMapInfo<ValueT> >
125 class DenseMapIterator;
126 template<typename KeyT, typename ValueT,
127 typename KeyInfoT = DenseMapInfo<KeyT>,
128 typename ValueInfoT = DenseMapInfo<ValueT> >
129 class DenseMapConstIterator;
131 template<typename KeyT, typename ValueT,
132 typename KeyInfoT = DenseMapInfo<KeyT>,
133 typename ValueInfoT = DenseMapInfo<ValueT> >
135 typedef std::pair<KeyT, ValueT> BucketT;
140 unsigned NumTombstones;
142 typedef KeyT key_type;
143 typedef ValueT mapped_type;
144 typedef BucketT value_type;
146 DenseMap(const DenseMap& other) {
151 explicit DenseMap(unsigned NumInitBuckets = 64) {
152 init(NumInitBuckets);
156 const KeyT EmptyKey = getEmptyKey(), TombstoneKey = getTombstoneKey();
157 for (BucketT *P = Buckets, *E = Buckets+NumBuckets; P != E; ++P) {
158 if (!KeyInfoT::isEqual(P->first, EmptyKey) &&
159 !KeyInfoT::isEqual(P->first, TombstoneKey))
163 operator delete(Buckets);
166 typedef DenseMapIterator<KeyT, ValueT, KeyInfoT> iterator;
167 typedef DenseMapConstIterator<KeyT, ValueT, KeyInfoT> const_iterator;
168 inline iterator begin() {
169 return iterator(Buckets, Buckets+NumBuckets);
171 inline iterator end() {
172 return iterator(Buckets+NumBuckets, Buckets+NumBuckets);
174 inline const_iterator begin() const {
175 return const_iterator(Buckets, Buckets+NumBuckets);
177 inline const_iterator end() const {
178 return const_iterator(Buckets+NumBuckets, Buckets+NumBuckets);
181 bool empty() const { return NumEntries == 0; }
182 unsigned size() const { return NumEntries; }
184 /// Grow the densemap so that it has at least Size buckets. Does not shrink
185 void resize(size_t Size) { grow(Size); }
188 // If the capacity of the array is huge, and the # elements used is small,
190 if (NumEntries * 4 < NumBuckets && NumBuckets > 64) {
195 const KeyT EmptyKey = getEmptyKey(), TombstoneKey = getTombstoneKey();
196 for (BucketT *P = Buckets, *E = Buckets+NumBuckets; P != E; ++P) {
197 if (!KeyInfoT::isEqual(P->first, EmptyKey)) {
198 if (!KeyInfoT::isEqual(P->first, TombstoneKey)) {
205 assert(NumEntries == 0 && "Node count imbalance!");
209 /// count - Return true if the specified key is in the map.
210 bool count(const KeyT &Val) const {
212 return LookupBucketFor(Val, TheBucket);
215 iterator find(const KeyT &Val) {
217 if (LookupBucketFor(Val, TheBucket))
218 return iterator(TheBucket, Buckets+NumBuckets);
221 const_iterator find(const KeyT &Val) const {
223 if (LookupBucketFor(Val, TheBucket))
224 return const_iterator(TheBucket, Buckets+NumBuckets);
228 /// lookup - Return the entry for the specified key, or a default
229 /// constructed value if no such entry exists.
230 ValueT lookup(const KeyT &Val) const {
232 if (LookupBucketFor(Val, TheBucket))
233 return TheBucket->second;
237 std::pair<iterator, bool> insert(const std::pair<KeyT, ValueT> &KV) {
239 if (LookupBucketFor(KV.first, TheBucket))
240 return std::make_pair(iterator(TheBucket, Buckets+NumBuckets),
241 false); // Already in map.
243 // Otherwise, insert the new element.
244 TheBucket = InsertIntoBucket(KV.first, KV.second, TheBucket);
245 return std::make_pair(iterator(TheBucket, Buckets+NumBuckets),
249 /// insert - Range insertion of pairs.
250 template<typename InputIt>
251 void insert(InputIt I, InputIt E) {
257 bool erase(const KeyT &Val) {
259 if (!LookupBucketFor(Val, TheBucket))
260 return false; // not in map.
262 TheBucket->second.~ValueT();
263 TheBucket->first = getTombstoneKey();
268 bool erase(iterator I) {
269 BucketT *TheBucket = &*I;
270 TheBucket->second.~ValueT();
271 TheBucket->first = getTombstoneKey();
277 value_type& FindAndConstruct(const KeyT &Key) {
279 if (LookupBucketFor(Key, TheBucket))
282 return *InsertIntoBucket(Key, ValueT(), TheBucket);
285 ValueT &operator[](const KeyT &Key) {
286 return FindAndConstruct(Key).second;
289 DenseMap& operator=(const DenseMap& other) {
294 /// isPointerIntoBucketsArray - Return true if the specified pointer points
295 /// somewhere into the DenseMap's array of buckets (i.e. either to a key or
296 /// value in the DenseMap).
297 bool isPointerIntoBucketsArray(const void *Ptr) const {
298 return Ptr >= Buckets && Ptr < Buckets+NumBuckets;
301 /// getPointerIntoBucketsArray() - Return an opaque pointer into the buckets
302 /// array. In conjunction with the previous method, this can be used to
303 /// determine whether an insertion caused the DenseMap to reallocate.
304 const void *getPointerIntoBucketsArray() const { return Buckets; }
307 void CopyFrom(const DenseMap& other) {
308 if (NumBuckets != 0 && (!KeyInfoT::isPod() || !ValueInfoT::isPod())) {
309 const KeyT EmptyKey = getEmptyKey(), TombstoneKey = getTombstoneKey();
310 for (BucketT *P = Buckets, *E = Buckets+NumBuckets; P != E; ++P) {
311 if (!KeyInfoT::isEqual(P->first, EmptyKey) &&
312 !KeyInfoT::isEqual(P->first, TombstoneKey))
318 NumEntries = other.NumEntries;
319 NumTombstones = other.NumTombstones;
322 operator delete(Buckets);
323 Buckets = static_cast<BucketT*>(operator new(sizeof(BucketT) *
326 if (KeyInfoT::isPod() && ValueInfoT::isPod())
327 memcpy(Buckets, other.Buckets, other.NumBuckets * sizeof(BucketT));
329 for (size_t i = 0; i < other.NumBuckets; ++i) {
330 new (&Buckets[i].first) KeyT(other.Buckets[i].first);
331 if (!KeyInfoT::isEqual(Buckets[i].first, getEmptyKey()) &&
332 !KeyInfoT::isEqual(Buckets[i].first, getTombstoneKey()))
333 new (&Buckets[i].second) ValueT(other.Buckets[i].second);
335 NumBuckets = other.NumBuckets;
338 BucketT *InsertIntoBucket(const KeyT &Key, const ValueT &Value,
339 BucketT *TheBucket) {
340 // If the load of the hash table is more than 3/4, or if fewer than 1/8 of
341 // the buckets are empty (meaning that many are filled with tombstones),
344 // The later case is tricky. For example, if we had one empty bucket with
345 // tons of tombstones, failing lookups (e.g. for insertion) would have to
346 // probe almost the entire table until it found the empty bucket. If the
347 // table completely filled with tombstones, no lookup would ever succeed,
348 // causing infinite loops in lookup.
350 if (NumEntries*4 >= NumBuckets*3 ||
351 NumBuckets-(NumEntries+NumTombstones) < NumBuckets/8) {
352 this->grow(NumBuckets * 2);
353 LookupBucketFor(Key, TheBucket);
356 // If we are writing over a tombstone, remember this.
357 if (!KeyInfoT::isEqual(TheBucket->first, getEmptyKey()))
360 TheBucket->first = Key;
361 new (&TheBucket->second) ValueT(Value);
365 static unsigned getHashValue(const KeyT &Val) {
366 return KeyInfoT::getHashValue(Val);
368 static const KeyT getEmptyKey() {
369 return KeyInfoT::getEmptyKey();
371 static const KeyT getTombstoneKey() {
372 return KeyInfoT::getTombstoneKey();
375 /// LookupBucketFor - Lookup the appropriate bucket for Val, returning it in
376 /// FoundBucket. If the bucket contains the key and a value, this returns
377 /// true, otherwise it returns a bucket with an empty marker or tombstone and
379 bool LookupBucketFor(const KeyT &Val, BucketT *&FoundBucket) const {
380 unsigned BucketNo = getHashValue(Val);
381 unsigned ProbeAmt = 1;
382 BucketT *BucketsPtr = Buckets;
384 // FoundTombstone - Keep track of whether we find a tombstone while probing.
385 BucketT *FoundTombstone = 0;
386 const KeyT EmptyKey = getEmptyKey();
387 const KeyT TombstoneKey = getTombstoneKey();
388 assert(!KeyInfoT::isEqual(Val, EmptyKey) &&
389 !KeyInfoT::isEqual(Val, TombstoneKey) &&
390 "Empty/Tombstone value shouldn't be inserted into map!");
393 BucketT *ThisBucket = BucketsPtr + (BucketNo & (NumBuckets-1));
394 // Found Val's bucket? If so, return it.
395 if (KeyInfoT::isEqual(ThisBucket->first, Val)) {
396 FoundBucket = ThisBucket;
400 // If we found an empty bucket, the key doesn't exist in the set.
401 // Insert it and return the default value.
402 if (KeyInfoT::isEqual(ThisBucket->first, EmptyKey)) {
403 // If we've already seen a tombstone while probing, fill it in instead
404 // of the empty bucket we eventually probed to.
405 if (FoundTombstone) ThisBucket = FoundTombstone;
406 FoundBucket = FoundTombstone ? FoundTombstone : ThisBucket;
410 // If this is a tombstone, remember it. If Val ends up not in the map, we
411 // prefer to return it than something that would require more probing.
412 if (KeyInfoT::isEqual(ThisBucket->first, TombstoneKey) && !FoundTombstone)
413 FoundTombstone = ThisBucket; // Remember the first tombstone found.
415 // Otherwise, it's a hash collision or a tombstone, continue quadratic
417 BucketNo += ProbeAmt++;
421 void init(unsigned InitBuckets) {
424 NumBuckets = InitBuckets;
425 assert(InitBuckets && (InitBuckets & (InitBuckets-1)) == 0 &&
426 "# initial buckets must be a power of two!");
427 Buckets = static_cast<BucketT*>(operator new(sizeof(BucketT)*InitBuckets));
428 // Initialize all the keys to EmptyKey.
429 const KeyT EmptyKey = getEmptyKey();
430 for (unsigned i = 0; i != InitBuckets; ++i)
431 new (&Buckets[i].first) KeyT(EmptyKey);
434 void grow(unsigned AtLeast) {
435 unsigned OldNumBuckets = NumBuckets;
436 BucketT *OldBuckets = Buckets;
438 // Double the number of buckets.
439 while (NumBuckets <= AtLeast)
442 Buckets = static_cast<BucketT*>(operator new(sizeof(BucketT)*NumBuckets));
444 // Initialize all the keys to EmptyKey.
445 const KeyT EmptyKey = getEmptyKey();
446 for (unsigned i = 0, e = NumBuckets; i != e; ++i)
447 new (&Buckets[i].first) KeyT(EmptyKey);
449 // Insert all the old elements.
450 const KeyT TombstoneKey = getTombstoneKey();
451 for (BucketT *B = OldBuckets, *E = OldBuckets+OldNumBuckets; B != E; ++B) {
452 if (!KeyInfoT::isEqual(B->first, EmptyKey) &&
453 !KeyInfoT::isEqual(B->first, TombstoneKey)) {
454 // Insert the key/value into the new table.
456 bool FoundVal = LookupBucketFor(B->first, DestBucket);
457 FoundVal = FoundVal; // silence warning.
458 assert(!FoundVal && "Key already in new map?");
459 DestBucket->first = B->first;
460 new (&DestBucket->second) ValueT(B->second);
468 // Free the old table.
469 operator delete(OldBuckets);
472 void shrink_and_clear() {
473 unsigned OldNumBuckets = NumBuckets;
474 BucketT *OldBuckets = Buckets;
476 // Reduce the number of buckets.
477 NumBuckets = NumEntries > 32 ? 1 << (Log2_32_Ceil(NumEntries) + 1)
480 Buckets = static_cast<BucketT*>(operator new(sizeof(BucketT)*NumBuckets));
482 // Initialize all the keys to EmptyKey.
483 const KeyT EmptyKey = getEmptyKey();
484 for (unsigned i = 0, e = NumBuckets; i != e; ++i)
485 new (&Buckets[i].first) KeyT(EmptyKey);
487 // Free the old buckets.
488 const KeyT TombstoneKey = getTombstoneKey();
489 for (BucketT *B = OldBuckets, *E = OldBuckets+OldNumBuckets; B != E; ++B) {
490 if (!KeyInfoT::isEqual(B->first, EmptyKey) &&
491 !KeyInfoT::isEqual(B->first, TombstoneKey)) {
498 // Free the old table.
499 operator delete(OldBuckets);
505 template<typename KeyT, typename ValueT, typename KeyInfoT, typename ValueInfoT>
506 class DenseMapIterator {
507 typedef std::pair<KeyT, ValueT> BucketT;
509 const BucketT *Ptr, *End;
511 DenseMapIterator(void) : Ptr(0), End(0) {}
513 DenseMapIterator(const BucketT *Pos, const BucketT *E) : Ptr(Pos), End(E) {
514 AdvancePastEmptyBuckets();
517 std::pair<KeyT, ValueT> &operator*() const {
518 return *const_cast<BucketT*>(Ptr);
520 std::pair<KeyT, ValueT> *operator->() const {
521 return const_cast<BucketT*>(Ptr);
524 bool operator==(const DenseMapIterator &RHS) const {
525 return Ptr == RHS.Ptr;
527 bool operator!=(const DenseMapIterator &RHS) const {
528 return Ptr != RHS.Ptr;
531 inline DenseMapIterator& operator++() { // Preincrement
533 AdvancePastEmptyBuckets();
536 DenseMapIterator operator++(int) { // Postincrement
537 DenseMapIterator tmp = *this; ++*this; return tmp;
541 void AdvancePastEmptyBuckets() {
542 const KeyT Empty = KeyInfoT::getEmptyKey();
543 const KeyT Tombstone = KeyInfoT::getTombstoneKey();
546 (KeyInfoT::isEqual(Ptr->first, Empty) ||
547 KeyInfoT::isEqual(Ptr->first, Tombstone)))
552 template<typename KeyT, typename ValueT, typename KeyInfoT, typename ValueInfoT>
553 class DenseMapConstIterator : public DenseMapIterator<KeyT, ValueT, KeyInfoT> {
555 DenseMapConstIterator(void) : DenseMapIterator<KeyT, ValueT, KeyInfoT>() {}
556 DenseMapConstIterator(const std::pair<KeyT, ValueT> *Pos,
557 const std::pair<KeyT, ValueT> *E)
558 : DenseMapIterator<KeyT, ValueT, KeyInfoT>(Pos, E) {
560 const std::pair<KeyT, ValueT> &operator*() const {
563 const std::pair<KeyT, ValueT> *operator->() const {
568 } // end namespace llvm