1 //===- llvm/ADT/DenseMap.h - Dense probed hash table ------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // 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/PointerLikeTypeTraits.h"
18 #include "llvm/Support/MathExtras.h"
19 #include "llvm/ADT/DenseMapInfo.h"
28 template<typename KeyT, typename ValueT,
29 typename KeyInfoT = DenseMapInfo<KeyT>,
30 typename ValueInfoT = DenseMapInfo<ValueT> >
31 class DenseMapIterator;
32 template<typename KeyT, typename ValueT,
33 typename KeyInfoT = DenseMapInfo<KeyT>,
34 typename ValueInfoT = DenseMapInfo<ValueT> >
35 class DenseMapConstIterator;
37 template<typename KeyT, typename ValueT,
38 typename KeyInfoT = DenseMapInfo<KeyT>,
39 typename ValueInfoT = DenseMapInfo<ValueT> >
41 typedef std::pair<KeyT, ValueT> BucketT;
46 unsigned NumTombstones;
48 typedef KeyT key_type;
49 typedef ValueT mapped_type;
50 typedef BucketT value_type;
52 DenseMap(const DenseMap& other) {
57 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 (!KeyInfoT::isEqual(P->first, EmptyKey) &&
65 !KeyInfoT::isEqual(P->first, TombstoneKey))
70 memset(Buckets, 0x5a, sizeof(BucketT)*NumBuckets);
72 operator delete(Buckets);
75 typedef DenseMapIterator<KeyT, ValueT, KeyInfoT> iterator;
76 typedef DenseMapConstIterator<KeyT, ValueT, KeyInfoT> const_iterator;
77 inline iterator begin() {
78 return iterator(Buckets, Buckets+NumBuckets);
80 inline iterator end() {
81 return iterator(Buckets+NumBuckets, Buckets+NumBuckets);
83 inline const_iterator begin() const {
84 return const_iterator(Buckets, Buckets+NumBuckets);
86 inline const_iterator end() const {
87 return const_iterator(Buckets+NumBuckets, Buckets+NumBuckets);
90 bool empty() const { return NumEntries == 0; }
91 unsigned size() const { return NumEntries; }
93 /// Grow the densemap so that it has at least Size buckets. Does not shrink
94 void resize(size_t Size) { grow(Size); }
97 if (NumEntries == 0 && NumTombstones == 0) return;
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 (!KeyInfoT::isEqual(P->first, EmptyKey)) {
109 if (!KeyInfoT::isEqual(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 /// lookup - Return the entry for the specified key, or a default
140 /// constructed value if no such entry exists.
141 ValueT lookup(const KeyT &Val) const {
143 if (LookupBucketFor(Val, TheBucket))
144 return TheBucket->second;
148 // Inserts key,value pair into the map if the key isn't already in the map.
149 // If the key is already in the map, it returns false and doesn't update the
151 std::pair<iterator, bool> insert(const std::pair<KeyT, ValueT> &KV) {
153 if (LookupBucketFor(KV.first, TheBucket))
154 return std::make_pair(iterator(TheBucket, Buckets+NumBuckets),
155 false); // Already in map.
157 // Otherwise, insert the new element.
158 TheBucket = InsertIntoBucket(KV.first, KV.second, TheBucket);
159 return std::make_pair(iterator(TheBucket, Buckets+NumBuckets),
163 /// insert - Range insertion of pairs.
164 template<typename InputIt>
165 void insert(InputIt I, InputIt E) {
171 bool erase(const KeyT &Val) {
173 if (!LookupBucketFor(Val, TheBucket))
174 return false; // not in map.
176 TheBucket->second.~ValueT();
177 TheBucket->first = getTombstoneKey();
182 bool erase(iterator I) {
183 BucketT *TheBucket = &*I;
184 TheBucket->second.~ValueT();
185 TheBucket->first = getTombstoneKey();
191 value_type& FindAndConstruct(const KeyT &Key) {
193 if (LookupBucketFor(Key, TheBucket))
196 return *InsertIntoBucket(Key, ValueT(), TheBucket);
199 ValueT &operator[](const KeyT &Key) {
200 return FindAndConstruct(Key).second;
203 DenseMap& operator=(const DenseMap& other) {
208 /// isPointerIntoBucketsArray - Return true if the specified pointer points
209 /// somewhere into the DenseMap's array of buckets (i.e. either to a key or
210 /// value in the DenseMap).
211 bool isPointerIntoBucketsArray(const void *Ptr) const {
212 return Ptr >= Buckets && Ptr < Buckets+NumBuckets;
215 /// getPointerIntoBucketsArray() - Return an opaque pointer into the buckets
216 /// array. In conjunction with the previous method, this can be used to
217 /// determine whether an insertion caused the DenseMap to reallocate.
218 const void *getPointerIntoBucketsArray() const { return Buckets; }
221 void CopyFrom(const DenseMap& other) {
222 if (NumBuckets != 0 && (!KeyInfoT::isPod() || !ValueInfoT::isPod())) {
223 const KeyT EmptyKey = getEmptyKey(), TombstoneKey = getTombstoneKey();
224 for (BucketT *P = Buckets, *E = Buckets+NumBuckets; P != E; ++P) {
225 if (!KeyInfoT::isEqual(P->first, EmptyKey) &&
226 !KeyInfoT::isEqual(P->first, TombstoneKey))
232 NumEntries = other.NumEntries;
233 NumTombstones = other.NumTombstones;
237 memset(Buckets, 0x5a, sizeof(BucketT)*NumBuckets);
239 operator delete(Buckets);
241 Buckets = static_cast<BucketT*>(operator new(sizeof(BucketT) *
244 if (KeyInfoT::isPod() && ValueInfoT::isPod())
245 memcpy(Buckets, other.Buckets, other.NumBuckets * sizeof(BucketT));
247 for (size_t i = 0; i < other.NumBuckets; ++i) {
248 new (&Buckets[i].first) KeyT(other.Buckets[i].first);
249 if (!KeyInfoT::isEqual(Buckets[i].first, getEmptyKey()) &&
250 !KeyInfoT::isEqual(Buckets[i].first, getTombstoneKey()))
251 new (&Buckets[i].second) ValueT(other.Buckets[i].second);
253 NumBuckets = other.NumBuckets;
256 BucketT *InsertIntoBucket(const KeyT &Key, const ValueT &Value,
257 BucketT *TheBucket) {
258 // If the load of the hash table is more than 3/4, or if fewer than 1/8 of
259 // the buckets are empty (meaning that many are filled with tombstones),
262 // The later case is tricky. For example, if we had one empty bucket with
263 // tons of tombstones, failing lookups (e.g. for insertion) would have to
264 // probe almost the entire table until it found the empty bucket. If the
265 // table completely filled with tombstones, no lookup would ever succeed,
266 // causing infinite loops in lookup.
268 if (NumEntries*4 >= NumBuckets*3 ||
269 NumBuckets-(NumEntries+NumTombstones) < NumBuckets/8) {
270 this->grow(NumBuckets * 2);
271 LookupBucketFor(Key, TheBucket);
274 // If we are writing over a tombstone, remember this.
275 if (!KeyInfoT::isEqual(TheBucket->first, getEmptyKey()))
278 TheBucket->first = Key;
279 new (&TheBucket->second) ValueT(Value);
283 static unsigned getHashValue(const KeyT &Val) {
284 return KeyInfoT::getHashValue(Val);
286 static const KeyT getEmptyKey() {
287 return KeyInfoT::getEmptyKey();
289 static const KeyT getTombstoneKey() {
290 return KeyInfoT::getTombstoneKey();
293 /// LookupBucketFor - Lookup the appropriate bucket for Val, returning it in
294 /// FoundBucket. If the bucket contains the key and a value, this returns
295 /// true, otherwise it returns a bucket with an empty marker or tombstone and
297 bool LookupBucketFor(const KeyT &Val, BucketT *&FoundBucket) const {
298 unsigned BucketNo = getHashValue(Val);
299 unsigned ProbeAmt = 1;
300 BucketT *BucketsPtr = Buckets;
302 // FoundTombstone - Keep track of whether we find a tombstone while probing.
303 BucketT *FoundTombstone = 0;
304 const KeyT EmptyKey = getEmptyKey();
305 const KeyT TombstoneKey = getTombstoneKey();
306 assert(!KeyInfoT::isEqual(Val, EmptyKey) &&
307 !KeyInfoT::isEqual(Val, TombstoneKey) &&
308 "Empty/Tombstone value shouldn't be inserted into map!");
311 BucketT *ThisBucket = BucketsPtr + (BucketNo & (NumBuckets-1));
312 // Found Val's bucket? If so, return it.
313 if (KeyInfoT::isEqual(ThisBucket->first, Val)) {
314 FoundBucket = ThisBucket;
318 // If we found an empty bucket, the key doesn't exist in the set.
319 // Insert it and return the default value.
320 if (KeyInfoT::isEqual(ThisBucket->first, EmptyKey)) {
321 // If we've already seen a tombstone while probing, fill it in instead
322 // of the empty bucket we eventually probed to.
323 if (FoundTombstone) ThisBucket = FoundTombstone;
324 FoundBucket = FoundTombstone ? FoundTombstone : ThisBucket;
328 // If this is a tombstone, remember it. If Val ends up not in the map, we
329 // prefer to return it than something that would require more probing.
330 if (KeyInfoT::isEqual(ThisBucket->first, TombstoneKey) && !FoundTombstone)
331 FoundTombstone = ThisBucket; // Remember the first tombstone found.
333 // Otherwise, it's a hash collision or a tombstone, continue quadratic
335 BucketNo += ProbeAmt++;
339 void init(unsigned InitBuckets) {
342 NumBuckets = InitBuckets;
343 assert(InitBuckets && (InitBuckets & (InitBuckets-1)) == 0 &&
344 "# initial buckets must be a power of two!");
345 Buckets = static_cast<BucketT*>(operator new(sizeof(BucketT)*InitBuckets));
346 // Initialize all the keys to EmptyKey.
347 const KeyT EmptyKey = getEmptyKey();
348 for (unsigned i = 0; i != InitBuckets; ++i)
349 new (&Buckets[i].first) KeyT(EmptyKey);
352 void grow(unsigned AtLeast) {
353 unsigned OldNumBuckets = NumBuckets;
354 BucketT *OldBuckets = Buckets;
356 // Double the number of buckets.
357 while (NumBuckets <= AtLeast)
360 Buckets = static_cast<BucketT*>(operator new(sizeof(BucketT)*NumBuckets));
362 // Initialize all the keys to EmptyKey.
363 const KeyT EmptyKey = getEmptyKey();
364 for (unsigned i = 0, e = NumBuckets; i != e; ++i)
365 new (&Buckets[i].first) KeyT(EmptyKey);
367 // Insert all the old elements.
368 const KeyT TombstoneKey = getTombstoneKey();
369 for (BucketT *B = OldBuckets, *E = OldBuckets+OldNumBuckets; B != E; ++B) {
370 if (!KeyInfoT::isEqual(B->first, EmptyKey) &&
371 !KeyInfoT::isEqual(B->first, TombstoneKey)) {
372 // Insert the key/value into the new table.
374 bool FoundVal = LookupBucketFor(B->first, DestBucket);
375 FoundVal = FoundVal; // silence warning.
376 assert(!FoundVal && "Key already in new map?");
377 DestBucket->first = B->first;
378 new (&DestBucket->second) ValueT(B->second);
387 memset(OldBuckets, 0x5a, sizeof(BucketT)*OldNumBuckets);
389 // Free the old table.
390 operator delete(OldBuckets);
393 void shrink_and_clear() {
394 unsigned OldNumBuckets = NumBuckets;
395 BucketT *OldBuckets = Buckets;
397 // Reduce the number of buckets.
398 NumBuckets = NumEntries > 32 ? 1 << (Log2_32_Ceil(NumEntries) + 1)
401 Buckets = static_cast<BucketT*>(operator new(sizeof(BucketT)*NumBuckets));
403 // Initialize all the keys to EmptyKey.
404 const KeyT EmptyKey = getEmptyKey();
405 for (unsigned i = 0, e = NumBuckets; i != e; ++i)
406 new (&Buckets[i].first) KeyT(EmptyKey);
408 // Free the old buckets.
409 const KeyT TombstoneKey = getTombstoneKey();
410 for (BucketT *B = OldBuckets, *E = OldBuckets+OldNumBuckets; B != E; ++B) {
411 if (!KeyInfoT::isEqual(B->first, EmptyKey) &&
412 !KeyInfoT::isEqual(B->first, TombstoneKey)) {
420 memset(OldBuckets, 0x5a, sizeof(BucketT)*OldNumBuckets);
422 // Free the old table.
423 operator delete(OldBuckets);
429 template<typename KeyT, typename ValueT, typename KeyInfoT, typename ValueInfoT>
430 class DenseMapIterator :
431 public std::iterator<std::forward_iterator_tag, std::pair<KeyT, ValueT>,
433 typedef std::pair<KeyT, ValueT> BucketT;
435 const BucketT *Ptr, *End;
437 DenseMapIterator() : Ptr(0), End(0) {}
439 DenseMapIterator(const BucketT *Pos, const BucketT *E) : Ptr(Pos), End(E) {
440 AdvancePastEmptyBuckets();
443 std::pair<KeyT, ValueT> &operator*() const {
444 return *const_cast<BucketT*>(Ptr);
446 std::pair<KeyT, ValueT> *operator->() const {
447 return const_cast<BucketT*>(Ptr);
450 bool operator==(const DenseMapIterator &RHS) const {
451 return Ptr == RHS.Ptr;
453 bool operator!=(const DenseMapIterator &RHS) const {
454 return Ptr != RHS.Ptr;
457 inline DenseMapIterator& operator++() { // Preincrement
459 AdvancePastEmptyBuckets();
462 DenseMapIterator operator++(int) { // Postincrement
463 DenseMapIterator tmp = *this; ++*this; return tmp;
467 void AdvancePastEmptyBuckets() {
468 const KeyT Empty = KeyInfoT::getEmptyKey();
469 const KeyT Tombstone = KeyInfoT::getTombstoneKey();
472 (KeyInfoT::isEqual(Ptr->first, Empty) ||
473 KeyInfoT::isEqual(Ptr->first, Tombstone)))
478 template<typename KeyT, typename ValueT, typename KeyInfoT, typename ValueInfoT>
479 class DenseMapConstIterator : public DenseMapIterator<KeyT, ValueT, KeyInfoT> {
481 DenseMapConstIterator() : DenseMapIterator<KeyT, ValueT, KeyInfoT>() {}
482 DenseMapConstIterator(const std::pair<KeyT, ValueT> *Pos,
483 const std::pair<KeyT, ValueT> *E)
484 : DenseMapIterator<KeyT, ValueT, KeyInfoT>(Pos, E) {
486 const std::pair<KeyT, ValueT> &operator*() const {
489 const std::pair<KeyT, ValueT> *operator->() const {
494 } // end namespace llvm