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 (!KeyInfoT::isEqual(P->first, EmptyKey) &&
79 !KeyInfoT::isEqual(P->first, TombstoneKey))
83 delete[] reinterpret_cast<char*>(Buckets);
86 typedef DenseMapIterator<KeyT, ValueT, KeyInfoT> iterator;
87 typedef DenseMapConstIterator<KeyT, ValueT, KeyInfoT> const_iterator;
88 inline iterator begin() {
89 return iterator(Buckets, Buckets+NumBuckets);
91 inline iterator end() {
92 return iterator(Buckets+NumBuckets, Buckets+NumBuckets);
94 inline const_iterator begin() const {
95 return const_iterator(Buckets, Buckets+NumBuckets);
97 inline const_iterator end() const {
98 return const_iterator(Buckets+NumBuckets, Buckets+NumBuckets);
101 bool empty() const { return NumEntries == 0; }
102 unsigned size() const { return NumEntries; }
104 /// Grow the densemap so that it has at least Size buckets. Does not shrink
105 void resize(size_t Size) { grow(Size); }
108 // If the capacity of the array is huge, and the # elements used is small,
110 if (NumEntries * 4 < NumBuckets && NumBuckets > 64) {
115 const KeyT EmptyKey = getEmptyKey(), TombstoneKey = getTombstoneKey();
116 for (BucketT *P = Buckets, *E = Buckets+NumBuckets; P != E; ++P) {
117 if (!KeyInfoT::isEqual(P->first, EmptyKey)) {
118 if (!KeyInfoT::isEqual(P->first, TombstoneKey)) {
125 assert(NumEntries == 0 && "Node count imbalance!");
129 /// count - Return true if the specified key is in the map.
130 bool count(const KeyT &Val) const {
132 return LookupBucketFor(Val, TheBucket);
135 iterator find(const KeyT &Val) {
137 if (LookupBucketFor(Val, TheBucket))
138 return iterator(TheBucket, Buckets+NumBuckets);
141 const_iterator find(const KeyT &Val) const {
143 if (LookupBucketFor(Val, TheBucket))
144 return const_iterator(TheBucket, Buckets+NumBuckets);
148 bool insert(const std::pair<KeyT, ValueT> &KV) {
150 if (LookupBucketFor(KV.first, TheBucket))
151 return false; // Already in map.
153 // Otherwise, insert the new element.
154 InsertIntoBucket(KV.first, KV.second, TheBucket);
158 bool erase(const KeyT &Val) {
160 if (!LookupBucketFor(Val, TheBucket))
161 return false; // not in map.
163 TheBucket->second.~ValueT();
164 TheBucket->first = getTombstoneKey();
169 bool erase(iterator I) {
170 BucketT *TheBucket = &*I;
171 TheBucket->second.~ValueT();
172 TheBucket->first = getTombstoneKey();
178 ValueT &operator[](const KeyT &Key) {
180 if (LookupBucketFor(Key, TheBucket))
181 return TheBucket->second;
183 return InsertIntoBucket(Key, ValueT(), TheBucket)->second;
186 DenseMap& operator=(const DenseMap& other) {
192 void CopyFrom(const DenseMap& other) {
193 if (NumBuckets != 0 && (!KeyInfoT::isPod() || !ValueInfoT::isPod())) {
194 const KeyT EmptyKey = getEmptyKey(), TombstoneKey = getTombstoneKey();
195 for (BucketT *P = Buckets, *E = Buckets+NumBuckets; P != E; ++P) {
196 if (!KeyInfoT::isEqual(P->first, EmptyKey) &&
197 !KeyInfoT::isEqual(P->first, TombstoneKey))
203 NumEntries = other.NumEntries;
204 NumTombstones = other.NumTombstones;
207 delete[] reinterpret_cast<char*>(Buckets);
208 Buckets = reinterpret_cast<BucketT*>(new char[sizeof(BucketT) *
211 if (KeyInfoT::isPod() && ValueInfoT::isPod())
212 memcpy(Buckets, other.Buckets, other.NumBuckets * sizeof(BucketT));
214 for (size_t i = 0; i < other.NumBuckets; ++i) {
215 new (Buckets[i].first) KeyT(other.Buckets[i].first);
216 if (!KeyInfoT::isEqual(Buckets[i].first, getEmptyKey()) &&
217 !KeyInfoT::isEqual(Buckets[i].first, getTombstoneKey()))
218 new (&Buckets[i].second) ValueT(other.Buckets[i].second);
220 NumBuckets = other.NumBuckets;
223 BucketT *InsertIntoBucket(const KeyT &Key, const ValueT &Value,
224 BucketT *TheBucket) {
225 // If the load of the hash table is more than 3/4, or if fewer than 1/8 of
226 // the buckets are empty (meaning that many are filled with tombstones),
229 // The later case is tricky. For example, if we had one empty bucket with
230 // tons of tombstones, failing lookups (e.g. for insertion) would have to
231 // probe almost the entire table until it found the empty bucket. If the
232 // table completely filled with tombstones, no lookup would ever succeed,
233 // causing infinite loops in lookup.
234 if (NumEntries*4 >= NumBuckets*3 ||
235 NumBuckets-(NumEntries+NumTombstones) < NumBuckets/8) {
236 this->grow(NumBuckets * 2);
237 LookupBucketFor(Key, TheBucket);
241 // If we are writing over a tombstone, remember this.
242 if (!KeyInfoT::isEqual(TheBucket->first, getEmptyKey()))
245 TheBucket->first = Key;
246 new (&TheBucket->second) ValueT(Value);
250 static unsigned getHashValue(const KeyT &Val) {
251 return KeyInfoT::getHashValue(Val);
253 static const KeyT getEmptyKey() {
254 return KeyInfoT::getEmptyKey();
256 static const KeyT getTombstoneKey() {
257 return KeyInfoT::getTombstoneKey();
260 /// LookupBucketFor - Lookup the appropriate bucket for Val, returning it in
261 /// FoundBucket. If the bucket contains the key and a value, this returns
262 /// true, otherwise it returns a bucket with an empty marker or tombstone and
264 bool LookupBucketFor(const KeyT &Val, BucketT *&FoundBucket) const {
265 unsigned BucketNo = getHashValue(Val);
266 unsigned ProbeAmt = 1;
267 BucketT *BucketsPtr = Buckets;
269 // FoundTombstone - Keep track of whether we find a tombstone while probing.
270 BucketT *FoundTombstone = 0;
271 const KeyT EmptyKey = getEmptyKey();
272 const KeyT TombstoneKey = getTombstoneKey();
273 assert(!KeyInfoT::isEqual(Val, EmptyKey) &&
274 !KeyInfoT::isEqual(Val, TombstoneKey) &&
275 "Empty/Tombstone value shouldn't be inserted into map!");
278 BucketT *ThisBucket = BucketsPtr + (BucketNo & (NumBuckets-1));
279 // Found Val's bucket? If so, return it.
280 if (KeyInfoT::isEqual(ThisBucket->first, Val)) {
281 FoundBucket = ThisBucket;
285 // If we found an empty bucket, the key doesn't exist in the set.
286 // Insert it and return the default value.
287 if (KeyInfoT::isEqual(ThisBucket->first, EmptyKey)) {
288 // If we've already seen a tombstone while probing, fill it in instead
289 // of the empty bucket we eventually probed to.
290 if (FoundTombstone) ThisBucket = FoundTombstone;
291 FoundBucket = FoundTombstone ? FoundTombstone : ThisBucket;
295 // If this is a tombstone, remember it. If Val ends up not in the map, we
296 // prefer to return it than something that would require more probing.
297 if (KeyInfoT::isEqual(ThisBucket->first, TombstoneKey) && !FoundTombstone)
298 FoundTombstone = ThisBucket; // Remember the first tombstone found.
300 // Otherwise, it's a hash collision or a tombstone, continue quadratic
302 BucketNo += ProbeAmt++;
306 void init(unsigned InitBuckets) {
309 NumBuckets = InitBuckets;
310 assert(InitBuckets && (InitBuckets & InitBuckets-1) == 0 &&
311 "# initial buckets must be a power of two!");
312 Buckets = reinterpret_cast<BucketT*>(new char[sizeof(BucketT)*InitBuckets]);
313 // Initialize all the keys to EmptyKey.
314 const KeyT EmptyKey = getEmptyKey();
315 for (unsigned i = 0; i != InitBuckets; ++i)
316 new (&Buckets[i].first) KeyT(EmptyKey);
319 void grow(unsigned AtLeast) {
320 unsigned OldNumBuckets = NumBuckets;
321 BucketT *OldBuckets = Buckets;
323 // Double the number of buckets.
324 while (NumBuckets <= AtLeast)
327 Buckets = reinterpret_cast<BucketT*>(new char[sizeof(BucketT)*NumBuckets]);
329 // Initialize all the keys to EmptyKey.
330 const KeyT EmptyKey = getEmptyKey();
331 for (unsigned i = 0, e = NumBuckets; i != e; ++i)
332 new (&Buckets[i].first) KeyT(EmptyKey);
334 // Insert all the old elements.
335 const KeyT TombstoneKey = getTombstoneKey();
336 for (BucketT *B = OldBuckets, *E = OldBuckets+OldNumBuckets; B != E; ++B) {
337 if (!KeyInfoT::isEqual(B->first, EmptyKey) &&
338 !KeyInfoT::isEqual(B->first, TombstoneKey)) {
339 // Insert the key/value into the new table.
341 bool FoundVal = LookupBucketFor(B->first, DestBucket);
342 FoundVal = FoundVal; // silence warning.
343 assert(!FoundVal && "Key already in new map?");
344 DestBucket->first = B->first;
345 new (&DestBucket->second) ValueT(B->second);
353 // Free the old table.
354 delete[] reinterpret_cast<char*>(OldBuckets);
357 void shrink_and_clear() {
358 unsigned OldNumBuckets = NumBuckets;
359 BucketT *OldBuckets = Buckets;
361 // Reduce the number of buckets.
362 NumBuckets = NumEntries > 32 ? 1 << (Log2_32_Ceil(NumEntries) + 1)
365 Buckets = reinterpret_cast<BucketT*>(new char[sizeof(BucketT)*NumBuckets]);
367 // Initialize all the keys to EmptyKey.
368 const KeyT EmptyKey = getEmptyKey();
369 for (unsigned i = 0, e = NumBuckets; i != e; ++i)
370 new (&Buckets[i].first) KeyT(EmptyKey);
372 // Free the old buckets.
373 const KeyT TombstoneKey = getTombstoneKey();
374 for (BucketT *B = OldBuckets, *E = OldBuckets+OldNumBuckets; B != E; ++B) {
375 if (!KeyInfoT::isEqual(B->first, EmptyKey) &&
376 !KeyInfoT::isEqual(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();
429 (KeyInfoT::isEqual(Ptr->first, Empty) ||
430 KeyInfoT::isEqual(Ptr->first, Tombstone)))
435 template<typename KeyT, typename ValueT, typename KeyInfoT, typename ValueInfoT>
436 class DenseMapConstIterator : public DenseMapIterator<KeyT, ValueT, KeyInfoT> {
438 DenseMapConstIterator(const std::pair<KeyT, ValueT> *Pos,
439 const std::pair<KeyT, ValueT> *E)
440 : DenseMapIterator<KeyT, ValueT, KeyInfoT>(Pos, E) {
442 const std::pair<KeyT, ValueT> &operator*() const {
445 const std::pair<KeyT, ValueT> *operator->() const {
450 } // end namespace llvm