1 //===- llvm/ADT/DenseMap.h - Dense probed hash table ------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
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.
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"
24 struct DenseMapKeyInfo {
25 //static inline T getEmptyKey();
26 //static inline T getTombstoneKey();
27 //static unsigned getHashValue(const T &Val);
32 struct DenseMapKeyInfo<T*> {
33 static inline T* getEmptyKey() { return (T*)-1; }
34 static inline T* getTombstoneKey() { return (T*)-2; }
35 static unsigned getHashValue(const T *PtrVal) {
36 return (unsigned)((uintptr_t)PtrVal >> 4) ^
37 (unsigned)((uintptr_t)PtrVal >> 9);
39 static bool isPod() { return true; }
42 template<typename KeyT, typename ValueT>
43 class DenseMapIterator;
45 template<typename KeyT, typename ValueT>
47 typedef std::pair<KeyT, ValueT> BucketT;
52 DenseMap(const DenseMap &); // not implemented.
54 explicit DenseMap(unsigned NumInitBuckets = 8) {
58 const KeyT EmptyKey = getEmptyKey(), TombstoneKey = getTombstoneKey();
59 for (BucketT *P = Buckets, *E = Buckets+NumBuckets; P != E; ++P) {
60 if (P->first != EmptyKey && P->first != TombstoneKey)
64 delete[] (char*)Buckets;
67 typedef DenseMapIterator<KeyT, ValueT> iterator;
68 typedef DenseMapIterator<KeyT, ValueT> const_iterator;
69 inline iterator begin() const;
70 inline iterator end() const;
72 unsigned size() const { return NumEntries; }
75 const KeyT EmptyKey = getEmptyKey(), TombstoneKey = getTombstoneKey();
76 for (BucketT *P = Buckets, *E = Buckets+NumBuckets; P != E; ++P) {
77 if (P->first != EmptyKey && P->first != TombstoneKey) {
83 assert(NumEntries == 0 && "Node count imbalance!");
86 /// count - Return true if the specified key is in the map.
87 bool count(const KeyT &Val) const {
89 return LookupBucketFor(Val, TheBucket);
92 ValueT &operator[](const KeyT &Val) {
94 if (LookupBucketFor(Val, TheBucket))
95 return TheBucket->second;
97 // If the load of the hash table is more than 3/4, grow it.
98 if (NumEntries*4 >= NumBuckets*3) {
100 LookupBucketFor(Val, TheBucket);
103 TheBucket->first = Val;
104 new (&TheBucket->second) ValueT();
105 return TheBucket->second;
109 unsigned getHashValue(const KeyT &Val) const {
110 return DenseMapKeyInfo<KeyT>::getHashValue(Val);
112 const KeyT getEmptyKey() const { return DenseMapKeyInfo<KeyT>::getEmptyKey();}
113 const KeyT getTombstoneKey() const {
114 return DenseMapKeyInfo<KeyT>::getTombstoneKey();
117 /// LookupBucketFor - Lookup the appropriate bucket for Val, returning it in
118 /// FoundBucket. If the bucket contains the key and a value, this returns
119 /// true, otherwise it returns a bucket with an empty marker or tombstone and
121 bool LookupBucketFor(const KeyT &Val, BucketT *&FoundBucket) const {
122 unsigned BucketNo = getHashValue(Val);
123 unsigned ProbeAmt = 1;
124 BucketT *BucketsPtr = Buckets;
126 // FoundTombstone - Keep track of whether we find a tombstone while probing.
127 BucketT *FoundTombstone = 0;
128 const KeyT EmptyKey = getEmptyKey();
129 const KeyT TombstoneKey = getTombstoneKey();
130 assert(Val != EmptyKey && Val != TombstoneKey &&
131 "Empty/Tombstone value shouldn't be inserted into map!");
134 BucketT *ThisBucket = BucketsPtr + (BucketNo & (NumBuckets-1));
135 // Found Val's bucket? If so, return it.
136 if (ThisBucket->first == Val) {
137 FoundBucket = ThisBucket;
141 // If we found an empty bucket, the key doesn't exist in the set.
142 // Insert it and return the default value.
143 if (ThisBucket->first == EmptyKey) {
144 // If we've already seen a tombstone while probing, fill it in instead
145 // of the empty bucket we eventually probed to.
146 if (FoundTombstone) ThisBucket = FoundTombstone;
147 FoundBucket = FoundTombstone ? FoundTombstone : ThisBucket;
151 // If this is a tombstone, remember it. If Val ends up not in the map, we
152 // prefer to return it than something that would require more probing.
153 if (ThisBucket->first == TombstoneKey && !FoundTombstone)
154 FoundTombstone = ThisBucket; // Remember the first tombstone found.
156 // Otherwise, it's a hash collision or a tombstone, continue quadratic
158 BucketNo += ProbeAmt++;
162 void init(unsigned InitBuckets) {
164 NumBuckets = InitBuckets;
165 assert(InitBuckets && (InitBuckets & InitBuckets-1) == 0 &&
166 "# initial buckets must be a power of two!");
167 Buckets = (BucketT*)new char[sizeof(BucketT)*InitBuckets];
168 // Initialize all the keys to EmptyKey.
169 const KeyT EmptyKey = getEmptyKey();
170 for (unsigned i = 0; i != InitBuckets; ++i)
171 new (&Buckets[i].first) KeyT(EmptyKey);
175 unsigned OldNumBuckets = NumBuckets;
176 BucketT *OldBuckets = Buckets;
178 // Double the number of buckets.
180 Buckets = (BucketT*)new char[sizeof(BucketT)*NumBuckets];
182 // Initialize all the keys to EmptyKey.
183 const KeyT EmptyKey = getEmptyKey();
184 for (unsigned i = 0, e = NumBuckets; i != e; ++i)
185 new (&Buckets[i].first) KeyT(EmptyKey);
187 // Insert all the old elements.
188 const KeyT TombstoneKey = getTombstoneKey();
189 for (BucketT *B = OldBuckets, *E = OldBuckets+OldNumBuckets; B != E; ++B) {
190 if (B->first != EmptyKey && B->first != TombstoneKey) {
191 // Insert the key/value into the new table.
193 bool FoundVal = LookupBucketFor(B->first, DestBucket);
194 assert(!FoundVal && "Key already in new map?");
195 DestBucket->first = B->first;
196 new (&DestBucket->second) ValueT(B->second);
204 // Free the old table.
205 delete[] (char*)OldBuckets;
209 template<typename KeyT, typename ValueT>
210 class DenseMapIterator {
211 typedef std::pair<KeyT, ValueT> BucketT;
212 const BucketT *Ptr, *End;
214 DenseMapIterator(const BucketT *Pos, const BucketT *E) : Ptr(Pos), End(E) {
215 AdvancePastEmptyBuckets();
218 const std::pair<KeyT, ValueT> &operator*() const {
221 const std::pair<KeyT, ValueT> *operator->() const {
225 bool operator==(const DenseMapIterator &RHS) const {
226 return Ptr == RHS.Ptr;
228 bool operator!=(const DenseMapIterator &RHS) const {
229 return Ptr != RHS.Ptr;
232 inline DenseMapIterator& operator++() { // Preincrement
234 AdvancePastEmptyBuckets();
237 DenseMapIterator operator++(int) { // Postincrement
238 DenseMapIterator tmp = *this; ++*this; return tmp;
242 void AdvancePastEmptyBuckets() {
243 const KeyT Empty = DenseMapKeyInfo<KeyT>::getEmptyKey();
244 const KeyT Tombstone = DenseMapKeyInfo<KeyT>::getTombstoneKey();
246 while (Ptr != End && Ptr->first != Empty && Ptr->first != Tombstone)
252 template<typename KeyT, typename ValueT>
253 inline DenseMapIterator<KeyT, ValueT> DenseMap<KeyT, ValueT>::begin() const {
254 return DenseMapIterator<KeyT, ValueT>(Buckets, Buckets+NumBuckets);
256 template<typename KeyT, typename ValueT>
257 inline DenseMapIterator<KeyT, ValueT> DenseMap<KeyT, ValueT>::end() const {
258 return DenseMapIterator<KeyT, ValueT>(Buckets+NumBuckets, Buckets+NumBuckets);
261 } // end namespace llvm