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 the capacity of the array is huge, and the # elements used is small,
96 if (NumEntries * 4 < NumBuckets && NumBuckets > 64) {
101 const KeyT EmptyKey = getEmptyKey(), TombstoneKey = getTombstoneKey();
102 for (BucketT *P = Buckets, *E = Buckets+NumBuckets; P != E; ++P) {
103 if (P->first != EmptyKey) {
104 if (P->first != TombstoneKey) {
111 assert(NumEntries == 0 && "Node count imbalance!");
115 /// count - Return true if the specified key is in the map.
116 bool count(const KeyT &Val) const {
118 return LookupBucketFor(Val, TheBucket);
121 iterator find(const KeyT &Val) {
123 if (LookupBucketFor(Val, TheBucket))
124 return iterator(TheBucket, Buckets+NumBuckets);
127 const_iterator find(const KeyT &Val) const {
129 if (LookupBucketFor(Val, TheBucket))
130 return const_iterator(TheBucket, Buckets+NumBuckets);
134 bool insert(const std::pair<KeyT, ValueT> &KV) {
136 if (LookupBucketFor(KV.first, TheBucket))
137 return false; // Already in map.
139 // Otherwise, insert the new element.
140 InsertIntoBucket(KV.first, KV.second, TheBucket);
144 bool erase(const KeyT &Val) {
146 if (!LookupBucketFor(Val, TheBucket))
147 return false; // not in map.
149 TheBucket->second.~ValueT();
150 TheBucket->first = getTombstoneKey();
155 bool erase(iterator I) {
156 BucketT *TheBucket = &*I;
157 TheBucket->second.~ValueT();
158 TheBucket->first = getTombstoneKey();
164 ValueT &operator[](const KeyT &Key) {
166 if (LookupBucketFor(Key, TheBucket))
167 return TheBucket->second;
169 return InsertIntoBucket(Key, ValueT(), TheBucket)->second;
173 BucketT *InsertIntoBucket(const KeyT &Key, const ValueT &Value,
174 BucketT *TheBucket) {
175 // If the load of the hash table is more than 3/4, or if fewer than 1/8 of
176 // the buckets are empty (meaning that many are filled with tombstones),
179 // The later case is tricky. For example, if we had one empty bucket with
180 // tons of tombstones, failing lookups (e.g. for insertion) would have to
181 // probe almost the entire table until it found the empty bucket. If the
182 // table completely filled with tombstones, no lookup would ever succeed,
183 // causing infinite loops in lookup.
184 if (NumEntries*4 >= NumBuckets*3 ||
185 NumBuckets-(NumEntries+NumTombstones) < NumBuckets/8) {
187 LookupBucketFor(Key, TheBucket);
191 // If we are writing over a tombstone, remember this.
192 if (TheBucket->first != getEmptyKey())
195 TheBucket->first = Key;
196 new (&TheBucket->second) ValueT(Value);
200 static unsigned getHashValue(const KeyT &Val) {
201 return KeyInfoT::getHashValue(Val);
203 static const KeyT getEmptyKey() {
204 return KeyInfoT::getEmptyKey();
206 static const KeyT getTombstoneKey() {
207 return KeyInfoT::getTombstoneKey();
210 /// LookupBucketFor - Lookup the appropriate bucket for Val, returning it in
211 /// FoundBucket. If the bucket contains the key and a value, this returns
212 /// true, otherwise it returns a bucket with an empty marker or tombstone and
214 bool LookupBucketFor(const KeyT &Val, BucketT *&FoundBucket) const {
215 unsigned BucketNo = getHashValue(Val);
216 unsigned ProbeAmt = 1;
217 BucketT *BucketsPtr = Buckets;
219 // FoundTombstone - Keep track of whether we find a tombstone while probing.
220 BucketT *FoundTombstone = 0;
221 const KeyT EmptyKey = getEmptyKey();
222 const KeyT TombstoneKey = getTombstoneKey();
223 assert(Val != EmptyKey && Val != TombstoneKey &&
224 "Empty/Tombstone value shouldn't be inserted into map!");
227 BucketT *ThisBucket = BucketsPtr + (BucketNo & (NumBuckets-1));
228 // Found Val's bucket? If so, return it.
229 if (ThisBucket->first == Val) {
230 FoundBucket = ThisBucket;
234 // If we found an empty bucket, the key doesn't exist in the set.
235 // Insert it and return the default value.
236 if (ThisBucket->first == EmptyKey) {
237 // If we've already seen a tombstone while probing, fill it in instead
238 // of the empty bucket we eventually probed to.
239 if (FoundTombstone) ThisBucket = FoundTombstone;
240 FoundBucket = FoundTombstone ? FoundTombstone : ThisBucket;
244 // If this is a tombstone, remember it. If Val ends up not in the map, we
245 // prefer to return it than something that would require more probing.
246 if (ThisBucket->first == TombstoneKey && !FoundTombstone)
247 FoundTombstone = ThisBucket; // Remember the first tombstone found.
249 // Otherwise, it's a hash collision or a tombstone, continue quadratic
251 BucketNo += ProbeAmt++;
255 void init(unsigned InitBuckets) {
258 NumBuckets = InitBuckets;
259 assert(InitBuckets && (InitBuckets & InitBuckets-1) == 0 &&
260 "# initial buckets must be a power of two!");
261 Buckets = (BucketT*)new char[sizeof(BucketT)*InitBuckets];
262 // Initialize all the keys to EmptyKey.
263 const KeyT EmptyKey = getEmptyKey();
264 for (unsigned i = 0; i != InitBuckets; ++i)
265 new (&Buckets[i].first) KeyT(EmptyKey);
269 unsigned OldNumBuckets = NumBuckets;
270 BucketT *OldBuckets = Buckets;
272 // Double the number of buckets.
275 Buckets = (BucketT*)new char[sizeof(BucketT)*NumBuckets];
277 // Initialize all the keys to EmptyKey.
278 const KeyT EmptyKey = getEmptyKey();
279 for (unsigned i = 0, e = NumBuckets; i != e; ++i)
280 new (&Buckets[i].first) KeyT(EmptyKey);
282 // Insert all the old elements.
283 const KeyT TombstoneKey = getTombstoneKey();
284 for (BucketT *B = OldBuckets, *E = OldBuckets+OldNumBuckets; B != E; ++B) {
285 if (B->first != EmptyKey && B->first != TombstoneKey) {
286 // Insert the key/value into the new table.
288 bool FoundVal = LookupBucketFor(B->first, DestBucket);
289 FoundVal = FoundVal; // silence warning.
290 assert(!FoundVal && "Key already in new map?");
291 DestBucket->first = B->first;
292 new (&DestBucket->second) ValueT(B->second);
300 // Free the old table.
301 delete[] (char*)OldBuckets;
304 void shrink_and_clear() {
305 unsigned OldNumBuckets = NumBuckets;
306 BucketT *OldBuckets = Buckets;
308 // Reduce the number of buckets.
309 NumBuckets = NumEntries > 32 ? 1 << (Log2_32_Ceil(NumEntries) + 1)
312 Buckets = (BucketT*)new char[sizeof(BucketT)*NumBuckets];
314 // Initialize all the keys to EmptyKey.
315 const KeyT EmptyKey = getEmptyKey();
316 for (unsigned i = 0, e = NumBuckets; i != e; ++i)
317 new (&Buckets[i].first) KeyT(EmptyKey);
319 // Free the old buckets.
320 const KeyT TombstoneKey = getTombstoneKey();
321 for (BucketT *B = OldBuckets, *E = OldBuckets+OldNumBuckets; B != E; ++B) {
322 if (B->first != EmptyKey && B->first != TombstoneKey) {
329 // Free the old table.
330 delete[] (char*)OldBuckets;
336 template<typename KeyT, typename ValueT, typename KeyInfoT>
337 class DenseMapIterator {
338 typedef std::pair<KeyT, ValueT> BucketT;
340 const BucketT *Ptr, *End;
342 DenseMapIterator(const BucketT *Pos, const BucketT *E) : Ptr(Pos), End(E) {
343 AdvancePastEmptyBuckets();
346 std::pair<KeyT, ValueT> &operator*() const {
347 return *const_cast<BucketT*>(Ptr);
349 std::pair<KeyT, ValueT> *operator->() const {
350 return const_cast<BucketT*>(Ptr);
353 bool operator==(const DenseMapIterator &RHS) const {
354 return Ptr == RHS.Ptr;
356 bool operator!=(const DenseMapIterator &RHS) const {
357 return Ptr != RHS.Ptr;
360 inline DenseMapIterator& operator++() { // Preincrement
362 AdvancePastEmptyBuckets();
365 DenseMapIterator operator++(int) { // Postincrement
366 DenseMapIterator tmp = *this; ++*this; return tmp;
370 void AdvancePastEmptyBuckets() {
371 const KeyT Empty = KeyInfoT::getEmptyKey();
372 const KeyT Tombstone = KeyInfoT::getTombstoneKey();
374 while (Ptr != End && (Ptr->first == Empty || Ptr->first == Tombstone))
379 template<typename KeyT, typename ValueT, typename KeyInfoT>
380 class DenseMapConstIterator : public DenseMapIterator<KeyT, ValueT, KeyInfoT> {
382 DenseMapConstIterator(const std::pair<KeyT, ValueT> *Pos,
383 const std::pair<KeyT, ValueT> *E)
384 : DenseMapIterator<KeyT, ValueT, KeyInfoT>(Pos, E) {
386 const std::pair<KeyT, ValueT> &operator*() const {
389 const std::pair<KeyT, ValueT> *operator->() const {
394 } // end namespace llvm