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"
18 #include "llvm/Support/MathExtras.h"
25 struct DenseMapKeyInfo {
26 //static inline T getEmptyKey();
27 //static inline T getTombstoneKey();
28 //static unsigned getHashValue(const T &Val);
32 // Provide DenseMapKeyInfo for all pointers.
34 struct DenseMapKeyInfo<T*> {
35 static inline T* getEmptyKey() { return (T*)-1; }
36 static inline T* getTombstoneKey() { return (T*)-2; }
37 static unsigned getHashValue(const T *PtrVal) {
38 return (unsigned)((uintptr_t)PtrVal >> 4) ^
39 (unsigned)((uintptr_t)PtrVal >> 9);
41 static bool isPod() { return true; }
44 template<typename KeyT, typename ValueT,
45 typename KeyInfoT = DenseMapKeyInfo<KeyT> >
46 class DenseMapIterator;
47 template<typename KeyT, typename ValueT,
48 typename KeyInfoT = DenseMapKeyInfo<KeyT> >
49 class DenseMapConstIterator;
51 template<typename KeyT, typename ValueT,
52 typename KeyInfoT = DenseMapKeyInfo<KeyT> >
54 typedef std::pair<KeyT, ValueT> BucketT;
59 unsigned NumTombstones;
60 DenseMap(const DenseMap &); // not implemented.
62 explicit DenseMap(unsigned NumInitBuckets = 64) {
66 const KeyT EmptyKey = getEmptyKey(), TombstoneKey = getTombstoneKey();
67 for (BucketT *P = Buckets, *E = Buckets+NumBuckets; P != E; ++P) {
68 if (P->first != EmptyKey && P->first != TombstoneKey)
72 delete[] (char*)Buckets;
75 typedef DenseMapIterator<KeyT, ValueT, KeyInfoT> iterator;
76 typedef DenseMapConstIterator<KeyT, ValueT, KeyInfoT> const_iterator;
77 inline iterator begin() {
78 return iterator(Buckets, Buckets+NumBuckets);
80 inline iterator end() {
81 return iterator(Buckets+NumBuckets, Buckets+NumBuckets);
83 inline const_iterator begin() const {
84 return const_iterator(Buckets, Buckets+NumBuckets);
86 inline const_iterator end() const {
87 return const_iterator(Buckets+NumBuckets, Buckets+NumBuckets);
90 bool empty() const { return NumEntries == 0; }
91 unsigned size() const { return NumEntries; }
94 if (NumEntries * 4 < NumBuckets && NumBuckets > 64) {
99 const KeyT EmptyKey = getEmptyKey(), TombstoneKey = getTombstoneKey();
100 for (BucketT *P = Buckets, *E = Buckets+NumBuckets; P != E; ++P) {
101 if (P->first != EmptyKey && P->first != TombstoneKey) {
107 assert(NumEntries == 0 && "Node count imbalance!");
111 /// count - Return true if the specified key is in the map.
112 bool count(const KeyT &Val) const {
114 return LookupBucketFor(Val, TheBucket);
117 iterator find(const KeyT &Val) {
119 if (LookupBucketFor(Val, TheBucket))
120 return iterator(TheBucket, Buckets+NumBuckets);
123 const_iterator find(const KeyT &Val) const {
125 if (LookupBucketFor(Val, TheBucket))
126 return const_iterator(TheBucket, Buckets+NumBuckets);
130 bool insert(const std::pair<KeyT, ValueT> &KV) {
132 if (LookupBucketFor(KV.first, TheBucket))
133 return false; // Already in map.
135 // Otherwise, insert the new element.
136 InsertIntoBucket(KV.first, KV.second, TheBucket);
140 bool erase(const KeyT &Val) {
142 if (!LookupBucketFor(Val, TheBucket))
143 return false; // not in map.
145 TheBucket->second.~ValueT();
146 TheBucket->first = getTombstoneKey();
151 bool erase(iterator I) {
152 BucketT *TheBucket = &*I;
153 TheBucket->second.~ValueT();
154 TheBucket->first = getTombstoneKey();
160 ValueT &operator[](const KeyT &Key) {
162 if (LookupBucketFor(Key, TheBucket))
163 return TheBucket->second;
165 return InsertIntoBucket(Key, ValueT(), TheBucket)->second;
169 BucketT *InsertIntoBucket(const KeyT &Key, const ValueT &Value,
170 BucketT *TheBucket) {
171 // If the load of the hash table is more than 3/4, or if fewer than 1/8 of
172 // the buckets are empty (meaning that many are filled with tombstones),
175 // The later case is tricky. For example, if we had one empty bucket with
176 // tons of tombstones, failing lookups (e.g. for insertion) would have to
177 // probe almost the entire table until it found the empty bucket. If the
178 // table completely filled with tombstones, no lookup would ever succeed,
179 // causing infinite loops in lookup.
180 if (NumEntries*4 >= NumBuckets*3 ||
181 NumBuckets-(NumEntries+NumTombstones) < NumBuckets/8) {
183 LookupBucketFor(Key, TheBucket);
187 // If we are writing over a tombstone, remember this.
188 if (TheBucket->first != getEmptyKey())
191 TheBucket->first = Key;
192 new (&TheBucket->second) ValueT(Value);
196 static unsigned getHashValue(const KeyT &Val) {
197 return KeyInfoT::getHashValue(Val);
199 static const KeyT getEmptyKey() {
200 return KeyInfoT::getEmptyKey();
202 static const KeyT getTombstoneKey() {
203 return KeyInfoT::getTombstoneKey();
206 /// LookupBucketFor - Lookup the appropriate bucket for Val, returning it in
207 /// FoundBucket. If the bucket contains the key and a value, this returns
208 /// true, otherwise it returns a bucket with an empty marker or tombstone and
210 bool LookupBucketFor(const KeyT &Val, BucketT *&FoundBucket) const {
211 unsigned BucketNo = getHashValue(Val);
212 unsigned ProbeAmt = 1;
213 BucketT *BucketsPtr = Buckets;
215 // FoundTombstone - Keep track of whether we find a tombstone while probing.
216 BucketT *FoundTombstone = 0;
217 const KeyT EmptyKey = getEmptyKey();
218 const KeyT TombstoneKey = getTombstoneKey();
219 assert(Val != EmptyKey && Val != TombstoneKey &&
220 "Empty/Tombstone value shouldn't be inserted into map!");
223 BucketT *ThisBucket = BucketsPtr + (BucketNo & (NumBuckets-1));
224 // Found Val's bucket? If so, return it.
225 if (ThisBucket->first == Val) {
226 FoundBucket = ThisBucket;
230 // If we found an empty bucket, the key doesn't exist in the set.
231 // Insert it and return the default value.
232 if (ThisBucket->first == EmptyKey) {
233 // If we've already seen a tombstone while probing, fill it in instead
234 // of the empty bucket we eventually probed to.
235 if (FoundTombstone) ThisBucket = FoundTombstone;
236 FoundBucket = FoundTombstone ? FoundTombstone : ThisBucket;
240 // If this is a tombstone, remember it. If Val ends up not in the map, we
241 // prefer to return it than something that would require more probing.
242 if (ThisBucket->first == TombstoneKey && !FoundTombstone)
243 FoundTombstone = ThisBucket; // Remember the first tombstone found.
245 // Otherwise, it's a hash collision or a tombstone, continue quadratic
247 BucketNo += ProbeAmt++;
251 void init(unsigned InitBuckets) {
254 NumBuckets = InitBuckets;
255 assert(InitBuckets && (InitBuckets & InitBuckets-1) == 0 &&
256 "# initial buckets must be a power of two!");
257 Buckets = (BucketT*)new char[sizeof(BucketT)*InitBuckets];
258 // Initialize all the keys to EmptyKey.
259 const KeyT EmptyKey = getEmptyKey();
260 for (unsigned i = 0; i != InitBuckets; ++i)
261 new (&Buckets[i].first) KeyT(EmptyKey);
265 unsigned OldNumBuckets = NumBuckets;
266 BucketT *OldBuckets = Buckets;
268 // Double the number of buckets.
271 Buckets = (BucketT*)new char[sizeof(BucketT)*NumBuckets];
273 // Initialize all the keys to EmptyKey.
274 const KeyT EmptyKey = getEmptyKey();
275 for (unsigned i = 0, e = NumBuckets; i != e; ++i)
276 new (&Buckets[i].first) KeyT(EmptyKey);
278 // Insert all the old elements.
279 const KeyT TombstoneKey = getTombstoneKey();
280 for (BucketT *B = OldBuckets, *E = OldBuckets+OldNumBuckets; B != E; ++B) {
281 if (B->first != EmptyKey && B->first != TombstoneKey) {
282 // Insert the key/value into the new table.
284 bool FoundVal = LookupBucketFor(B->first, DestBucket);
285 FoundVal = FoundVal; // silence warning.
286 assert(!FoundVal && "Key already in new map?");
287 DestBucket->first = B->first;
288 new (&DestBucket->second) ValueT(B->second);
296 // Free the old table.
297 delete[] (char*)OldBuckets;
300 void shrink_and_clear() {
301 unsigned OldNumBuckets = NumBuckets;
302 BucketT *OldBuckets = Buckets;
304 // Reduce the number of buckets.
305 NumBuckets = NumEntries > 32 ? 1 << (Log2_32_Ceil(NumEntries) + 1)
308 Buckets = (BucketT*)new char[sizeof(BucketT)*NumBuckets];
310 // Initialize all the keys to EmptyKey.
311 const KeyT EmptyKey = getEmptyKey();
312 for (unsigned i = 0, e = NumBuckets; i != e; ++i)
313 new (&Buckets[i].first) KeyT(EmptyKey);
315 // Free the old buckets.
316 const KeyT TombstoneKey = getTombstoneKey();
317 for (BucketT *B = OldBuckets, *E = OldBuckets+OldNumBuckets; B != E; ++B) {
318 if (B->first != EmptyKey && B->first != TombstoneKey) {
325 // Free the old table.
326 delete[] (char*)OldBuckets;
332 template<typename KeyT, typename ValueT, typename KeyInfoT>
333 class DenseMapIterator {
334 typedef std::pair<KeyT, ValueT> BucketT;
336 const BucketT *Ptr, *End;
338 DenseMapIterator(const BucketT *Pos, const BucketT *E) : Ptr(Pos), End(E) {
339 AdvancePastEmptyBuckets();
342 std::pair<KeyT, ValueT> &operator*() const {
343 return *const_cast<BucketT*>(Ptr);
345 std::pair<KeyT, ValueT> *operator->() const {
346 return const_cast<BucketT*>(Ptr);
349 bool operator==(const DenseMapIterator &RHS) const {
350 return Ptr == RHS.Ptr;
352 bool operator!=(const DenseMapIterator &RHS) const {
353 return Ptr != RHS.Ptr;
356 inline DenseMapIterator& operator++() { // Preincrement
358 AdvancePastEmptyBuckets();
361 DenseMapIterator operator++(int) { // Postincrement
362 DenseMapIterator tmp = *this; ++*this; return tmp;
366 void AdvancePastEmptyBuckets() {
367 const KeyT Empty = KeyInfoT::getEmptyKey();
368 const KeyT Tombstone = KeyInfoT::getTombstoneKey();
370 while (Ptr != End && (Ptr->first == Empty || Ptr->first == Tombstone))
375 template<typename KeyT, typename ValueT, typename KeyInfoT>
376 class DenseMapConstIterator : public DenseMapIterator<KeyT, ValueT, KeyInfoT> {
378 DenseMapConstIterator(const std::pair<KeyT, ValueT> *Pos,
379 const std::pair<KeyT, ValueT> *E)
380 : DenseMapIterator<KeyT, ValueT, KeyInfoT>(Pos, E) {
382 const std::pair<KeyT, ValueT> &operator*() const {
385 const std::pair<KeyT, ValueT> *operator->() const {
390 } // end namespace llvm