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/DataTypes.h"
18 #include "llvm/Support/MathExtras.h"
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);
33 // Provide DenseMapInfo for all pointers.
35 struct 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);
42 static bool isEqual(const T *LHS, const T *RHS) { return LHS == RHS; }
43 static bool isPod() { return true; }
46 // Provide DenseMapInfo for unsigned ints.
47 template<> struct DenseMapInfo<uint32_t> {
48 static inline uint32_t getEmptyKey() { return ~0; }
49 static inline uint32_t getTombstoneKey() { return ~0 - 1; }
50 static unsigned getHashValue(const uint32_t& Val) { return Val * 37; }
51 static bool isPod() { return true; }
52 static bool isEqual(const uint32_t& LHS, const uint32_t& RHS) {
57 // Provide DenseMapInfo for all pairs whose members have info.
58 template<typename T, typename U>
59 struct DenseMapInfo<std::pair<T, U> > {
60 typedef std::pair<T, U> Pair;
61 typedef DenseMapInfo<T> FirstInfo;
62 typedef DenseMapInfo<U> SecondInfo;
64 static inline Pair getEmptyKey() {
65 return std::make_pair(FirstInfo::getEmptyKey(),
66 SecondInfo::getEmptyKey());
68 static inline Pair getTombstoneKey() {
69 return std::make_pair(FirstInfo::getTombstoneKey(),
70 SecondInfo::getEmptyKey());
72 static unsigned getHashValue(const Pair& PairVal) {
73 uint64_t key = (uint64_t)FirstInfo::getHashValue(PairVal.first) << 32
74 | (uint64_t)SecondInfo::getHashValue(PairVal.second);
85 static bool isEqual(const Pair& LHS, const Pair& RHS) { return LHS == RHS; }
86 static bool isPod() { return FirstInfo::isPod() && SecondInfo::isPod(); }
89 template<typename KeyT, typename ValueT,
90 typename KeyInfoT = DenseMapInfo<KeyT>,
91 typename ValueInfoT = DenseMapInfo<ValueT> >
92 class DenseMapIterator;
93 template<typename KeyT, typename ValueT,
94 typename KeyInfoT = DenseMapInfo<KeyT>,
95 typename ValueInfoT = DenseMapInfo<ValueT> >
96 class DenseMapConstIterator;
98 template<typename KeyT, typename ValueT,
99 typename KeyInfoT = DenseMapInfo<KeyT>,
100 typename ValueInfoT = DenseMapInfo<ValueT> >
102 typedef std::pair<KeyT, ValueT> BucketT;
107 unsigned NumTombstones;
109 typedef KeyT key_type;
110 typedef ValueT mapped_type;
111 typedef BucketT value_type;
113 DenseMap(const DenseMap& other) {
118 explicit DenseMap(unsigned NumInitBuckets = 64) {
119 init(NumInitBuckets);
123 const KeyT EmptyKey = getEmptyKey(), TombstoneKey = getTombstoneKey();
124 for (BucketT *P = Buckets, *E = Buckets+NumBuckets; P != E; ++P) {
125 if (!KeyInfoT::isEqual(P->first, EmptyKey) &&
126 !KeyInfoT::isEqual(P->first, TombstoneKey))
130 operator delete(Buckets);
133 typedef DenseMapIterator<KeyT, ValueT, KeyInfoT> iterator;
134 typedef DenseMapConstIterator<KeyT, ValueT, KeyInfoT> const_iterator;
135 inline iterator begin() {
136 return iterator(Buckets, Buckets+NumBuckets);
138 inline iterator end() {
139 return iterator(Buckets+NumBuckets, Buckets+NumBuckets);
141 inline const_iterator begin() const {
142 return const_iterator(Buckets, Buckets+NumBuckets);
144 inline const_iterator end() const {
145 return const_iterator(Buckets+NumBuckets, Buckets+NumBuckets);
148 bool empty() const { return NumEntries == 0; }
149 unsigned size() const { return NumEntries; }
151 /// Grow the densemap so that it has at least Size buckets. Does not shrink
152 void resize(size_t Size) { grow(Size); }
155 // If the capacity of the array is huge, and the # elements used is small,
157 if (NumEntries * 4 < NumBuckets && NumBuckets > 64) {
162 const KeyT EmptyKey = getEmptyKey(), TombstoneKey = getTombstoneKey();
163 for (BucketT *P = Buckets, *E = Buckets+NumBuckets; P != E; ++P) {
164 if (!KeyInfoT::isEqual(P->first, EmptyKey)) {
165 if (!KeyInfoT::isEqual(P->first, TombstoneKey)) {
172 assert(NumEntries == 0 && "Node count imbalance!");
176 /// count - Return true if the specified key is in the map.
177 bool count(const KeyT &Val) const {
179 return LookupBucketFor(Val, TheBucket);
182 iterator find(const KeyT &Val) {
184 if (LookupBucketFor(Val, TheBucket))
185 return iterator(TheBucket, Buckets+NumBuckets);
188 const_iterator find(const KeyT &Val) const {
190 if (LookupBucketFor(Val, TheBucket))
191 return const_iterator(TheBucket, Buckets+NumBuckets);
195 /// lookup - Return the entry for the specified key, or a default
196 /// constructed value if no such entry exists.
197 ValueT lookup(const KeyT &Val) const {
199 if (LookupBucketFor(Val, TheBucket))
200 return TheBucket->second;
204 std::pair<iterator, bool> insert(const std::pair<KeyT, ValueT> &KV) {
206 if (LookupBucketFor(KV.first, TheBucket))
207 return std::make_pair(iterator(TheBucket, Buckets+NumBuckets),
208 false); // Already in map.
210 // Otherwise, insert the new element.
211 TheBucket = InsertIntoBucket(KV.first, KV.second, TheBucket);
212 return std::make_pair(iterator(TheBucket, Buckets+NumBuckets),
216 /// insert - Range insertion of pairs.
217 template<typename InputIt>
218 void insert(InputIt I, InputIt E) {
224 bool erase(const KeyT &Val) {
226 if (!LookupBucketFor(Val, TheBucket))
227 return false; // not in map.
229 TheBucket->second.~ValueT();
230 TheBucket->first = getTombstoneKey();
235 bool erase(iterator I) {
236 BucketT *TheBucket = &*I;
237 TheBucket->second.~ValueT();
238 TheBucket->first = getTombstoneKey();
244 value_type& FindAndConstruct(const KeyT &Key) {
246 if (LookupBucketFor(Key, TheBucket))
249 return *InsertIntoBucket(Key, ValueT(), TheBucket);
252 ValueT &operator[](const KeyT &Key) {
253 return FindAndConstruct(Key).second;
256 DenseMap& operator=(const DenseMap& other) {
262 void CopyFrom(const DenseMap& other) {
263 if (NumBuckets != 0 && (!KeyInfoT::isPod() || !ValueInfoT::isPod())) {
264 const KeyT EmptyKey = getEmptyKey(), TombstoneKey = getTombstoneKey();
265 for (BucketT *P = Buckets, *E = Buckets+NumBuckets; P != E; ++P) {
266 if (!KeyInfoT::isEqual(P->first, EmptyKey) &&
267 !KeyInfoT::isEqual(P->first, TombstoneKey))
273 NumEntries = other.NumEntries;
274 NumTombstones = other.NumTombstones;
277 operator delete(Buckets);
278 Buckets = static_cast<BucketT*>(operator new(sizeof(BucketT) *
281 if (KeyInfoT::isPod() && ValueInfoT::isPod())
282 memcpy(Buckets, other.Buckets, other.NumBuckets * sizeof(BucketT));
284 for (size_t i = 0; i < other.NumBuckets; ++i) {
285 new (&Buckets[i].first) KeyT(other.Buckets[i].first);
286 if (!KeyInfoT::isEqual(Buckets[i].first, getEmptyKey()) &&
287 !KeyInfoT::isEqual(Buckets[i].first, getTombstoneKey()))
288 new (&Buckets[i].second) ValueT(other.Buckets[i].second);
290 NumBuckets = other.NumBuckets;
293 BucketT *InsertIntoBucket(const KeyT &Key, const ValueT &Value,
294 BucketT *TheBucket) {
295 // If the load of the hash table is more than 3/4, or if fewer than 1/8 of
296 // the buckets are empty (meaning that many are filled with tombstones),
299 // The later case is tricky. For example, if we had one empty bucket with
300 // tons of tombstones, failing lookups (e.g. for insertion) would have to
301 // probe almost the entire table until it found the empty bucket. If the
302 // table completely filled with tombstones, no lookup would ever succeed,
303 // causing infinite loops in lookup.
304 if (NumEntries*4 >= NumBuckets*3 ||
305 NumBuckets-(NumEntries+NumTombstones) < NumBuckets/8) {
306 this->grow(NumBuckets * 2);
307 LookupBucketFor(Key, TheBucket);
311 // If we are writing over a tombstone, remember this.
312 if (!KeyInfoT::isEqual(TheBucket->first, getEmptyKey()))
315 TheBucket->first = Key;
316 new (&TheBucket->second) ValueT(Value);
320 static unsigned getHashValue(const KeyT &Val) {
321 return KeyInfoT::getHashValue(Val);
323 static const KeyT getEmptyKey() {
324 return KeyInfoT::getEmptyKey();
326 static const KeyT getTombstoneKey() {
327 return KeyInfoT::getTombstoneKey();
330 /// LookupBucketFor - Lookup the appropriate bucket for Val, returning it in
331 /// FoundBucket. If the bucket contains the key and a value, this returns
332 /// true, otherwise it returns a bucket with an empty marker or tombstone and
334 bool LookupBucketFor(const KeyT &Val, BucketT *&FoundBucket) const {
335 unsigned BucketNo = getHashValue(Val);
336 unsigned ProbeAmt = 1;
337 BucketT *BucketsPtr = Buckets;
339 // FoundTombstone - Keep track of whether we find a tombstone while probing.
340 BucketT *FoundTombstone = 0;
341 const KeyT EmptyKey = getEmptyKey();
342 const KeyT TombstoneKey = getTombstoneKey();
343 assert(!KeyInfoT::isEqual(Val, EmptyKey) &&
344 !KeyInfoT::isEqual(Val, TombstoneKey) &&
345 "Empty/Tombstone value shouldn't be inserted into map!");
348 BucketT *ThisBucket = BucketsPtr + (BucketNo & (NumBuckets-1));
349 // Found Val's bucket? If so, return it.
350 if (KeyInfoT::isEqual(ThisBucket->first, Val)) {
351 FoundBucket = ThisBucket;
355 // If we found an empty bucket, the key doesn't exist in the set.
356 // Insert it and return the default value.
357 if (KeyInfoT::isEqual(ThisBucket->first, EmptyKey)) {
358 // If we've already seen a tombstone while probing, fill it in instead
359 // of the empty bucket we eventually probed to.
360 if (FoundTombstone) ThisBucket = FoundTombstone;
361 FoundBucket = FoundTombstone ? FoundTombstone : ThisBucket;
365 // If this is a tombstone, remember it. If Val ends up not in the map, we
366 // prefer to return it than something that would require more probing.
367 if (KeyInfoT::isEqual(ThisBucket->first, TombstoneKey) && !FoundTombstone)
368 FoundTombstone = ThisBucket; // Remember the first tombstone found.
370 // Otherwise, it's a hash collision or a tombstone, continue quadratic
372 BucketNo += ProbeAmt++;
376 void init(unsigned InitBuckets) {
379 NumBuckets = InitBuckets;
380 assert(InitBuckets && (InitBuckets & (InitBuckets-1)) == 0 &&
381 "# initial buckets must be a power of two!");
382 Buckets = static_cast<BucketT*>(operator new(sizeof(BucketT)*InitBuckets));
383 // Initialize all the keys to EmptyKey.
384 const KeyT EmptyKey = getEmptyKey();
385 for (unsigned i = 0; i != InitBuckets; ++i)
386 new (&Buckets[i].first) KeyT(EmptyKey);
389 void grow(unsigned AtLeast) {
390 unsigned OldNumBuckets = NumBuckets;
391 BucketT *OldBuckets = Buckets;
393 // Double the number of buckets.
394 while (NumBuckets <= AtLeast)
397 Buckets = static_cast<BucketT*>(operator new(sizeof(BucketT)*NumBuckets));
399 // Initialize all the keys to EmptyKey.
400 const KeyT EmptyKey = getEmptyKey();
401 for (unsigned i = 0, e = NumBuckets; i != e; ++i)
402 new (&Buckets[i].first) KeyT(EmptyKey);
404 // Insert all the old elements.
405 const KeyT TombstoneKey = getTombstoneKey();
406 for (BucketT *B = OldBuckets, *E = OldBuckets+OldNumBuckets; B != E; ++B) {
407 if (!KeyInfoT::isEqual(B->first, EmptyKey) &&
408 !KeyInfoT::isEqual(B->first, TombstoneKey)) {
409 // Insert the key/value into the new table.
411 bool FoundVal = LookupBucketFor(B->first, DestBucket);
412 FoundVal = FoundVal; // silence warning.
413 assert(!FoundVal && "Key already in new map?");
414 DestBucket->first = B->first;
415 new (&DestBucket->second) ValueT(B->second);
423 // Free the old table.
424 operator delete(OldBuckets);
427 void shrink_and_clear() {
428 unsigned OldNumBuckets = NumBuckets;
429 BucketT *OldBuckets = Buckets;
431 // Reduce the number of buckets.
432 NumBuckets = NumEntries > 32 ? 1 << (Log2_32_Ceil(NumEntries) + 1)
435 Buckets = static_cast<BucketT*>(operator new(sizeof(BucketT)*NumBuckets));
437 // Initialize all the keys to EmptyKey.
438 const KeyT EmptyKey = getEmptyKey();
439 for (unsigned i = 0, e = NumBuckets; i != e; ++i)
440 new (&Buckets[i].first) KeyT(EmptyKey);
442 // Free the old buckets.
443 const KeyT TombstoneKey = getTombstoneKey();
444 for (BucketT *B = OldBuckets, *E = OldBuckets+OldNumBuckets; B != E; ++B) {
445 if (!KeyInfoT::isEqual(B->first, EmptyKey) &&
446 !KeyInfoT::isEqual(B->first, TombstoneKey)) {
453 // Free the old table.
454 operator delete(OldBuckets);
460 template<typename KeyT, typename ValueT, typename KeyInfoT, typename ValueInfoT>
461 class DenseMapIterator {
462 typedef std::pair<KeyT, ValueT> BucketT;
464 const BucketT *Ptr, *End;
466 DenseMapIterator(void) : Ptr(0), End(0) {}
468 DenseMapIterator(const BucketT *Pos, const BucketT *E) : Ptr(Pos), End(E) {
469 AdvancePastEmptyBuckets();
472 std::pair<KeyT, ValueT> &operator*() const {
473 return *const_cast<BucketT*>(Ptr);
475 std::pair<KeyT, ValueT> *operator->() const {
476 return const_cast<BucketT*>(Ptr);
479 bool operator==(const DenseMapIterator &RHS) const {
480 return Ptr == RHS.Ptr;
482 bool operator!=(const DenseMapIterator &RHS) const {
483 return Ptr != RHS.Ptr;
486 inline DenseMapIterator& operator++() { // Preincrement
488 AdvancePastEmptyBuckets();
491 DenseMapIterator operator++(int) { // Postincrement
492 DenseMapIterator tmp = *this; ++*this; return tmp;
496 void AdvancePastEmptyBuckets() {
497 const KeyT Empty = KeyInfoT::getEmptyKey();
498 const KeyT Tombstone = KeyInfoT::getTombstoneKey();
501 (KeyInfoT::isEqual(Ptr->first, Empty) ||
502 KeyInfoT::isEqual(Ptr->first, Tombstone)))
507 template<typename KeyT, typename ValueT, typename KeyInfoT, typename ValueInfoT>
508 class DenseMapConstIterator : public DenseMapIterator<KeyT, ValueT, KeyInfoT> {
510 DenseMapConstIterator(void) : DenseMapIterator<KeyT, ValueT, KeyInfoT>() {}
511 DenseMapConstIterator(const std::pair<KeyT, ValueT> *Pos,
512 const std::pair<KeyT, ValueT> *E)
513 : DenseMapIterator<KeyT, ValueT, KeyInfoT>(Pos, E) {
515 const std::pair<KeyT, ValueT> &operator*() const {
518 const std::pair<KeyT, ValueT> *operator->() const {
523 } // end namespace llvm