14#ifndef WPIUTIL_WPI_DENSEMAP_H
15#define WPIUTIL_WPI_DENSEMAP_H
30#include <initializer_list>
42template <
typename KeyT,
typename ValueT>
44 using std::pair<KeyT, ValueT>::pair;
46 KeyT &
getFirst() {
return std::pair<KeyT, ValueT>::first; }
47 const KeyT &
getFirst()
const {
return std::pair<KeyT, ValueT>::first; }
48 ValueT &
getSecond() {
return std::pair<KeyT, ValueT>::second; }
49 const ValueT &
getSecond()
const {
return std::pair<KeyT, ValueT>::second; }
54template <
typename KeyT,
typename ValueT,
55 typename KeyInfoT = DenseMapInfo<KeyT>,
58class DenseMapIterator;
60template <
typename DerivedT,
typename KeyT,
typename ValueT,
typename KeyInfoT,
82 return makeIterator(getBucketsEnd() - 1, getBuckets(), *
this);
83 return makeIterator(getBuckets(), getBucketsEnd(), *
this);
86 return makeIterator(getBucketsEnd(), getBucketsEnd(), *
this,
true);
92 return makeConstIterator(getBucketsEnd() - 1, getBuckets(), *
this);
93 return makeConstIterator(getBuckets(), getBucketsEnd(), *
this);
96 return makeConstIterator(getBucketsEnd(), getBucketsEnd(), *
this,
true);
99 [[nodiscard]]
bool empty()
const {
return getNumEntries() == 0; }
100 unsigned size()
const {
return getNumEntries(); }
107 if (NumBuckets > getNumBuckets())
113 if (getNumEntries() == 0 && getNumTombstones() == 0)
118 if (getNumEntries() * 4 < getNumBuckets() && getNumBuckets() > 64) {
124 if constexpr (std::is_trivially_destructible_v<ValueT>) {
126 for (BucketT *P = getBuckets(), *E = getBucketsEnd(); P != E; ++P)
127 P->getFirst() = EmptyKey;
130 [[maybe_unused]]
unsigned NumEntries = getNumEntries();
131 for (BucketT *P = getBuckets(), *E = getBucketsEnd(); P != E; ++P) {
132 if (!KeyInfoT::isEqual(P->getFirst(), EmptyKey)) {
133 if (!KeyInfoT::isEqual(P->getFirst(), TombstoneKey)) {
134 P->getSecond().~ValueT();
137 P->getFirst() = EmptyKey;
140 assert(NumEntries == 0 &&
"Node count imbalance!");
149 return doFind(Val) !=
nullptr;
158 if (BucketT *Bucket = doFind(Val))
165 if (
const BucketT *Bucket = doFind(Val))
166 return makeConstIterator(
178 if (BucketT *Bucket = doFind(Val))
184 template <
class LookupKeyT>
186 if (
const BucketT *Bucket = doFind(Val))
187 return makeConstIterator(
195 ValueT
lookup(const_arg_type_t<KeyT> Val)
const {
196 if (
const BucketT *Bucket = doFind(Val))
197 return Bucket->getSecond();
203 const ValueT &
at(const_arg_type_t<KeyT> Val)
const {
204 auto Iter = this->
find(std::move(Val));
205 assert(Iter != this->
end() &&
"DenseMap::at failed due to a missing key");
212 std::pair<iterator, bool>
insert(
const std::pair<KeyT, ValueT> &KV) {
219 std::pair<iterator, bool>
insert(std::pair<KeyT, ValueT> &&KV) {
220 return try_emplace(std::move(KV.first), std::move(KV.second));
226 template <
typename... Ts>
229 if (LookupBucketFor(Key, TheBucket))
230 return std::make_pair(makeIterator(TheBucket,
239 InsertIntoBucket(TheBucket, std::move(Key), std::forward<Ts>(Args)...);
240 return std::make_pair(makeIterator(TheBucket,
251 template <
typename... Ts>
252 std::pair<iterator, bool>
try_emplace(
const KeyT &Key, Ts &&...Args) {
254 if (LookupBucketFor(Key, TheBucket))
255 return std::make_pair(makeIterator(TheBucket,
263 TheBucket = InsertIntoBucket(TheBucket, Key, std::forward<Ts>(Args)...);
264 return std::make_pair(makeIterator(TheBucket,
277 template <
typename LookupKeyT>
278 std::pair<iterator, bool>
insert_as(std::pair<KeyT, ValueT> &&KV,
279 const LookupKeyT &Val) {
281 if (LookupBucketFor(Val, TheBucket))
282 return std::make_pair(makeIterator(TheBucket,
290 TheBucket = InsertIntoBucketWithLookup(TheBucket, std::move(KV.first),
291 std::move(KV.second), Val);
292 return std::make_pair(makeIterator(TheBucket,
301 template <
typename InputIt>
void insert(InputIt I, InputIt E) {
306 template <
typename V>
310 Ret.first->second = std::forward<V>(Val);
314 template <
typename V>
316 auto Ret =
try_emplace(std::move(Key), std::forward<V>(Val));
318 Ret.first->second = std::forward<V>(Val);
323 BucketT *TheBucket = doFind(Val);
327 TheBucket->getSecond().~ValueT();
329 decrementNumEntries();
330 incrementNumTombstones();
334 BucketT *TheBucket = &*I;
335 TheBucket->getSecond().~ValueT();
337 decrementNumEntries();
338 incrementNumTombstones();
343 if (LookupBucketFor(Key, TheBucket))
344 return TheBucket->second;
346 return InsertIntoBucket(TheBucket, Key)->second;
351 if (LookupBucketFor(Key, TheBucket))
352 return TheBucket->second;
354 return InsertIntoBucket(TheBucket, std::move(Key))->second;
361 return Ptr >= getBuckets() && Ptr < getBucketsEnd();
373 if (getNumBuckets() == 0)
377 for (BucketT *P = getBuckets(), *E = getBucketsEnd(); P != E; ++P) {
378 if (!KeyInfoT::isEqual(P->getFirst(), EmptyKey) &&
379 !KeyInfoT::isEqual(P->getFirst(), TombstoneKey))
380 P->getSecond().~ValueT();
381 P->getFirst().~KeyT();
389 assert((getNumBuckets() & (getNumBuckets() - 1)) == 0 &&
390 "# initial buckets must be a power of two!");
392 for (BucketT *B = getBuckets(), *E = getBucketsEnd(); B != E; ++B)
393 ::new (&B->getFirst()) KeyT(EmptyKey);
404 return static_cast<unsigned>(
NextPowerOf2(NumEntries * 4 / 3 + 1));
413 for (BucketT *B = OldBucketsBegin, *E = OldBucketsEnd; B != E; ++B) {
414 if (!KeyInfoT::isEqual(B->getFirst(), EmptyKey) &&
415 !KeyInfoT::isEqual(B->getFirst(), TombstoneKey)) {
418 bool FoundVal = LookupBucketFor(B->getFirst(), DestBucket);
420 assert(!FoundVal &&
"Key already in new map?");
421 DestBucket->getFirst() = std::move(B->getFirst());
422 ::new (&DestBucket->getSecond()) ValueT(std::move(B->getSecond()));
423 incrementNumEntries();
426 B->getSecond().~ValueT();
428 B->getFirst().~KeyT();
432 template <
typename OtherBaseT>
435 assert(&other !=
this);
436 assert(getNumBuckets() == other.getNumBuckets());
438 setNumEntries(other.getNumEntries());
439 setNumTombstones(other.getNumTombstones());
441 BucketT *Buckets = getBuckets();
442 const BucketT *OtherBuckets = other.getBuckets();
443 const size_t NumBuckets = getNumBuckets();
444 if constexpr (std::is_trivially_copyable_v<KeyT> &&
445 std::is_trivially_copyable_v<ValueT>) {
446 memcpy(
reinterpret_cast<void *
>(Buckets), OtherBuckets,
447 NumBuckets *
sizeof(BucketT));
451 for (
size_t I = 0; I < NumBuckets; ++I) {
452 ::new (&Buckets[I].getFirst()) KeyT(OtherBuckets[I].getFirst());
453 if (!KeyInfoT::isEqual(Buckets[I].getFirst(), EmptyKey) &&
454 !KeyInfoT::isEqual(Buckets[I].getFirst(), TombstoneKey))
455 ::new (&Buckets[I].getSecond()) ValueT(OtherBuckets[I].getSecond());
461 return KeyInfoT::getHashValue(Val);
464 template <
typename LookupKeyT>
466 return KeyInfoT::getHashValue(Val);
470 static_assert(std::is_base_of_v<DenseMapBase, DerivedT>,
471 "Must pass the derived type to this template!");
472 return KeyInfoT::getEmptyKey();
479 bool NoAdvance =
false) {
481 BucketT *B = P == getBucketsEnd() ? getBuckets() : P + 1;
482 return iterator(B, E, Epoch, NoAdvance);
484 return iterator(P, E, Epoch, NoAdvance);
487 const_iterator makeConstIterator(
const BucketT *P,
const BucketT *E,
489 const bool NoAdvance =
false)
const {
491 const BucketT *B = P == getBucketsEnd() ? getBuckets() : P + 1;
497 unsigned getNumEntries()
const {
498 return static_cast<const DerivedT *
>(
this)->getNumEntries();
501 void setNumEntries(
unsigned Num) {
502 static_cast<DerivedT *
>(
this)->setNumEntries(Num);
505 void incrementNumEntries() { setNumEntries(getNumEntries() + 1); }
507 void decrementNumEntries() { setNumEntries(getNumEntries() - 1); }
509 unsigned getNumTombstones()
const {
510 return static_cast<const DerivedT *
>(
this)->getNumTombstones();
513 void setNumTombstones(
unsigned Num) {
514 static_cast<DerivedT *
>(
this)->setNumTombstones(Num);
517 void incrementNumTombstones() { setNumTombstones(getNumTombstones() + 1); }
519 void decrementNumTombstones() { setNumTombstones(getNumTombstones() - 1); }
521 const BucketT *getBuckets()
const {
522 return static_cast<const DerivedT *
>(
this)->getBuckets();
525 BucketT *getBuckets() {
return static_cast<DerivedT *
>(
this)->getBuckets(); }
527 unsigned getNumBuckets()
const {
528 return static_cast<const DerivedT *
>(
this)->getNumBuckets();
531 BucketT *getBucketsEnd() {
return getBuckets() + getNumBuckets(); }
533 const BucketT *getBucketsEnd()
const {
534 return getBuckets() + getNumBuckets();
537 void grow(
unsigned AtLeast) {
static_cast<DerivedT *
>(
this)->grow(AtLeast); }
539 void shrink_and_clear() {
static_cast<DerivedT *
>(
this)->shrink_and_clear(); }
541 template <
typename KeyArg,
typename... ValueArgs>
542 BucketT *InsertIntoBucket(BucketT *TheBucket, KeyArg &&Key,
543 ValueArgs &&...Values) {
544 TheBucket = InsertIntoBucketImpl(Key, TheBucket);
546 TheBucket->getFirst() = std::forward<KeyArg>(Key);
547 ::new (&TheBucket->getSecond()) ValueT(
std::forward<ValueArgs>(Values)...);
551 template <typename LookupKeyT>
552 BucketT *InsertIntoBucketWithLookup(BucketT *TheBucket, KeyT &&Key,
553 ValueT &&Value, LookupKeyT &Lookup) {
554 TheBucket = InsertIntoBucketImpl(Lookup, TheBucket);
556 TheBucket->getFirst() = std::move(Key);
557 ::new (&TheBucket->getSecond()) ValueT(
std::move(Value));
561 template <typename LookupKeyT>
562 BucketT *InsertIntoBucketImpl(const LookupKeyT &Lookup, BucketT *TheBucket) {
574 unsigned NewNumEntries = getNumEntries() + 1;
575 unsigned NumBuckets = getNumBuckets();
577 this->grow(NumBuckets * 2);
578 LookupBucketFor(Lookup, TheBucket);
579 NumBuckets = getNumBuckets();
581 (NewNumEntries + getNumTombstones()) <=
583 this->grow(NumBuckets);
584 LookupBucketFor(Lookup, TheBucket);
590 incrementNumEntries();
594 if (!KeyInfoT::isEqual(TheBucket->getFirst(), EmptyKey))
595 decrementNumTombstones();
600 template <
typename LookupKeyT> BucketT *doFind(
const LookupKeyT &Val) {
601 BucketT *BucketsPtr = getBuckets();
602 const unsigned NumBuckets = getNumBuckets();
607 unsigned BucketNo =
getHashValue(Val) & (NumBuckets - 1);
608 unsigned ProbeAmt = 1;
610 BucketT *Bucket = BucketsPtr + BucketNo;
611 if (
LLVM_LIKELY(KeyInfoT::isEqual(Val, Bucket->getFirst())))
613 if (
LLVM_LIKELY(KeyInfoT::isEqual(Bucket->getFirst(), EmptyKey)))
618 BucketNo += ProbeAmt++;
619 BucketNo &= NumBuckets - 1;
623 template <
typename LookupKeyT>
624 const BucketT *doFind(
const LookupKeyT &Val)
const {
632 template <
typename LookupKeyT>
633 bool LookupBucketFor(
const LookupKeyT &Val, BucketT *&FoundBucket) {
634 BucketT *BucketsPtr = getBuckets();
635 const unsigned NumBuckets = getNumBuckets();
637 if (NumBuckets == 0) {
638 FoundBucket =
nullptr;
643 BucketT *FoundTombstone =
nullptr;
646 assert(!KeyInfoT::isEqual(Val, EmptyKey) &&
647 !KeyInfoT::isEqual(Val, TombstoneKey) &&
648 "Empty/Tombstone value shouldn't be inserted into map!");
650 unsigned BucketNo =
getHashValue(Val) & (NumBuckets - 1);
651 unsigned ProbeAmt = 1;
653 BucketT *ThisBucket = BucketsPtr + BucketNo;
655 if (
LLVM_LIKELY(KeyInfoT::isEqual(Val, ThisBucket->getFirst()))) {
656 FoundBucket = ThisBucket;
662 if (
LLVM_LIKELY(KeyInfoT::isEqual(ThisBucket->getFirst(), EmptyKey))) {
665 FoundBucket = FoundTombstone ? FoundTombstone : ThisBucket;
671 if (KeyInfoT::isEqual(ThisBucket->getFirst(), TombstoneKey) &&
673 FoundTombstone = ThisBucket;
677 BucketNo += ProbeAmt++;
678 BucketNo &= (NumBuckets - 1);
696template <
typename DerivedT,
typename KeyT,
typename ValueT,
typename KeyInfoT,
704 for (
auto &KV : LHS) {
705 auto I = RHS.
find(KV.first);
706 if (I == RHS.
end() || I->second != KV.second)
716template <
typename DerivedT,
typename KeyT,
typename ValueT,
typename KeyInfoT,
721 return !(LHS == RHS);
724template <
typename KeyT,
typename ValueT,
725 typename KeyInfoT = DenseMapInfo<KeyT>,
728 KeyT, ValueT, KeyInfoT, BucketT> {
737 unsigned NumTombstones;
743 explicit DenseMap(
unsigned InitialReserve = 0) { init(InitialReserve); }
755 template <
typename InputIt>
DenseMap(
const InputIt &I,
const InputIt &E) {
756 init(std::distance(I, E));
760 DenseMap(std::initializer_list<typename BaseT::value_type> Vals) {
762 this->insert(Vals.begin(), Vals.end());
771 this->incrementEpoch();
775 std::swap(NumTombstones, RHS.NumTombstones);
796 if (allocateBuckets(other.NumBuckets)) {
797 this->BaseT::copyFrom(other);
804 void init(
unsigned InitNumEntries) {
805 auto InitBuckets = BaseT::getMinBucketToReserveForEntries(InitNumEntries);
806 if (allocateBuckets(InitBuckets)) {
807 this->BaseT::initEmpty();
815 unsigned OldNumBuckets = NumBuckets;
816 BucketT *OldBuckets = Buckets;
818 allocateBuckets(std::max<unsigned>(
822 this->BaseT::initEmpty();
826 this->moveFromOldBuckets(OldBuckets, OldBuckets + OldNumBuckets);
834 unsigned OldNumBuckets = NumBuckets;
835 unsigned OldNumEntries = NumEntries;
839 unsigned NewNumBuckets = 0;
841 NewNumBuckets = (std::max)(64, 1 << (
Log2_32_Ceil(OldNumEntries) + 1));
842 if (NewNumBuckets == NumBuckets) {
843 this->BaseT::initEmpty();
853 unsigned getNumEntries()
const {
return NumEntries; }
855 void setNumEntries(
unsigned Num) { NumEntries = Num; }
857 unsigned getNumTombstones()
const {
return NumTombstones; }
859 void setNumTombstones(
unsigned Num) { NumTombstones = Num; }
861 BucketT *getBuckets()
const {
return Buckets; }
863 unsigned getNumBuckets()
const {
return NumBuckets; }
865 bool allocateBuckets(
unsigned Num) {
867 if (NumBuckets == 0) {
872 Buckets =
static_cast<BucketT *
>(
878template <
typename KeyT,
typename ValueT,
unsigned InlineBuckets = 4,
879 typename KeyInfoT = DenseMapInfo<KeyT>,
883 SmallDenseMap<KeyT, ValueT, InlineBuckets, KeyInfoT, BucketT>, KeyT,
884 ValueT, KeyInfoT, BucketT> {
892 "InlineBuckets must be a power of 2.");
895 unsigned NumEntries : 31;
896 unsigned NumTombstones;
909 if (NumInitBuckets > InlineBuckets)
910 NumInitBuckets = std::bit_ceil(NumInitBuckets);
911 init(NumInitBuckets);
924 template <
typename InputIt>
939 unsigned TmpNumEntries = RHS.NumEntries;
940 RHS.NumEntries = NumEntries;
941 NumEntries = TmpNumEntries;
942 std::swap(NumTombstones, RHS.NumTombstones);
944 const KeyT EmptyKey = this->getEmptyKey();
945 const KeyT TombstoneKey = this->getTombstoneKey();
946 if (Small && RHS.Small) {
951 for (
unsigned i = 0, e = InlineBuckets; i != e; ++i) {
952 BucketT *LHSB = &getInlineBuckets()[i],
953 *RHSB = &RHS.getInlineBuckets()[i];
954 bool hasLHSValue = (!KeyInfoT::isEqual(LHSB->getFirst(), EmptyKey) &&
955 !KeyInfoT::isEqual(LHSB->getFirst(), TombstoneKey));
956 bool hasRHSValue = (!KeyInfoT::isEqual(RHSB->getFirst(), EmptyKey) &&
957 !KeyInfoT::isEqual(RHSB->getFirst(), TombstoneKey));
958 if (hasLHSValue && hasRHSValue) {
964 std::swap(LHSB->getFirst(), RHSB->getFirst());
966 ::new (&RHSB->getSecond()) ValueT(std::move(LHSB->getSecond()));
967 LHSB->getSecond().~ValueT();
968 }
else if (hasRHSValue) {
969 ::new (&LHSB->getSecond()) ValueT(std::move(RHSB->getSecond()));
970 RHSB->getSecond().~ValueT();
975 if (!Small && !RHS.Small) {
976 std::swap(getLargeRep()->Buckets, RHS.getLargeRep()->Buckets);
977 std::swap(getLargeRep()->NumBuckets, RHS.getLargeRep()->NumBuckets);
985 LargeRep TmpRep = std::move(*LargeSide.getLargeRep());
986 LargeSide.getLargeRep()->~LargeRep();
987 LargeSide.Small =
true;
992 for (
unsigned i = 0, e = InlineBuckets; i != e; ++i) {
993 BucketT *NewB = &LargeSide.getInlineBuckets()[i],
994 *OldB = &SmallSide.getInlineBuckets()[i];
995 ::new (&NewB->getFirst()) KeyT(std::move(OldB->getFirst()));
996 OldB->getFirst().~KeyT();
997 if (!KeyInfoT::isEqual(NewB->getFirst(), EmptyKey) &&
998 !KeyInfoT::isEqual(NewB->getFirst(), TombstoneKey)) {
999 ::new (&NewB->getSecond()) ValueT(std::move(OldB->getSecond()));
1000 OldB->getSecond().~ValueT();
1006 SmallSide.Small =
false;
1007 new (SmallSide.getLargeRep()) LargeRep(std::move(TmpRep));
1018 deallocateBuckets();
1026 deallocateBuckets();
1028 if (other.getNumBuckets() > InlineBuckets) {
1030 new (getLargeRep()) LargeRep(allocateBuckets(other.getNumBuckets()));
1032 this->BaseT::copyFrom(other);
1037 if (InitBuckets > InlineBuckets) {
1039 new (getLargeRep()) LargeRep(allocateBuckets(InitBuckets));
1041 this->BaseT::initEmpty();
1045 if (AtLeast > InlineBuckets)
1046 AtLeast = std::max<unsigned>(64,
NextPowerOf2(AtLeast - 1));
1051 BucketT *TmpBegin =
reinterpret_cast<BucketT *
>(&TmpStorage);
1052 BucketT *TmpEnd = TmpBegin;
1056 const KeyT EmptyKey = this->getEmptyKey();
1057 const KeyT TombstoneKey = this->getTombstoneKey();
1058 for (BucketT *P = getBuckets(), *E = P + InlineBuckets; P != E; ++P) {
1059 if (!KeyInfoT::isEqual(P->getFirst(), EmptyKey) &&
1060 !KeyInfoT::isEqual(P->getFirst(), TombstoneKey)) {
1061 assert(
size_t(TmpEnd - TmpBegin) < InlineBuckets &&
1062 "Too many inline buckets!");
1063 ::new (&TmpEnd->getFirst()) KeyT(std::move(P->getFirst()));
1064 ::new (&TmpEnd->getSecond()) ValueT(std::move(P->getSecond()));
1066 P->getSecond().~ValueT();
1068 P->getFirst().~KeyT();
1074 if (AtLeast > InlineBuckets) {
1076 new (getLargeRep()) LargeRep(allocateBuckets(AtLeast));
1078 this->moveFromOldBuckets(TmpBegin, TmpEnd);
1082 LargeRep OldRep = std::move(*getLargeRep());
1083 getLargeRep()->~LargeRep();
1084 if (AtLeast <= InlineBuckets) {
1087 new (getLargeRep()) LargeRep(allocateBuckets(AtLeast));
1090 this->moveFromOldBuckets(OldRep.Buckets,
1091 OldRep.Buckets + OldRep.NumBuckets);
1099 unsigned OldSize = this->size();
1103 unsigned NewNumBuckets = 0;
1106 if (NewNumBuckets > InlineBuckets && NewNumBuckets < 64u)
1109 if ((Small && NewNumBuckets <= InlineBuckets) ||
1110 (!Small && NewNumBuckets == getLargeRep()->NumBuckets)) {
1111 this->BaseT::initEmpty();
1115 deallocateBuckets();
1116 init(NewNumBuckets);
1120 unsigned getNumEntries()
const {
return NumEntries; }
1122 void setNumEntries(
unsigned Num) {
1124 assert(Num < (1U << 31) &&
"Cannot support more than 1<<31 entries");
1128 unsigned getNumTombstones()
const {
return NumTombstones; }
1130 void setNumTombstones(
unsigned Num) { NumTombstones = Num; }
1132 const BucketT *getInlineBuckets()
const {
1137 return reinterpret_cast<const BucketT *
>(&storage);
1140 BucketT *getInlineBuckets() {
1141 return const_cast<BucketT *
>(
1142 const_cast<const SmallDenseMap *
>(
this)->getInlineBuckets());
1145 const LargeRep *getLargeRep()
const {
1148 return reinterpret_cast<const LargeRep *
>(&storage);
1151 LargeRep *getLargeRep() {
1152 return const_cast<LargeRep *
>(
1153 const_cast<const SmallDenseMap *
>(
this)->getLargeRep());
1156 const BucketT *getBuckets()
const {
1157 return Small ? getInlineBuckets() : getLargeRep()->Buckets;
1160 BucketT *getBuckets() {
1161 return const_cast<BucketT *
>(
1162 const_cast<const SmallDenseMap *
>(
this)->getBuckets());
1165 unsigned getNumBuckets()
const {
1166 return Small ? InlineBuckets : getLargeRep()->NumBuckets;
1169 void deallocateBuckets() {
1174 sizeof(BucketT) * getLargeRep()->NumBuckets,
1176 getLargeRep()->~LargeRep();
1179 LargeRep allocateBuckets(
unsigned Num) {
1180 assert(Num > InlineBuckets &&
"Must allocate more buckets than are inline");
1182 sizeof(BucketT) * Num,
alignof(BucketT))),
1188template <
typename KeyT,
typename ValueT,
typename KeyInfoT,
typename Bucket,
1196 using value_type = std::conditional_t<IsConst, const Bucket, Bucket>;
1209 bool NoAdvance =
false)
1211 assert(isHandleInSync() &&
"invalid construction!");
1215 if (shouldReverseIterate<KeyT>()) {
1216 RetreatPastEmptyBuckets();
1219 AdvancePastEmptyBuckets();
1225 template <
bool IsConstSrc,
1226 typename = std::enable_if_t<!IsConstSrc && IsConst>>
1232 assert(isHandleInSync() &&
"invalid iterator access!");
1233 assert(Ptr != End &&
"dereferencing end() iterator");
1234 if (shouldReverseIterate<KeyT>())
1239 assert(isHandleInSync() &&
"invalid iterator access!");
1240 assert(Ptr != End &&
"dereferencing end() iterator");
1241 if (shouldReverseIterate<KeyT>())
1248 assert((!LHS.Ptr || LHS.isHandleInSync()) &&
"handle not in sync!");
1249 assert((!RHS.Ptr || RHS.isHandleInSync()) &&
"handle not in sync!");
1250 assert(LHS.getEpochAddress() == RHS.getEpochAddress() &&
1251 "comparing incomparable iterators!");
1252 return LHS.Ptr == RHS.Ptr;
1257 return !(LHS == RHS);
1261 assert(isHandleInSync() &&
"invalid iterator access!");
1262 assert(Ptr != End &&
"incrementing end() iterator");
1263 if (shouldReverseIterate<KeyT>()) {
1265 RetreatPastEmptyBuckets();
1269 AdvancePastEmptyBuckets();
1273 assert(isHandleInSync() &&
"invalid iterator access!");
1280 void AdvancePastEmptyBuckets() {
1282 const KeyT Empty = KeyInfoT::getEmptyKey();
1283 const KeyT Tombstone = KeyInfoT::getTombstoneKey();
1285 while (Ptr != End && (KeyInfoT::isEqual(Ptr->getFirst(), Empty) ||
1286 KeyInfoT::isEqual(Ptr->getFirst(), Tombstone)))
1290 void RetreatPastEmptyBuckets() {
1292 const KeyT Empty = KeyInfoT::getEmptyKey();
1293 const KeyT Tombstone = KeyInfoT::getTombstoneKey();
1295 while (Ptr != End && (KeyInfoT::isEqual(Ptr[-1].getFirst(), Empty) ||
1296 KeyInfoT::isEqual(Ptr[-1].getFirst(), Tombstone)))
1301template <
typename KeyT,
typename ValueT,
typename KeyInfoT>
#define LLVM_UNLIKELY(EXPR)
Definition Compiler.h:332
#define LLVM_LIKELY(EXPR)
Definition Compiler.h:331
This file defines DenseMapInfo traits for DenseMap.
This file defines the DebugEpochBase and DebugEpochBase::HandleBase classes.
This file defines counterparts of C library allocation functions defined in the namespace 'std'.
A base class for iterator classes ("handles") that wish to poll for iterator invalidating modificatio...
Definition EpochTracker.h:58
A base class for data structure classes wishing to make iterators ("handles") pointing into themselve...
Definition EpochTracker.h:36
void incrementEpoch()
Calling incrementEpoch invalidates all handles pointing into the calling instance.
Definition EpochTracker.h:44
void copyFrom(const DenseMapBase< OtherBaseT, KeyT, ValueT, KeyInfoT, BucketT > &other)
Definition DenseMap.h:433
bool isPointerIntoBucketsArray(const void *Ptr) const
isPointerIntoBucketsArray - Return true if the specified pointer points somewhere into the DenseMap's...
Definition DenseMap.h:360
std::pair< iterator, bool > try_emplace(KeyT &&Key, Ts &&...Args)
Definition DenseMap.h:227
const_iterator find_as(const LookupKeyT &Val) const
Definition DenseMap.h:185
DenseMapIterator< KeyT, ValueT, KeyInfoT, BucketT > iterator
Definition DenseMap.h:72
iterator end()
Definition DenseMap.h:85
std::pair< iterator, bool > insert(std::pair< KeyT, ValueT > &&KV)
Definition DenseMap.h:219
static const KeyT getEmptyKey()
Definition DenseMap.h:469
bool erase(const KeyT &Val)
Definition DenseMap.h:322
size_type count(const_arg_type_t< KeyT > Val) const
Return 1 if the specified key is in the map, 0 otherwise.
Definition DenseMap.h:153
ValueT mapped_type
Definition DenseMap.h:69
void erase(iterator I)
Definition DenseMap.h:333
void initEmpty()
Definition DenseMap.h:385
unsigned size() const
Definition DenseMap.h:100
static unsigned getHashValue(const KeyT &Val)
Definition DenseMap.h:460
void reserve(size_type NumEntries)
Grow the densemap so that it can contain at least NumEntries items before resizing again.
Definition DenseMap.h:104
void insert(InputIt I, InputIt E)
insert - Range insertion of pairs.
Definition DenseMap.h:301
BucketT value_type
Definition DenseMap.h:70
const void * getPointerIntoBucketsArray() const
getPointerIntoBucketsArray() - Return an opaque pointer into the buckets array.
Definition DenseMap.h:367
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Definition DenseMap.h:212
const_iterator end() const
Definition DenseMap.h:95
bool contains(const_arg_type_t< KeyT > Val) const
Return true if the specified key is in the map, false otherwise.
Definition DenseMap.h:148
std::pair< iterator, bool > try_emplace(const KeyT &Key, Ts &&...Args)
Definition DenseMap.h:252
KeyT key_type
Definition DenseMap.h:68
DenseMapIterator< KeyT, ValueT, KeyInfoT, BucketT, true > const_iterator
Definition DenseMap.h:73
void clear()
Definition DenseMap.h:111
const ValueT & at(const_arg_type_t< KeyT > Val) const
at - Return the entry for the specified key, or abort if no such entry exists.
Definition DenseMap.h:203
void destroyAll()
Definition DenseMap.h:372
std::pair< iterator, bool > insert_or_assign(KeyT &&Key, V &&Val)
Definition DenseMap.h:315
iterator begin()
Definition DenseMap.h:76
ValueT & operator[](const KeyT &Key)
Definition DenseMap.h:341
ValueT lookup(const_arg_type_t< KeyT > Val) const
lookup - Return the entry for the specified key, or a default constructed value if no such entry exis...
Definition DenseMap.h:195
static unsigned getHashValue(const LookupKeyT &Val)
Definition DenseMap.h:465
unsigned size_type
Definition DenseMap.h:67
std::pair< iterator, bool > insert_as(std::pair< KeyT, ValueT > &&KV, const LookupKeyT &Val)
Alternate version of insert() which allows a different, and possibly less expensive,...
Definition DenseMap.h:278
unsigned getMinBucketToReserveForEntries(unsigned NumEntries)
Returns the number of buckets to allocate to ensure that the DenseMap can accommodate NumEntries with...
Definition DenseMap.h:398
void moveFromOldBuckets(BucketT *OldBucketsBegin, BucketT *OldBucketsEnd)
Definition DenseMap.h:407
const_iterator find(const_arg_type_t< KeyT > Val) const
Definition DenseMap.h:164
static const KeyT getTombstoneKey()
Definition DenseMap.h:475
std::pair< iterator, bool > insert_or_assign(const KeyT &Key, V &&Val)
Definition DenseMap.h:307
ValueT & operator[](KeyT &&Key)
Definition DenseMap.h:349
iterator find(const_arg_type_t< KeyT > Val)
Definition DenseMap.h:157
iterator find_as(const LookupKeyT &Val)
Alternate version of find() which allows a different, and possibly less expensive,...
Definition DenseMap.h:177
size_t getMemorySize() const
Return the approximate size (in bytes) of the actual map.
Definition DenseMap.h:687
const_iterator begin() const
Definition DenseMap.h:88
bool empty() const
Definition DenseMap.h:99
Definition DenseMap.h:728
void init(unsigned InitNumEntries)
Definition DenseMap.h:804
void copyFrom(const DenseMap &other)
Definition DenseMap.h:793
DenseMap & operator=(DenseMap &&other)
Definition DenseMap.h:785
DenseMap & operator=(const DenseMap &other)
Definition DenseMap.h:779
DenseMap(const DenseMap &other)
Definition DenseMap.h:745
DenseMap(std::initializer_list< typename BaseT::value_type > Vals)
Definition DenseMap.h:760
void grow(unsigned AtLeast)
Definition DenseMap.h:814
void shrink_and_clear()
Definition DenseMap.h:833
DenseMap(const InputIt &I, const InputIt &E)
Definition DenseMap.h:755
void swap(DenseMap &RHS)
Definition DenseMap.h:770
DenseMap(DenseMap &&other)
Definition DenseMap.h:750
DenseMap(unsigned InitialReserve=0)
Create a DenseMap with an optional InitialReserve that guarantee that this number of elements can be ...
Definition DenseMap.h:743
~DenseMap()
Definition DenseMap.h:765
Definition DenseMap.h:1190
DenseMapIterator & operator++()
Definition DenseMap.h:1260
friend bool operator!=(const DenseMapIterator &LHS, const DenseMapIterator &RHS)
Definition DenseMap.h:1255
DenseMapIterator(const DenseMapIterator< KeyT, ValueT, KeyInfoT, Bucket, IsConstSrc > &I)
Definition DenseMap.h:1227
reference operator*() const
Definition DenseMap.h:1231
pointer operator->() const
Definition DenseMap.h:1238
DenseMapIterator()=default
DenseMapIterator(pointer Pos, pointer E, const DebugEpochBase &Epoch, bool NoAdvance=false)
Definition DenseMap.h:1208
std::forward_iterator_tag iterator_category
Definition DenseMap.h:1199
ptrdiff_t difference_type
Definition DenseMap.h:1195
std::conditional_t< IsConst, const Bucket, Bucket > value_type
Definition DenseMap.h:1196
value_type * pointer
Definition DenseMap.h:1197
DenseMapIterator operator++(int)
Definition DenseMap.h:1272
friend bool operator==(const DenseMapIterator &LHS, const DenseMapIterator &RHS)
Definition DenseMap.h:1246
value_type & reference
Definition DenseMap.h:1198
Definition DenseMap.h:884
~SmallDenseMap()
Definition DenseMap.h:933
SmallDenseMap(unsigned NumInitBuckets=0)
Definition DenseMap.h:908
void swap(SmallDenseMap &RHS)
Definition DenseMap.h:938
void copyFrom(const SmallDenseMap &other)
Definition DenseMap.h:1024
void init(unsigned InitBuckets)
Definition DenseMap.h:1035
SmallDenseMap & operator=(SmallDenseMap &&other)
Definition DenseMap.h:1016
SmallDenseMap(const SmallDenseMap &other)
Definition DenseMap.h:914
SmallDenseMap(SmallDenseMap &&other)
Definition DenseMap.h:919
void shrink_and_clear()
Definition DenseMap.h:1098
void grow(unsigned AtLeast)
Definition DenseMap.h:1044
SmallDenseMap(const InputIt &I, const InputIt &E)
Definition DenseMap.h:925
SmallDenseMap(std::initializer_list< typename BaseT::value_type > Vals)
Definition DenseMap.h:930
SmallDenseMap & operator=(const SmallDenseMap &other)
Definition DenseMap.h:1010
detail namespace with internal helper functions
Definition input_adapters.h:32
Definition PointerIntPair.h:280
WPI_BASIC_JSON_TPL_DECLARATION void swap(wpi::WPI_BASIC_JSON_TPL &j1, wpi::WPI_BASIC_JSON_TPL &j2) noexcept(//NOLINT(readability-inconsistent-declaration-parameter-name, cert-dcl58-cpp) is_nothrow_move_constructible< wpi::WPI_BASIC_JSON_TPL >::value &&//NOLINT(misc-redundant-expression, cppcoreguidelines-noexcept-swap, performance-noexcept-swap) is_nothrow_move_assignable< wpi::WPI_BASIC_JSON_TPL >::value)
exchanges the values of two JSON objects
Definition json.h:5258
Definition ntcore_cpp.h:26
unsigned Log2_32_Ceil(uint32_t Value)
Return the ceil log base 2 of the specified value, 32 if the value is zero.
Definition MathExtras.h:327
void deallocate_buffer(void *Ptr, size_t Size, size_t Alignment)
Deallocate a buffer of memory with the given size and alignment.
size_t capacity_in_bytes(const DenseMap< KeyT, ValueT, KeyInfoT > &X)
Definition DenseMap.h:1302
bool operator==(const DenseMapBase< DerivedT, KeyT, ValueT, KeyInfoT, BucketT > &LHS, const DenseMapBase< DerivedT, KeyT, ValueT, KeyInfoT, BucketT > &RHS)
Equality comparison for DenseMap.
Definition DenseMap.h:698
constexpr bool isPowerOf2_64(uint64_t Value)
Return true if the argument is a power of two > 0 (64 bit edition.)
Definition MathExtras.h:270
constexpr uint64_t NextPowerOf2(uint64_t A)
Returns the next power of two (in 64-bits) that is strictly greater than A.
Definition MathExtras.h:356
LLVM_ATTRIBUTE_RETURNS_NONNULL LLVM_ATTRIBUTE_RETURNS_NOALIAS void * allocate_buffer(size_t Size, size_t Alignment)
Allocate a buffer of memory with the given size and alignment.
bool operator!=(const DenseMapBase< DerivedT, KeyT, ValueT, KeyInfoT, BucketT > &LHS, const DenseMapBase< DerivedT, KeyT, ValueT, KeyInfoT, BucketT > &RHS)
Inequality comparison for DenseMap.
Definition DenseMap.h:718
bool shouldReverseIterate()
Definition ReverseIteration.h:9
A suitably aligned and sized character array member which can hold elements of any type.
Definition AlignOf.h:23
const T & type
Definition type_traits.h:64
KeyT & getFirst()
Definition DenseMap.h:46
ValueT & getSecond()
Definition DenseMap.h:48
const KeyT & getFirst() const
Definition DenseMap.h:47
const ValueT & getSecond() const
Definition DenseMap.h:49