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 = 0) {
60 template<typename InputIt>
61 DenseMap(const InputIt &I, const InputIt &E) {
62 init(NextPowerOf2(std::distance(I, 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))
76 memset((void*)Buckets, 0x5a, sizeof(BucketT)*NumBuckets);
78 operator delete(Buckets);
81 typedef DenseMapIterator<KeyT, ValueT, KeyInfoT> iterator;
82 typedef DenseMapIterator<KeyT, ValueT,
83 KeyInfoT, ValueInfoT, true> const_iterator;
84 inline iterator begin() {
85 // When the map is empty, avoid the overhead of AdvancePastEmptyBuckets().
86 return empty() ? end() : iterator(Buckets, Buckets+NumBuckets);
88 inline iterator end() {
89 return iterator(Buckets+NumBuckets, Buckets+NumBuckets, true);
91 inline const_iterator begin() const {
92 return empty() ? end() : const_iterator(Buckets, Buckets+NumBuckets);
94 inline const_iterator end() const {
95 return const_iterator(Buckets+NumBuckets, Buckets+NumBuckets, true);
98 bool empty() const { return NumEntries == 0; }
99 unsigned size() const { return NumEntries; }
101 /// Grow the densemap so that it has at least Size buckets. Does not shrink
102 void resize(size_t Size) {
103 if (Size > NumBuckets)
108 if (NumEntries == 0 && NumTombstones == 0) return;
110 // If the capacity of the array is huge, and the # elements used is small,
112 if (NumEntries * 4 < NumBuckets && NumBuckets > 64) {
117 const KeyT EmptyKey = getEmptyKey(), TombstoneKey = getTombstoneKey();
118 for (BucketT *P = Buckets, *E = Buckets+NumBuckets; P != E; ++P) {
119 if (!KeyInfoT::isEqual(P->first, EmptyKey)) {
120 if (!KeyInfoT::isEqual(P->first, TombstoneKey)) {
127 assert(NumEntries == 0 && "Node count imbalance!");
131 /// count - Return true if the specified key is in the map.
132 bool count(const KeyT &Val) const {
134 return LookupBucketFor(Val, TheBucket);
137 iterator find(const KeyT &Val) {
139 if (LookupBucketFor(Val, TheBucket))
140 return iterator(TheBucket, Buckets+NumBuckets, true);
143 const_iterator find(const KeyT &Val) const {
145 if (LookupBucketFor(Val, TheBucket))
146 return const_iterator(TheBucket, Buckets+NumBuckets, true);
150 /// Alternate version of find() which allows a different, and possibly
151 /// less expensive, key type.
152 /// The DenseMapInfo is responsible for supplying methods
153 /// getHashValue(LookupKeyT) and isEqual(LookupKeyT, KeyT) for each key
155 template<class LookupKeyT>
156 iterator find_as(const LookupKeyT &Val) {
158 if (LookupBucketFor(Val, TheBucket))
159 return iterator(TheBucket, Buckets+NumBuckets, true);
162 template<class LookupKeyT>
163 const_iterator find_as(const LookupKeyT &Val) const {
165 if (LookupBucketFor(Val, TheBucket))
166 return const_iterator(TheBucket, Buckets+NumBuckets, true);
170 /// lookup - Return the entry for the specified key, or a default
171 /// constructed value if no such entry exists.
172 ValueT lookup(const KeyT &Val) const {
174 if (LookupBucketFor(Val, TheBucket))
175 return TheBucket->second;
179 // Inserts key,value pair into the map if the key isn't already in the map.
180 // If the key is already in the map, it returns false and doesn't update the
182 std::pair<iterator, bool> insert(const std::pair<KeyT, ValueT> &KV) {
184 if (LookupBucketFor(KV.first, TheBucket))
185 return std::make_pair(iterator(TheBucket, Buckets+NumBuckets, true),
186 false); // Already in map.
188 // Otherwise, insert the new element.
189 TheBucket = InsertIntoBucket(KV.first, KV.second, TheBucket);
190 return std::make_pair(iterator(TheBucket, Buckets+NumBuckets, true), true);
193 /// insert - Range insertion of pairs.
194 template<typename InputIt>
195 void insert(InputIt I, InputIt E) {
201 bool erase(const KeyT &Val) {
203 if (!LookupBucketFor(Val, TheBucket))
204 return false; // not in map.
206 TheBucket->second.~ValueT();
207 TheBucket->first = getTombstoneKey();
212 void erase(iterator I) {
213 BucketT *TheBucket = &*I;
214 TheBucket->second.~ValueT();
215 TheBucket->first = getTombstoneKey();
220 void swap(DenseMap& RHS) {
221 std::swap(NumBuckets, RHS.NumBuckets);
222 std::swap(Buckets, RHS.Buckets);
223 std::swap(NumEntries, RHS.NumEntries);
224 std::swap(NumTombstones, RHS.NumTombstones);
227 value_type& FindAndConstruct(const KeyT &Key) {
229 if (LookupBucketFor(Key, TheBucket))
232 return *InsertIntoBucket(Key, ValueT(), TheBucket);
235 ValueT &operator[](const KeyT &Key) {
236 return FindAndConstruct(Key).second;
239 DenseMap& operator=(const DenseMap& other) {
244 /// isPointerIntoBucketsArray - Return true if the specified pointer points
245 /// somewhere into the DenseMap's array of buckets (i.e. either to a key or
246 /// value in the DenseMap).
247 bool isPointerIntoBucketsArray(const void *Ptr) const {
248 return Ptr >= Buckets && Ptr < Buckets+NumBuckets;
251 /// getPointerIntoBucketsArray() - Return an opaque pointer into the buckets
252 /// array. In conjunction with the previous method, this can be used to
253 /// determine whether an insertion caused the DenseMap to reallocate.
254 const void *getPointerIntoBucketsArray() const { return Buckets; }
257 void CopyFrom(const DenseMap& other) {
258 if (NumBuckets != 0 &&
259 (!isPodLike<KeyInfoT>::value || !isPodLike<ValueInfoT>::value)) {
260 const KeyT EmptyKey = getEmptyKey(), TombstoneKey = getTombstoneKey();
261 for (BucketT *P = Buckets, *E = Buckets+NumBuckets; P != E; ++P) {
262 if (!KeyInfoT::isEqual(P->first, EmptyKey) &&
263 !KeyInfoT::isEqual(P->first, TombstoneKey))
269 NumEntries = other.NumEntries;
270 NumTombstones = other.NumTombstones;
274 memset((void*)Buckets, 0x5a, sizeof(BucketT)*NumBuckets);
276 operator delete(Buckets);
279 NumBuckets = other.NumBuckets;
281 if (NumBuckets == 0) {
286 Buckets = static_cast<BucketT*>(operator new(sizeof(BucketT) * NumBuckets));
288 if (isPodLike<KeyInfoT>::value && isPodLike<ValueInfoT>::value)
289 memcpy(Buckets, other.Buckets, NumBuckets * sizeof(BucketT));
291 for (size_t i = 0; i < NumBuckets; ++i) {
292 new (&Buckets[i].first) KeyT(other.Buckets[i].first);
293 if (!KeyInfoT::isEqual(Buckets[i].first, getEmptyKey()) &&
294 !KeyInfoT::isEqual(Buckets[i].first, getTombstoneKey()))
295 new (&Buckets[i].second) ValueT(other.Buckets[i].second);
299 BucketT *InsertIntoBucket(const KeyT &Key, const ValueT &Value,
300 BucketT *TheBucket) {
301 // If the load of the hash table is more than 3/4, or if fewer than 1/8 of
302 // the buckets are empty (meaning that many are filled with tombstones),
305 // The later case is tricky. For example, if we had one empty bucket with
306 // tons of tombstones, failing lookups (e.g. for insertion) would have to
307 // probe almost the entire table until it found the empty bucket. If the
308 // table completely filled with tombstones, no lookup would ever succeed,
309 // causing infinite loops in lookup.
311 if (NumEntries*4 >= NumBuckets*3) {
312 this->grow(NumBuckets * 2);
313 LookupBucketFor(Key, TheBucket);
315 if (NumBuckets-(NumEntries+NumTombstones) < NumBuckets/8) {
316 this->grow(NumBuckets);
317 LookupBucketFor(Key, TheBucket);
320 // If we are writing over a tombstone, remember this.
321 if (!KeyInfoT::isEqual(TheBucket->first, getEmptyKey()))
324 TheBucket->first = Key;
325 new (&TheBucket->second) ValueT(Value);
329 static unsigned getHashValue(const KeyT &Val) {
330 return KeyInfoT::getHashValue(Val);
332 template<typename LookupKeyT>
333 static unsigned getHashValue(const LookupKeyT &Val) {
334 return KeyInfoT::getHashValue(Val);
336 static const KeyT getEmptyKey() {
337 return KeyInfoT::getEmptyKey();
339 static const KeyT getTombstoneKey() {
340 return KeyInfoT::getTombstoneKey();
343 /// LookupBucketFor - Lookup the appropriate bucket for Val, returning it in
344 /// FoundBucket. If the bucket contains the key and a value, this returns
345 /// true, otherwise it returns a bucket with an empty marker or tombstone and
347 template<typename LookupKeyT>
348 bool LookupBucketFor(const LookupKeyT &Val, BucketT *&FoundBucket) const {
349 unsigned BucketNo = getHashValue(Val);
350 unsigned ProbeAmt = 1;
351 BucketT *BucketsPtr = Buckets;
353 if (NumBuckets == 0) {
358 // FoundTombstone - Keep track of whether we find a tombstone while probing.
359 BucketT *FoundTombstone = 0;
360 const KeyT EmptyKey = getEmptyKey();
361 const KeyT TombstoneKey = getTombstoneKey();
362 assert(!KeyInfoT::isEqual(Val, EmptyKey) &&
363 !KeyInfoT::isEqual(Val, TombstoneKey) &&
364 "Empty/Tombstone value shouldn't be inserted into map!");
367 BucketT *ThisBucket = BucketsPtr + (BucketNo & (NumBuckets-1));
368 // Found Val's bucket? If so, return it.
369 if (KeyInfoT::isEqual(Val, ThisBucket->first)) {
370 FoundBucket = ThisBucket;
374 // If we found an empty bucket, the key doesn't exist in the set.
375 // Insert it and return the default value.
376 if (KeyInfoT::isEqual(ThisBucket->first, EmptyKey)) {
377 // If we've already seen a tombstone while probing, fill it in instead
378 // of the empty bucket we eventually probed to.
379 if (FoundTombstone) ThisBucket = FoundTombstone;
380 FoundBucket = FoundTombstone ? FoundTombstone : ThisBucket;
384 // If this is a tombstone, remember it. If Val ends up not in the map, we
385 // prefer to return it than something that would require more probing.
386 if (KeyInfoT::isEqual(ThisBucket->first, TombstoneKey) && !FoundTombstone)
387 FoundTombstone = ThisBucket; // Remember the first tombstone found.
389 // Otherwise, it's a hash collision or a tombstone, continue quadratic
391 BucketNo += ProbeAmt++;
395 void init(unsigned InitBuckets) {
398 NumBuckets = InitBuckets;
400 if (InitBuckets == 0) {
405 assert(InitBuckets && (InitBuckets & (InitBuckets-1)) == 0 &&
406 "# initial buckets must be a power of two!");
407 Buckets = static_cast<BucketT*>(operator new(sizeof(BucketT)*InitBuckets));
408 // Initialize all the keys to EmptyKey.
409 const KeyT EmptyKey = getEmptyKey();
410 for (unsigned i = 0; i != InitBuckets; ++i)
411 new (&Buckets[i].first) KeyT(EmptyKey);
414 void grow(unsigned AtLeast) {
415 unsigned OldNumBuckets = NumBuckets;
416 BucketT *OldBuckets = Buckets;
421 // Double the number of buckets.
422 while (NumBuckets < AtLeast)
425 Buckets = static_cast<BucketT*>(operator new(sizeof(BucketT)*NumBuckets));
427 // Initialize all the keys to EmptyKey.
428 const KeyT EmptyKey = getEmptyKey();
429 for (unsigned i = 0, e = NumBuckets; i != e; ++i)
430 new (&Buckets[i].first) KeyT(EmptyKey);
432 // Insert all the old elements.
433 const KeyT TombstoneKey = getTombstoneKey();
434 for (BucketT *B = OldBuckets, *E = OldBuckets+OldNumBuckets; B != E; ++B) {
435 if (!KeyInfoT::isEqual(B->first, EmptyKey) &&
436 !KeyInfoT::isEqual(B->first, TombstoneKey)) {
437 // Insert the key/value into the new table.
439 bool FoundVal = LookupBucketFor(B->first, DestBucket);
440 (void)FoundVal; // silence warning.
441 assert(!FoundVal && "Key already in new map?");
442 DestBucket->first = B->first;
443 new (&DestBucket->second) ValueT(B->second);
453 memset((void*)OldBuckets, 0x5a, sizeof(BucketT)*OldNumBuckets);
455 // Free the old table.
456 operator delete(OldBuckets);
459 void shrink_and_clear() {
460 unsigned OldNumBuckets = NumBuckets;
461 BucketT *OldBuckets = Buckets;
463 // Reduce the number of buckets.
464 NumBuckets = NumEntries > 32 ? 1 << (Log2_32_Ceil(NumEntries) + 1)
467 Buckets = static_cast<BucketT*>(operator new(sizeof(BucketT)*NumBuckets));
469 // Initialize all the keys to EmptyKey.
470 const KeyT EmptyKey = getEmptyKey();
471 for (unsigned i = 0, e = NumBuckets; i != e; ++i)
472 new (&Buckets[i].first) KeyT(EmptyKey);
474 // Free the old buckets.
475 const KeyT TombstoneKey = getTombstoneKey();
476 for (BucketT *B = OldBuckets, *E = OldBuckets+OldNumBuckets; B != E; ++B) {
477 if (!KeyInfoT::isEqual(B->first, EmptyKey) &&
478 !KeyInfoT::isEqual(B->first, TombstoneKey)) {
486 memset((void*)OldBuckets, 0x5a, sizeof(BucketT)*OldNumBuckets);
488 // Free the old table.
489 operator delete(OldBuckets);
495 /// Return the approximate size (in bytes) of the actual map.
496 /// This is just the raw memory used by DenseMap.
497 /// If entries are pointers to objects, the size of the referenced objects
498 /// are not included.
499 size_t getMemorySize() const {
500 return NumBuckets * sizeof(BucketT);
504 template<typename KeyT, typename ValueT,
505 typename KeyInfoT, typename ValueInfoT, bool IsConst>
506 class DenseMapIterator {
507 typedef std::pair<KeyT, ValueT> Bucket;
508 typedef DenseMapIterator<KeyT, ValueT,
509 KeyInfoT, ValueInfoT, true> ConstIterator;
510 friend class DenseMapIterator<KeyT, ValueT, KeyInfoT, ValueInfoT, true>;
512 typedef ptrdiff_t difference_type;
513 typedef typename conditional<IsConst, const Bucket, Bucket>::type value_type;
514 typedef value_type *pointer;
515 typedef value_type &reference;
516 typedef std::forward_iterator_tag iterator_category;
520 DenseMapIterator() : Ptr(0), End(0) {}
522 DenseMapIterator(pointer Pos, pointer E, bool NoAdvance = false)
524 if (!NoAdvance) AdvancePastEmptyBuckets();
527 // If IsConst is true this is a converting constructor from iterator to
528 // const_iterator and the default copy constructor is used.
529 // Otherwise this is a copy constructor for iterator.
530 DenseMapIterator(const DenseMapIterator<KeyT, ValueT,
531 KeyInfoT, ValueInfoT, false>& I)
532 : Ptr(I.Ptr), End(I.End) {}
534 reference operator*() const {
537 pointer operator->() const {
541 bool operator==(const ConstIterator &RHS) const {
542 return Ptr == RHS.operator->();
544 bool operator!=(const ConstIterator &RHS) const {
545 return Ptr != RHS.operator->();
548 inline DenseMapIterator& operator++() { // Preincrement
550 AdvancePastEmptyBuckets();
553 DenseMapIterator operator++(int) { // Postincrement
554 DenseMapIterator tmp = *this; ++*this; return tmp;
558 void AdvancePastEmptyBuckets() {
559 const KeyT Empty = KeyInfoT::getEmptyKey();
560 const KeyT Tombstone = KeyInfoT::getTombstoneKey();
563 (KeyInfoT::isEqual(Ptr->first, Empty) ||
564 KeyInfoT::isEqual(Ptr->first, Tombstone)))
569 template<typename KeyT, typename ValueT, typename KeyInfoT, typename ValueInfoT>
571 capacity_in_bytes(const DenseMap<KeyT, ValueT, KeyInfoT, ValueInfoT> &X) {
572 return X.getMemorySize();
575 } // end namespace llvm