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/MathExtras.h"
18 #include "llvm/Support/PointerLikeTypeTraits.h"
19 #include "llvm/Support/type_traits.h"
20 #include "llvm/ADT/DenseMapInfo.h"
31 template<typename KeyT, typename ValueT,
32 typename KeyInfoT = DenseMapInfo<KeyT>,
33 typename ValueInfoT = DenseMapInfo<ValueT>, bool IsConst = false>
34 class DenseMapIterator;
36 template<typename KeyT, typename ValueT,
37 typename KeyInfoT = DenseMapInfo<KeyT>,
38 typename ValueInfoT = DenseMapInfo<ValueT> >
40 typedef std::pair<KeyT, ValueT> BucketT;
45 unsigned NumTombstones;
47 typedef KeyT key_type;
48 typedef ValueT mapped_type;
49 typedef BucketT value_type;
51 DenseMap(const DenseMap &other) {
56 explicit DenseMap(unsigned NumInitBuckets = 64) {
60 template<typename InputIt>
61 DenseMap(const InputIt &I, const InputIt &E) {
67 const KeyT EmptyKey = getEmptyKey(), TombstoneKey = getTombstoneKey();
68 for (BucketT *P = Buckets, *E = Buckets+NumBuckets; P != E; ++P) {
69 if (!KeyInfoT::isEqual(P->first, EmptyKey) &&
70 !KeyInfoT::isEqual(P->first, TombstoneKey))
75 memset(Buckets, 0x5a, sizeof(BucketT)*NumBuckets);
77 operator delete(Buckets);
80 typedef DenseMapIterator<KeyT, ValueT, KeyInfoT> iterator;
81 typedef DenseMapIterator<KeyT, ValueT,
82 KeyInfoT, ValueInfoT, true> const_iterator;
83 inline iterator begin() {
84 // When the map is empty, avoid the overhead of AdvancePastEmptyBuckets().
85 return empty() ? end() : iterator(Buckets, Buckets+NumBuckets);
87 inline iterator end() {
88 return iterator(Buckets+NumBuckets, Buckets+NumBuckets);
90 inline const_iterator begin() const {
91 return empty() ? end() : const_iterator(Buckets, Buckets+NumBuckets);
93 inline const_iterator end() const {
94 return const_iterator(Buckets+NumBuckets, Buckets+NumBuckets);
97 bool empty() const { return NumEntries == 0; }
98 unsigned size() const { return NumEntries; }
100 /// Grow the densemap so that it has at least Size buckets. Does not shrink
101 void resize(size_t Size) { grow(Size); }
104 if (NumEntries == 0 && NumTombstones == 0) return;
106 // If the capacity of the array is huge, and the # elements used is small,
108 if (NumEntries * 4 < NumBuckets && NumBuckets > 64) {
113 const KeyT EmptyKey = getEmptyKey(), TombstoneKey = getTombstoneKey();
114 for (BucketT *P = Buckets, *E = Buckets+NumBuckets; P != E; ++P) {
115 if (!KeyInfoT::isEqual(P->first, EmptyKey)) {
116 if (!KeyInfoT::isEqual(P->first, TombstoneKey)) {
123 assert(NumEntries == 0 && "Node count imbalance!");
127 /// count - Return true if the specified key is in the map.
128 bool count(const KeyT &Val) const {
130 return LookupBucketFor(Val, TheBucket);
133 iterator find(const KeyT &Val) {
135 if (LookupBucketFor(Val, TheBucket))
136 return iterator(TheBucket, Buckets+NumBuckets);
139 const_iterator find(const KeyT &Val) const {
141 if (LookupBucketFor(Val, TheBucket))
142 return const_iterator(TheBucket, Buckets+NumBuckets);
146 /// lookup - Return the entry for the specified key, or a default
147 /// constructed value if no such entry exists.
148 ValueT lookup(const KeyT &Val) const {
150 if (LookupBucketFor(Val, TheBucket))
151 return TheBucket->second;
155 // Inserts key,value pair into the map if the key isn't already in the map.
156 // If the key is already in the map, it returns false and doesn't update the
158 std::pair<iterator, bool> insert(const std::pair<KeyT, ValueT> &KV) {
160 if (LookupBucketFor(KV.first, TheBucket))
161 return std::make_pair(iterator(TheBucket, Buckets+NumBuckets),
162 false); // Already in map.
164 // Otherwise, insert the new element.
165 TheBucket = InsertIntoBucket(KV.first, KV.second, TheBucket);
166 return std::make_pair(iterator(TheBucket, Buckets+NumBuckets),
170 /// insert - Range insertion of pairs.
171 template<typename InputIt>
172 void insert(InputIt I, InputIt E) {
178 bool erase(const KeyT &Val) {
180 if (!LookupBucketFor(Val, TheBucket))
181 return false; // not in map.
183 TheBucket->second.~ValueT();
184 TheBucket->first = getTombstoneKey();
189 void erase(iterator I) {
190 BucketT *TheBucket = &*I;
191 TheBucket->second.~ValueT();
192 TheBucket->first = getTombstoneKey();
197 void swap(DenseMap& RHS) {
198 std::swap(NumBuckets, RHS.NumBuckets);
199 std::swap(Buckets, RHS.Buckets);
200 std::swap(NumEntries, RHS.NumEntries);
201 std::swap(NumTombstones, RHS.NumTombstones);
204 value_type& FindAndConstruct(const KeyT &Key) {
206 if (LookupBucketFor(Key, TheBucket))
209 return *InsertIntoBucket(Key, ValueT(), TheBucket);
212 ValueT &operator[](const KeyT &Key) {
213 return FindAndConstruct(Key).second;
216 DenseMap& operator=(const DenseMap& other) {
221 /// isPointerIntoBucketsArray - Return true if the specified pointer points
222 /// somewhere into the DenseMap's array of buckets (i.e. either to a key or
223 /// value in the DenseMap).
224 bool isPointerIntoBucketsArray(const void *Ptr) const {
225 return Ptr >= Buckets && Ptr < Buckets+NumBuckets;
228 /// getPointerIntoBucketsArray() - Return an opaque pointer into the buckets
229 /// array. In conjunction with the previous method, this can be used to
230 /// determine whether an insertion caused the DenseMap to reallocate.
231 const void *getPointerIntoBucketsArray() const { return Buckets; }
234 void CopyFrom(const DenseMap& other) {
235 if (NumBuckets != 0 &&
236 (!isPodLike<KeyInfoT>::value || !isPodLike<ValueInfoT>::value)) {
237 const KeyT EmptyKey = getEmptyKey(), TombstoneKey = getTombstoneKey();
238 for (BucketT *P = Buckets, *E = Buckets+NumBuckets; P != E; ++P) {
239 if (!KeyInfoT::isEqual(P->first, EmptyKey) &&
240 !KeyInfoT::isEqual(P->first, TombstoneKey))
246 NumEntries = other.NumEntries;
247 NumTombstones = other.NumTombstones;
251 memset(Buckets, 0x5a, sizeof(BucketT)*NumBuckets);
253 operator delete(Buckets);
255 Buckets = static_cast<BucketT*>(operator new(sizeof(BucketT) *
258 if (isPodLike<KeyInfoT>::value && isPodLike<ValueInfoT>::value)
259 memcpy(Buckets, other.Buckets, other.NumBuckets * sizeof(BucketT));
261 for (size_t i = 0; i < other.NumBuckets; ++i) {
262 new (&Buckets[i].first) KeyT(other.Buckets[i].first);
263 if (!KeyInfoT::isEqual(Buckets[i].first, getEmptyKey()) &&
264 !KeyInfoT::isEqual(Buckets[i].first, getTombstoneKey()))
265 new (&Buckets[i].second) ValueT(other.Buckets[i].second);
267 NumBuckets = other.NumBuckets;
270 BucketT *InsertIntoBucket(const KeyT &Key, const ValueT &Value,
271 BucketT *TheBucket) {
272 // If the load of the hash table is more than 3/4, or if fewer than 1/8 of
273 // the buckets are empty (meaning that many are filled with tombstones),
276 // The later case is tricky. For example, if we had one empty bucket with
277 // tons of tombstones, failing lookups (e.g. for insertion) would have to
278 // probe almost the entire table until it found the empty bucket. If the
279 // table completely filled with tombstones, no lookup would ever succeed,
280 // causing infinite loops in lookup.
282 if (NumEntries*4 >= NumBuckets*3 ||
283 NumBuckets-(NumEntries+NumTombstones) < NumBuckets/8) {
284 this->grow(NumBuckets * 2);
285 LookupBucketFor(Key, TheBucket);
288 // If we are writing over a tombstone, remember this.
289 if (!KeyInfoT::isEqual(TheBucket->first, getEmptyKey()))
292 TheBucket->first = Key;
293 new (&TheBucket->second) ValueT(Value);
297 static unsigned getHashValue(const KeyT &Val) {
298 return KeyInfoT::getHashValue(Val);
300 static const KeyT getEmptyKey() {
301 return KeyInfoT::getEmptyKey();
303 static const KeyT getTombstoneKey() {
304 return KeyInfoT::getTombstoneKey();
307 /// LookupBucketFor - Lookup the appropriate bucket for Val, returning it in
308 /// FoundBucket. If the bucket contains the key and a value, this returns
309 /// true, otherwise it returns a bucket with an empty marker or tombstone and
311 bool LookupBucketFor(const KeyT &Val, BucketT *&FoundBucket) const {
312 unsigned BucketNo = getHashValue(Val);
313 unsigned ProbeAmt = 1;
314 BucketT *BucketsPtr = Buckets;
316 // FoundTombstone - Keep track of whether we find a tombstone while probing.
317 BucketT *FoundTombstone = 0;
318 const KeyT EmptyKey = getEmptyKey();
319 const KeyT TombstoneKey = getTombstoneKey();
320 assert(!KeyInfoT::isEqual(Val, EmptyKey) &&
321 !KeyInfoT::isEqual(Val, TombstoneKey) &&
322 "Empty/Tombstone value shouldn't be inserted into map!");
325 BucketT *ThisBucket = BucketsPtr + (BucketNo & (NumBuckets-1));
326 // Found Val's bucket? If so, return it.
327 if (KeyInfoT::isEqual(ThisBucket->first, Val)) {
328 FoundBucket = ThisBucket;
332 // If we found an empty bucket, the key doesn't exist in the set.
333 // Insert it and return the default value.
334 if (KeyInfoT::isEqual(ThisBucket->first, EmptyKey)) {
335 // If we've already seen a tombstone while probing, fill it in instead
336 // of the empty bucket we eventually probed to.
337 if (FoundTombstone) ThisBucket = FoundTombstone;
338 FoundBucket = FoundTombstone ? FoundTombstone : ThisBucket;
342 // If this is a tombstone, remember it. If Val ends up not in the map, we
343 // prefer to return it than something that would require more probing.
344 if (KeyInfoT::isEqual(ThisBucket->first, TombstoneKey) && !FoundTombstone)
345 FoundTombstone = ThisBucket; // Remember the first tombstone found.
347 // Otherwise, it's a hash collision or a tombstone, continue quadratic
349 BucketNo += ProbeAmt++;
353 void init(unsigned InitBuckets) {
356 NumBuckets = InitBuckets;
357 assert(InitBuckets && (InitBuckets & (InitBuckets-1)) == 0 &&
358 "# initial buckets must be a power of two!");
359 Buckets = static_cast<BucketT*>(operator new(sizeof(BucketT)*InitBuckets));
360 // Initialize all the keys to EmptyKey.
361 const KeyT EmptyKey = getEmptyKey();
362 for (unsigned i = 0; i != InitBuckets; ++i)
363 new (&Buckets[i].first) KeyT(EmptyKey);
366 void grow(unsigned AtLeast) {
367 unsigned OldNumBuckets = NumBuckets;
368 BucketT *OldBuckets = Buckets;
370 // Double the number of buckets.
371 while (NumBuckets < AtLeast)
374 Buckets = static_cast<BucketT*>(operator new(sizeof(BucketT)*NumBuckets));
376 // Initialize all the keys to EmptyKey.
377 const KeyT EmptyKey = getEmptyKey();
378 for (unsigned i = 0, e = NumBuckets; i != e; ++i)
379 new (&Buckets[i].first) KeyT(EmptyKey);
381 // Insert all the old elements.
382 const KeyT TombstoneKey = getTombstoneKey();
383 for (BucketT *B = OldBuckets, *E = OldBuckets+OldNumBuckets; B != E; ++B) {
384 if (!KeyInfoT::isEqual(B->first, EmptyKey) &&
385 !KeyInfoT::isEqual(B->first, TombstoneKey)) {
386 // Insert the key/value into the new table.
388 bool FoundVal = LookupBucketFor(B->first, DestBucket);
389 (void)FoundVal; // silence warning.
390 assert(!FoundVal && "Key already in new map?");
391 DestBucket->first = B->first;
392 new (&DestBucket->second) ValueT(B->second);
401 memset(OldBuckets, 0x5a, sizeof(BucketT)*OldNumBuckets);
403 // Free the old table.
404 operator delete(OldBuckets);
407 void shrink_and_clear() {
408 unsigned OldNumBuckets = NumBuckets;
409 BucketT *OldBuckets = Buckets;
411 // Reduce the number of buckets.
412 NumBuckets = NumEntries > 32 ? 1 << (Log2_32_Ceil(NumEntries) + 1)
415 Buckets = static_cast<BucketT*>(operator new(sizeof(BucketT)*NumBuckets));
417 // Initialize all the keys to EmptyKey.
418 const KeyT EmptyKey = getEmptyKey();
419 for (unsigned i = 0, e = NumBuckets; i != e; ++i)
420 new (&Buckets[i].first) KeyT(EmptyKey);
422 // Free the old buckets.
423 const KeyT TombstoneKey = getTombstoneKey();
424 for (BucketT *B = OldBuckets, *E = OldBuckets+OldNumBuckets; B != E; ++B) {
425 if (!KeyInfoT::isEqual(B->first, EmptyKey) &&
426 !KeyInfoT::isEqual(B->first, TombstoneKey)) {
434 memset(OldBuckets, 0x5a, sizeof(BucketT)*OldNumBuckets);
436 // Free the old table.
437 operator delete(OldBuckets);
443 template<typename KeyT, typename ValueT,
444 typename KeyInfoT, typename ValueInfoT, bool IsConst>
445 class DenseMapIterator {
446 typedef std::pair<KeyT, ValueT> Bucket;
447 typedef DenseMapIterator<KeyT, ValueT,
448 KeyInfoT, ValueInfoT, true> ConstIterator;
449 friend class DenseMapIterator<KeyT, ValueT, KeyInfoT, ValueInfoT, true>;
451 typedef ptrdiff_t difference_type;
452 typedef typename conditional<IsConst, const Bucket, Bucket>::type value_type;
453 typedef value_type *pointer;
454 typedef value_type &reference;
455 typedef std::forward_iterator_tag iterator_category;
459 DenseMapIterator() : Ptr(0), End(0) {}
461 DenseMapIterator(pointer Pos, pointer E) : Ptr(Pos), End(E) {
462 AdvancePastEmptyBuckets();
465 // If IsConst is true this is a converting constructor from iterator to
466 // const_iterator and the default copy constructor is used.
467 // Otherwise this is a copy constructor for iterator.
468 DenseMapIterator(const DenseMapIterator<KeyT, ValueT,
469 KeyInfoT, ValueInfoT, false>& I)
470 : Ptr(I.Ptr), End(I.End) {}
472 reference operator*() const {
475 pointer operator->() const {
479 bool operator==(const ConstIterator &RHS) const {
480 return Ptr == RHS.operator->();
482 bool operator!=(const ConstIterator &RHS) const {
483 return Ptr != RHS.operator->();
486 inline DenseMapIterator& operator++() { // Preincrement
488 AdvancePastEmptyBuckets();
491 DenseMapIterator operator++(int) { // Postincrement
492 DenseMapIterator tmp = *this; ++*this; return tmp;
496 void AdvancePastEmptyBuckets() {
497 const KeyT Empty = KeyInfoT::getEmptyKey();
498 const KeyT Tombstone = KeyInfoT::getTombstoneKey();
501 (KeyInfoT::isEqual(Ptr->first, Empty) ||
502 KeyInfoT::isEqual(Ptr->first, Tombstone)))
507 } // end namespace llvm