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"
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 typedef unsigned size_type;
61 typedef KeyT key_type;
62 typedef ValueT mapped_type;
63 typedef BucketT value_type;
65 typedef DenseMapIterator<KeyT, ValueT, KeyInfoT, BucketT> iterator;
66 typedef DenseMapIterator<KeyT, ValueT, KeyInfoT, BucketT, true>
68 inline iterator begin() {
69 // When the map is empty, avoid the overhead of AdvancePastEmptyBuckets().
70 return empty() ? end() : iterator(getBuckets(), getBucketsEnd(), *this);
72 inline iterator end() {
73 return iterator(getBucketsEnd(), getBucketsEnd(), *this, true);
75 inline const_iterator begin() const {
76 return empty() ? end()
77 : const_iterator(getBuckets(), getBucketsEnd(), *this);
79 inline const_iterator end() const {
80 return const_iterator(getBucketsEnd(), getBucketsEnd(), *this, true);
83 LLVM_NODISCARD bool empty() const {
84 return getNumEntries() == 0;
86 unsigned size() const { return getNumEntries(); }
88 /// Grow the densemap so that it can contain at least \p NumEntries items
89 /// before resizing again.
90 void reserve(size_type NumEntries) {
91 auto NumBuckets = getMinBucketToReserveForEntries(NumEntries);
93 if (NumBuckets > getNumBuckets())
99 if (getNumEntries() == 0 && getNumTombstones() == 0) return;
101 // If the capacity of the array is huge, and the # elements used is small,
103 if (getNumEntries() * 4 < getNumBuckets() && getNumBuckets() > 64) {
108 const KeyT EmptyKey = getEmptyKey(), TombstoneKey = getTombstoneKey();
109 unsigned NumEntries = getNumEntries();
110 for (BucketT *P = getBuckets(), *E = getBucketsEnd(); P != E; ++P) {
111 if (!KeyInfoT::isEqual(P->getFirst(), EmptyKey)) {
112 if (!KeyInfoT::isEqual(P->getFirst(), TombstoneKey)) {
113 P->getSecond().~ValueT();
116 P->getFirst() = EmptyKey;
119 assert(NumEntries == 0 && "Node count imbalance!");
124 /// Return 1 if the specified key is in the map, 0 otherwise.
125 size_type count(const_arg_type_t<KeyT> Val) const {
126 const BucketT *TheBucket;
127 return LookupBucketFor(Val, TheBucket) ? 1 : 0;
130 iterator find(const_arg_type_t<KeyT> Val) {
132 if (LookupBucketFor(Val, TheBucket))
133 return iterator(TheBucket, getBucketsEnd(), *this, true);
136 const_iterator find(const_arg_type_t<KeyT> Val) const {
137 const BucketT *TheBucket;
138 if (LookupBucketFor(Val, TheBucket))
139 return const_iterator(TheBucket, getBucketsEnd(), *this, true);
143 /// Alternate version of find() which allows a different, and possibly
144 /// less expensive, key type.
145 /// The DenseMapInfo is responsible for supplying methods
146 /// getHashValue(LookupKeyT) and isEqual(LookupKeyT, KeyT) for each key
148 template<class LookupKeyT>
149 iterator find_as(const LookupKeyT &Val) {
151 if (LookupBucketFor(Val, TheBucket))
152 return iterator(TheBucket, getBucketsEnd(), *this, true);
155 template<class LookupKeyT>
156 const_iterator find_as(const LookupKeyT &Val) const {
157 const BucketT *TheBucket;
158 if (LookupBucketFor(Val, TheBucket))
159 return const_iterator(TheBucket, getBucketsEnd(), *this, true);
163 /// lookup - Return the entry for the specified key, or a default
164 /// constructed value if no such entry exists.
165 ValueT lookup(const_arg_type_t<KeyT> Val) const {
166 const BucketT *TheBucket;
167 if (LookupBucketFor(Val, TheBucket))
168 return TheBucket->getSecond();
172 // Inserts key,value pair into the map if the key isn't already in the map.
173 // If the key is already in the map, it returns false and doesn't update the
175 std::pair<iterator, bool> insert(const std::pair<KeyT, ValueT> &KV) {
176 return try_emplace(KV.first, KV.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(std::pair<KeyT, ValueT> &&KV) {
183 return try_emplace(std::move(KV.first), std::move(KV.second));
186 // Inserts key,value pair into the map if the key isn't already in the map.
187 // The value is constructed in-place if the key is not in the map, otherwise
189 template <typename... Ts>
190 std::pair<iterator, bool> try_emplace(KeyT &&Key, Ts &&... Args) {
192 if (LookupBucketFor(Key, TheBucket))
193 return std::make_pair(iterator(TheBucket, getBucketsEnd(), *this, true),
194 false); // Already in map.
196 // Otherwise, insert the new element.
198 InsertIntoBucket(TheBucket, std::move(Key), std::forward<Ts>(Args)...);
199 return std::make_pair(iterator(TheBucket, getBucketsEnd(), *this, true),
203 // Inserts key,value pair into the map if the key isn't already in the map.
204 // The value is constructed in-place if the key is not in the map, otherwise
206 template <typename... Ts>
207 std::pair<iterator, bool> try_emplace(const KeyT &Key, Ts &&... Args) {
209 if (LookupBucketFor(Key, TheBucket))
210 return std::make_pair(iterator(TheBucket, getBucketsEnd(), *this, true),
211 false); // Already in map.
213 // Otherwise, insert the new element.
214 TheBucket = InsertIntoBucket(TheBucket, Key, std::forward<Ts>(Args)...);
215 return std::make_pair(iterator(TheBucket, getBucketsEnd(), *this, true),
219 /// Alternate version of insert() which allows a different, and possibly
220 /// less expensive, key type.
221 /// The DenseMapInfo is responsible for supplying methods
222 /// getHashValue(LookupKeyT) and isEqual(LookupKeyT, KeyT) for each key
224 template <typename LookupKeyT>
225 std::pair<iterator, bool> insert_as(std::pair<KeyT, ValueT> &&KV,
226 const LookupKeyT &Val) {
228 if (LookupBucketFor(Val, TheBucket))
229 return std::make_pair(iterator(TheBucket, getBucketsEnd(), *this, true),
230 false); // Already in map.
232 // Otherwise, insert the new element.
233 TheBucket = InsertIntoBucketWithLookup(TheBucket, std::move(KV.first),
234 std::move(KV.second), Val);
235 return std::make_pair(iterator(TheBucket, getBucketsEnd(), *this, true),
239 /// insert - Range insertion of pairs.
240 template<typename InputIt>
241 void insert(InputIt I, InputIt E) {
246 bool erase(const KeyT &Val) {
248 if (!LookupBucketFor(Val, TheBucket))
249 return false; // not in map.
251 TheBucket->getSecond().~ValueT();
252 TheBucket->getFirst() = getTombstoneKey();
253 decrementNumEntries();
254 incrementNumTombstones();
257 void erase(iterator I) {
258 BucketT *TheBucket = &*I;
259 TheBucket->getSecond().~ValueT();
260 TheBucket->getFirst() = getTombstoneKey();
261 decrementNumEntries();
262 incrementNumTombstones();
265 value_type& FindAndConstruct(const KeyT &Key) {
267 if (LookupBucketFor(Key, TheBucket))
270 return *InsertIntoBucket(TheBucket, Key);
273 ValueT &operator[](const KeyT &Key) {
274 return FindAndConstruct(Key).second;
277 value_type& FindAndConstruct(KeyT &&Key) {
279 if (LookupBucketFor(Key, TheBucket))
282 return *InsertIntoBucket(TheBucket, std::move(Key));
285 ValueT &operator[](KeyT &&Key) {
286 return FindAndConstruct(std::move(Key)).second;
289 /// isPointerIntoBucketsArray - Return true if the specified pointer points
290 /// somewhere into the DenseMap's array of buckets (i.e. either to a key or
291 /// value in the DenseMap).
292 bool isPointerIntoBucketsArray(const void *Ptr) const {
293 return Ptr >= getBuckets() && Ptr < getBucketsEnd();
296 /// getPointerIntoBucketsArray() - Return an opaque pointer into the buckets
297 /// array. In conjunction with the previous method, this can be used to
298 /// determine whether an insertion caused the DenseMap to reallocate.
299 const void *getPointerIntoBucketsArray() const { return getBuckets(); }
302 DenseMapBase() = default;
305 if (getNumBuckets() == 0) // Nothing to do.
308 const KeyT EmptyKey = getEmptyKey(), TombstoneKey = getTombstoneKey();
309 for (BucketT *P = getBuckets(), *E = getBucketsEnd(); P != E; ++P) {
310 if (!KeyInfoT::isEqual(P->getFirst(), EmptyKey) &&
311 !KeyInfoT::isEqual(P->getFirst(), TombstoneKey))
312 P->getSecond().~ValueT();
313 P->getFirst().~KeyT();
321 assert((getNumBuckets() & (getNumBuckets()-1)) == 0 &&
322 "# initial buckets must be a power of two!");
323 const KeyT EmptyKey = getEmptyKey();
324 for (BucketT *B = getBuckets(), *E = getBucketsEnd(); B != E; ++B)
325 ::new (&B->getFirst()) KeyT(EmptyKey);
328 /// Returns the number of buckets to allocate to ensure that the DenseMap can
329 /// accommodate \p NumEntries without need to grow().
330 unsigned getMinBucketToReserveForEntries(unsigned NumEntries) {
331 // Ensure that "NumEntries * 4 < NumBuckets * 3"
334 // +1 is required because of the strict equality.
335 // For example if NumEntries is 48, we need to return 401.
336 return NextPowerOf2(NumEntries * 4 / 3 + 1);
339 void moveFromOldBuckets(BucketT *OldBucketsBegin, BucketT *OldBucketsEnd) {
342 // Insert all the old elements.
343 const KeyT EmptyKey = getEmptyKey();
344 const KeyT TombstoneKey = getTombstoneKey();
345 for (BucketT *B = OldBucketsBegin, *E = OldBucketsEnd; B != E; ++B) {
346 if (!KeyInfoT::isEqual(B->getFirst(), EmptyKey) &&
347 !KeyInfoT::isEqual(B->getFirst(), TombstoneKey)) {
348 // Insert the key/value into the new table.
350 bool FoundVal = LookupBucketFor(B->getFirst(), DestBucket);
351 (void)FoundVal; // silence warning.
352 assert(!FoundVal && "Key already in new map?");
353 DestBucket->getFirst() = std::move(B->getFirst());
354 ::new (&DestBucket->getSecond()) ValueT(std::move(B->getSecond()));
355 incrementNumEntries();
358 B->getSecond().~ValueT();
360 B->getFirst().~KeyT();
364 template <typename OtherBaseT>
366 const DenseMapBase<OtherBaseT, KeyT, ValueT, KeyInfoT, BucketT> &other) {
367 assert(&other != this);
368 assert(getNumBuckets() == other.getNumBuckets());
370 setNumEntries(other.getNumEntries());
371 setNumTombstones(other.getNumTombstones());
373 if (isPodLike<KeyT>::value && isPodLike<ValueT>::value)
374 memcpy(getBuckets(), other.getBuckets(),
375 getNumBuckets() * sizeof(BucketT));
377 for (size_t i = 0; i < getNumBuckets(); ++i) {
378 ::new (&getBuckets()[i].getFirst())
379 KeyT(other.getBuckets()[i].getFirst());
380 if (!KeyInfoT::isEqual(getBuckets()[i].getFirst(), getEmptyKey()) &&
381 !KeyInfoT::isEqual(getBuckets()[i].getFirst(), getTombstoneKey()))
382 ::new (&getBuckets()[i].getSecond())
383 ValueT(other.getBuckets()[i].getSecond());
387 static unsigned getHashValue(const KeyT &Val) {
388 return KeyInfoT::getHashValue(Val);
390 template<typename LookupKeyT>
391 static unsigned getHashValue(const LookupKeyT &Val) {
392 return KeyInfoT::getHashValue(Val);
394 static const KeyT getEmptyKey() {
395 static_assert(std::is_base_of<DenseMapBase, DerivedT>::value,
396 "Must pass the derived type to this template!");
397 return KeyInfoT::getEmptyKey();
399 static const KeyT getTombstoneKey() {
400 return KeyInfoT::getTombstoneKey();
404 unsigned getNumEntries() const {
405 return static_cast<const DerivedT *>(this)->getNumEntries();
407 void setNumEntries(unsigned Num) {
408 static_cast<DerivedT *>(this)->setNumEntries(Num);
410 void incrementNumEntries() {
411 setNumEntries(getNumEntries() + 1);
413 void decrementNumEntries() {
414 setNumEntries(getNumEntries() - 1);
416 unsigned getNumTombstones() const {
417 return static_cast<const DerivedT *>(this)->getNumTombstones();
419 void setNumTombstones(unsigned Num) {
420 static_cast<DerivedT *>(this)->setNumTombstones(Num);
422 void incrementNumTombstones() {
423 setNumTombstones(getNumTombstones() + 1);
425 void decrementNumTombstones() {
426 setNumTombstones(getNumTombstones() - 1);
428 const BucketT *getBuckets() const {
429 return static_cast<const DerivedT *>(this)->getBuckets();
431 BucketT *getBuckets() {
432 return static_cast<DerivedT *>(this)->getBuckets();
434 unsigned getNumBuckets() const {
435 return static_cast<const DerivedT *>(this)->getNumBuckets();
437 BucketT *getBucketsEnd() {
438 return getBuckets() + getNumBuckets();
440 const BucketT *getBucketsEnd() const {
441 return getBuckets() + getNumBuckets();
444 void grow(unsigned AtLeast) {
445 static_cast<DerivedT *>(this)->grow(AtLeast);
448 void shrink_and_clear() {
449 static_cast<DerivedT *>(this)->shrink_and_clear();
452 template <typename KeyArg, typename... ValueArgs>
453 BucketT *InsertIntoBucket(BucketT *TheBucket, KeyArg &&Key,
454 ValueArgs &&... Values) {
455 TheBucket = InsertIntoBucketImpl(Key, Key, TheBucket);
457 TheBucket->getFirst() = std::forward<KeyArg>(Key);
458 ::new (&TheBucket->getSecond()) ValueT(std::forward<ValueArgs>(Values)...);
462 template <typename LookupKeyT>
463 BucketT *InsertIntoBucketWithLookup(BucketT *TheBucket, KeyT &&Key,
464 ValueT &&Value, LookupKeyT &Lookup) {
465 TheBucket = InsertIntoBucketImpl(Key, Lookup, TheBucket);
467 TheBucket->getFirst() = std::move(Key);
468 ::new (&TheBucket->getSecond()) ValueT(std::move(Value));
472 template <typename LookupKeyT>
473 BucketT *InsertIntoBucketImpl(const KeyT &Key, const LookupKeyT &Lookup,
474 BucketT *TheBucket) {
477 // If the load of the hash table is more than 3/4, or if fewer than 1/8 of
478 // the buckets are empty (meaning that many are filled with tombstones),
481 // The later case is tricky. For example, if we had one empty bucket with
482 // tons of tombstones, failing lookups (e.g. for insertion) would have to
483 // probe almost the entire table until it found the empty bucket. If the
484 // table completely filled with tombstones, no lookup would ever succeed,
485 // causing infinite loops in lookup.
486 unsigned NewNumEntries = getNumEntries() + 1;
487 unsigned NumBuckets = getNumBuckets();
488 if (LLVM_UNLIKELY(NewNumEntries * 4 >= NumBuckets * 3)) {
489 this->grow(NumBuckets * 2);
490 LookupBucketFor(Lookup, TheBucket);
491 NumBuckets = getNumBuckets();
492 } else if (LLVM_UNLIKELY(NumBuckets-(NewNumEntries+getNumTombstones()) <=
494 this->grow(NumBuckets);
495 LookupBucketFor(Lookup, TheBucket);
499 // Only update the state after we've grown our bucket space appropriately
500 // so that when growing buckets we have self-consistent entry count.
501 incrementNumEntries();
503 // If we are writing over a tombstone, remember this.
504 const KeyT EmptyKey = getEmptyKey();
505 if (!KeyInfoT::isEqual(TheBucket->getFirst(), EmptyKey))
506 decrementNumTombstones();
511 /// LookupBucketFor - Lookup the appropriate bucket for Val, returning it in
512 /// FoundBucket. If the bucket contains the key and a value, this returns
513 /// true, otherwise it returns a bucket with an empty marker or tombstone and
515 template<typename LookupKeyT>
516 bool LookupBucketFor(const LookupKeyT &Val,
517 const BucketT *&FoundBucket) const {
518 const BucketT *BucketsPtr = getBuckets();
519 const unsigned NumBuckets = getNumBuckets();
521 if (NumBuckets == 0) {
522 FoundBucket = nullptr;
526 // FoundTombstone - Keep track of whether we find a tombstone while probing.
527 const BucketT *FoundTombstone = nullptr;
528 const KeyT EmptyKey = getEmptyKey();
529 const KeyT TombstoneKey = getTombstoneKey();
530 assert(!KeyInfoT::isEqual(Val, EmptyKey) &&
531 !KeyInfoT::isEqual(Val, TombstoneKey) &&
532 "Empty/Tombstone value shouldn't be inserted into map!");
534 unsigned BucketNo = getHashValue(Val) & (NumBuckets-1);
535 unsigned ProbeAmt = 1;
537 const BucketT *ThisBucket = BucketsPtr + BucketNo;
538 // Found Val's bucket? If so, return it.
539 if (LLVM_LIKELY(KeyInfoT::isEqual(Val, ThisBucket->getFirst()))) {
540 FoundBucket = ThisBucket;
544 // If we found an empty bucket, the key doesn't exist in the set.
545 // Insert it and return the default value.
546 if (LLVM_LIKELY(KeyInfoT::isEqual(ThisBucket->getFirst(), EmptyKey))) {
547 // If we've already seen a tombstone while probing, fill it in instead
548 // of the empty bucket we eventually probed to.
549 FoundBucket = FoundTombstone ? FoundTombstone : ThisBucket;
553 // If this is a tombstone, remember it. If Val ends up not in the map, we
554 // prefer to return it than something that would require more probing.
555 if (KeyInfoT::isEqual(ThisBucket->getFirst(), TombstoneKey) &&
557 FoundTombstone = ThisBucket; // Remember the first tombstone found.
559 // Otherwise, it's a hash collision or a tombstone, continue quadratic
561 BucketNo += ProbeAmt++;
562 BucketNo &= (NumBuckets-1);
566 template <typename LookupKeyT>
567 bool LookupBucketFor(const LookupKeyT &Val, BucketT *&FoundBucket) {
568 const BucketT *ConstFoundBucket;
569 bool Result = const_cast<const DenseMapBase *>(this)
570 ->LookupBucketFor(Val, ConstFoundBucket);
571 FoundBucket = const_cast<BucketT *>(ConstFoundBucket);
576 /// Return the approximate size (in bytes) of the actual map.
577 /// This is just the raw memory used by DenseMap.
578 /// If entries are pointers to objects, the size of the referenced objects
579 /// are not included.
580 size_t getMemorySize() const {
581 return getNumBuckets() * sizeof(BucketT);
585 template <typename KeyT, typename ValueT,
586 typename KeyInfoT = DenseMapInfo<KeyT>,
587 typename BucketT = detail::DenseMapPair<KeyT, ValueT>>
588 class DenseMap : public DenseMapBase<DenseMap<KeyT, ValueT, KeyInfoT, BucketT>,
589 KeyT, ValueT, KeyInfoT, BucketT> {
590 // Lift some types from the dependent base class into this class for
591 // simplicity of referring to them.
592 typedef DenseMapBase<DenseMap, KeyT, ValueT, KeyInfoT, BucketT> BaseT;
593 friend class DenseMapBase<DenseMap, KeyT, ValueT, KeyInfoT, BucketT>;
597 unsigned NumTombstones;
601 /// Create a DenseMap wth an optional \p InitialReserve that guarantee that
602 /// this number of elements can be inserted in the map without grow()
603 explicit DenseMap(unsigned InitialReserve = 0) { init(InitialReserve); }
605 DenseMap(const DenseMap &other) : BaseT() {
610 DenseMap(DenseMap &&other) : BaseT() {
615 template<typename InputIt>
616 DenseMap(const InputIt &I, const InputIt &E) {
617 init(std::distance(I, E));
623 operator delete(Buckets);
626 void swap(DenseMap& RHS) {
627 this->incrementEpoch();
628 RHS.incrementEpoch();
629 std::swap(Buckets, RHS.Buckets);
630 std::swap(NumEntries, RHS.NumEntries);
631 std::swap(NumTombstones, RHS.NumTombstones);
632 std::swap(NumBuckets, RHS.NumBuckets);
635 DenseMap& operator=(const DenseMap& other) {
641 DenseMap& operator=(DenseMap &&other) {
643 operator delete(Buckets);
649 void copyFrom(const DenseMap& other) {
651 operator delete(Buckets);
652 if (allocateBuckets(other.NumBuckets)) {
653 this->BaseT::copyFrom(other);
660 void init(unsigned InitNumEntries) {
661 auto InitBuckets = BaseT::getMinBucketToReserveForEntries(InitNumEntries);
662 if (allocateBuckets(InitBuckets)) {
663 this->BaseT::initEmpty();
670 void grow(unsigned AtLeast) {
671 unsigned OldNumBuckets = NumBuckets;
672 BucketT *OldBuckets = Buckets;
674 allocateBuckets(std::max<unsigned>(64, static_cast<unsigned>(NextPowerOf2(AtLeast-1))));
677 this->BaseT::initEmpty();
681 this->moveFromOldBuckets(OldBuckets, OldBuckets+OldNumBuckets);
683 // Free the old table.
684 operator delete(OldBuckets);
687 void shrink_and_clear() {
688 unsigned OldNumEntries = NumEntries;
691 // Reduce the number of buckets.
692 unsigned NewNumBuckets = 0;
694 NewNumBuckets = std::max(64, 1 << (Log2_32_Ceil(OldNumEntries) + 1));
695 if (NewNumBuckets == NumBuckets) {
696 this->BaseT::initEmpty();
700 operator delete(Buckets);
705 unsigned getNumEntries() const {
708 void setNumEntries(unsigned Num) {
712 unsigned getNumTombstones() const {
713 return NumTombstones;
715 void setNumTombstones(unsigned Num) {
719 BucketT *getBuckets() const {
723 unsigned getNumBuckets() const {
727 bool allocateBuckets(unsigned Num) {
729 if (NumBuckets == 0) {
734 Buckets = static_cast<BucketT*>(operator new(sizeof(BucketT) * NumBuckets));
739 template <typename KeyT, typename ValueT, unsigned InlineBuckets = 4,
740 typename KeyInfoT = DenseMapInfo<KeyT>,
741 typename BucketT = detail::DenseMapPair<KeyT, ValueT>>
743 : public DenseMapBase<
744 SmallDenseMap<KeyT, ValueT, InlineBuckets, KeyInfoT, BucketT>, KeyT,
745 ValueT, KeyInfoT, BucketT> {
746 // Lift some types from the dependent base class into this class for
747 // simplicity of referring to them.
748 typedef DenseMapBase<SmallDenseMap, KeyT, ValueT, KeyInfoT, BucketT> BaseT;
749 friend class DenseMapBase<SmallDenseMap, KeyT, ValueT, KeyInfoT, BucketT>;
750 static_assert(isPowerOf2_64(InlineBuckets),
751 "InlineBuckets must be a power of 2.");
754 unsigned NumEntries : 31;
755 unsigned NumTombstones;
762 /// A "union" of an inline bucket array and the struct representing
763 /// a large bucket. This union will be discriminated by the 'Small' bit.
764 AlignedCharArrayUnion<BucketT[InlineBuckets], LargeRep> storage;
767 explicit SmallDenseMap(unsigned NumInitBuckets = 0) {
768 init(NumInitBuckets);
771 SmallDenseMap(const SmallDenseMap &other) : BaseT() {
776 SmallDenseMap(SmallDenseMap &&other) : BaseT() {
781 template<typename InputIt>
782 SmallDenseMap(const InputIt &I, const InputIt &E) {
783 init(NextPowerOf2(std::distance(I, E)));
792 void swap(SmallDenseMap& RHS) {
793 unsigned TmpNumEntries = RHS.NumEntries;
794 RHS.NumEntries = NumEntries;
795 NumEntries = TmpNumEntries;
796 std::swap(NumTombstones, RHS.NumTombstones);
798 const KeyT EmptyKey = this->getEmptyKey();
799 const KeyT TombstoneKey = this->getTombstoneKey();
800 if (Small && RHS.Small) {
801 // If we're swapping inline bucket arrays, we have to cope with some of
802 // the tricky bits of DenseMap's storage system: the buckets are not
803 // fully initialized. Thus we swap every key, but we may have
804 // a one-directional move of the value.
805 for (unsigned i = 0, e = InlineBuckets; i != e; ++i) {
806 BucketT *LHSB = &getInlineBuckets()[i],
807 *RHSB = &RHS.getInlineBuckets()[i];
808 bool hasLHSValue = (!KeyInfoT::isEqual(LHSB->getFirst(), EmptyKey) &&
809 !KeyInfoT::isEqual(LHSB->getFirst(), TombstoneKey));
810 bool hasRHSValue = (!KeyInfoT::isEqual(RHSB->getFirst(), EmptyKey) &&
811 !KeyInfoT::isEqual(RHSB->getFirst(), TombstoneKey));
812 if (hasLHSValue && hasRHSValue) {
813 // Swap together if we can...
814 std::swap(*LHSB, *RHSB);
817 // Swap separately and handle any assymetry.
818 std::swap(LHSB->getFirst(), RHSB->getFirst());
820 ::new (&RHSB->getSecond()) ValueT(std::move(LHSB->getSecond()));
821 LHSB->getSecond().~ValueT();
822 } else if (hasRHSValue) {
823 ::new (&LHSB->getSecond()) ValueT(std::move(RHSB->getSecond()));
824 RHSB->getSecond().~ValueT();
829 if (!Small && !RHS.Small) {
830 std::swap(getLargeRep()->Buckets, RHS.getLargeRep()->Buckets);
831 std::swap(getLargeRep()->NumBuckets, RHS.getLargeRep()->NumBuckets);
835 SmallDenseMap &SmallSide = Small ? *this : RHS;
836 SmallDenseMap &LargeSide = Small ? RHS : *this;
838 // First stash the large side's rep and move the small side across.
839 LargeRep TmpRep = std::move(*LargeSide.getLargeRep());
840 LargeSide.getLargeRep()->~LargeRep();
841 LargeSide.Small = true;
842 // This is similar to the standard move-from-old-buckets, but the bucket
843 // count hasn't actually rotated in this case. So we have to carefully
844 // move construct the keys and values into their new locations, but there
845 // is no need to re-hash things.
846 for (unsigned i = 0, e = InlineBuckets; i != e; ++i) {
847 BucketT *NewB = &LargeSide.getInlineBuckets()[i],
848 *OldB = &SmallSide.getInlineBuckets()[i];
849 ::new (&NewB->getFirst()) KeyT(std::move(OldB->getFirst()));
850 OldB->getFirst().~KeyT();
851 if (!KeyInfoT::isEqual(NewB->getFirst(), EmptyKey) &&
852 !KeyInfoT::isEqual(NewB->getFirst(), TombstoneKey)) {
853 ::new (&NewB->getSecond()) ValueT(std::move(OldB->getSecond()));
854 OldB->getSecond().~ValueT();
858 // The hard part of moving the small buckets across is done, just move
859 // the TmpRep into its new home.
860 SmallSide.Small = false;
861 new (SmallSide.getLargeRep()) LargeRep(std::move(TmpRep));
864 SmallDenseMap& operator=(const SmallDenseMap& other) {
870 SmallDenseMap& operator=(SmallDenseMap &&other) {
878 void copyFrom(const SmallDenseMap& other) {
882 if (other.getNumBuckets() > InlineBuckets) {
884 new (getLargeRep()) LargeRep(allocateBuckets(other.getNumBuckets()));
886 this->BaseT::copyFrom(other);
889 void init(unsigned InitBuckets) {
891 if (InitBuckets > InlineBuckets) {
893 new (getLargeRep()) LargeRep(allocateBuckets(InitBuckets));
895 this->BaseT::initEmpty();
898 void grow(unsigned AtLeast) {
899 if (AtLeast >= InlineBuckets)
900 AtLeast = std::max<unsigned>(64, NextPowerOf2(AtLeast-1));
903 if (AtLeast < InlineBuckets)
904 return; // Nothing to do.
906 // First move the inline buckets into a temporary storage.
907 AlignedCharArrayUnion<BucketT[InlineBuckets]> TmpStorage;
908 BucketT *TmpBegin = reinterpret_cast<BucketT *>(TmpStorage.buffer);
909 BucketT *TmpEnd = TmpBegin;
911 // Loop over the buckets, moving non-empty, non-tombstones into the
912 // temporary storage. Have the loop move the TmpEnd forward as it goes.
913 const KeyT EmptyKey = this->getEmptyKey();
914 const KeyT TombstoneKey = this->getTombstoneKey();
915 for (BucketT *P = getBuckets(), *E = P + InlineBuckets; P != E; ++P) {
916 if (!KeyInfoT::isEqual(P->getFirst(), EmptyKey) &&
917 !KeyInfoT::isEqual(P->getFirst(), TombstoneKey)) {
918 assert(size_t(TmpEnd - TmpBegin) < InlineBuckets &&
919 "Too many inline buckets!");
920 ::new (&TmpEnd->getFirst()) KeyT(std::move(P->getFirst()));
921 ::new (&TmpEnd->getSecond()) ValueT(std::move(P->getSecond()));
923 P->getSecond().~ValueT();
925 P->getFirst().~KeyT();
928 // Now make this map use the large rep, and move all the entries back
931 new (getLargeRep()) LargeRep(allocateBuckets(AtLeast));
932 this->moveFromOldBuckets(TmpBegin, TmpEnd);
936 LargeRep OldRep = std::move(*getLargeRep());
937 getLargeRep()->~LargeRep();
938 if (AtLeast <= InlineBuckets) {
941 new (getLargeRep()) LargeRep(allocateBuckets(AtLeast));
944 this->moveFromOldBuckets(OldRep.Buckets, OldRep.Buckets+OldRep.NumBuckets);
946 // Free the old table.
947 operator delete(OldRep.Buckets);
950 void shrink_and_clear() {
951 unsigned OldSize = this->size();
954 // Reduce the number of buckets.
955 unsigned NewNumBuckets = 0;
957 NewNumBuckets = 1 << (Log2_32_Ceil(OldSize) + 1);
958 if (NewNumBuckets > InlineBuckets && NewNumBuckets < 64u)
961 if ((Small && NewNumBuckets <= InlineBuckets) ||
962 (!Small && NewNumBuckets == getLargeRep()->NumBuckets)) {
963 this->BaseT::initEmpty();
972 unsigned getNumEntries() const {
975 void setNumEntries(unsigned Num) {
976 // NumEntries is hardcoded to be 31 bits wide.
977 assert(Num < (1U << 31) && "Cannot support more than 1<<31 entries");
981 unsigned getNumTombstones() const {
982 return NumTombstones;
984 void setNumTombstones(unsigned Num) {
988 const BucketT *getInlineBuckets() const {
990 // Note that this cast does not violate aliasing rules as we assert that
991 // the memory's dynamic type is the small, inline bucket buffer, and the
992 // 'storage.buffer' static type is 'char *'.
993 return reinterpret_cast<const BucketT *>(storage.buffer);
995 BucketT *getInlineBuckets() {
996 return const_cast<BucketT *>(
997 const_cast<const SmallDenseMap *>(this)->getInlineBuckets());
999 const LargeRep *getLargeRep() const {
1001 // Note, same rule about aliasing as with getInlineBuckets.
1002 return reinterpret_cast<const LargeRep *>(storage.buffer);
1004 LargeRep *getLargeRep() {
1005 return const_cast<LargeRep *>(
1006 const_cast<const SmallDenseMap *>(this)->getLargeRep());
1009 const BucketT *getBuckets() const {
1010 return Small ? getInlineBuckets() : getLargeRep()->Buckets;
1012 BucketT *getBuckets() {
1013 return const_cast<BucketT *>(
1014 const_cast<const SmallDenseMap *>(this)->getBuckets());
1016 unsigned getNumBuckets() const {
1017 return Small ? InlineBuckets : getLargeRep()->NumBuckets;
1020 void deallocateBuckets() {
1024 operator delete(getLargeRep()->Buckets);
1025 getLargeRep()->~LargeRep();
1028 LargeRep allocateBuckets(unsigned Num) {
1029 assert(Num > InlineBuckets && "Must allocate more buckets than are inline");
1031 static_cast<BucketT*>(operator new(sizeof(BucketT) * Num)), Num
1037 template <typename KeyT, typename ValueT, typename KeyInfoT, typename Bucket,
1039 class DenseMapIterator : DebugEpochBase::HandleBase {
1040 typedef DenseMapIterator<KeyT, ValueT, KeyInfoT, Bucket, true> ConstIterator;
1041 friend class DenseMapIterator<KeyT, ValueT, KeyInfoT, Bucket, true>;
1042 friend class DenseMapIterator<KeyT, ValueT, KeyInfoT, Bucket, false>;
1045 typedef ptrdiff_t difference_type;
1046 typedef typename std::conditional<IsConst, const Bucket, Bucket>::type
1048 typedef value_type *pointer;
1049 typedef value_type &reference;
1050 typedef std::forward_iterator_tag iterator_category;
1056 DenseMapIterator() : Ptr(nullptr), End(nullptr) {}
1058 DenseMapIterator(pointer Pos, pointer E, const DebugEpochBase &Epoch,
1059 bool NoAdvance = false)
1060 : DebugEpochBase::HandleBase(&Epoch), Ptr(Pos), End(E) {
1061 assert(isHandleInSync() && "invalid construction!");
1062 if (!NoAdvance) AdvancePastEmptyBuckets();
1065 // Converting ctor from non-const iterators to const iterators. SFINAE'd out
1066 // for const iterator destinations so it doesn't end up as a user defined copy
1068 template <bool IsConstSrc,
1069 typename = typename std::enable_if<!IsConstSrc && IsConst>::type>
1071 const DenseMapIterator<KeyT, ValueT, KeyInfoT, Bucket, IsConstSrc> &I)
1072 : DebugEpochBase::HandleBase(I), Ptr(I.Ptr), End(I.End) {}
1074 reference operator*() const {
1075 assert(isHandleInSync() && "invalid iterator access!");
1078 pointer operator->() const {
1079 assert(isHandleInSync() && "invalid iterator access!");
1083 bool operator==(const ConstIterator &RHS) const {
1084 assert((!Ptr || isHandleInSync()) && "handle not in sync!");
1085 assert((!RHS.Ptr || RHS.isHandleInSync()) && "handle not in sync!");
1086 assert(getEpochAddress() == RHS.getEpochAddress() &&
1087 "comparing incomparable iterators!");
1088 return Ptr == RHS.Ptr;
1090 bool operator!=(const ConstIterator &RHS) const {
1091 assert((!Ptr || isHandleInSync()) && "handle not in sync!");
1092 assert((!RHS.Ptr || RHS.isHandleInSync()) && "handle not in sync!");
1093 assert(getEpochAddress() == RHS.getEpochAddress() &&
1094 "comparing incomparable iterators!");
1095 return Ptr != RHS.Ptr;
1098 inline DenseMapIterator& operator++() { // Preincrement
1099 assert(isHandleInSync() && "invalid iterator access!");
1101 AdvancePastEmptyBuckets();
1104 DenseMapIterator operator++(int) { // Postincrement
1105 assert(isHandleInSync() && "invalid iterator access!");
1106 DenseMapIterator tmp = *this; ++*this; return tmp;
1110 void AdvancePastEmptyBuckets() {
1111 const KeyT Empty = KeyInfoT::getEmptyKey();
1112 const KeyT Tombstone = KeyInfoT::getTombstoneKey();
1114 while (Ptr != End && (KeyInfoT::isEqual(Ptr->getFirst(), Empty) ||
1115 KeyInfoT::isEqual(Ptr->getFirst(), Tombstone)))
1120 template<typename KeyT, typename ValueT, typename KeyInfoT>
1121 static inline size_t
1122 capacity_in_bytes(const DenseMap<KeyT, ValueT, KeyInfoT> &X) {
1123 return X.getMemorySize();
1126 } // end namespace llvm
1128 #endif // LLVM_ADT_DENSEMAP_H