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);
31 // Provide DenseMapKeyInfo for all pointers.
33 struct DenseMapKeyInfo<T*> {
34 static inline T* getEmptyKey() { return (T*)-1; }
35 static inline T* getTombstoneKey() { return (T*)-2; }
36 static unsigned getHashValue(const T *PtrVal) {
37 return (unsigned)((uintptr_t)PtrVal >> 4) ^
38 (unsigned)((uintptr_t)PtrVal >> 9);
40 static bool isPod() { return true; }
43 template<typename KeyT, typename ValueT>
44 class DenseMapIterator;
45 template<typename KeyT, typename ValueT>
46 class DenseMapConstIterator;
48 template<typename KeyT, typename ValueT>
50 typedef std::pair<KeyT, ValueT> BucketT;
55 unsigned NumTombstones;
56 DenseMap(const DenseMap &); // not implemented.
58 explicit DenseMap(unsigned NumInitBuckets = 64) {
62 const KeyT EmptyKey = getEmptyKey(), TombstoneKey = getTombstoneKey();
63 for (BucketT *P = Buckets, *E = Buckets+NumBuckets; P != E; ++P) {
64 if (P->first != EmptyKey && P->first != TombstoneKey)
68 delete[] (char*)Buckets;
71 typedef DenseMapIterator<KeyT, ValueT> iterator;
72 typedef DenseMapConstIterator<KeyT, ValueT> const_iterator;
73 inline iterator begin() {
74 return DenseMapIterator<KeyT, ValueT>(Buckets, Buckets+NumBuckets);
76 inline iterator end() {
77 return DenseMapIterator<KeyT, ValueT>(Buckets+NumBuckets,
80 inline const_iterator begin() const {
81 return DenseMapConstIterator<KeyT, ValueT>(Buckets, Buckets+NumBuckets);
83 inline const_iterator end() const {
84 return DenseMapConstIterator<KeyT, ValueT>(Buckets+NumBuckets,
88 bool empty() const { return NumEntries == 0; }
89 unsigned size() const { return NumEntries; }
92 const KeyT EmptyKey = getEmptyKey(), TombstoneKey = getTombstoneKey();
93 for (BucketT *P = Buckets, *E = Buckets+NumBuckets; P != E; ++P) {
94 if (P->first != EmptyKey && P->first != TombstoneKey) {
100 assert(NumEntries == 0 && "Node count imbalance!");
104 /// count - Return true if the specified key is in the map.
105 bool count(const KeyT &Val) const {
107 return LookupBucketFor(Val, TheBucket);
110 iterator find(const KeyT &Val) const {
112 if (LookupBucketFor(Val, TheBucket))
113 return iterator(TheBucket, Buckets+NumBuckets);
117 bool insert(const std::pair<KeyT, ValueT> &KV) {
119 if (LookupBucketFor(KV.first, TheBucket))
120 return false; // Already in map.
122 // Otherwise, insert the new element.
123 InsertIntoBucket(KV.first, KV.second, TheBucket);
127 bool erase(const KeyT &Val) {
129 if (!LookupBucketFor(Val, TheBucket))
130 return false; // not in map.
132 TheBucket->second.~ValueT();
133 TheBucket->first = getTombstoneKey();
138 bool erase(iterator I) {
139 BucketT *TheBucket = &*I;
140 TheBucket->second.~ValueT();
141 TheBucket->first = getTombstoneKey();
147 ValueT &operator[](const KeyT &Key) {
149 if (LookupBucketFor(Key, TheBucket))
150 return TheBucket->second;
152 return InsertIntoBucket(Key, ValueT(), TheBucket)->second;
156 BucketT *InsertIntoBucket(const KeyT &Key, const ValueT &Value,
157 BucketT *TheBucket) {
158 // If the load of the hash table is more than 3/4, or if fewer than 1/8 of
159 // the buckets are empty (meaning that many are filled with tombstones),
162 // The later case is tricky. For example, if we had one empty bucket with
163 // tons of tombstones, failing lookups (e.g. for insertion) would have to
164 // probe almost the entire table until it found the empty bucket. If the
165 // table completely filled with tombstones, no lookup would ever succeed,
166 // causing infinite loops in lookup.
167 if (NumEntries*4 >= NumBuckets*3 ||
168 NumBuckets-(NumEntries+NumTombstones) < NumBuckets/8) {
170 LookupBucketFor(Key, TheBucket);
174 // If we are writing over a tombstone, remember this.
175 if (TheBucket->first != getEmptyKey())
178 TheBucket->first = Key;
179 new (&TheBucket->second) ValueT(Value);
183 static unsigned getHashValue(const KeyT &Val) {
184 return DenseMapKeyInfo<KeyT>::getHashValue(Val);
186 static const KeyT getEmptyKey() {
187 return DenseMapKeyInfo<KeyT>::getEmptyKey();
189 static const KeyT getTombstoneKey() {
190 return DenseMapKeyInfo<KeyT>::getTombstoneKey();
193 /// LookupBucketFor - Lookup the appropriate bucket for Val, returning it in
194 /// FoundBucket. If the bucket contains the key and a value, this returns
195 /// true, otherwise it returns a bucket with an empty marker or tombstone and
197 bool LookupBucketFor(const KeyT &Val, BucketT *&FoundBucket) const {
198 unsigned BucketNo = getHashValue(Val);
199 unsigned ProbeAmt = 1;
200 BucketT *BucketsPtr = Buckets;
202 // FoundTombstone - Keep track of whether we find a tombstone while probing.
203 BucketT *FoundTombstone = 0;
204 const KeyT EmptyKey = getEmptyKey();
205 const KeyT TombstoneKey = getTombstoneKey();
206 assert(Val != EmptyKey && Val != TombstoneKey &&
207 "Empty/Tombstone value shouldn't be inserted into map!");
210 BucketT *ThisBucket = BucketsPtr + (BucketNo & (NumBuckets-1));
211 // Found Val's bucket? If so, return it.
212 if (ThisBucket->first == Val) {
213 FoundBucket = ThisBucket;
217 // If we found an empty bucket, the key doesn't exist in the set.
218 // Insert it and return the default value.
219 if (ThisBucket->first == EmptyKey) {
220 // If we've already seen a tombstone while probing, fill it in instead
221 // of the empty bucket we eventually probed to.
222 if (FoundTombstone) ThisBucket = FoundTombstone;
223 FoundBucket = FoundTombstone ? FoundTombstone : ThisBucket;
227 // If this is a tombstone, remember it. If Val ends up not in the map, we
228 // prefer to return it than something that would require more probing.
229 if (ThisBucket->first == TombstoneKey && !FoundTombstone)
230 FoundTombstone = ThisBucket; // Remember the first tombstone found.
232 // Otherwise, it's a hash collision or a tombstone, continue quadratic
234 BucketNo += ProbeAmt++;
238 void init(unsigned InitBuckets) {
241 NumBuckets = InitBuckets;
242 assert(InitBuckets && (InitBuckets & InitBuckets-1) == 0 &&
243 "# initial buckets must be a power of two!");
244 Buckets = (BucketT*)new char[sizeof(BucketT)*InitBuckets];
245 // Initialize all the keys to EmptyKey.
246 const KeyT EmptyKey = getEmptyKey();
247 for (unsigned i = 0; i != InitBuckets; ++i)
248 new (&Buckets[i].first) KeyT(EmptyKey);
252 unsigned OldNumBuckets = NumBuckets;
253 BucketT *OldBuckets = Buckets;
255 // Double the number of buckets.
258 Buckets = (BucketT*)new char[sizeof(BucketT)*NumBuckets];
260 // Initialize all the keys to EmptyKey.
261 const KeyT EmptyKey = getEmptyKey();
262 for (unsigned i = 0, e = NumBuckets; i != e; ++i)
263 new (&Buckets[i].first) KeyT(EmptyKey);
265 // Insert all the old elements.
266 const KeyT TombstoneKey = getTombstoneKey();
267 for (BucketT *B = OldBuckets, *E = OldBuckets+OldNumBuckets; B != E; ++B) {
268 if (B->first != EmptyKey && B->first != TombstoneKey) {
269 // Insert the key/value into the new table.
271 bool FoundVal = LookupBucketFor(B->first, DestBucket);
272 FoundVal = FoundVal; // silence warning.
273 assert(!FoundVal && "Key already in new map?");
274 DestBucket->first = B->first;
275 new (&DestBucket->second) ValueT(B->second);
283 // Free the old table.
284 delete[] (char*)OldBuckets;
288 template<typename KeyT, typename ValueT>
289 class DenseMapIterator {
290 typedef std::pair<KeyT, ValueT> BucketT;
292 const BucketT *Ptr, *End;
294 DenseMapIterator(const BucketT *Pos, const BucketT *E) : Ptr(Pos), End(E) {
295 AdvancePastEmptyBuckets();
298 std::pair<KeyT, ValueT> &operator*() const {
299 return *const_cast<BucketT*>(Ptr);
301 std::pair<KeyT, ValueT> *operator->() const {
302 return const_cast<BucketT*>(Ptr);
305 bool operator==(const DenseMapIterator &RHS) const {
306 return Ptr == RHS.Ptr;
308 bool operator!=(const DenseMapIterator &RHS) const {
309 return Ptr != RHS.Ptr;
312 inline DenseMapIterator& operator++() { // Preincrement
314 AdvancePastEmptyBuckets();
317 DenseMapIterator operator++(int) { // Postincrement
318 DenseMapIterator tmp = *this; ++*this; return tmp;
322 void AdvancePastEmptyBuckets() {
323 const KeyT Empty = DenseMapKeyInfo<KeyT>::getEmptyKey();
324 const KeyT Tombstone = DenseMapKeyInfo<KeyT>::getTombstoneKey();
326 while (Ptr != End && (Ptr->first == Empty || Ptr->first == Tombstone))
331 template<typename KeyT, typename ValueT>
332 class DenseMapConstIterator : public DenseMapIterator<KeyT, ValueT> {
334 DenseMapConstIterator(const std::pair<KeyT, ValueT> *Pos,
335 const std::pair<KeyT, ValueT> *E)
336 : DenseMapIterator<KeyT, ValueT>(Pos, E) {
338 const std::pair<KeyT, ValueT> &operator*() const {
341 const std::pair<KeyT, ValueT> *operator->() const {
346 } // end namespace llvm