14 #ifndef LLVM_ADT_DENSEMAP_H
15 #define LLVM_ADT_DENSEMAP_H
17 #include "llvm/DenseMapInfo.h"
18 #include "llvm/EpochTracker.h"
19 #include "llvm/AlignOf.h"
20 #include "llvm/Compiler.h"
21 #include "llvm/MathExtras.h"
22 #include "llvm/type_traits.h"
37 template <
typename KeyT,
typename ValueT>
39 KeyT &getFirst() {
return std::pair<KeyT, ValueT>::first; }
40 const KeyT &getFirst()
const {
return std::pair<KeyT, ValueT>::first; }
41 ValueT &getSecond() {
return std::pair<KeyT, ValueT>::second; }
42 const ValueT &getSecond()
const {
return std::pair<KeyT, ValueT>::second; }
51 template <
typename DerivedT,
typename KeyT,
typename ValueT,
typename KeyInfoT,
55 typedef unsigned size_type;
56 typedef KeyT key_type;
57 typedef ValueT mapped_type;
58 typedef BucketT value_type;
65 return empty() ? end() :
iterator(getBuckets(), getBucketsEnd(), *
this);
68 return iterator(getBucketsEnd(), getBucketsEnd(), *
this,
true);
71 return empty() ? end()
75 return const_iterator(getBucketsEnd(), getBucketsEnd(), *
this,
true);
78 bool LLVM_ATTRIBUTE_UNUSED_RESULT empty()
const {
79 return getNumEntries() == 0;
81 unsigned size()
const {
return getNumEntries(); }
86 if (Size > getNumBuckets())
92 if (getNumEntries() == 0 && getNumTombstones() == 0)
return;
96 if (getNumEntries() * 4 < getNumBuckets() && getNumBuckets() > 64) {
101 const KeyT EmptyKey = getEmptyKey(), TombstoneKey = getTombstoneKey();
102 unsigned NumEntries = getNumEntries();
103 for (BucketT *P = getBuckets(), *E = getBucketsEnd(); P != E; ++P) {
104 if (!KeyInfoT::isEqual(P->getFirst(), EmptyKey)) {
105 if (!KeyInfoT::isEqual(P->getFirst(), TombstoneKey)) {
106 P->getSecond().~ValueT();
109 P->getFirst() = EmptyKey;
112 assert(NumEntries == 0 &&
"Node count imbalance!");
118 size_type
count(
const KeyT &Val)
const {
119 const BucketT *TheBucket;
120 return LookupBucketFor(Val, TheBucket) ? 1 : 0;
123 iterator find(
const KeyT &Val) {
125 if (LookupBucketFor(Val, TheBucket))
126 return iterator(TheBucket, getBucketsEnd(), *
this,
true);
129 const_iterator find(
const KeyT &Val)
const {
130 const BucketT *TheBucket;
131 if (LookupBucketFor(Val, TheBucket))
132 return const_iterator(TheBucket, getBucketsEnd(), *
this,
true);
141 template<
class LookupKeyT>
144 if (LookupBucketFor(Val, TheBucket))
145 return iterator(TheBucket, getBucketsEnd(), *
this,
true);
148 template<
class LookupKeyT>
149 const_iterator
find_as(
const LookupKeyT &Val)
const {
150 const BucketT *TheBucket;
151 if (LookupBucketFor(Val, TheBucket))
152 return const_iterator(TheBucket, getBucketsEnd(), *
this,
true);
159 const BucketT *TheBucket;
160 if (LookupBucketFor(Val, TheBucket))
161 return TheBucket->getSecond();
168 std::pair<iterator, bool> insert(
const std::pair<KeyT, ValueT> &KV) {
170 if (LookupBucketFor(KV.first, TheBucket))
171 return std::make_pair(iterator(TheBucket, getBucketsEnd(), *
this,
true),
175 TheBucket = InsertIntoBucket(KV.first, KV.second, TheBucket);
176 return std::make_pair(iterator(TheBucket, getBucketsEnd(), *
this,
true),
183 std::pair<iterator, bool> insert(std::pair<KeyT, ValueT> &&KV) {
185 if (LookupBucketFor(KV.first, TheBucket))
186 return std::make_pair(iterator(TheBucket, getBucketsEnd(), *
this,
true),
190 TheBucket = InsertIntoBucket(std::move(KV.first),
191 std::move(KV.second),
193 return std::make_pair(iterator(TheBucket, getBucketsEnd(), *
this,
true),
198 template<
typename InputIt>
205 bool erase(
const KeyT &Val) {
207 if (!LookupBucketFor(Val, TheBucket))
210 TheBucket->getSecond().~ValueT();
211 TheBucket->getFirst() = getTombstoneKey();
212 decrementNumEntries();
213 incrementNumTombstones();
216 void erase(iterator I) {
217 BucketT *TheBucket = &*I;
218 TheBucket->getSecond().~ValueT();
219 TheBucket->getFirst() = getTombstoneKey();
220 decrementNumEntries();
221 incrementNumTombstones();
224 value_type& FindAndConstruct(
const KeyT &Key) {
226 if (LookupBucketFor(Key, TheBucket))
229 return *InsertIntoBucket(Key, ValueT(), TheBucket);
232 ValueT &operator[](
const KeyT &Key) {
233 return FindAndConstruct(Key).second;
236 value_type& FindAndConstruct(KeyT &&Key) {
238 if (LookupBucketFor(Key, TheBucket))
241 return *InsertIntoBucket(std::move(Key), ValueT(), TheBucket);
244 ValueT &operator[](KeyT &&Key) {
245 return FindAndConstruct(std::move(Key)).second;
252 return Ptr >= getBuckets() && Ptr < getBucketsEnd();
264 if (getNumBuckets() == 0)
267 const KeyT EmptyKey = getEmptyKey(), TombstoneKey = getTombstoneKey();
268 for (BucketT *P = getBuckets(), *E = getBucketsEnd(); P != E; ++P) {
269 if (!KeyInfoT::isEqual(P->getFirst(), EmptyKey) &&
270 !KeyInfoT::isEqual(P->getFirst(), TombstoneKey))
271 P->getSecond().~ValueT();
272 P->getFirst().~KeyT();
280 assert((getNumBuckets() & (getNumBuckets()-1)) == 0 &&
281 "# initial buckets must be a power of two!");
282 const KeyT EmptyKey = getEmptyKey();
283 for (BucketT *B = getBuckets(), *E = getBucketsEnd(); B != E; ++B)
284 new (&B->getFirst()) KeyT(EmptyKey);
287 void moveFromOldBuckets(BucketT *OldBucketsBegin, BucketT *OldBucketsEnd) {
291 const KeyT EmptyKey = getEmptyKey();
292 const KeyT TombstoneKey = getTombstoneKey();
293 for (BucketT *B = OldBucketsBegin, *E = OldBucketsEnd; B != E; ++B) {
294 if (!KeyInfoT::isEqual(B->getFirst(), EmptyKey) &&
295 !KeyInfoT::isEqual(B->getFirst(), TombstoneKey)) {
298 bool FoundVal = LookupBucketFor(B->getFirst(), DestBucket);
300 assert(!FoundVal &&
"Key already in new map?");
301 DestBucket->getFirst() = std::move(B->getFirst());
302 new (&DestBucket->getSecond()) ValueT(std::move(B->getSecond()));
303 incrementNumEntries();
306 B->getSecond().~ValueT();
308 B->getFirst().~KeyT();
312 template <
typename OtherBaseT>
314 const DenseMapBase<OtherBaseT, KeyT, ValueT, KeyInfoT, BucketT> &other) {
315 assert(&other !=
this);
316 assert(getNumBuckets() == other.getNumBuckets());
318 setNumEntries(other.getNumEntries());
319 setNumTombstones(other.getNumTombstones());
321 if (isPodLike<KeyT>::value && isPodLike<ValueT>::value)
322 memcpy(getBuckets(), other.getBuckets(),
323 getNumBuckets() *
sizeof(BucketT));
325 for (
size_t i = 0; i < getNumBuckets(); ++i) {
326 new (&getBuckets()[i].getFirst())
327 KeyT(other.getBuckets()[i].getFirst());
328 if (!KeyInfoT::isEqual(getBuckets()[i].getFirst(), getEmptyKey()) &&
329 !KeyInfoT::isEqual(getBuckets()[i].getFirst(), getTombstoneKey()))
330 new (&getBuckets()[i].getSecond())
331 ValueT(other.getBuckets()[i].getSecond());
335 static unsigned getHashValue(
const KeyT &Val) {
336 return KeyInfoT::getHashValue(Val);
338 template<
typename LookupKeyT>
339 static unsigned getHashValue(
const LookupKeyT &Val) {
340 return KeyInfoT::getHashValue(Val);
342 static const KeyT getEmptyKey() {
343 return KeyInfoT::getEmptyKey();
345 static const KeyT getTombstoneKey() {
346 return KeyInfoT::getTombstoneKey();
350 unsigned getNumEntries()
const {
351 return static_cast<const DerivedT *
>(
this)->getNumEntries();
353 void setNumEntries(
unsigned Num) {
354 static_cast<DerivedT *
>(
this)->setNumEntries(Num);
356 void incrementNumEntries() {
357 setNumEntries(getNumEntries() + 1);
359 void decrementNumEntries() {
360 setNumEntries(getNumEntries() - 1);
362 unsigned getNumTombstones()
const {
363 return static_cast<const DerivedT *
>(
this)->getNumTombstones();
365 void setNumTombstones(
unsigned Num) {
366 static_cast<DerivedT *
>(
this)->setNumTombstones(Num);
368 void incrementNumTombstones() {
369 setNumTombstones(getNumTombstones() + 1);
371 void decrementNumTombstones() {
372 setNumTombstones(getNumTombstones() - 1);
374 const BucketT *getBuckets()
const {
375 return static_cast<const DerivedT *
>(
this)->getBuckets();
377 BucketT *getBuckets() {
378 return static_cast<DerivedT *
>(
this)->getBuckets();
380 unsigned getNumBuckets()
const {
381 return static_cast<const DerivedT *
>(
this)->getNumBuckets();
383 BucketT *getBucketsEnd() {
384 return getBuckets() + getNumBuckets();
386 const BucketT *getBucketsEnd()
const {
387 return getBuckets() + getNumBuckets();
390 void grow(
unsigned AtLeast) {
391 static_cast<DerivedT *
>(
this)->grow(AtLeast);
394 void shrink_and_clear() {
395 static_cast<DerivedT *
>(
this)->shrink_and_clear();
399 BucketT *InsertIntoBucket(
const KeyT &Key,
const ValueT &Value,
400 BucketT *TheBucket) {
401 TheBucket = InsertIntoBucketImpl(Key, TheBucket);
403 TheBucket->getFirst() = Key;
404 new (&TheBucket->getSecond()) ValueT(Value);
408 BucketT *InsertIntoBucket(
const KeyT &Key, ValueT &&Value,
409 BucketT *TheBucket) {
410 TheBucket = InsertIntoBucketImpl(Key, TheBucket);
412 TheBucket->getFirst() = Key;
413 new (&TheBucket->getSecond()) ValueT(std::move(Value));
417 BucketT *InsertIntoBucket(KeyT &&Key, ValueT &&Value, BucketT *TheBucket) {
418 TheBucket = InsertIntoBucketImpl(Key, TheBucket);
420 TheBucket->getFirst() = std::move(Key);
421 new (&TheBucket->getSecond()) ValueT(std::move(Value));
425 BucketT *InsertIntoBucketImpl(
const KeyT &Key, BucketT *TheBucket) {
437 unsigned NewNumEntries = getNumEntries() + 1;
438 unsigned NumBuckets = getNumBuckets();
439 if (LLVM_UNLIKELY(NewNumEntries * 4 >= NumBuckets * 3)) {
440 this->grow(NumBuckets * 2);
441 LookupBucketFor(Key, TheBucket);
442 NumBuckets = getNumBuckets();
443 }
else if (LLVM_UNLIKELY(NumBuckets-(NewNumEntries+getNumTombstones()) <=
445 this->grow(NumBuckets);
446 LookupBucketFor(Key, TheBucket);
452 incrementNumEntries();
455 const KeyT EmptyKey = getEmptyKey();
456 if (!KeyInfoT::isEqual(TheBucket->getFirst(), EmptyKey))
457 decrementNumTombstones();
466 template<
typename LookupKeyT>
467 bool LookupBucketFor(
const LookupKeyT &Val,
468 const BucketT *&FoundBucket)
const {
469 const BucketT *BucketsPtr = getBuckets();
470 const unsigned NumBuckets = getNumBuckets();
472 if (NumBuckets == 0) {
473 FoundBucket =
nullptr;
478 const BucketT *FoundTombstone =
nullptr;
479 const KeyT EmptyKey = getEmptyKey();
480 const KeyT TombstoneKey = getTombstoneKey();
481 assert(!KeyInfoT::isEqual(Val, EmptyKey) &&
482 !KeyInfoT::isEqual(Val, TombstoneKey) &&
483 "Empty/Tombstone value shouldn't be inserted into map!");
485 unsigned BucketNo = getHashValue(Val) & (NumBuckets-1);
486 unsigned ProbeAmt = 1;
488 const BucketT *ThisBucket = BucketsPtr + BucketNo;
490 if (LLVM_LIKELY(KeyInfoT::isEqual(Val, ThisBucket->getFirst()))) {
491 FoundBucket = ThisBucket;
497 if (LLVM_LIKELY(KeyInfoT::isEqual(ThisBucket->getFirst(), EmptyKey))) {
500 FoundBucket = FoundTombstone ? FoundTombstone : ThisBucket;
506 if (KeyInfoT::isEqual(ThisBucket->getFirst(), TombstoneKey) &&
508 FoundTombstone = ThisBucket;
512 BucketNo += ProbeAmt++;
513 BucketNo &= (NumBuckets-1);
517 template <
typename LookupKeyT>
518 bool LookupBucketFor(
const LookupKeyT &Val, BucketT *&FoundBucket) {
519 const BucketT *ConstFoundBucket;
520 bool Result =
const_cast<const DenseMapBase *
>(
this)
521 ->LookupBucketFor(Val, ConstFoundBucket);
522 FoundBucket =
const_cast<BucketT *
>(ConstFoundBucket);
532 return getNumBuckets() *
sizeof(BucketT);
536 template <
typename KeyT,
typename ValueT,
537 typename KeyInfoT = DenseMapInfo<KeyT>,
538 typename BucketT = detail::DenseMapPair<KeyT, ValueT>>
540 KeyT, ValueT, KeyInfoT, BucketT> {
544 friend class DenseMapBase<DenseMap, KeyT, ValueT, KeyInfoT, BucketT>;
548 unsigned NumTombstones;
552 explicit DenseMap(
unsigned NumInitBuckets = 0) {
553 init(NumInitBuckets);
556 DenseMap(
const DenseMap &other) :
BaseT() {
561 DenseMap(DenseMap &&other) :
BaseT() {
566 template<
typename InputIt>
567 DenseMap(
const InputIt &I,
const InputIt &E) {
568 init(NextPowerOf2(std::distance(I, E)));
574 operator delete(Buckets);
577 void swap(DenseMap& RHS) {
580 std::swap(Buckets, RHS.Buckets);
581 std::swap(NumEntries, RHS.NumEntries);
582 std::swap(NumTombstones, RHS.NumTombstones);
583 std::swap(NumBuckets, RHS.NumBuckets);
586 DenseMap& operator=(
const DenseMap& other) {
592 DenseMap& operator=(DenseMap &&other) {
594 operator delete(Buckets);
600 void copyFrom(
const DenseMap& other) {
602 operator delete(Buckets);
603 if (allocateBuckets(other.NumBuckets)) {
604 this->BaseT::copyFrom(other);
611 void init(
unsigned InitBuckets) {
612 if (allocateBuckets(InitBuckets)) {
613 this->BaseT::initEmpty();
620 void grow(
unsigned AtLeast) {
621 unsigned OldNumBuckets = NumBuckets;
622 BucketT *OldBuckets = Buckets;
624 allocateBuckets(std::max<unsigned>(64, static_cast<unsigned>(NextPowerOf2(AtLeast-1))));
627 this->BaseT::initEmpty();
631 this->moveFromOldBuckets(OldBuckets, OldBuckets+OldNumBuckets);
634 operator delete(OldBuckets);
637 void shrink_and_clear() {
638 unsigned OldNumEntries = NumEntries;
642 unsigned NewNumBuckets = 0;
644 NewNumBuckets = std::max(64, 1 << (Log2_32_Ceil(OldNumEntries) + 1));
645 if (NewNumBuckets == NumBuckets) {
646 this->BaseT::initEmpty();
650 operator delete(Buckets);
655 unsigned getNumEntries()
const {
658 void setNumEntries(
unsigned Num) {
662 unsigned getNumTombstones()
const {
663 return NumTombstones;
665 void setNumTombstones(
unsigned Num) {
669 BucketT *getBuckets()
const {
673 unsigned getNumBuckets()
const {
677 bool allocateBuckets(
unsigned Num) {
679 if (NumBuckets == 0) {
684 Buckets =
static_cast<BucketT*
>(
operator new(
sizeof(BucketT) * NumBuckets));
689 template <
typename KeyT,
typename ValueT,
unsigned InlineBuckets = 4,
694 SmallDenseMap<KeyT, ValueT, InlineBuckets, KeyInfoT, BucketT>, KeyT,
695 ValueT, KeyInfoT, BucketT> {
699 friend class DenseMapBase<SmallDenseMap, KeyT, ValueT, KeyInfoT, BucketT>;
702 unsigned NumEntries : 31;
703 unsigned NumTombstones;
715 explicit SmallDenseMap(
unsigned NumInitBuckets = 0) {
716 init(NumInitBuckets);
719 SmallDenseMap(
const SmallDenseMap &other) :
BaseT() {
724 SmallDenseMap(SmallDenseMap &&other) :
BaseT() {
729 template<
typename InputIt>
730 SmallDenseMap(
const InputIt &I,
const InputIt &E) {
731 init(NextPowerOf2(std::distance(I, E)));
740 void swap(SmallDenseMap& RHS) {
741 unsigned TmpNumEntries = RHS.NumEntries;
742 RHS.NumEntries = NumEntries;
743 NumEntries = TmpNumEntries;
744 std::swap(NumTombstones, RHS.NumTombstones);
746 const KeyT EmptyKey = this->getEmptyKey();
747 const KeyT TombstoneKey = this->getTombstoneKey();
748 if (Small && RHS.Small) {
753 for (
unsigned i = 0, e = InlineBuckets; i != e; ++i) {
754 BucketT *LHSB = &getInlineBuckets()[i],
755 *RHSB = &RHS.getInlineBuckets()[i];
756 bool hasLHSValue = (!KeyInfoT::isEqual(LHSB->getFirst(), EmptyKey) &&
757 !KeyInfoT::isEqual(LHSB->getFirst(), TombstoneKey));
758 bool hasRHSValue = (!KeyInfoT::isEqual(RHSB->getFirst(), EmptyKey) &&
759 !KeyInfoT::isEqual(RHSB->getFirst(), TombstoneKey));
760 if (hasLHSValue && hasRHSValue) {
762 std::swap(*LHSB, *RHSB);
766 std::swap(LHSB->getFirst(), RHSB->getFirst());
768 new (&RHSB->getSecond()) ValueT(std::move(LHSB->getSecond()));
769 LHSB->getSecond().~ValueT();
770 }
else if (hasRHSValue) {
771 new (&LHSB->getSecond()) ValueT(std::move(RHSB->getSecond()));
772 RHSB->getSecond().~ValueT();
777 if (!Small && !RHS.Small) {
778 std::swap(getLargeRep()->Buckets, RHS.getLargeRep()->Buckets);
779 std::swap(getLargeRep()->NumBuckets, RHS.getLargeRep()->NumBuckets);
783 SmallDenseMap &SmallSide = Small ? *
this : RHS;
784 SmallDenseMap &LargeSide = Small ? RHS : *
this;
787 LargeRep TmpRep = std::move(*LargeSide.getLargeRep());
788 LargeSide.getLargeRep()->~LargeRep();
789 LargeSide.Small =
true;
794 for (
unsigned i = 0, e = InlineBuckets; i != e; ++i) {
795 BucketT *NewB = &LargeSide.getInlineBuckets()[i],
796 *OldB = &SmallSide.getInlineBuckets()[i];
797 new (&NewB->getFirst()) KeyT(std::move(OldB->getFirst()));
798 OldB->getFirst().~KeyT();
799 if (!KeyInfoT::isEqual(NewB->getFirst(), EmptyKey) &&
800 !KeyInfoT::isEqual(NewB->getFirst(), TombstoneKey)) {
801 new (&NewB->getSecond()) ValueT(std::move(OldB->getSecond()));
802 OldB->getSecond().~ValueT();
808 SmallSide.Small =
false;
809 new (SmallSide.getLargeRep()) LargeRep(std::move(TmpRep));
812 SmallDenseMap& operator=(
const SmallDenseMap& other) {
818 SmallDenseMap& operator=(SmallDenseMap &&other) {
826 void copyFrom(
const SmallDenseMap& other) {
830 if (other.getNumBuckets() > InlineBuckets) {
832 new (getLargeRep()) LargeRep(allocateBuckets(other.getNumBuckets()));
834 this->BaseT::copyFrom(other);
837 void init(
unsigned InitBuckets) {
839 if (InitBuckets > InlineBuckets) {
841 new (getLargeRep()) LargeRep(allocateBuckets(InitBuckets));
843 this->BaseT::initEmpty();
846 void grow(
unsigned AtLeast) {
847 if (AtLeast >= InlineBuckets)
848 AtLeast = std::max<unsigned>(64, NextPowerOf2(AtLeast-1));
851 if (AtLeast < InlineBuckets)
856 BucketT *TmpBegin =
reinterpret_cast<BucketT *
>(TmpStorage.buffer);
857 BucketT *TmpEnd = TmpBegin;
861 const KeyT EmptyKey = this->getEmptyKey();
862 const KeyT TombstoneKey = this->getTombstoneKey();
863 for (BucketT *P = getBuckets(), *E = P + InlineBuckets; P != E; ++P) {
864 if (!KeyInfoT::isEqual(P->getFirst(), EmptyKey) &&
865 !KeyInfoT::isEqual(P->getFirst(), TombstoneKey)) {
866 assert(
size_t(TmpEnd - TmpBegin) < InlineBuckets &&
867 "Too many inline buckets!");
868 new (&TmpEnd->getFirst()) KeyT(std::move(P->getFirst()));
869 new (&TmpEnd->getSecond()) ValueT(std::move(P->getSecond()));
871 P->getSecond().~ValueT();
873 P->getFirst().~KeyT();
879 new (getLargeRep()) LargeRep(allocateBuckets(AtLeast));
880 this->moveFromOldBuckets(TmpBegin, TmpEnd);
884 LargeRep OldRep = std::move(*getLargeRep());
885 getLargeRep()->~LargeRep();
886 if (AtLeast <= InlineBuckets) {
889 new (getLargeRep()) LargeRep(allocateBuckets(AtLeast));
892 this->moveFromOldBuckets(OldRep.Buckets, OldRep.Buckets+OldRep.NumBuckets);
895 operator delete(OldRep.Buckets);
898 void shrink_and_clear() {
899 unsigned OldSize = this->size();
903 unsigned NewNumBuckets = 0;
905 NewNumBuckets = 1 << (Log2_32_Ceil(OldSize) + 1);
906 if (NewNumBuckets > InlineBuckets && NewNumBuckets < 64u)
909 if ((Small && NewNumBuckets <= InlineBuckets) ||
910 (!Small && NewNumBuckets == getLargeRep()->NumBuckets)) {
911 this->BaseT::initEmpty();
920 unsigned getNumEntries()
const {
923 void setNumEntries(
unsigned Num) {
924 assert(Num < INT_MAX &&
"Cannot support more than INT_MAX entries");
928 unsigned getNumTombstones()
const {
929 return NumTombstones;
931 void setNumTombstones(
unsigned Num) {
935 const BucketT *getInlineBuckets()
const {
940 return reinterpret_cast<const BucketT *
>(storage.buffer);
942 BucketT *getInlineBuckets() {
943 return const_cast<BucketT *
>(
944 const_cast<const SmallDenseMap *
>(
this)->getInlineBuckets());
946 const LargeRep *getLargeRep()
const {
949 return reinterpret_cast<const LargeRep *
>(storage.buffer);
951 LargeRep *getLargeRep() {
952 return const_cast<LargeRep *
>(
953 const_cast<const SmallDenseMap *
>(
this)->getLargeRep());
956 const BucketT *getBuckets()
const {
957 return Small ? getInlineBuckets() : getLargeRep()->Buckets;
959 BucketT *getBuckets() {
960 return const_cast<BucketT *
>(
961 const_cast<const SmallDenseMap *
>(
this)->getBuckets());
963 unsigned getNumBuckets()
const {
964 return Small ? InlineBuckets : getLargeRep()->NumBuckets;
967 void deallocateBuckets() {
971 operator delete(getLargeRep()->Buckets);
972 getLargeRep()->~LargeRep();
975 LargeRep allocateBuckets(
unsigned Num) {
976 assert(Num > InlineBuckets &&
"Must allocate more buckets than are inline");
978 static_cast<BucketT*
>(
operator new(
sizeof(BucketT) * Num)), Num
984 template <
typename KeyT,
typename ValueT,
typename KeyInfoT,
typename Bucket,
992 typedef ptrdiff_t difference_type;
993 typedef typename std::conditional<IsConst, const Bucket, Bucket>::type
995 typedef value_type *pointer;
996 typedef value_type &reference;
997 typedef std::forward_iterator_tag iterator_category;
1004 bool NoAdvance =
false)
1006 assert(isHandleInSync() &&
"invalid construction!");
1007 if (!NoAdvance) AdvancePastEmptyBuckets();
1013 template <
bool IsConstSrc,
1014 typename =
typename std::enable_if<!IsConstSrc && IsConst>::type>
1019 reference operator*()
const {
1020 assert(isHandleInSync() &&
"invalid iterator access!");
1023 pointer operator->()
const {
1024 assert(isHandleInSync() &&
"invalid iterator access!");
1028 bool operator==(
const ConstIterator &RHS)
const {
1029 assert((!Ptr || isHandleInSync()) &&
"handle not in sync!");
1030 assert((!RHS.Ptr || RHS.isHandleInSync()) &&
"handle not in sync!");
1031 assert(getEpochAddress() == RHS.getEpochAddress() &&
1032 "comparing incomparable iterators!");
1033 return Ptr == RHS.Ptr;
1035 bool operator!=(
const ConstIterator &RHS)
const {
1036 assert((!Ptr || isHandleInSync()) &&
"handle not in sync!");
1037 assert((!RHS.Ptr || RHS.isHandleInSync()) &&
"handle not in sync!");
1038 assert(getEpochAddress() == RHS.getEpochAddress() &&
1039 "comparing incomparable iterators!");
1040 return Ptr != RHS.Ptr;
1043 inline DenseMapIterator& operator++() {
1044 assert(isHandleInSync() &&
"invalid iterator access!");
1046 AdvancePastEmptyBuckets();
1049 DenseMapIterator operator++(
int) {
1050 assert(isHandleInSync() &&
"invalid iterator access!");
1051 DenseMapIterator tmp = *
this; ++*
this;
return tmp;
1055 void AdvancePastEmptyBuckets() {
1056 const KeyT Empty = KeyInfoT::getEmptyKey();
1057 const KeyT Tombstone = KeyInfoT::getTombstoneKey();
1059 while (Ptr != End && (KeyInfoT::isEqual(Ptr->getFirst(), Empty) ||
1060 KeyInfoT::isEqual(Ptr->getFirst(), Tombstone)))
1065 template<
typename KeyT,
typename ValueT,
typename KeyInfoT>
1066 static inline size_t
Definition: DenseMap.h:692
ValueT lookup(const KeyT &Val) const
lookup - Return the entry for the specified key, or a default constructed value if no such entry exis...
Definition: DenseMap.h:158
Definition: DenseMap.h:539
Definition: DenseMapInfo.h:23
size_t getMemorySize() const
Return the approximate size (in bytes) of the actual map.
Definition: DenseMap.h:531
A base class for data structure classes wishing to make iterators ("handles") pointing into themselve...
Definition: EpochTracker.h:49
const void * getPointerIntoBucketsArray() const
getPointerIntoBucketsArray() - Return an opaque pointer into the buckets array.
Definition: DenseMap.h:258
void incrementEpoch()
Calling incrementEpoch invalidates all handles pointing into the calling instance.
Definition: EpochTracker.h:57
A base class for iterator classes ("handles") that wish to poll for iterator invalidating modificatio...
Definition: EpochTracker.h:71
Definition: DenseMap.h:38
void insert(InputIt I, InputIt E)
insert - Range insertion of pairs.
Definition: DenseMap.h:199
void resize(size_type Size)
Grow the densemap so that it has at least Size buckets. Does not shrink.
Definition: DenseMap.h:84
size_type count(const KeyT &Val) const
Return 1 if the specified key is in the map, 0 otherwise.
Definition: DenseMap.h:118
bool isPointerIntoBucketsArray(const void *Ptr) const
isPointerIntoBucketsArray - Return true if the specified pointer points somewhere into the DenseMap's...
Definition: DenseMap.h:251
iterator find_as(const LookupKeyT &Val)
Alternate version of find() which allows a different, and possibly less expensive, key type.
Definition: DenseMap.h:142
Definition: DenseMap.h:49
Definition: DenseMap.h:53