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/ADT/DenseMapInfo.h"
18 #include "llvm/ADT/EpochTracker.h"
19 #include "llvm/Support/AlignOf.h"
20 #include "llvm/Support/Compiler.h"
21 #include "llvm/Support/MathExtras.h"
22 #include "llvm/Support/type_traits.h"
29 #include <type_traits>
36 // We extend a pair to allow users to override the bucket type with their own
37 // implementation without requiring two members.
38 template <typename KeyT, typename ValueT>
39 struct DenseMapPair : public std::pair<KeyT, ValueT> {
40 KeyT &getFirst() { return std::pair<KeyT, ValueT>::first; }
41 const KeyT &getFirst() const { return std::pair<KeyT, ValueT>::first; }
42 ValueT &getSecond() { return std::pair<KeyT, ValueT>::second; }
43 const ValueT &getSecond() const { return std::pair<KeyT, ValueT>::second; }
46 } // end namespace detail
49 typename KeyT, typename ValueT, typename KeyInfoT = DenseMapInfo<KeyT>,
50 typename Bucket = detail::DenseMapPair<KeyT, ValueT>, bool IsConst = false>
51 class DenseMapIterator;
53 template <typename DerivedT, typename KeyT, typename ValueT, typename KeyInfoT,
55 class DenseMapBase : public DebugEpochBase {
57 using const_arg_type_t = typename const_pointer_or_const_ref<T>::type;
60 using size_type = unsigned;
61 using key_type = KeyT;
62 using mapped_type = ValueT;
63 using value_type = BucketT;
65 using iterator = DenseMapIterator<KeyT, ValueT, KeyInfoT, BucketT>;
66 using const_iterator =
67 DenseMapIterator<KeyT, ValueT, KeyInfoT, BucketT, true>;
69 inline iterator begin() {
70 // When the map is empty, avoid the overhead of AdvancePastEmptyBuckets().
71 return empty() ? end() : iterator(getBuckets(), getBucketsEnd(), *this);
73 inline iterator end() {
74 return iterator(getBucketsEnd(), getBucketsEnd(), *this, true);
76 inline const_iterator begin() const {
77 return empty() ? end()
78 : const_iterator(getBuckets(), getBucketsEnd(), *this);
80 inline const_iterator end() const {
81 return const_iterator(getBucketsEnd(), getBucketsEnd(), *this, true);
84 LLVM_NODISCARD bool empty() const {
85 return getNumEntries() == 0;
87 unsigned size() const { return getNumEntries(); }
89 /// Grow the densemap so that it can contain at least \p NumEntries items
90 /// before resizing again.
91 void reserve(size_type NumEntries) {
92 auto NumBuckets = getMinBucketToReserveForEntries(NumEntries);
94 if (NumBuckets > getNumBuckets())
100 if (getNumEntries() == 0 && getNumTombstones() == 0) return;
102 // If the capacity of the array is huge, and the # elements used is small,
104 if (getNumEntries() * 4 < getNumBuckets() && getNumBuckets() > 64) {
109 const KeyT EmptyKey = getEmptyKey(), TombstoneKey = getTombstoneKey();
110 unsigned NumEntries = getNumEntries();
111 for (BucketT *P = getBuckets(), *E = getBucketsEnd(); P != E; ++P) {
112 if (!KeyInfoT::isEqual(P->getFirst(), EmptyKey)) {
113 if (!KeyInfoT::isEqual(P->getFirst(), TombstoneKey)) {
114 P->getSecond().~ValueT();
117 P->getFirst() = EmptyKey;
120 assert(NumEntries == 0 && "Node count imbalance!");
125 /// Return 1 if the specified key is in the map, 0 otherwise.
126 size_type count(const_arg_type_t<KeyT> Val) const {
127 const BucketT *TheBucket;
128 return LookupBucketFor(Val, TheBucket) ? 1 : 0;
131 iterator find(const_arg_type_t<KeyT> Val) {
133 if (LookupBucketFor(Val, TheBucket))
134 return iterator(TheBucket, getBucketsEnd(), *this, true);
137 const_iterator find(const_arg_type_t<KeyT> Val) const {
138 const BucketT *TheBucket;
139 if (LookupBucketFor(Val, TheBucket))
140 return const_iterator(TheBucket, getBucketsEnd(), *this, true);
144 /// Alternate version of find() which allows a different, and possibly
145 /// less expensive, key type.
146 /// The DenseMapInfo is responsible for supplying methods
147 /// getHashValue(LookupKeyT) and isEqual(LookupKeyT, KeyT) for each key
149 template<class LookupKeyT>
150 iterator find_as(const LookupKeyT &Val) {
152 if (LookupBucketFor(Val, TheBucket))
153 return iterator(TheBucket, getBucketsEnd(), *this, true);
156 template<class LookupKeyT>
157 const_iterator find_as(const LookupKeyT &Val) const {
158 const BucketT *TheBucket;
159 if (LookupBucketFor(Val, TheBucket))
160 return const_iterator(TheBucket, getBucketsEnd(), *this, true);
164 /// lookup - Return the entry for the specified key, or a default
165 /// constructed value if no such entry exists.
166 ValueT lookup(const_arg_type_t<KeyT> Val) const {
167 const BucketT *TheBucket;
168 if (LookupBucketFor(Val, TheBucket))
169 return TheBucket->getSecond();
173 // Inserts key,value pair into the map if the key isn't already in the map.
174 // If the key is already in the map, it returns false and doesn't update the
176 std::pair<iterator, bool> insert(const std::pair<KeyT, ValueT> &KV) {
177 return try_emplace(KV.first, KV.second);
180 // Inserts key,value pair into the map if the key isn't already in the map.
181 // If the key is already in the map, it returns false and doesn't update the
183 std::pair<iterator, bool> insert(std::pair<KeyT, ValueT> &&KV) {
184 return try_emplace(std::move(KV.first), std::move(KV.second));
187 // Inserts key,value pair into the map if the key isn't already in the map.
188 // The value is constructed in-place if the key is not in the map, otherwise
190 template <typename... Ts>
191 std::pair<iterator, bool> try_emplace(KeyT &&Key, Ts &&... Args) {
193 if (LookupBucketFor(Key, TheBucket))
194 return std::make_pair(iterator(TheBucket, getBucketsEnd(), *this, true),
195 false); // Already in map.
197 // Otherwise, insert the new element.
199 InsertIntoBucket(TheBucket, std::move(Key), std::forward<Ts>(Args)...);
200 return std::make_pair(iterator(TheBucket, getBucketsEnd(), *this, true),
204 // Inserts key,value pair into the map if the key isn't already in the map.
205 // The value is constructed in-place if the key is not in the map, otherwise
207 template <typename... Ts>
208 std::pair<iterator, bool> try_emplace(const KeyT &Key, Ts &&... Args) {
210 if (LookupBucketFor(Key, TheBucket))
211 return std::make_pair(iterator(TheBucket, getBucketsEnd(), *this, true),
212 false); // Already in map.
214 // Otherwise, insert the new element.
215 TheBucket = InsertIntoBucket(TheBucket, Key, std::forward<Ts>(Args)...);
216 return std::make_pair(iterator(TheBucket, getBucketsEnd(), *this, true),
220 /// Alternate version of insert() which allows a different, and possibly
221 /// less expensive, key type.
222 /// The DenseMapInfo is responsible for supplying methods
223 /// getHashValue(LookupKeyT) and isEqual(LookupKeyT, KeyT) for each key
225 template <typename LookupKeyT>
226 std::pair<iterator, bool> insert_as(std::pair<KeyT, ValueT> &&KV,
227 const LookupKeyT &Val) {
229 if (LookupBucketFor(Val, TheBucket))
230 return std::make_pair(iterator(TheBucket, getBucketsEnd(), *this, true),
231 false); // Already in map.
233 // Otherwise, insert the new element.
234 TheBucket = InsertIntoBucketWithLookup(TheBucket, std::move(KV.first),
235 std::move(KV.second), Val);
236 return std::make_pair(iterator(TheBucket, getBucketsEnd(), *this, true),
240 /// insert - Range insertion of pairs.
241 template<typename InputIt>
242 void insert(InputIt I, InputIt E) {
247 bool erase(const KeyT &Val) {
249 if (!LookupBucketFor(Val, TheBucket))
250 return false; // not in map.
252 TheBucket->getSecond().~ValueT();
253 TheBucket->getFirst() = getTombstoneKey();
254 decrementNumEntries();
255 incrementNumTombstones();
258 void erase(iterator I) {
259 BucketT *TheBucket = &*I;
260 TheBucket->getSecond().~ValueT();
261 TheBucket->getFirst() = getTombstoneKey();
262 decrementNumEntries();
263 incrementNumTombstones();
266 value_type& FindAndConstruct(const KeyT &Key) {
268 if (LookupBucketFor(Key, TheBucket))
271 return *InsertIntoBucket(TheBucket, Key);
274 ValueT &operator[](const KeyT &Key) {
275 return FindAndConstruct(Key).second;
278 value_type& FindAndConstruct(KeyT &&Key) {
280 if (LookupBucketFor(Key, TheBucket))
283 return *InsertIntoBucket(TheBucket, std::move(Key));
286 ValueT &operator[](KeyT &&Key) {
287 return FindAndConstruct(std::move(Key)).second;
290 /// isPointerIntoBucketsArray - Return true if the specified pointer points
291 /// somewhere into the DenseMap's array of buckets (i.e. either to a key or
292 /// value in the DenseMap).
293 bool isPointerIntoBucketsArray(const void *Ptr) const {
294 return Ptr >= getBuckets() && Ptr < getBucketsEnd();
297 /// getPointerIntoBucketsArray() - Return an opaque pointer into the buckets
298 /// array. In conjunction with the previous method, this can be used to
299 /// determine whether an insertion caused the DenseMap to reallocate.
300 const void *getPointerIntoBucketsArray() const { return getBuckets(); }
303 DenseMapBase() = default;
306 if (getNumBuckets() == 0) // Nothing to do.
309 const KeyT EmptyKey = getEmptyKey(), TombstoneKey = getTombstoneKey();
310 for (BucketT *P = getBuckets(), *E = getBucketsEnd(); P != E; ++P) {
311 if (!KeyInfoT::isEqual(P->getFirst(), EmptyKey) &&
312 !KeyInfoT::isEqual(P->getFirst(), TombstoneKey))
313 P->getSecond().~ValueT();
314 P->getFirst().~KeyT();
322 assert((getNumBuckets() & (getNumBuckets()-1)) == 0 &&
323 "# initial buckets must be a power of two!");
324 const KeyT EmptyKey = getEmptyKey();
325 for (BucketT *B = getBuckets(), *E = getBucketsEnd(); B != E; ++B)
326 ::new (&B->getFirst()) KeyT(EmptyKey);
329 /// Returns the number of buckets to allocate to ensure that the DenseMap can
330 /// accommodate \p NumEntries without need to grow().
331 unsigned getMinBucketToReserveForEntries(unsigned NumEntries) {
332 // Ensure that "NumEntries * 4 < NumBuckets * 3"
335 // +1 is required because of the strict equality.
336 // For example if NumEntries is 48, we need to return 401.
337 return NextPowerOf2(NumEntries * 4 / 3 + 1);
340 void moveFromOldBuckets(BucketT *OldBucketsBegin, BucketT *OldBucketsEnd) {
343 // Insert all the old elements.
344 const KeyT EmptyKey = getEmptyKey();
345 const KeyT TombstoneKey = getTombstoneKey();
346 for (BucketT *B = OldBucketsBegin, *E = OldBucketsEnd; B != E; ++B) {
347 if (!KeyInfoT::isEqual(B->getFirst(), EmptyKey) &&
348 !KeyInfoT::isEqual(B->getFirst(), TombstoneKey)) {
349 // Insert the key/value into the new table.
351 bool FoundVal = LookupBucketFor(B->getFirst(), DestBucket);
352 (void)FoundVal; // silence warning.
353 assert(!FoundVal && "Key already in new map?");
354 DestBucket->getFirst() = std::move(B->getFirst());
355 ::new (&DestBucket->getSecond()) ValueT(std::move(B->getSecond()));
356 incrementNumEntries();
359 B->getSecond().~ValueT();
361 B->getFirst().~KeyT();
365 template <typename OtherBaseT>
367 const DenseMapBase<OtherBaseT, KeyT, ValueT, KeyInfoT, BucketT> &other) {
368 assert(&other != this);
369 assert(getNumBuckets() == other.getNumBuckets());
371 setNumEntries(other.getNumEntries());
372 setNumTombstones(other.getNumTombstones());
374 if (isPodLike<KeyT>::value && isPodLike<ValueT>::value)
375 memcpy(getBuckets(), other.getBuckets(),
376 getNumBuckets() * sizeof(BucketT));
378 for (size_t i = 0; i < getNumBuckets(); ++i) {
379 ::new (&getBuckets()[i].getFirst())
380 KeyT(other.getBuckets()[i].getFirst());
381 if (!KeyInfoT::isEqual(getBuckets()[i].getFirst(), getEmptyKey()) &&
382 !KeyInfoT::isEqual(getBuckets()[i].getFirst(), getTombstoneKey()))
383 ::new (&getBuckets()[i].getSecond())
384 ValueT(other.getBuckets()[i].getSecond());
388 static unsigned getHashValue(const KeyT &Val) {
389 return KeyInfoT::getHashValue(Val);
392 template<typename LookupKeyT>
393 static unsigned getHashValue(const LookupKeyT &Val) {
394 return KeyInfoT::getHashValue(Val);
397 static const KeyT getEmptyKey() {
398 static_assert(std::is_base_of<DenseMapBase, DerivedT>::value,
399 "Must pass the derived type to this template!");
400 return KeyInfoT::getEmptyKey();
403 static const KeyT getTombstoneKey() {
404 return KeyInfoT::getTombstoneKey();
408 unsigned getNumEntries() const {
409 return static_cast<const DerivedT *>(this)->getNumEntries();
412 void setNumEntries(unsigned Num) {
413 static_cast<DerivedT *>(this)->setNumEntries(Num);
416 void incrementNumEntries() {
417 setNumEntries(getNumEntries() + 1);
420 void decrementNumEntries() {
421 setNumEntries(getNumEntries() - 1);
424 unsigned getNumTombstones() const {
425 return static_cast<const DerivedT *>(this)->getNumTombstones();
428 void setNumTombstones(unsigned Num) {
429 static_cast<DerivedT *>(this)->setNumTombstones(Num);
432 void incrementNumTombstones() {
433 setNumTombstones(getNumTombstones() + 1);
436 void decrementNumTombstones() {
437 setNumTombstones(getNumTombstones() - 1);
440 const BucketT *getBuckets() const {
441 return static_cast<const DerivedT *>(this)->getBuckets();
444 BucketT *getBuckets() {
445 return static_cast<DerivedT *>(this)->getBuckets();
448 unsigned getNumBuckets() const {
449 return static_cast<const DerivedT *>(this)->getNumBuckets();
452 BucketT *getBucketsEnd() {
453 return getBuckets() + getNumBuckets();
456 const BucketT *getBucketsEnd() const {
457 return getBuckets() + getNumBuckets();
460 void grow(unsigned AtLeast) {
461 static_cast<DerivedT *>(this)->grow(AtLeast);
464 void shrink_and_clear() {
465 static_cast<DerivedT *>(this)->shrink_and_clear();
468 template <typename KeyArg, typename... ValueArgs>
469 BucketT *InsertIntoBucket(BucketT *TheBucket, KeyArg &&Key,
470 ValueArgs &&... Values) {
471 TheBucket = InsertIntoBucketImpl(Key, Key, TheBucket);
473 TheBucket->getFirst() = std::forward<KeyArg>(Key);
474 ::new (&TheBucket->getSecond()) ValueT(std::forward<ValueArgs>(Values)...);
478 template <typename LookupKeyT>
479 BucketT *InsertIntoBucketWithLookup(BucketT *TheBucket, KeyT &&Key,
480 ValueT &&Value, LookupKeyT &Lookup) {
481 TheBucket = InsertIntoBucketImpl(Key, Lookup, TheBucket);
483 TheBucket->getFirst() = std::move(Key);
484 ::new (&TheBucket->getSecond()) ValueT(std::move(Value));
488 template <typename LookupKeyT>
489 BucketT *InsertIntoBucketImpl(const KeyT &Key, const LookupKeyT &Lookup,
490 BucketT *TheBucket) {
493 // If the load of the hash table is more than 3/4, or if fewer than 1/8 of
494 // the buckets are empty (meaning that many are filled with tombstones),
497 // The later case is tricky. For example, if we had one empty bucket with
498 // tons of tombstones, failing lookups (e.g. for insertion) would have to
499 // probe almost the entire table until it found the empty bucket. If the
500 // table completely filled with tombstones, no lookup would ever succeed,
501 // causing infinite loops in lookup.
502 unsigned NewNumEntries = getNumEntries() + 1;
503 unsigned NumBuckets = getNumBuckets();
504 if (LLVM_UNLIKELY(NewNumEntries * 4 >= NumBuckets * 3)) {
505 this->grow(NumBuckets * 2);
506 LookupBucketFor(Lookup, TheBucket);
507 NumBuckets = getNumBuckets();
508 } else if (LLVM_UNLIKELY(NumBuckets-(NewNumEntries+getNumTombstones()) <=
510 this->grow(NumBuckets);
511 LookupBucketFor(Lookup, TheBucket);
515 // Only update the state after we've grown our bucket space appropriately
516 // so that when growing buckets we have self-consistent entry count.
517 incrementNumEntries();
519 // If we are writing over a tombstone, remember this.
520 const KeyT EmptyKey = getEmptyKey();
521 if (!KeyInfoT::isEqual(TheBucket->getFirst(), EmptyKey))
522 decrementNumTombstones();
527 /// LookupBucketFor - Lookup the appropriate bucket for Val, returning it in
528 /// FoundBucket. If the bucket contains the key and a value, this returns
529 /// true, otherwise it returns a bucket with an empty marker or tombstone and
531 template<typename LookupKeyT>
532 bool LookupBucketFor(const LookupKeyT &Val,
533 const BucketT *&FoundBucket) const {
534 const BucketT *BucketsPtr = getBuckets();
535 const unsigned NumBuckets = getNumBuckets();
537 if (NumBuckets == 0) {
538 FoundBucket = nullptr;
542 // FoundTombstone - Keep track of whether we find a tombstone while probing.
543 const BucketT *FoundTombstone = nullptr;
544 const KeyT EmptyKey = getEmptyKey();
545 const KeyT TombstoneKey = getTombstoneKey();
546 assert(!KeyInfoT::isEqual(Val, EmptyKey) &&
547 !KeyInfoT::isEqual(Val, TombstoneKey) &&
548 "Empty/Tombstone value shouldn't be inserted into map!");
550 unsigned BucketNo = getHashValue(Val) & (NumBuckets-1);
551 unsigned ProbeAmt = 1;
553 const BucketT *ThisBucket = BucketsPtr + BucketNo;
554 // Found Val's bucket? If so, return it.
555 if (LLVM_LIKELY(KeyInfoT::isEqual(Val, ThisBucket->getFirst()))) {
556 FoundBucket = ThisBucket;
560 // If we found an empty bucket, the key doesn't exist in the set.
561 // Insert it and return the default value.
562 if (LLVM_LIKELY(KeyInfoT::isEqual(ThisBucket->getFirst(), EmptyKey))) {
563 // If we've already seen a tombstone while probing, fill it in instead
564 // of the empty bucket we eventually probed to.
565 FoundBucket = FoundTombstone ? FoundTombstone : ThisBucket;
569 // If this is a tombstone, remember it. If Val ends up not in the map, we
570 // prefer to return it than something that would require more probing.
571 if (KeyInfoT::isEqual(ThisBucket->getFirst(), TombstoneKey) &&
573 FoundTombstone = ThisBucket; // Remember the first tombstone found.
575 // Otherwise, it's a hash collision or a tombstone, continue quadratic
577 BucketNo += ProbeAmt++;
578 BucketNo &= (NumBuckets-1);
582 template <typename LookupKeyT>
583 bool LookupBucketFor(const LookupKeyT &Val, BucketT *&FoundBucket) {
584 const BucketT *ConstFoundBucket;
585 bool Result = const_cast<const DenseMapBase *>(this)
586 ->LookupBucketFor(Val, ConstFoundBucket);
587 FoundBucket = const_cast<BucketT *>(ConstFoundBucket);
592 /// Return the approximate size (in bytes) of the actual map.
593 /// This is just the raw memory used by DenseMap.
594 /// If entries are pointers to objects, the size of the referenced objects
595 /// are not included.
596 size_t getMemorySize() const {
597 return getNumBuckets() * sizeof(BucketT);
601 template <typename KeyT, typename ValueT,
602 typename KeyInfoT = DenseMapInfo<KeyT>,
603 typename BucketT = detail::DenseMapPair<KeyT, ValueT>>
604 class DenseMap : public DenseMapBase<DenseMap<KeyT, ValueT, KeyInfoT, BucketT>,
605 KeyT, ValueT, KeyInfoT, BucketT> {
606 friend class DenseMapBase<DenseMap, KeyT, ValueT, KeyInfoT, BucketT>;
608 // Lift some types from the dependent base class into this class for
609 // simplicity of referring to them.
610 using BaseT = DenseMapBase<DenseMap, KeyT, ValueT, KeyInfoT, BucketT>;
614 unsigned NumTombstones;
618 /// Create a DenseMap wth an optional \p InitialReserve that guarantee that
619 /// this number of elements can be inserted in the map without grow()
620 explicit DenseMap(unsigned InitialReserve = 0) { init(InitialReserve); }
622 DenseMap(const DenseMap &other) : BaseT() {
627 DenseMap(DenseMap &&other) : BaseT() {
632 template<typename InputIt>
633 DenseMap(const InputIt &I, const InputIt &E) {
634 init(std::distance(I, E));
640 operator delete(Buckets);
643 void swap(DenseMap& RHS) {
644 this->incrementEpoch();
645 RHS.incrementEpoch();
646 std::swap(Buckets, RHS.Buckets);
647 std::swap(NumEntries, RHS.NumEntries);
648 std::swap(NumTombstones, RHS.NumTombstones);
649 std::swap(NumBuckets, RHS.NumBuckets);
652 DenseMap& operator=(const DenseMap& other) {
658 DenseMap& operator=(DenseMap &&other) {
660 operator delete(Buckets);
666 void copyFrom(const DenseMap& other) {
668 operator delete(Buckets);
669 if (allocateBuckets(other.NumBuckets)) {
670 this->BaseT::copyFrom(other);
677 void init(unsigned InitNumEntries) {
678 auto InitBuckets = BaseT::getMinBucketToReserveForEntries(InitNumEntries);
679 if (allocateBuckets(InitBuckets)) {
680 this->BaseT::initEmpty();
687 void grow(unsigned AtLeast) {
688 unsigned OldNumBuckets = NumBuckets;
689 BucketT *OldBuckets = Buckets;
691 allocateBuckets(std::max<unsigned>(64, static_cast<unsigned>(NextPowerOf2(AtLeast-1))));
694 this->BaseT::initEmpty();
698 this->moveFromOldBuckets(OldBuckets, OldBuckets+OldNumBuckets);
700 // Free the old table.
701 operator delete(OldBuckets);
704 void shrink_and_clear() {
705 unsigned OldNumEntries = NumEntries;
708 // Reduce the number of buckets.
709 unsigned NewNumBuckets = 0;
711 NewNumBuckets = std::max(64, 1 << (Log2_32_Ceil(OldNumEntries) + 1));
712 if (NewNumBuckets == NumBuckets) {
713 this->BaseT::initEmpty();
717 operator delete(Buckets);
722 unsigned getNumEntries() const {
726 void setNumEntries(unsigned Num) {
730 unsigned getNumTombstones() const {
731 return NumTombstones;
734 void setNumTombstones(unsigned Num) {
738 BucketT *getBuckets() const {
742 unsigned getNumBuckets() const {
746 bool allocateBuckets(unsigned Num) {
748 if (NumBuckets == 0) {
753 Buckets = static_cast<BucketT*>(operator new(sizeof(BucketT) * NumBuckets));
758 template <typename KeyT, typename ValueT, unsigned InlineBuckets = 4,
759 typename KeyInfoT = DenseMapInfo<KeyT>,
760 typename BucketT = detail::DenseMapPair<KeyT, ValueT>>
762 : public DenseMapBase<
763 SmallDenseMap<KeyT, ValueT, InlineBuckets, KeyInfoT, BucketT>, KeyT,
764 ValueT, KeyInfoT, BucketT> {
765 friend class DenseMapBase<SmallDenseMap, KeyT, ValueT, KeyInfoT, BucketT>;
767 // Lift some types from the dependent base class into this class for
768 // simplicity of referring to them.
769 using BaseT = DenseMapBase<SmallDenseMap, KeyT, ValueT, KeyInfoT, BucketT>;
771 static_assert(isPowerOf2_64(InlineBuckets),
772 "InlineBuckets must be a power of 2.");
775 unsigned NumEntries : 31;
776 unsigned NumTombstones;
783 /// A "union" of an inline bucket array and the struct representing
784 /// a large bucket. This union will be discriminated by the 'Small' bit.
785 AlignedCharArrayUnion<BucketT[InlineBuckets], LargeRep> storage;
788 explicit SmallDenseMap(unsigned NumInitBuckets = 0) {
789 init(NumInitBuckets);
792 SmallDenseMap(const SmallDenseMap &other) : BaseT() {
797 SmallDenseMap(SmallDenseMap &&other) : BaseT() {
802 template<typename InputIt>
803 SmallDenseMap(const InputIt &I, const InputIt &E) {
804 init(NextPowerOf2(std::distance(I, E)));
813 void swap(SmallDenseMap& RHS) {
814 unsigned TmpNumEntries = RHS.NumEntries;
815 RHS.NumEntries = NumEntries;
816 NumEntries = TmpNumEntries;
817 std::swap(NumTombstones, RHS.NumTombstones);
819 const KeyT EmptyKey = this->getEmptyKey();
820 const KeyT TombstoneKey = this->getTombstoneKey();
821 if (Small && RHS.Small) {
822 // If we're swapping inline bucket arrays, we have to cope with some of
823 // the tricky bits of DenseMap's storage system: the buckets are not
824 // fully initialized. Thus we swap every key, but we may have
825 // a one-directional move of the value.
826 for (unsigned i = 0, e = InlineBuckets; i != e; ++i) {
827 BucketT *LHSB = &getInlineBuckets()[i],
828 *RHSB = &RHS.getInlineBuckets()[i];
829 bool hasLHSValue = (!KeyInfoT::isEqual(LHSB->getFirst(), EmptyKey) &&
830 !KeyInfoT::isEqual(LHSB->getFirst(), TombstoneKey));
831 bool hasRHSValue = (!KeyInfoT::isEqual(RHSB->getFirst(), EmptyKey) &&
832 !KeyInfoT::isEqual(RHSB->getFirst(), TombstoneKey));
833 if (hasLHSValue && hasRHSValue) {
834 // Swap together if we can...
835 std::swap(*LHSB, *RHSB);
838 // Swap separately and handle any assymetry.
839 std::swap(LHSB->getFirst(), RHSB->getFirst());
841 ::new (&RHSB->getSecond()) ValueT(std::move(LHSB->getSecond()));
842 LHSB->getSecond().~ValueT();
843 } else if (hasRHSValue) {
844 ::new (&LHSB->getSecond()) ValueT(std::move(RHSB->getSecond()));
845 RHSB->getSecond().~ValueT();
850 if (!Small && !RHS.Small) {
851 std::swap(getLargeRep()->Buckets, RHS.getLargeRep()->Buckets);
852 std::swap(getLargeRep()->NumBuckets, RHS.getLargeRep()->NumBuckets);
856 SmallDenseMap &SmallSide = Small ? *this : RHS;
857 SmallDenseMap &LargeSide = Small ? RHS : *this;
859 // First stash the large side's rep and move the small side across.
860 LargeRep TmpRep = std::move(*LargeSide.getLargeRep());
861 LargeSide.getLargeRep()->~LargeRep();
862 LargeSide.Small = true;
863 // This is similar to the standard move-from-old-buckets, but the bucket
864 // count hasn't actually rotated in this case. So we have to carefully
865 // move construct the keys and values into their new locations, but there
866 // is no need to re-hash things.
867 for (unsigned i = 0, e = InlineBuckets; i != e; ++i) {
868 BucketT *NewB = &LargeSide.getInlineBuckets()[i],
869 *OldB = &SmallSide.getInlineBuckets()[i];
870 ::new (&NewB->getFirst()) KeyT(std::move(OldB->getFirst()));
871 OldB->getFirst().~KeyT();
872 if (!KeyInfoT::isEqual(NewB->getFirst(), EmptyKey) &&
873 !KeyInfoT::isEqual(NewB->getFirst(), TombstoneKey)) {
874 ::new (&NewB->getSecond()) ValueT(std::move(OldB->getSecond()));
875 OldB->getSecond().~ValueT();
879 // The hard part of moving the small buckets across is done, just move
880 // the TmpRep into its new home.
881 SmallSide.Small = false;
882 new (SmallSide.getLargeRep()) LargeRep(std::move(TmpRep));
885 SmallDenseMap& operator=(const SmallDenseMap& other) {
891 SmallDenseMap& operator=(SmallDenseMap &&other) {
899 void copyFrom(const SmallDenseMap& other) {
903 if (other.getNumBuckets() > InlineBuckets) {
905 new (getLargeRep()) LargeRep(allocateBuckets(other.getNumBuckets()));
907 this->BaseT::copyFrom(other);
910 void init(unsigned InitBuckets) {
912 if (InitBuckets > InlineBuckets) {
914 new (getLargeRep()) LargeRep(allocateBuckets(InitBuckets));
916 this->BaseT::initEmpty();
919 void grow(unsigned AtLeast) {
920 if (AtLeast >= InlineBuckets)
921 AtLeast = std::max<unsigned>(64, NextPowerOf2(AtLeast-1));
924 if (AtLeast < InlineBuckets)
925 return; // Nothing to do.
927 // First move the inline buckets into a temporary storage.
928 AlignedCharArrayUnion<BucketT[InlineBuckets]> TmpStorage;
929 BucketT *TmpBegin = reinterpret_cast<BucketT *>(TmpStorage.buffer);
930 BucketT *TmpEnd = TmpBegin;
932 // Loop over the buckets, moving non-empty, non-tombstones into the
933 // temporary storage. Have the loop move the TmpEnd forward as it goes.
934 const KeyT EmptyKey = this->getEmptyKey();
935 const KeyT TombstoneKey = this->getTombstoneKey();
936 for (BucketT *P = getBuckets(), *E = P + InlineBuckets; P != E; ++P) {
937 if (!KeyInfoT::isEqual(P->getFirst(), EmptyKey) &&
938 !KeyInfoT::isEqual(P->getFirst(), TombstoneKey)) {
939 assert(size_t(TmpEnd - TmpBegin) < InlineBuckets &&
940 "Too many inline buckets!");
941 ::new (&TmpEnd->getFirst()) KeyT(std::move(P->getFirst()));
942 ::new (&TmpEnd->getSecond()) ValueT(std::move(P->getSecond()));
944 P->getSecond().~ValueT();
946 P->getFirst().~KeyT();
949 // Now make this map use the large rep, and move all the entries back
952 new (getLargeRep()) LargeRep(allocateBuckets(AtLeast));
953 this->moveFromOldBuckets(TmpBegin, TmpEnd);
957 LargeRep OldRep = std::move(*getLargeRep());
958 getLargeRep()->~LargeRep();
959 if (AtLeast <= InlineBuckets) {
962 new (getLargeRep()) LargeRep(allocateBuckets(AtLeast));
965 this->moveFromOldBuckets(OldRep.Buckets, OldRep.Buckets+OldRep.NumBuckets);
967 // Free the old table.
968 operator delete(OldRep.Buckets);
971 void shrink_and_clear() {
972 unsigned OldSize = this->size();
975 // Reduce the number of buckets.
976 unsigned NewNumBuckets = 0;
978 NewNumBuckets = 1 << (Log2_32_Ceil(OldSize) + 1);
979 if (NewNumBuckets > InlineBuckets && NewNumBuckets < 64u)
982 if ((Small && NewNumBuckets <= InlineBuckets) ||
983 (!Small && NewNumBuckets == getLargeRep()->NumBuckets)) {
984 this->BaseT::initEmpty();
993 unsigned getNumEntries() const {
997 void setNumEntries(unsigned Num) {
998 // NumEntries is hardcoded to be 31 bits wide.
999 assert(Num < (1U << 31) && "Cannot support more than 1<<31 entries");
1003 unsigned getNumTombstones() const {
1004 return NumTombstones;
1007 void setNumTombstones(unsigned Num) {
1008 NumTombstones = Num;
1011 const BucketT *getInlineBuckets() const {
1013 // Note that this cast does not violate aliasing rules as we assert that
1014 // the memory's dynamic type is the small, inline bucket buffer, and the
1015 // 'storage.buffer' static type is 'char *'.
1016 return reinterpret_cast<const BucketT *>(storage.buffer);
1019 BucketT *getInlineBuckets() {
1020 return const_cast<BucketT *>(
1021 const_cast<const SmallDenseMap *>(this)->getInlineBuckets());
1024 const LargeRep *getLargeRep() const {
1026 // Note, same rule about aliasing as with getInlineBuckets.
1027 return reinterpret_cast<const LargeRep *>(storage.buffer);
1030 LargeRep *getLargeRep() {
1031 return const_cast<LargeRep *>(
1032 const_cast<const SmallDenseMap *>(this)->getLargeRep());
1035 const BucketT *getBuckets() const {
1036 return Small ? getInlineBuckets() : getLargeRep()->Buckets;
1039 BucketT *getBuckets() {
1040 return const_cast<BucketT *>(
1041 const_cast<const SmallDenseMap *>(this)->getBuckets());
1044 unsigned getNumBuckets() const {
1045 return Small ? InlineBuckets : getLargeRep()->NumBuckets;
1048 void deallocateBuckets() {
1052 operator delete(getLargeRep()->Buckets);
1053 getLargeRep()->~LargeRep();
1056 LargeRep allocateBuckets(unsigned Num) {
1057 assert(Num > InlineBuckets && "Must allocate more buckets than are inline");
1059 static_cast<BucketT*>(operator new(sizeof(BucketT) * Num)), Num
1065 template <typename KeyT, typename ValueT, typename KeyInfoT, typename Bucket,
1067 class DenseMapIterator : DebugEpochBase::HandleBase {
1068 friend class DenseMapIterator<KeyT, ValueT, KeyInfoT, Bucket, true>;
1069 friend class DenseMapIterator<KeyT, ValueT, KeyInfoT, Bucket, false>;
1071 using ConstIterator = DenseMapIterator<KeyT, ValueT, KeyInfoT, Bucket, true>;
1074 using difference_type = ptrdiff_t;
1076 typename std::conditional<IsConst, const Bucket, Bucket>::type;
1077 using pointer = value_type *;
1078 using reference = value_type &;
1079 using iterator_category = std::forward_iterator_tag;
1082 pointer Ptr = nullptr;
1083 pointer End = nullptr;
1086 DenseMapIterator() = default;
1088 DenseMapIterator(pointer Pos, pointer E, const DebugEpochBase &Epoch,
1089 bool NoAdvance = false)
1090 : DebugEpochBase::HandleBase(&Epoch), Ptr(Pos), End(E) {
1091 assert(isHandleInSync() && "invalid construction!");
1092 if (!NoAdvance) AdvancePastEmptyBuckets();
1095 // Converting ctor from non-const iterators to const iterators. SFINAE'd out
1096 // for const iterator destinations so it doesn't end up as a user defined copy
1098 template <bool IsConstSrc,
1099 typename = typename std::enable_if<!IsConstSrc && IsConst>::type>
1101 const DenseMapIterator<KeyT, ValueT, KeyInfoT, Bucket, IsConstSrc> &I)
1102 : DebugEpochBase::HandleBase(I), Ptr(I.Ptr), End(I.End) {}
1104 reference operator*() const {
1105 assert(isHandleInSync() && "invalid iterator access!");
1108 pointer operator->() const {
1109 assert(isHandleInSync() && "invalid iterator access!");
1113 bool operator==(const ConstIterator &RHS) const {
1114 assert((!Ptr || isHandleInSync()) && "handle not in sync!");
1115 assert((!RHS.Ptr || RHS.isHandleInSync()) && "handle not in sync!");
1116 assert(getEpochAddress() == RHS.getEpochAddress() &&
1117 "comparing incomparable iterators!");
1118 return Ptr == RHS.Ptr;
1120 bool operator!=(const ConstIterator &RHS) const {
1121 assert((!Ptr || isHandleInSync()) && "handle not in sync!");
1122 assert((!RHS.Ptr || RHS.isHandleInSync()) && "handle not in sync!");
1123 assert(getEpochAddress() == RHS.getEpochAddress() &&
1124 "comparing incomparable iterators!");
1125 return Ptr != RHS.Ptr;
1128 inline DenseMapIterator& operator++() { // Preincrement
1129 assert(isHandleInSync() && "invalid iterator access!");
1131 AdvancePastEmptyBuckets();
1134 DenseMapIterator operator++(int) { // Postincrement
1135 assert(isHandleInSync() && "invalid iterator access!");
1136 DenseMapIterator tmp = *this; ++*this; return tmp;
1140 void AdvancePastEmptyBuckets() {
1141 const KeyT Empty = KeyInfoT::getEmptyKey();
1142 const KeyT Tombstone = KeyInfoT::getTombstoneKey();
1144 while (Ptr != End && (KeyInfoT::isEqual(Ptr->getFirst(), Empty) ||
1145 KeyInfoT::isEqual(Ptr->getFirst(), Tombstone)))
1150 template<typename KeyT, typename ValueT, typename KeyInfoT>
1151 static inline size_t
1152 capacity_in_bytes(const DenseMap<KeyT, ValueT, KeyInfoT> &X) {
1153 return X.getMemorySize();
1156 } // end namespace llvm
1158 #endif // LLVM_ADT_DENSEMAP_H