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; }
45 struct DenseMapValueInfo {
49 // Provide DenseMapValueInfo for all pointers.
51 struct DenseMapValueInfo<T*> {
52 static bool isPod() { return true; }
55 template<typename KeyT, typename ValueT,
56 typename KeyInfoT = DenseMapKeyInfo<KeyT>,
57 typename ValueInfoT = DenseMapValueInfo<ValueT> >
58 class DenseMapIterator;
59 template<typename KeyT, typename ValueT,
60 typename KeyInfoT = DenseMapKeyInfo<KeyT>,
61 typename ValueInfoT = DenseMapValueInfo<ValueT> >
62 class DenseMapConstIterator;
64 template<typename KeyT, typename ValueT,
65 typename KeyInfoT = DenseMapKeyInfo<KeyT>,
66 typename ValueInfoT = DenseMapValueInfo<ValueT> >
68 typedef std::pair<KeyT, ValueT> BucketT;
73 unsigned NumTombstones;
75 DenseMap(const DenseMap& other) {
80 explicit DenseMap(unsigned NumInitBuckets = 64) {
85 const KeyT EmptyKey = getEmptyKey(), TombstoneKey = getTombstoneKey();
86 for (BucketT *P = Buckets, *E = Buckets+NumBuckets; P != E; ++P) {
87 if (P->first != EmptyKey && P->first != TombstoneKey)
91 delete[] reinterpret_cast<char*>(Buckets);
94 typedef DenseMapIterator<KeyT, ValueT, KeyInfoT> iterator;
95 typedef DenseMapConstIterator<KeyT, ValueT, KeyInfoT> const_iterator;
96 inline iterator begin() {
97 return iterator(Buckets, Buckets+NumBuckets);
99 inline iterator end() {
100 return iterator(Buckets+NumBuckets, Buckets+NumBuckets);
102 inline const_iterator begin() const {
103 return const_iterator(Buckets, Buckets+NumBuckets);
105 inline const_iterator end() const {
106 return const_iterator(Buckets+NumBuckets, Buckets+NumBuckets);
109 bool empty() const { return NumEntries == 0; }
110 unsigned size() const { return NumEntries; }
113 // If the capacity of the array is huge, and the # elements used is small,
115 if (NumEntries * 4 < NumBuckets && NumBuckets > 64) {
120 const KeyT EmptyKey = getEmptyKey(), TombstoneKey = getTombstoneKey();
121 for (BucketT *P = Buckets, *E = Buckets+NumBuckets; P != E; ++P) {
122 if (P->first != EmptyKey) {
123 if (P->first != TombstoneKey) {
130 assert(NumEntries == 0 && "Node count imbalance!");
134 /// count - Return true if the specified key is in the map.
135 bool count(const KeyT &Val) const {
137 return LookupBucketFor(Val, TheBucket);
140 iterator find(const KeyT &Val) {
142 if (LookupBucketFor(Val, TheBucket))
143 return iterator(TheBucket, Buckets+NumBuckets);
146 const_iterator find(const KeyT &Val) const {
148 if (LookupBucketFor(Val, TheBucket))
149 return const_iterator(TheBucket, Buckets+NumBuckets);
153 bool insert(const std::pair<KeyT, ValueT> &KV) {
155 if (LookupBucketFor(KV.first, TheBucket))
156 return false; // Already in map.
158 // Otherwise, insert the new element.
159 InsertIntoBucket(KV.first, KV.second, TheBucket);
163 bool erase(const KeyT &Val) {
165 if (!LookupBucketFor(Val, TheBucket))
166 return false; // not in map.
168 TheBucket->second.~ValueT();
169 TheBucket->first = getTombstoneKey();
174 bool erase(iterator I) {
175 BucketT *TheBucket = &*I;
176 TheBucket->second.~ValueT();
177 TheBucket->first = getTombstoneKey();
183 ValueT &operator[](const KeyT &Key) {
185 if (LookupBucketFor(Key, TheBucket))
186 return TheBucket->second;
188 return InsertIntoBucket(Key, ValueT(), TheBucket)->second;
191 DenseMap& operator=(const DenseMap& other) {
197 void CopyFrom(const DenseMap& other) {
198 if (NumBuckets != 0 && (!KeyInfoT::isPod() || !ValueInfoT::isPod())) {
199 const KeyT EmptyKey = getEmptyKey(), TombstoneKey = getTombstoneKey();
200 for (BucketT *P = Buckets, *E = Buckets+NumBuckets; P != E; ++P) {
201 if (P->first != EmptyKey && P->first != TombstoneKey)
207 NumEntries = other.NumEntries;
208 NumTombstones = other.NumTombstones;
211 delete[] reinterpret_cast<char*>(Buckets);
212 Buckets = reinterpret_cast<BucketT*>(new char[sizeof(BucketT) *
215 if (KeyInfoT::isPod() && ValueInfoT::isPod())
216 memcpy(Buckets, other.Buckets, other.NumBuckets * sizeof(BucketT));
218 for (size_t i = 0; i < other.NumBuckets; ++i) {
219 new (Buckets[i].first) KeyT(other.Buckets[i].first);
220 if (Buckets[i].first != getEmptyKey() &&
221 Buckets[i].first != getTombstoneKey())
222 new (Buckets[i].second) ValueT(other.Buckets[i].second);
224 NumBuckets = other.NumBuckets;
227 BucketT *InsertIntoBucket(const KeyT &Key, const ValueT &Value,
228 BucketT *TheBucket) {
229 // If the load of the hash table is more than 3/4, or if fewer than 1/8 of
230 // the buckets are empty (meaning that many are filled with tombstones),
233 // The later case is tricky. For example, if we had one empty bucket with
234 // tons of tombstones, failing lookups (e.g. for insertion) would have to
235 // probe almost the entire table until it found the empty bucket. If the
236 // table completely filled with tombstones, no lookup would ever succeed,
237 // causing infinite loops in lookup.
238 if (NumEntries*4 >= NumBuckets*3 ||
239 NumBuckets-(NumEntries+NumTombstones) < NumBuckets/8) {
241 LookupBucketFor(Key, TheBucket);
245 // If we are writing over a tombstone, remember this.
246 if (TheBucket->first != getEmptyKey())
249 TheBucket->first = Key;
250 new (&TheBucket->second) ValueT(Value);
254 static unsigned getHashValue(const KeyT &Val) {
255 return KeyInfoT::getHashValue(Val);
257 static const KeyT getEmptyKey() {
258 return KeyInfoT::getEmptyKey();
260 static const KeyT getTombstoneKey() {
261 return KeyInfoT::getTombstoneKey();
264 /// LookupBucketFor - Lookup the appropriate bucket for Val, returning it in
265 /// FoundBucket. If the bucket contains the key and a value, this returns
266 /// true, otherwise it returns a bucket with an empty marker or tombstone and
268 bool LookupBucketFor(const KeyT &Val, BucketT *&FoundBucket) const {
269 unsigned BucketNo = getHashValue(Val);
270 unsigned ProbeAmt = 1;
271 BucketT *BucketsPtr = Buckets;
273 // FoundTombstone - Keep track of whether we find a tombstone while probing.
274 BucketT *FoundTombstone = 0;
275 const KeyT EmptyKey = getEmptyKey();
276 const KeyT TombstoneKey = getTombstoneKey();
277 assert(Val != EmptyKey && Val != TombstoneKey &&
278 "Empty/Tombstone value shouldn't be inserted into map!");
281 BucketT *ThisBucket = BucketsPtr + (BucketNo & (NumBuckets-1));
282 // Found Val's bucket? If so, return it.
283 if (ThisBucket->first == Val) {
284 FoundBucket = ThisBucket;
288 // If we found an empty bucket, the key doesn't exist in the set.
289 // Insert it and return the default value.
290 if (ThisBucket->first == EmptyKey) {
291 // If we've already seen a tombstone while probing, fill it in instead
292 // of the empty bucket we eventually probed to.
293 if (FoundTombstone) ThisBucket = FoundTombstone;
294 FoundBucket = FoundTombstone ? FoundTombstone : ThisBucket;
298 // If this is a tombstone, remember it. If Val ends up not in the map, we
299 // prefer to return it than something that would require more probing.
300 if (ThisBucket->first == TombstoneKey && !FoundTombstone)
301 FoundTombstone = ThisBucket; // Remember the first tombstone found.
303 // Otherwise, it's a hash collision or a tombstone, continue quadratic
305 BucketNo += ProbeAmt++;
309 void init(unsigned InitBuckets) {
312 NumBuckets = InitBuckets;
313 assert(InitBuckets && (InitBuckets & InitBuckets-1) == 0 &&
314 "# initial buckets must be a power of two!");
315 Buckets = reinterpret_cast<BucketT*>(new char[sizeof(BucketT)*InitBuckets]);
316 // Initialize all the keys to EmptyKey.
317 const KeyT EmptyKey = getEmptyKey();
318 for (unsigned i = 0; i != InitBuckets; ++i)
319 new (&Buckets[i].first) KeyT(EmptyKey);
323 unsigned OldNumBuckets = NumBuckets;
324 BucketT *OldBuckets = Buckets;
326 // Double the number of buckets.
329 Buckets = reinterpret_cast<BucketT*>(new char[sizeof(BucketT)*NumBuckets]);
331 // Initialize all the keys to EmptyKey.
332 const KeyT EmptyKey = getEmptyKey();
333 for (unsigned i = 0, e = NumBuckets; i != e; ++i)
334 new (&Buckets[i].first) KeyT(EmptyKey);
336 // Insert all the old elements.
337 const KeyT TombstoneKey = getTombstoneKey();
338 for (BucketT *B = OldBuckets, *E = OldBuckets+OldNumBuckets; B != E; ++B) {
339 if (B->first != EmptyKey && B->first != TombstoneKey) {
340 // Insert the key/value into the new table.
342 bool FoundVal = LookupBucketFor(B->first, DestBucket);
343 FoundVal = FoundVal; // silence warning.
344 assert(!FoundVal && "Key already in new map?");
345 DestBucket->first = B->first;
346 new (&DestBucket->second) ValueT(B->second);
354 // Free the old table.
355 delete[] reinterpret_cast<char*>(OldBuckets);
358 void shrink_and_clear() {
359 unsigned OldNumBuckets = NumBuckets;
360 BucketT *OldBuckets = Buckets;
362 // Reduce the number of buckets.
363 NumBuckets = NumEntries > 32 ? 1 << (Log2_32_Ceil(NumEntries) + 1)
366 Buckets = reinterpret_cast<BucketT*>(new char[sizeof(BucketT)*NumBuckets]);
368 // Initialize all the keys to EmptyKey.
369 const KeyT EmptyKey = getEmptyKey();
370 for (unsigned i = 0, e = NumBuckets; i != e; ++i)
371 new (&Buckets[i].first) KeyT(EmptyKey);
373 // Free the old buckets.
374 const KeyT TombstoneKey = getTombstoneKey();
375 for (BucketT *B = OldBuckets, *E = OldBuckets+OldNumBuckets; B != E; ++B) {
376 if (B->first != EmptyKey && B->first != TombstoneKey) {
383 // Free the old table.
384 delete[] reinterpret_cast<char*>(OldBuckets);
390 template<typename KeyT, typename ValueT, typename KeyInfoT, typename ValueInfoT>
391 class DenseMapIterator {
392 typedef std::pair<KeyT, ValueT> BucketT;
394 const BucketT *Ptr, *End;
396 DenseMapIterator(const BucketT *Pos, const BucketT *E) : Ptr(Pos), End(E) {
397 AdvancePastEmptyBuckets();
400 std::pair<KeyT, ValueT> &operator*() const {
401 return *const_cast<BucketT*>(Ptr);
403 std::pair<KeyT, ValueT> *operator->() const {
404 return const_cast<BucketT*>(Ptr);
407 bool operator==(const DenseMapIterator &RHS) const {
408 return Ptr == RHS.Ptr;
410 bool operator!=(const DenseMapIterator &RHS) const {
411 return Ptr != RHS.Ptr;
414 inline DenseMapIterator& operator++() { // Preincrement
416 AdvancePastEmptyBuckets();
419 DenseMapIterator operator++(int) { // Postincrement
420 DenseMapIterator tmp = *this; ++*this; return tmp;
424 void AdvancePastEmptyBuckets() {
425 const KeyT Empty = KeyInfoT::getEmptyKey();
426 const KeyT Tombstone = KeyInfoT::getTombstoneKey();
428 while (Ptr != End && (Ptr->first == Empty || Ptr->first == Tombstone))
433 template<typename KeyT, typename ValueT, typename KeyInfoT, typename ValueInfoT>
434 class DenseMapConstIterator : public DenseMapIterator<KeyT, ValueT, KeyInfoT> {
436 DenseMapConstIterator(const std::pair<KeyT, ValueT> *Pos,
437 const std::pair<KeyT, ValueT> *E)
438 : DenseMapIterator<KeyT, ValueT, KeyInfoT>(Pos, E) {
440 const std::pair<KeyT, ValueT> &operator*() const {
443 const std::pair<KeyT, ValueT> *operator->() const {
448 } // end namespace llvm