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;
44 template<typename KeyT, typename ValueT>
45 class DenseMapConstIterator;
47 template<typename KeyT, typename ValueT>
49 typedef std::pair<KeyT, ValueT> BucketT;
54 DenseMap(const DenseMap &); // not implemented.
56 explicit DenseMap(unsigned NumInitBuckets = 8) {
60 const KeyT EmptyKey = getEmptyKey(), TombstoneKey = getTombstoneKey();
61 for (BucketT *P = Buckets, *E = Buckets+NumBuckets; P != E; ++P) {
62 if (P->first != EmptyKey && P->first != TombstoneKey)
66 delete[] (char*)Buckets;
69 typedef DenseMapIterator<KeyT, ValueT> iterator;
70 typedef DenseMapConstIterator<KeyT, ValueT> const_iterator;
71 inline iterator begin() {
72 return DenseMapIterator<KeyT, ValueT>(Buckets, Buckets+NumBuckets);
74 inline iterator end() {
75 return DenseMapIterator<KeyT, ValueT>(Buckets+NumBuckets,
78 inline const_iterator begin() const {
79 return DenseMapConstIterator<KeyT, ValueT>(Buckets, Buckets+NumBuckets);
81 inline const_iterator end() const {
82 return DenseMapConstIterator<KeyT, ValueT>(Buckets+NumBuckets,
86 bool empty() const { return NumEntries == 0; }
87 unsigned size() const { return NumEntries; }
90 const KeyT EmptyKey = getEmptyKey(), TombstoneKey = getTombstoneKey();
91 for (BucketT *P = Buckets, *E = Buckets+NumBuckets; P != E; ++P) {
92 if (P->first != EmptyKey && P->first != TombstoneKey) {
98 assert(NumEntries == 0 && "Node count imbalance!");
101 /// count - Return true if the specified key is in the map.
102 bool count(const KeyT &Val) const {
104 return LookupBucketFor(Val, TheBucket);
107 iterator find(const KeyT &Val) const {
109 if (LookupBucketFor(Val, TheBucket))
110 return iterator(TheBucket, Buckets+NumBuckets);
114 bool erase(const KeyT &Val) {
116 if (!LookupBucketFor(Val, TheBucket))
117 return false; // not in map.
119 TheBucket->second.~ValueT();
120 TheBucket->first = getTombstoneKey();
124 bool erase(iterator I) {
125 BucketT *TheBucket = &*I;
126 TheBucket->second.~ValueT();
127 TheBucket->first = getTombstoneKey();
132 ValueT &operator[](const KeyT &Val) {
134 if (LookupBucketFor(Val, TheBucket))
135 return TheBucket->second;
137 // If the load of the hash table is more than 3/4, grow it.
138 if (NumEntries*4 >= NumBuckets*3) {
140 LookupBucketFor(Val, TheBucket);
143 TheBucket->first = Val;
144 new (&TheBucket->second) ValueT();
145 return TheBucket->second;
149 static unsigned getHashValue(const KeyT &Val) {
150 return DenseMapKeyInfo<KeyT>::getHashValue(Val);
152 static const KeyT getEmptyKey() {
153 return DenseMapKeyInfo<KeyT>::getEmptyKey();
155 static const KeyT getTombstoneKey() {
156 return DenseMapKeyInfo<KeyT>::getTombstoneKey();
159 /// LookupBucketFor - Lookup the appropriate bucket for Val, returning it in
160 /// FoundBucket. If the bucket contains the key and a value, this returns
161 /// true, otherwise it returns a bucket with an empty marker or tombstone and
163 bool LookupBucketFor(const KeyT &Val, BucketT *&FoundBucket) const {
164 unsigned BucketNo = getHashValue(Val);
165 unsigned ProbeAmt = 1;
166 BucketT *BucketsPtr = Buckets;
168 // FoundTombstone - Keep track of whether we find a tombstone while probing.
169 BucketT *FoundTombstone = 0;
170 const KeyT EmptyKey = getEmptyKey();
171 const KeyT TombstoneKey = getTombstoneKey();
172 assert(Val != EmptyKey && Val != TombstoneKey &&
173 "Empty/Tombstone value shouldn't be inserted into map!");
176 BucketT *ThisBucket = BucketsPtr + (BucketNo & (NumBuckets-1));
177 // Found Val's bucket? If so, return it.
178 if (ThisBucket->first == Val) {
179 FoundBucket = ThisBucket;
183 // If we found an empty bucket, the key doesn't exist in the set.
184 // Insert it and return the default value.
185 if (ThisBucket->first == EmptyKey) {
186 // If we've already seen a tombstone while probing, fill it in instead
187 // of the empty bucket we eventually probed to.
188 if (FoundTombstone) ThisBucket = FoundTombstone;
189 FoundBucket = FoundTombstone ? FoundTombstone : ThisBucket;
193 // If this is a tombstone, remember it. If Val ends up not in the map, we
194 // prefer to return it than something that would require more probing.
195 if (ThisBucket->first == TombstoneKey && !FoundTombstone)
196 FoundTombstone = ThisBucket; // Remember the first tombstone found.
198 // Otherwise, it's a hash collision or a tombstone, continue quadratic
200 BucketNo += ProbeAmt++;
204 void init(unsigned InitBuckets) {
206 NumBuckets = InitBuckets;
207 assert(InitBuckets && (InitBuckets & InitBuckets-1) == 0 &&
208 "# initial buckets must be a power of two!");
209 Buckets = (BucketT*)new char[sizeof(BucketT)*InitBuckets];
210 // Initialize all the keys to EmptyKey.
211 const KeyT EmptyKey = getEmptyKey();
212 for (unsigned i = 0; i != InitBuckets; ++i)
213 new (&Buckets[i].first) KeyT(EmptyKey);
217 unsigned OldNumBuckets = NumBuckets;
218 BucketT *OldBuckets = Buckets;
220 // Double the number of buckets.
222 Buckets = (BucketT*)new char[sizeof(BucketT)*NumBuckets];
224 // Initialize all the keys to EmptyKey.
225 const KeyT EmptyKey = getEmptyKey();
226 for (unsigned i = 0, e = NumBuckets; i != e; ++i)
227 new (&Buckets[i].first) KeyT(EmptyKey);
229 // Insert all the old elements.
230 const KeyT TombstoneKey = getTombstoneKey();
231 for (BucketT *B = OldBuckets, *E = OldBuckets+OldNumBuckets; B != E; ++B) {
232 if (B->first != EmptyKey && B->first != TombstoneKey) {
233 // Insert the key/value into the new table.
235 bool FoundVal = LookupBucketFor(B->first, DestBucket);
236 FoundVal = FoundVal; // silence warning.
237 assert(!FoundVal && "Key already in new map?");
238 DestBucket->first = B->first;
239 new (&DestBucket->second) ValueT(B->second);
247 // Free the old table.
248 delete[] (char*)OldBuckets;
252 template<typename KeyT, typename ValueT>
253 class DenseMapIterator {
254 typedef std::pair<KeyT, ValueT> BucketT;
256 const BucketT *Ptr, *End;
258 DenseMapIterator(const BucketT *Pos, const BucketT *E) : Ptr(Pos), End(E) {
259 AdvancePastEmptyBuckets();
262 std::pair<KeyT, ValueT> &operator*() const {
263 return *const_cast<BucketT*>(Ptr);
265 std::pair<KeyT, ValueT> *operator->() const {
266 return const_cast<BucketT*>(Ptr);
269 bool operator==(const DenseMapIterator &RHS) const {
270 return Ptr == RHS.Ptr;
272 bool operator!=(const DenseMapIterator &RHS) const {
273 return Ptr != RHS.Ptr;
276 inline DenseMapIterator& operator++() { // Preincrement
278 AdvancePastEmptyBuckets();
281 DenseMapIterator operator++(int) { // Postincrement
282 DenseMapIterator tmp = *this; ++*this; return tmp;
286 void AdvancePastEmptyBuckets() {
287 const KeyT Empty = DenseMapKeyInfo<KeyT>::getEmptyKey();
288 const KeyT Tombstone = DenseMapKeyInfo<KeyT>::getTombstoneKey();
290 while (Ptr != End && (Ptr->first == Empty || Ptr->first == Tombstone))
295 template<typename KeyT, typename ValueT>
296 class DenseMapConstIterator : public DenseMapIterator<KeyT, ValueT> {
298 DenseMapConstIterator(const std::pair<KeyT, ValueT> *Pos,
299 const std::pair<KeyT, ValueT> *E)
300 : DenseMapIterator<KeyT, ValueT>(Pos, E) {
302 const std::pair<KeyT, ValueT> &operator*() const {
305 const std::pair<KeyT, ValueT> *operator->() const {
310 } // end namespace llvm