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"
26 //static inline T getEmptyKey();
27 //static inline T getTombstoneKey();
28 //static unsigned getHashValue(const T &Val);
29 //static bool isEqual(const T &LHS, const T &RHS);
33 // Provide DenseMapInfo for all pointers.
35 struct DenseMapInfo<T*> {
36 static inline T* getEmptyKey() { return reinterpret_cast<T*>(-1); }
37 static inline T* getTombstoneKey() { return reinterpret_cast<T*>(-2); }
38 static unsigned getHashValue(const T *PtrVal) {
39 return (unsigned(uintptr_t(PtrVal)) >> 4) ^
40 (unsigned(uintptr_t(PtrVal)) >> 9);
42 static bool isEqual(const T *LHS, const T *RHS) { return LHS == RHS; }
43 static bool isPod() { return true; }
46 template<typename KeyT, typename ValueT,
47 typename KeyInfoT = DenseMapInfo<KeyT>,
48 typename ValueInfoT = DenseMapInfo<ValueT> >
49 class DenseMapIterator;
50 template<typename KeyT, typename ValueT,
51 typename KeyInfoT = DenseMapInfo<KeyT>,
52 typename ValueInfoT = DenseMapInfo<ValueT> >
53 class DenseMapConstIterator;
55 template<typename KeyT, typename ValueT,
56 typename KeyInfoT = DenseMapInfo<KeyT>,
57 typename ValueInfoT = DenseMapInfo<ValueT> >
59 typedef std::pair<KeyT, ValueT> BucketT;
64 unsigned NumTombstones;
66 DenseMap(const DenseMap& other) {
71 explicit DenseMap(unsigned NumInitBuckets = 64) {
76 const KeyT EmptyKey = getEmptyKey(), TombstoneKey = getTombstoneKey();
77 for (BucketT *P = Buckets, *E = Buckets+NumBuckets; P != E; ++P) {
78 if (P->first != EmptyKey && P->first != TombstoneKey)
82 delete[] reinterpret_cast<char*>(Buckets);
85 typedef DenseMapIterator<KeyT, ValueT, KeyInfoT> iterator;
86 typedef DenseMapConstIterator<KeyT, ValueT, KeyInfoT> const_iterator;
87 inline iterator begin() {
88 return iterator(Buckets, Buckets+NumBuckets);
90 inline iterator end() {
91 return iterator(Buckets+NumBuckets, Buckets+NumBuckets);
93 inline const_iterator begin() const {
94 return const_iterator(Buckets, Buckets+NumBuckets);
96 inline const_iterator end() const {
97 return const_iterator(Buckets+NumBuckets, Buckets+NumBuckets);
100 bool empty() const { return NumEntries == 0; }
101 unsigned size() const { return NumEntries; }
104 // If the capacity of the array is huge, and the # elements used is small,
106 if (NumEntries * 4 < NumBuckets && NumBuckets > 64) {
111 const KeyT EmptyKey = getEmptyKey(), TombstoneKey = getTombstoneKey();
112 for (BucketT *P = Buckets, *E = Buckets+NumBuckets; P != E; ++P) {
113 if (P->first != EmptyKey) {
114 if (P->first != TombstoneKey) {
121 assert(NumEntries == 0 && "Node count imbalance!");
125 /// count - Return true if the specified key is in the map.
126 bool count(const KeyT &Val) const {
128 return LookupBucketFor(Val, TheBucket);
131 iterator find(const KeyT &Val) {
133 if (LookupBucketFor(Val, TheBucket))
134 return iterator(TheBucket, Buckets+NumBuckets);
137 const_iterator find(const KeyT &Val) const {
139 if (LookupBucketFor(Val, TheBucket))
140 return const_iterator(TheBucket, Buckets+NumBuckets);
144 bool insert(const std::pair<KeyT, ValueT> &KV) {
146 if (LookupBucketFor(KV.first, TheBucket))
147 return false; // Already in map.
149 // Otherwise, insert the new element.
150 InsertIntoBucket(KV.first, KV.second, TheBucket);
154 bool erase(const KeyT &Val) {
156 if (!LookupBucketFor(Val, TheBucket))
157 return false; // not in map.
159 TheBucket->second.~ValueT();
160 TheBucket->first = getTombstoneKey();
165 bool erase(iterator I) {
166 BucketT *TheBucket = &*I;
167 TheBucket->second.~ValueT();
168 TheBucket->first = getTombstoneKey();
174 ValueT &operator[](const KeyT &Key) {
176 if (LookupBucketFor(Key, TheBucket))
177 return TheBucket->second;
179 return InsertIntoBucket(Key, ValueT(), TheBucket)->second;
182 DenseMap& operator=(const DenseMap& other) {
188 void CopyFrom(const DenseMap& other) {
189 if (NumBuckets != 0 && (!KeyInfoT::isPod() || !ValueInfoT::isPod())) {
190 const KeyT EmptyKey = getEmptyKey(), TombstoneKey = getTombstoneKey();
191 for (BucketT *P = Buckets, *E = Buckets+NumBuckets; P != E; ++P) {
192 if (P->first != EmptyKey && P->first != TombstoneKey)
198 NumEntries = other.NumEntries;
199 NumTombstones = other.NumTombstones;
202 delete[] reinterpret_cast<char*>(Buckets);
203 Buckets = reinterpret_cast<BucketT*>(new char[sizeof(BucketT) *
206 if (KeyInfoT::isPod() && ValueInfoT::isPod())
207 memcpy(Buckets, other.Buckets, other.NumBuckets * sizeof(BucketT));
209 for (size_t i = 0; i < other.NumBuckets; ++i) {
210 new (Buckets[i].first) KeyT(other.Buckets[i].first);
211 if (Buckets[i].first != getEmptyKey() &&
212 Buckets[i].first != getTombstoneKey())
213 new (&Buckets[i].second) ValueT(other.Buckets[i].second);
215 NumBuckets = other.NumBuckets;
218 BucketT *InsertIntoBucket(const KeyT &Key, const ValueT &Value,
219 BucketT *TheBucket) {
220 // If the load of the hash table is more than 3/4, or if fewer than 1/8 of
221 // the buckets are empty (meaning that many are filled with tombstones),
224 // The later case is tricky. For example, if we had one empty bucket with
225 // tons of tombstones, failing lookups (e.g. for insertion) would have to
226 // probe almost the entire table until it found the empty bucket. If the
227 // table completely filled with tombstones, no lookup would ever succeed,
228 // causing infinite loops in lookup.
229 if (NumEntries*4 >= NumBuckets*3 ||
230 NumBuckets-(NumEntries+NumTombstones) < NumBuckets/8) {
232 LookupBucketFor(Key, TheBucket);
236 // If we are writing over a tombstone, remember this.
237 if (TheBucket->first != getEmptyKey())
240 TheBucket->first = Key;
241 new (&TheBucket->second) ValueT(Value);
245 static unsigned getHashValue(const KeyT &Val) {
246 return KeyInfoT::getHashValue(Val);
248 static const KeyT getEmptyKey() {
249 return KeyInfoT::getEmptyKey();
251 static const KeyT getTombstoneKey() {
252 return KeyInfoT::getTombstoneKey();
255 /// LookupBucketFor - Lookup the appropriate bucket for Val, returning it in
256 /// FoundBucket. If the bucket contains the key and a value, this returns
257 /// true, otherwise it returns a bucket with an empty marker or tombstone and
259 bool LookupBucketFor(const KeyT &Val, BucketT *&FoundBucket) const {
260 unsigned BucketNo = getHashValue(Val);
261 unsigned ProbeAmt = 1;
262 BucketT *BucketsPtr = Buckets;
264 // FoundTombstone - Keep track of whether we find a tombstone while probing.
265 BucketT *FoundTombstone = 0;
266 const KeyT EmptyKey = getEmptyKey();
267 const KeyT TombstoneKey = getTombstoneKey();
268 assert(Val != EmptyKey && Val != TombstoneKey &&
269 "Empty/Tombstone value shouldn't be inserted into map!");
272 BucketT *ThisBucket = BucketsPtr + (BucketNo & (NumBuckets-1));
273 // Found Val's bucket? If so, return it.
274 if (KeyInfoT::isEqual(ThisBucket->first, Val)) {
275 FoundBucket = ThisBucket;
279 // If we found an empty bucket, the key doesn't exist in the set.
280 // Insert it and return the default value.
281 if (KeyInfoT::isEqual(ThisBucket->first, EmptyKey)) {
282 // If we've already seen a tombstone while probing, fill it in instead
283 // of the empty bucket we eventually probed to.
284 if (FoundTombstone) ThisBucket = FoundTombstone;
285 FoundBucket = FoundTombstone ? FoundTombstone : ThisBucket;
289 // If this is a tombstone, remember it. If Val ends up not in the map, we
290 // prefer to return it than something that would require more probing.
291 if (KeyInfoT::isEqual(ThisBucket->first, TombstoneKey) && !FoundTombstone)
292 FoundTombstone = ThisBucket; // Remember the first tombstone found.
294 // Otherwise, it's a hash collision or a tombstone, continue quadratic
296 BucketNo += ProbeAmt++;
300 void init(unsigned InitBuckets) {
303 NumBuckets = InitBuckets;
304 assert(InitBuckets && (InitBuckets & InitBuckets-1) == 0 &&
305 "# initial buckets must be a power of two!");
306 Buckets = reinterpret_cast<BucketT*>(new char[sizeof(BucketT)*InitBuckets]);
307 // Initialize all the keys to EmptyKey.
308 const KeyT EmptyKey = getEmptyKey();
309 for (unsigned i = 0; i != InitBuckets; ++i)
310 new (&Buckets[i].first) KeyT(EmptyKey);
314 unsigned OldNumBuckets = NumBuckets;
315 BucketT *OldBuckets = Buckets;
317 // Double the number of buckets.
320 Buckets = reinterpret_cast<BucketT*>(new char[sizeof(BucketT)*NumBuckets]);
322 // Initialize all the keys to EmptyKey.
323 const KeyT EmptyKey = getEmptyKey();
324 for (unsigned i = 0, e = NumBuckets; i != e; ++i)
325 new (&Buckets[i].first) KeyT(EmptyKey);
327 // Insert all the old elements.
328 const KeyT TombstoneKey = getTombstoneKey();
329 for (BucketT *B = OldBuckets, *E = OldBuckets+OldNumBuckets; B != E; ++B) {
330 if (B->first != EmptyKey && B->first != TombstoneKey) {
331 // Insert the key/value into the new table.
333 bool FoundVal = LookupBucketFor(B->first, DestBucket);
334 FoundVal = FoundVal; // silence warning.
335 assert(!FoundVal && "Key already in new map?");
336 DestBucket->first = B->first;
337 new (&DestBucket->second) ValueT(B->second);
345 // Free the old table.
346 delete[] reinterpret_cast<char*>(OldBuckets);
349 void shrink_and_clear() {
350 unsigned OldNumBuckets = NumBuckets;
351 BucketT *OldBuckets = Buckets;
353 // Reduce the number of buckets.
354 NumBuckets = NumEntries > 32 ? 1 << (Log2_32_Ceil(NumEntries) + 1)
357 Buckets = reinterpret_cast<BucketT*>(new char[sizeof(BucketT)*NumBuckets]);
359 // Initialize all the keys to EmptyKey.
360 const KeyT EmptyKey = getEmptyKey();
361 for (unsigned i = 0, e = NumBuckets; i != e; ++i)
362 new (&Buckets[i].first) KeyT(EmptyKey);
364 // Free the old buckets.
365 const KeyT TombstoneKey = getTombstoneKey();
366 for (BucketT *B = OldBuckets, *E = OldBuckets+OldNumBuckets; B != E; ++B) {
367 if (B->first != EmptyKey && B->first != TombstoneKey) {
374 // Free the old table.
375 delete[] reinterpret_cast<char*>(OldBuckets);
381 template<typename KeyT, typename ValueT, typename KeyInfoT, typename ValueInfoT>
382 class DenseMapIterator {
383 typedef std::pair<KeyT, ValueT> BucketT;
385 const BucketT *Ptr, *End;
387 DenseMapIterator(const BucketT *Pos, const BucketT *E) : Ptr(Pos), End(E) {
388 AdvancePastEmptyBuckets();
391 std::pair<KeyT, ValueT> &operator*() const {
392 return *const_cast<BucketT*>(Ptr);
394 std::pair<KeyT, ValueT> *operator->() const {
395 return const_cast<BucketT*>(Ptr);
398 bool operator==(const DenseMapIterator &RHS) const {
399 return Ptr == RHS.Ptr;
401 bool operator!=(const DenseMapIterator &RHS) const {
402 return Ptr != RHS.Ptr;
405 inline DenseMapIterator& operator++() { // Preincrement
407 AdvancePastEmptyBuckets();
410 DenseMapIterator operator++(int) { // Postincrement
411 DenseMapIterator tmp = *this; ++*this; return tmp;
415 void AdvancePastEmptyBuckets() {
416 const KeyT Empty = KeyInfoT::getEmptyKey();
417 const KeyT Tombstone = KeyInfoT::getTombstoneKey();
420 (KeyInfoT::isEqual(Ptr->first, Empty) ||
421 KeyInfoT::isEqual(Ptr->first, Tombstone)))
426 template<typename KeyT, typename ValueT, typename KeyInfoT, typename ValueInfoT>
427 class DenseMapConstIterator : public DenseMapIterator<KeyT, ValueT, KeyInfoT> {
429 DenseMapConstIterator(const std::pair<KeyT, ValueT> *Pos,
430 const std::pair<KeyT, ValueT> *E)
431 : DenseMapIterator<KeyT, ValueT, KeyInfoT>(Pos, E) {
433 const std::pair<KeyT, ValueT> &operator*() const {
436 const std::pair<KeyT, ValueT> *operator->() const {
441 } // end namespace llvm