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 reinterpret_cast<T*>(-1); }
36 static inline T* getTombstoneKey() { return reinterpret_cast<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;
61 DenseMap(const DenseMap& other) {
66 explicit DenseMap(unsigned NumInitBuckets = 64) {
71 const KeyT EmptyKey = getEmptyKey(), TombstoneKey = getTombstoneKey();
72 for (BucketT *P = Buckets, *E = Buckets+NumBuckets; P != E; ++P) {
73 if (P->first != EmptyKey && P->first != TombstoneKey)
77 delete[] reinterpret_cast<char*>(Buckets);
80 typedef DenseMapIterator<KeyT, ValueT, KeyInfoT> iterator;
81 typedef DenseMapConstIterator<KeyT, ValueT, KeyInfoT> const_iterator;
82 inline iterator begin() {
83 return iterator(Buckets, Buckets+NumBuckets);
85 inline iterator end() {
86 return iterator(Buckets+NumBuckets, Buckets+NumBuckets);
88 inline const_iterator begin() const {
89 return const_iterator(Buckets, Buckets+NumBuckets);
91 inline const_iterator end() const {
92 return const_iterator(Buckets+NumBuckets, Buckets+NumBuckets);
95 bool empty() const { return NumEntries == 0; }
96 unsigned size() const { return NumEntries; }
99 // If the capacity of the array is huge, and the # elements used is small,
101 if (NumEntries * 4 < NumBuckets && NumBuckets > 64) {
106 const KeyT EmptyKey = getEmptyKey(), TombstoneKey = getTombstoneKey();
107 for (BucketT *P = Buckets, *E = Buckets+NumBuckets; P != E; ++P) {
108 if (P->first != EmptyKey) {
109 if (P->first != TombstoneKey) {
116 assert(NumEntries == 0 && "Node count imbalance!");
120 /// count - Return true if the specified key is in the map.
121 bool count(const KeyT &Val) const {
123 return LookupBucketFor(Val, TheBucket);
126 iterator find(const KeyT &Val) {
128 if (LookupBucketFor(Val, TheBucket))
129 return iterator(TheBucket, Buckets+NumBuckets);
132 const_iterator find(const KeyT &Val) const {
134 if (LookupBucketFor(Val, TheBucket))
135 return const_iterator(TheBucket, Buckets+NumBuckets);
139 bool insert(const std::pair<KeyT, ValueT> &KV) {
141 if (LookupBucketFor(KV.first, TheBucket))
142 return false; // Already in map.
144 // Otherwise, insert the new element.
145 InsertIntoBucket(KV.first, KV.second, TheBucket);
149 bool erase(const KeyT &Val) {
151 if (!LookupBucketFor(Val, TheBucket))
152 return false; // not in map.
154 TheBucket->second.~ValueT();
155 TheBucket->first = getTombstoneKey();
160 bool erase(iterator I) {
161 BucketT *TheBucket = &*I;
162 TheBucket->second.~ValueT();
163 TheBucket->first = getTombstoneKey();
169 ValueT &operator[](const KeyT &Key) {
171 if (LookupBucketFor(Key, TheBucket))
172 return TheBucket->second;
174 return InsertIntoBucket(Key, ValueT(), TheBucket)->second;
177 DenseMap& operator=(const DenseMap& other) {
183 void CopyFrom(const DenseMap& other) {
184 if (NumBuckets != 0 && !KeyInfoT::isPod()) {
185 const KeyT EmptyKey = getEmptyKey(), TombstoneKey = getTombstoneKey();
186 for (BucketT *P = Buckets, *E = Buckets+NumBuckets; P != E; ++P) {
187 if (P->first != EmptyKey && P->first != TombstoneKey)
193 NumEntries = other.NumEntries;
194 NumTombstones = other.NumTombstones;
197 delete[] reinterpret_cast<char*>(Buckets);
198 Buckets = reinterpret_cast<BucketT*>(new char[sizeof(BucketT) *
201 if (KeyInfoT::isPod())
202 memcpy(Buckets, other.Buckets, other.NumBuckets * sizeof(BucketT));
204 for (size_t i = 0; i < other.NumBuckets; ++i) {
205 new (Buckets[i].first) KeyT(other.Buckets[i].first);
206 new (Buckets[i].second) ValueT(other.Buckets[i].second);
208 NumBuckets = other.NumBuckets;
211 BucketT *InsertIntoBucket(const KeyT &Key, const ValueT &Value,
212 BucketT *TheBucket) {
213 // If the load of the hash table is more than 3/4, or if fewer than 1/8 of
214 // the buckets are empty (meaning that many are filled with tombstones),
217 // The later case is tricky. For example, if we had one empty bucket with
218 // tons of tombstones, failing lookups (e.g. for insertion) would have to
219 // probe almost the entire table until it found the empty bucket. If the
220 // table completely filled with tombstones, no lookup would ever succeed,
221 // causing infinite loops in lookup.
222 if (NumEntries*4 >= NumBuckets*3 ||
223 NumBuckets-(NumEntries+NumTombstones) < NumBuckets/8) {
225 LookupBucketFor(Key, TheBucket);
229 // If we are writing over a tombstone, remember this.
230 if (TheBucket->first != getEmptyKey())
233 TheBucket->first = Key;
234 new (&TheBucket->second) ValueT(Value);
238 static unsigned getHashValue(const KeyT &Val) {
239 return KeyInfoT::getHashValue(Val);
241 static const KeyT getEmptyKey() {
242 return KeyInfoT::getEmptyKey();
244 static const KeyT getTombstoneKey() {
245 return KeyInfoT::getTombstoneKey();
248 /// LookupBucketFor - Lookup the appropriate bucket for Val, returning it in
249 /// FoundBucket. If the bucket contains the key and a value, this returns
250 /// true, otherwise it returns a bucket with an empty marker or tombstone and
252 bool LookupBucketFor(const KeyT &Val, BucketT *&FoundBucket) const {
253 unsigned BucketNo = getHashValue(Val);
254 unsigned ProbeAmt = 1;
255 BucketT *BucketsPtr = Buckets;
257 // FoundTombstone - Keep track of whether we find a tombstone while probing.
258 BucketT *FoundTombstone = 0;
259 const KeyT EmptyKey = getEmptyKey();
260 const KeyT TombstoneKey = getTombstoneKey();
261 assert(Val != EmptyKey && Val != TombstoneKey &&
262 "Empty/Tombstone value shouldn't be inserted into map!");
265 BucketT *ThisBucket = BucketsPtr + (BucketNo & (NumBuckets-1));
266 // Found Val's bucket? If so, return it.
267 if (ThisBucket->first == Val) {
268 FoundBucket = ThisBucket;
272 // If we found an empty bucket, the key doesn't exist in the set.
273 // Insert it and return the default value.
274 if (ThisBucket->first == EmptyKey) {
275 // If we've already seen a tombstone while probing, fill it in instead
276 // of the empty bucket we eventually probed to.
277 if (FoundTombstone) ThisBucket = FoundTombstone;
278 FoundBucket = FoundTombstone ? FoundTombstone : ThisBucket;
282 // If this is a tombstone, remember it. If Val ends up not in the map, we
283 // prefer to return it than something that would require more probing.
284 if (ThisBucket->first == TombstoneKey && !FoundTombstone)
285 FoundTombstone = ThisBucket; // Remember the first tombstone found.
287 // Otherwise, it's a hash collision or a tombstone, continue quadratic
289 BucketNo += ProbeAmt++;
293 void init(unsigned InitBuckets) {
296 NumBuckets = InitBuckets;
297 assert(InitBuckets && (InitBuckets & InitBuckets-1) == 0 &&
298 "# initial buckets must be a power of two!");
299 Buckets = reinterpret_cast<BucketT*>(new char[sizeof(BucketT)*InitBuckets]);
300 // Initialize all the keys to EmptyKey.
301 const KeyT EmptyKey = getEmptyKey();
302 for (unsigned i = 0; i != InitBuckets; ++i)
303 new (&Buckets[i].first) KeyT(EmptyKey);
307 unsigned OldNumBuckets = NumBuckets;
308 BucketT *OldBuckets = Buckets;
310 // Double the number of buckets.
313 Buckets = reinterpret_cast<BucketT*>(new char[sizeof(BucketT)*NumBuckets]);
315 // Initialize all the keys to EmptyKey.
316 const KeyT EmptyKey = getEmptyKey();
317 for (unsigned i = 0, e = NumBuckets; i != e; ++i)
318 new (&Buckets[i].first) KeyT(EmptyKey);
320 // Insert all the old elements.
321 const KeyT TombstoneKey = getTombstoneKey();
322 for (BucketT *B = OldBuckets, *E = OldBuckets+OldNumBuckets; B != E; ++B) {
323 if (B->first != EmptyKey && B->first != TombstoneKey) {
324 // Insert the key/value into the new table.
326 bool FoundVal = LookupBucketFor(B->first, DestBucket);
327 FoundVal = FoundVal; // silence warning.
328 assert(!FoundVal && "Key already in new map?");
329 DestBucket->first = B->first;
330 new (&DestBucket->second) ValueT(B->second);
338 // Free the old table.
339 delete[] reinterpret_cast<char*>(OldBuckets);
342 void shrink_and_clear() {
343 unsigned OldNumBuckets = NumBuckets;
344 BucketT *OldBuckets = Buckets;
346 // Reduce the number of buckets.
347 NumBuckets = NumEntries > 32 ? 1 << (Log2_32_Ceil(NumEntries) + 1)
350 Buckets = reinterpret_cast<BucketT*>(new char[sizeof(BucketT)*NumBuckets]);
352 // Initialize all the keys to EmptyKey.
353 const KeyT EmptyKey = getEmptyKey();
354 for (unsigned i = 0, e = NumBuckets; i != e; ++i)
355 new (&Buckets[i].first) KeyT(EmptyKey);
357 // Free the old buckets.
358 const KeyT TombstoneKey = getTombstoneKey();
359 for (BucketT *B = OldBuckets, *E = OldBuckets+OldNumBuckets; B != E; ++B) {
360 if (B->first != EmptyKey && B->first != TombstoneKey) {
367 // Free the old table.
368 delete[] reinterpret_cast<char*>(OldBuckets);
374 template<typename KeyT, typename ValueT, typename KeyInfoT>
375 class DenseMapIterator {
376 typedef std::pair<KeyT, ValueT> BucketT;
378 const BucketT *Ptr, *End;
380 DenseMapIterator(const BucketT *Pos, const BucketT *E) : Ptr(Pos), End(E) {
381 AdvancePastEmptyBuckets();
384 std::pair<KeyT, ValueT> &operator*() const {
385 return *const_cast<BucketT*>(Ptr);
387 std::pair<KeyT, ValueT> *operator->() const {
388 return const_cast<BucketT*>(Ptr);
391 bool operator==(const DenseMapIterator &RHS) const {
392 return Ptr == RHS.Ptr;
394 bool operator!=(const DenseMapIterator &RHS) const {
395 return Ptr != RHS.Ptr;
398 inline DenseMapIterator& operator++() { // Preincrement
400 AdvancePastEmptyBuckets();
403 DenseMapIterator operator++(int) { // Postincrement
404 DenseMapIterator tmp = *this; ++*this; return tmp;
408 void AdvancePastEmptyBuckets() {
409 const KeyT Empty = KeyInfoT::getEmptyKey();
410 const KeyT Tombstone = KeyInfoT::getTombstoneKey();
412 while (Ptr != End && (Ptr->first == Empty || Ptr->first == Tombstone))
417 template<typename KeyT, typename ValueT, typename KeyInfoT>
418 class DenseMapConstIterator : public DenseMapIterator<KeyT, ValueT, KeyInfoT> {
420 DenseMapConstIterator(const std::pair<KeyT, ValueT> *Pos,
421 const std::pair<KeyT, ValueT> *E)
422 : DenseMapIterator<KeyT, ValueT, KeyInfoT>(Pos, E) {
424 const std::pair<KeyT, ValueT> &operator*() const {
427 const std::pair<KeyT, ValueT> *operator->() const {
432 } // end namespace llvm