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)
return;
117 if (getNumEntries() * 4 < getNumBuckets() && getNumBuckets() > 64) {
123 if (std::is_trivially_destructible<ValueT>::value) {
125 for (BucketT *P = getBuckets(), *E = getBucketsEnd(); P != E; ++P)
126 P->getFirst() = EmptyKey;
128 [[maybe_unused]]
unsigned NumEntries = getNumEntries();
129 for (BucketT *P = getBuckets(), *E = getBucketsEnd(); P != E; ++P) {
130 if (!KeyInfoT::isEqual(P->getFirst(), EmptyKey)) {
131 if (!KeyInfoT::isEqual(P->getFirst(), TombstoneKey)) {
132 P->getSecond().~ValueT();
135 P->getFirst() = EmptyKey;
138 assert(NumEntries == 0 &&
"Node count imbalance!");
147 const BucketT *TheBucket;
148 return LookupBucketFor(Val, TheBucket);
158 if (LookupBucketFor(Val, TheBucket))
159 return makeIterator(TheBucket,
166 const BucketT *TheBucket;
167 if (LookupBucketFor(Val, TheBucket))
168 return makeConstIterator(TheBucket,
180 template<
class LookupKeyT>
183 if (LookupBucketFor(Val, TheBucket))
184 return makeIterator(TheBucket,
190 template<
class LookupKeyT>
192 const BucketT *TheBucket;
193 if (LookupBucketFor(Val, TheBucket))
194 return makeConstIterator(TheBucket,
203 ValueT
lookup(const_arg_type_t<KeyT> Val)
const {
204 const BucketT *TheBucket;
205 if (LookupBucketFor(Val, TheBucket))
206 return TheBucket->getSecond();
212 const ValueT &
at(const_arg_type_t<KeyT> Val)
const {
213 auto Iter = this->
find(std::move(Val));
214 assert(Iter != this->
end() &&
"DenseMap::at failed due to a missing key");
221 std::pair<iterator, bool>
insert(
const std::pair<KeyT, ValueT> &KV) {
228 std::pair<iterator, bool>
insert(std::pair<KeyT, ValueT> &&KV) {
229 return try_emplace(std::move(KV.first), std::move(KV.second));
235 template <
typename... Ts>
236 std::pair<iterator, bool>
try_emplace(KeyT &&Key, Ts &&... Args) {
238 if (LookupBucketFor(Key, TheBucket))
239 return std::make_pair(makeIterator(TheBucket,
248 InsertIntoBucket(TheBucket, std::move(Key), std::forward<Ts>(Args)...);
249 return std::make_pair(makeIterator(TheBucket,
260 template <
typename... Ts>
261 std::pair<iterator, bool>
try_emplace(
const KeyT &Key, Ts &&... Args) {
263 if (LookupBucketFor(Key, TheBucket))
264 return std::make_pair(makeIterator(TheBucket,
272 TheBucket = InsertIntoBucket(TheBucket, Key, std::forward<Ts>(Args)...);
273 return std::make_pair(makeIterator(TheBucket,
286 template <
typename LookupKeyT>
287 std::pair<iterator, bool>
insert_as(std::pair<KeyT, ValueT> &&KV,
288 const LookupKeyT &Val) {
290 if (LookupBucketFor(Val, TheBucket))
291 return std::make_pair(makeIterator(TheBucket,
299 TheBucket = InsertIntoBucketWithLookup(TheBucket, std::move(KV.first),
300 std::move(KV.second), Val);
301 return std::make_pair(makeIterator(TheBucket,
310 template<
typename InputIt>
316 template <
typename V>
320 Ret.first->second = std::forward<V>(Val);
324 template <
typename V>
326 auto Ret =
try_emplace(std::move(Key), std::forward<V>(Val));
328 Ret.first->second = std::forward<V>(Val);
348 if (!LookupBucketFor(Val, TheBucket))
351 TheBucket->getSecond().~ValueT();
353 decrementNumEntries();
354 incrementNumTombstones();
358 BucketT *TheBucket = &*I;
359 TheBucket->getSecond().~ValueT();
361 decrementNumEntries();
362 incrementNumTombstones();
367 if (LookupBucketFor(Key, TheBucket))
370 return *InsertIntoBucket(TheBucket, Key);
379 if (LookupBucketFor(Key, TheBucket))
382 return *InsertIntoBucket(TheBucket, std::move(Key));
393 return Ptr >= getBuckets() && Ptr < getBucketsEnd();
405 if (getNumBuckets() == 0)
409 for (BucketT *P = getBuckets(), *E = getBucketsEnd(); P != E; ++P) {
410 if (!KeyInfoT::isEqual(P->getFirst(), EmptyKey) &&
411 !KeyInfoT::isEqual(P->getFirst(), TombstoneKey))
412 P->getSecond().~ValueT();
413 P->getFirst().~KeyT();
421 assert((getNumBuckets() & (getNumBuckets()-1)) == 0 &&
422 "# initial buckets must be a power of two!");
424 for (BucketT *B = getBuckets(), *E = getBucketsEnd(); B != E; ++B)
425 ::new (&B->getFirst()) KeyT(EmptyKey);
436 return static_cast<unsigned>(
NextPowerOf2(NumEntries * 4 / 3 + 1));
445 for (BucketT *B = OldBucketsBegin, *E = OldBucketsEnd; B != E; ++B) {
446 if (!KeyInfoT::isEqual(B->getFirst(), EmptyKey) &&
447 !KeyInfoT::isEqual(B->getFirst(), TombstoneKey)) {
450 bool FoundVal = LookupBucketFor(B->getFirst(), DestBucket);
452 assert(!FoundVal &&
"Key already in new map?");
453 DestBucket->getFirst() = std::move(B->getFirst());
454 ::new (&DestBucket->getSecond()) ValueT(std::move(B->getSecond()));
455 incrementNumEntries();
458 B->getSecond().~ValueT();
460 B->getFirst().~KeyT();
464 template <
typename OtherBaseT>
467 assert(&other !=
this);
468 assert(getNumBuckets() == other.getNumBuckets());
470 setNumEntries(other.getNumEntries());
471 setNumTombstones(other.getNumTombstones());
473 if (std::is_trivially_copyable<KeyT>::value &&
474 std::is_trivially_copyable<ValueT>::value)
475 memcpy(
reinterpret_cast<void *
>(getBuckets()), other.getBuckets(),
476 getNumBuckets() *
sizeof(BucketT));
478 for (
size_t i = 0; i < getNumBuckets(); ++i) {
479 ::new (&getBuckets()[i].getFirst())
480 KeyT(other.getBuckets()[i].getFirst());
481 if (!KeyInfoT::isEqual(getBuckets()[i].getFirst(),
getEmptyKey()) &&
483 ::new (&getBuckets()[i].getSecond())
484 ValueT(other.getBuckets()[i].getSecond());
489 return KeyInfoT::getHashValue(Val);
492 template<
typename LookupKeyT>
494 return KeyInfoT::getHashValue(Val);
498 static_assert(std::is_base_of<DenseMapBase, DerivedT>::value,
499 "Must pass the derived type to this template!");
500 return KeyInfoT::getEmptyKey();
504 return KeyInfoT::getTombstoneKey();
508 iterator makeIterator(BucketT *P, BucketT *E,
510 bool NoAdvance=
false) {
512 BucketT *B = P == getBucketsEnd() ? getBuckets() : P + 1;
513 return iterator(B, E, Epoch, NoAdvance);
515 return iterator(P, E, Epoch, NoAdvance);
518 const_iterator makeConstIterator(
const BucketT *P,
const BucketT *E,
520 const bool NoAdvance=
false)
const {
522 const BucketT *B = P == getBucketsEnd() ? getBuckets() : P + 1;
528 unsigned getNumEntries()
const {
529 return static_cast<const DerivedT *
>(
this)->getNumEntries();
532 void setNumEntries(
unsigned Num) {
533 static_cast<DerivedT *
>(
this)->setNumEntries(Num);
536 void incrementNumEntries() {
537 setNumEntries(getNumEntries() + 1);
540 void decrementNumEntries() {
541 setNumEntries(getNumEntries() - 1);
544 unsigned getNumTombstones()
const {
545 return static_cast<const DerivedT *
>(
this)->getNumTombstones();
548 void setNumTombstones(
unsigned Num) {
549 static_cast<DerivedT *
>(
this)->setNumTombstones(Num);
552 void incrementNumTombstones() {
553 setNumTombstones(getNumTombstones() + 1);
556 void decrementNumTombstones() {
557 setNumTombstones(getNumTombstones() - 1);
560 const BucketT *getBuckets()
const {
561 return static_cast<const DerivedT *
>(
this)->getBuckets();
564 BucketT *getBuckets() {
565 return static_cast<DerivedT *
>(
this)->getBuckets();
568 unsigned getNumBuckets()
const {
569 return static_cast<const DerivedT *
>(
this)->getNumBuckets();
572 BucketT *getBucketsEnd() {
573 return getBuckets() + getNumBuckets();
576 const BucketT *getBucketsEnd()
const {
577 return getBuckets() + getNumBuckets();
580 void grow(
unsigned AtLeast) {
581 static_cast<DerivedT *
>(
this)->grow(AtLeast);
584 void shrink_and_clear() {
585 static_cast<DerivedT *
>(
this)->shrink_and_clear();
588 template <
typename KeyArg,
typename... ValueArgs>
589 BucketT *InsertIntoBucket(BucketT *TheBucket, KeyArg &&Key,
590 ValueArgs &&... Values) {
591 TheBucket = InsertIntoBucketImpl(Key, Key, TheBucket);
593 TheBucket->getFirst() = std::forward<KeyArg>(Key);
594 ::new (&TheBucket->getSecond()) ValueT(
std::forward<ValueArgs>(Values)...);
598 template <typename LookupKeyT>
599 BucketT *InsertIntoBucketWithLookup(BucketT *TheBucket, KeyT &&Key,
600 ValueT &&Value, LookupKeyT &Lookup) {
601 TheBucket = InsertIntoBucketImpl(Key, Lookup, TheBucket);
603 TheBucket->getFirst() = std::move(Key);
604 ::new (&TheBucket->getSecond()) ValueT(
std::move(Value));
608 template <typename LookupKeyT>
609 BucketT *InsertIntoBucketImpl(const KeyT &Key, const LookupKeyT &Lookup,
610 BucketT *TheBucket) {
622 unsigned NewNumEntries = getNumEntries() + 1;
623 unsigned NumBuckets = getNumBuckets();
625 this->grow(NumBuckets * 2);
626 LookupBucketFor(Lookup, TheBucket);
627 NumBuckets = getNumBuckets();
628 }
else if (
LLVM_UNLIKELY(NumBuckets-(NewNumEntries+getNumTombstones()) <=
630 this->grow(NumBuckets);
631 LookupBucketFor(Lookup, TheBucket);
637 incrementNumEntries();
641 if (!KeyInfoT::isEqual(TheBucket->getFirst(), EmptyKey))
642 decrementNumTombstones();
651 template<
typename LookupKeyT>
652 bool LookupBucketFor(
const LookupKeyT &Val,
653 const BucketT *&FoundBucket)
const {
654 const BucketT *BucketsPtr = getBuckets();
655 const unsigned NumBuckets = getNumBuckets();
657 if (NumBuckets == 0) {
658 FoundBucket =
nullptr;
663 const BucketT *FoundTombstone =
nullptr;
666 assert(!KeyInfoT::isEqual(Val, EmptyKey) &&
667 !KeyInfoT::isEqual(Val, TombstoneKey) &&
668 "Empty/Tombstone value shouldn't be inserted into map!");
671 unsigned ProbeAmt = 1;
673 const BucketT *ThisBucket = BucketsPtr + BucketNo;
675 if (
LLVM_LIKELY(KeyInfoT::isEqual(Val, ThisBucket->getFirst()))) {
676 FoundBucket = ThisBucket;
682 if (
LLVM_LIKELY(KeyInfoT::isEqual(ThisBucket->getFirst(), EmptyKey))) {
685 FoundBucket = FoundTombstone ? FoundTombstone : ThisBucket;
691 if (KeyInfoT::isEqual(ThisBucket->getFirst(), TombstoneKey) &&
693 FoundTombstone = ThisBucket;
697 BucketNo += ProbeAmt++;
698 BucketNo &= (NumBuckets-1);
702 template <
typename LookupKeyT>
703 bool LookupBucketFor(
const LookupKeyT &Val, BucketT *&FoundBucket) {
704 const BucketT *ConstFoundBucket;
706 ->LookupBucketFor(Val, ConstFoundBucket);
707 FoundBucket =
const_cast<BucketT *
>(ConstFoundBucket);
717 return getNumBuckets() *
sizeof(BucketT);
727template <
typename DerivedT,
typename KeyT,
typename ValueT,
typename KeyInfoT,
735 for (
auto &KV : LHS) {
736 auto I = RHS.
find(KV.first);
737 if (I == RHS.
end() || I->second != KV.second)
747template <
typename DerivedT,
typename KeyT,
typename ValueT,
typename KeyInfoT,
752 return !(LHS == RHS);
755template <
typename KeyT,
typename ValueT,
756 typename KeyInfoT = DenseMapInfo<KeyT>,
759 KeyT, ValueT, KeyInfoT, BucketT> {
768 unsigned NumTombstones;
774 explicit DenseMap(
unsigned InitialReserve = 0) { init(InitialReserve); }
786 template<
typename InputIt>
788 init(std::distance(I, E));
792 DenseMap(std::initializer_list<typename BaseT::value_type> Vals) {
794 this->insert(Vals.begin(), Vals.end());
803 this->incrementEpoch();
807 std::swap(NumTombstones, RHS.NumTombstones);
828 if (allocateBuckets(other.NumBuckets)) {
829 this->BaseT::copyFrom(other);
836 void init(
unsigned InitNumEntries) {
837 auto InitBuckets = BaseT::getMinBucketToReserveForEntries(InitNumEntries);
838 if (allocateBuckets(InitBuckets)) {
839 this->BaseT::initEmpty();
847 unsigned OldNumBuckets = NumBuckets;
848 BucketT *OldBuckets = Buckets;
850 allocateBuckets(std::max<unsigned>(64,
static_cast<unsigned>(
NextPowerOf2(AtLeast-1))));
853 this->BaseT::initEmpty();
857 this->moveFromOldBuckets(OldBuckets, OldBuckets+OldNumBuckets);
865 unsigned OldNumBuckets = NumBuckets;
866 unsigned OldNumEntries = NumEntries;
870 unsigned NewNumBuckets = 0;
872 NewNumBuckets = (std::max)(64, 1 << (
Log2_32_Ceil(OldNumEntries) + 1));
873 if (NewNumBuckets == NumBuckets) {
874 this->BaseT::initEmpty();
884 unsigned getNumEntries()
const {
888 void setNumEntries(
unsigned Num) {
892 unsigned getNumTombstones()
const {
893 return NumTombstones;
896 void setNumTombstones(
unsigned Num) {
900 BucketT *getBuckets()
const {
904 unsigned getNumBuckets()
const {
908 bool allocateBuckets(
unsigned Num) {
910 if (NumBuckets == 0) {
915 Buckets =
static_cast<BucketT *
>(
921template <
typename KeyT,
typename ValueT,
unsigned InlineBuckets = 4,
922 typename KeyInfoT = DenseMapInfo<KeyT>,
926 SmallDenseMap<KeyT, ValueT, InlineBuckets, KeyInfoT, BucketT>, KeyT,
927 ValueT, KeyInfoT, BucketT> {
935 "InlineBuckets must be a power of 2.");
938 unsigned NumEntries : 31;
939 unsigned NumTombstones;
952 if (NumInitBuckets > InlineBuckets)
953 NumInitBuckets = std::bit_ceil(NumInitBuckets);
954 init(NumInitBuckets);
967 template<
typename InputIt>
982 unsigned TmpNumEntries = RHS.NumEntries;
983 RHS.NumEntries = NumEntries;
984 NumEntries = TmpNumEntries;
985 std::swap(NumTombstones, RHS.NumTombstones);
987 const KeyT EmptyKey = this->getEmptyKey();
988 const KeyT TombstoneKey = this->getTombstoneKey();
989 if (Small && RHS.Small) {
994 for (
unsigned i = 0, e = InlineBuckets; i != e; ++i) {
995 BucketT *LHSB = &getInlineBuckets()[i],
996 *RHSB = &RHS.getInlineBuckets()[i];
997 bool hasLHSValue = (!KeyInfoT::isEqual(LHSB->getFirst(), EmptyKey) &&
998 !KeyInfoT::isEqual(LHSB->getFirst(), TombstoneKey));
999 bool hasRHSValue = (!KeyInfoT::isEqual(RHSB->getFirst(), EmptyKey) &&
1000 !KeyInfoT::isEqual(RHSB->getFirst(), TombstoneKey));
1001 if (hasLHSValue && hasRHSValue) {
1007 std::swap(LHSB->getFirst(), RHSB->getFirst());
1009 ::new (&RHSB->getSecond()) ValueT(std::move(LHSB->getSecond()));
1010 LHSB->getSecond().~ValueT();
1011 }
else if (hasRHSValue) {
1012 ::new (&LHSB->getSecond()) ValueT(std::move(RHSB->getSecond()));
1013 RHSB->getSecond().~ValueT();
1018 if (!Small && !RHS.Small) {
1019 std::swap(getLargeRep()->Buckets, RHS.getLargeRep()->Buckets);
1020 std::swap(getLargeRep()->NumBuckets, RHS.getLargeRep()->NumBuckets);
1028 LargeRep TmpRep = std::move(*LargeSide.getLargeRep());
1029 LargeSide.getLargeRep()->~LargeRep();
1030 LargeSide.Small =
true;
1035 for (
unsigned i = 0, e = InlineBuckets; i != e; ++i) {
1036 BucketT *NewB = &LargeSide.getInlineBuckets()[i],
1037 *OldB = &SmallSide.getInlineBuckets()[i];
1038 ::new (&NewB->getFirst()) KeyT(std::move(OldB->getFirst()));
1039 OldB->getFirst().~KeyT();
1040 if (!KeyInfoT::isEqual(NewB->getFirst(), EmptyKey) &&
1041 !KeyInfoT::isEqual(NewB->getFirst(), TombstoneKey)) {
1042 ::new (&NewB->getSecond()) ValueT(std::move(OldB->getSecond()));
1043 OldB->getSecond().~ValueT();
1049 SmallSide.Small =
false;
1050 new (SmallSide.getLargeRep()) LargeRep(std::move(TmpRep));
1061 deallocateBuckets();
1069 deallocateBuckets();
1071 if (other.getNumBuckets() > InlineBuckets) {
1073 new (getLargeRep()) LargeRep(allocateBuckets(other.getNumBuckets()));
1075 this->BaseT::copyFrom(other);
1080 if (InitBuckets > InlineBuckets) {
1082 new (getLargeRep()) LargeRep(allocateBuckets(InitBuckets));
1084 this->BaseT::initEmpty();
1088 if (AtLeast > InlineBuckets)
1089 AtLeast = std::max<unsigned>(64,
NextPowerOf2(AtLeast-1));
1094 BucketT *TmpBegin =
reinterpret_cast<BucketT *
>(&TmpStorage);
1095 BucketT *TmpEnd = TmpBegin;
1099 const KeyT EmptyKey = this->getEmptyKey();
1100 const KeyT TombstoneKey = this->getTombstoneKey();
1101 for (BucketT *P = getBuckets(), *E = P + InlineBuckets; P != E; ++P) {
1102 if (!KeyInfoT::isEqual(P->getFirst(), EmptyKey) &&
1103 !KeyInfoT::isEqual(P->getFirst(), TombstoneKey)) {
1104 assert(
size_t(TmpEnd - TmpBegin) < InlineBuckets &&
1105 "Too many inline buckets!");
1106 ::new (&TmpEnd->getFirst()) KeyT(std::move(P->getFirst()));
1107 ::new (&TmpEnd->getSecond()) ValueT(std::move(P->getSecond()));
1109 P->getSecond().~ValueT();
1111 P->getFirst().~KeyT();
1117 if (AtLeast > InlineBuckets) {
1119 new (getLargeRep()) LargeRep(allocateBuckets(AtLeast));
1121 this->moveFromOldBuckets(TmpBegin, TmpEnd);
1125 LargeRep OldRep = std::move(*getLargeRep());
1126 getLargeRep()->~LargeRep();
1127 if (AtLeast <= InlineBuckets) {
1130 new (getLargeRep()) LargeRep(allocateBuckets(AtLeast));
1133 this->moveFromOldBuckets(OldRep.Buckets, OldRep.Buckets+OldRep.NumBuckets);
1141 unsigned OldSize = this->size();
1145 unsigned NewNumBuckets = 0;
1148 if (NewNumBuckets > InlineBuckets && NewNumBuckets < 64u)
1151 if ((Small && NewNumBuckets <= InlineBuckets) ||
1152 (!Small && NewNumBuckets == getLargeRep()->NumBuckets)) {
1153 this->BaseT::initEmpty();
1157 deallocateBuckets();
1158 init(NewNumBuckets);
1162 unsigned getNumEntries()
const {
1166 void setNumEntries(
unsigned Num) {
1168 assert(Num < (1U << 31) &&
"Cannot support more than 1<<31 entries");
1172 unsigned getNumTombstones()
const {
1173 return NumTombstones;
1176 void setNumTombstones(
unsigned Num) {
1177 NumTombstones = Num;
1180 const BucketT *getInlineBuckets()
const {
1185 return reinterpret_cast<const BucketT *
>(&storage);
1188 BucketT *getInlineBuckets() {
1189 return const_cast<BucketT *
>(
1190 const_cast<const SmallDenseMap *
>(
this)->getInlineBuckets());
1193 const LargeRep *getLargeRep()
const {
1196 return reinterpret_cast<const LargeRep *
>(&storage);
1199 LargeRep *getLargeRep() {
1200 return const_cast<LargeRep *
>(
1201 const_cast<const SmallDenseMap *
>(
this)->getLargeRep());
1204 const BucketT *getBuckets()
const {
1205 return Small ? getInlineBuckets() : getLargeRep()->Buckets;
1208 BucketT *getBuckets() {
1209 return const_cast<BucketT *
>(
1210 const_cast<const SmallDenseMap *
>(
this)->getBuckets());
1213 unsigned getNumBuckets()
const {
1214 return Small ? InlineBuckets : getLargeRep()->NumBuckets;
1217 void deallocateBuckets() {
1222 sizeof(BucketT) * getLargeRep()->NumBuckets,
1224 getLargeRep()->~LargeRep();
1227 LargeRep allocateBuckets(
unsigned Num) {
1228 assert(Num > InlineBuckets &&
"Must allocate more buckets than are inline");
1230 sizeof(BucketT) * Num,
alignof(BucketT))),
1236template <
typename KeyT,
typename ValueT,
typename KeyInfoT,
typename Bucket,
1244 using value_type = std::conditional_t<IsConst, const Bucket, Bucket>;
1257 bool NoAdvance =
false)
1259 assert(isHandleInSync() &&
"invalid construction!");
1261 if (NoAdvance)
return;
1262 if (shouldReverseIterate<KeyT>()) {
1263 RetreatPastEmptyBuckets();
1266 AdvancePastEmptyBuckets();
1272 template <
bool IsConstSrc,
1273 typename = std::enable_if_t<!IsConstSrc && IsConst>>
1279 assert(isHandleInSync() &&
"invalid iterator access!");
1280 assert(Ptr != End &&
"dereferencing end() iterator");
1281 if (shouldReverseIterate<KeyT>())
1286 assert(isHandleInSync() &&
"invalid iterator access!");
1287 assert(Ptr != End &&
"dereferencing end() iterator");
1288 if (shouldReverseIterate<KeyT>())
1295 assert((!LHS.Ptr || LHS.isHandleInSync()) &&
"handle not in sync!");
1296 assert((!RHS.Ptr || RHS.isHandleInSync()) &&
"handle not in sync!");
1297 assert(LHS.getEpochAddress() == RHS.getEpochAddress() &&
1298 "comparing incomparable iterators!");
1299 return LHS.Ptr == RHS.Ptr;
1304 return !(LHS == RHS);
1308 assert(isHandleInSync() &&
"invalid iterator access!");
1309 assert(Ptr != End &&
"incrementing end() iterator");
1310 if (shouldReverseIterate<KeyT>()) {
1312 RetreatPastEmptyBuckets();
1316 AdvancePastEmptyBuckets();
1320 assert(isHandleInSync() &&
"invalid iterator access!");
1325 void AdvancePastEmptyBuckets() {
1327 const KeyT Empty = KeyInfoT::getEmptyKey();
1328 const KeyT Tombstone = KeyInfoT::getTombstoneKey();
1330 while (Ptr != End && (KeyInfoT::isEqual(Ptr->getFirst(), Empty) ||
1331 KeyInfoT::isEqual(Ptr->getFirst(), Tombstone)))
1335 void RetreatPastEmptyBuckets() {
1337 const KeyT Empty = KeyInfoT::getEmptyKey();
1338 const KeyT Tombstone = KeyInfoT::getTombstoneKey();
1340 while (Ptr != End && (KeyInfoT::isEqual(Ptr[-1].getFirst(), Empty) ||
1341 KeyInfoT::isEqual(Ptr[-1].getFirst(), Tombstone)))
1346template <
typename KeyT,
typename ValueT,
typename KeyInfoT>
#define LLVM_UNLIKELY(EXPR)
Definition Compiler.h:253
#define LLVM_LIKELY(EXPR)
Definition Compiler.h:252
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:465
bool isPointerIntoBucketsArray(const void *Ptr) const
isPointerIntoBucketsArray - Return true if the specified pointer points somewhere into the DenseMap's...
Definition DenseMap.h:392
const_iterator find_as(const LookupKeyT &Val) const
Definition DenseMap.h:191
DenseMapIterator< KeyT, ValueT, KeyInfoT, BucketT > iterator
Definition DenseMap.h:72
std::pair< iterator, bool > try_emplace(KeyT &&Key, Ts &&... Args)
Definition DenseMap.h:236
iterator end()
Definition DenseMap.h:85
std::pair< iterator, bool > insert(std::pair< KeyT, ValueT > &&KV)
Definition DenseMap.h:228
static const KeyT getEmptyKey()
Definition DenseMap.h:497
bool erase(const KeyT &Val)
Definition DenseMap.h:346
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:152
ValueT mapped_type
Definition DenseMap.h:69
void erase(iterator I)
Definition DenseMap.h:357
void initEmpty()
Definition DenseMap.h:417
unsigned size() const
Definition DenseMap.h:100
static unsigned getHashValue(const KeyT &Val)
Definition DenseMap.h:488
void reserve(size_type NumEntries)
Grow the densemap so that it can contain at least NumEntries items before resizing again.
Definition DenseMap.h:104
ValueT & getOrInsertDefault(KeyT &&Key)
Returns the value associated to the key in the map if it exists.
Definition DenseMap.h:335
void insert(InputIt I, InputIt E)
insert - Range insertion of pairs.
Definition DenseMap.h:311
ValueT & getOrInsertDefault(const KeyT &Key)
Returns the value associated to the key in the map if it exists.
Definition DenseMap.h:342
BucketT value_type
Definition DenseMap.h:70
const void * getPointerIntoBucketsArray() const
getPointerIntoBucketsArray() - Return an opaque pointer into the buckets array.
Definition DenseMap.h:399
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Definition DenseMap.h:221
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:146
KeyT key_type
Definition DenseMap.h:68
DenseMapIterator< KeyT, ValueT, KeyInfoT, BucketT, true > const_iterator
Definition DenseMap.h:73
value_type & FindAndConstruct(const KeyT &Key)
Definition DenseMap.h:365
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:212
void destroyAll()
Definition DenseMap.h:404
std::pair< iterator, bool > insert_or_assign(KeyT &&Key, V &&Val)
Definition DenseMap.h:325
iterator begin()
Definition DenseMap.h:76
ValueT & operator[](const KeyT &Key)
Definition DenseMap.h:373
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:203
static unsigned getHashValue(const LookupKeyT &Val)
Definition DenseMap.h:493
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:287
unsigned getMinBucketToReserveForEntries(unsigned NumEntries)
Returns the number of buckets to allocate to ensure that the DenseMap can accommodate NumEntries with...
Definition DenseMap.h:430
void moveFromOldBuckets(BucketT *OldBucketsBegin, BucketT *OldBucketsEnd)
Definition DenseMap.h:439
const_iterator find(const_arg_type_t< KeyT > Val) const
Definition DenseMap.h:165
static const KeyT getTombstoneKey()
Definition DenseMap.h:503
std::pair< iterator, bool > insert_or_assign(const KeyT &Key, V &&Val)
Definition DenseMap.h:317
ValueT & operator[](KeyT &&Key)
Definition DenseMap.h:385
iterator find(const_arg_type_t< KeyT > Val)
Definition DenseMap.h:156
value_type & FindAndConstruct(KeyT &&Key)
Definition DenseMap.h:377
iterator find_as(const LookupKeyT &Val)
Alternate version of find() which allows a different, and possibly less expensive,...
Definition DenseMap.h:181
size_t getMemorySize() const
Return the approximate size (in bytes) of the actual map.
Definition DenseMap.h:716
const_iterator begin() const
Definition DenseMap.h:88
bool empty() const
Definition DenseMap.h:99
std::pair< iterator, bool > try_emplace(const KeyT &Key, Ts &&... Args)
Definition DenseMap.h:261
Definition DenseMap.h:759
void init(unsigned InitNumEntries)
Definition DenseMap.h:836
void copyFrom(const DenseMap &other)
Definition DenseMap.h:825
DenseMap & operator=(DenseMap &&other)
Definition DenseMap.h:817
DenseMap & operator=(const DenseMap &other)
Definition DenseMap.h:811
DenseMap(const DenseMap &other)
Definition DenseMap.h:776
DenseMap(std::initializer_list< typename BaseT::value_type > Vals)
Definition DenseMap.h:792
void grow(unsigned AtLeast)
Definition DenseMap.h:846
void shrink_and_clear()
Definition DenseMap.h:864
DenseMap(const InputIt &I, const InputIt &E)
Definition DenseMap.h:787
void swap(DenseMap &RHS)
Definition DenseMap.h:802
DenseMap(DenseMap &&other)
Definition DenseMap.h:781
DenseMap(unsigned InitialReserve=0)
Create a DenseMap with an optional InitialReserve that guarantee that this number of elements can be ...
Definition DenseMap.h:774
~DenseMap()
Definition DenseMap.h:797
Definition DenseMap.h:1238
DenseMapIterator & operator++()
Definition DenseMap.h:1307
friend bool operator!=(const DenseMapIterator &LHS, const DenseMapIterator &RHS)
Definition DenseMap.h:1302
DenseMapIterator(const DenseMapIterator< KeyT, ValueT, KeyInfoT, Bucket, IsConstSrc > &I)
Definition DenseMap.h:1274
reference operator*() const
Definition DenseMap.h:1278
pointer operator->() const
Definition DenseMap.h:1285
DenseMapIterator()=default
DenseMapIterator(pointer Pos, pointer E, const DebugEpochBase &Epoch, bool NoAdvance=false)
Definition DenseMap.h:1256
std::forward_iterator_tag iterator_category
Definition DenseMap.h:1247
ptrdiff_t difference_type
Definition DenseMap.h:1243
std::conditional_t< IsConst, const Bucket, Bucket > value_type
Definition DenseMap.h:1244
value_type * pointer
Definition DenseMap.h:1245
DenseMapIterator operator++(int)
Definition DenseMap.h:1319
friend bool operator==(const DenseMapIterator &LHS, const DenseMapIterator &RHS)
Definition DenseMap.h:1293
value_type & reference
Definition DenseMap.h:1246
Definition DenseMap.h:927
~SmallDenseMap()
Definition DenseMap.h:976
SmallDenseMap(unsigned NumInitBuckets=0)
Definition DenseMap.h:951
void swap(SmallDenseMap &RHS)
Definition DenseMap.h:981
void copyFrom(const SmallDenseMap &other)
Definition DenseMap.h:1067
void init(unsigned InitBuckets)
Definition DenseMap.h:1078
SmallDenseMap & operator=(SmallDenseMap &&other)
Definition DenseMap.h:1059
SmallDenseMap(const SmallDenseMap &other)
Definition DenseMap.h:957
SmallDenseMap(SmallDenseMap &&other)
Definition DenseMap.h:962
void shrink_and_clear()
Definition DenseMap.h:1140
void grow(unsigned AtLeast)
Definition DenseMap.h:1087
SmallDenseMap(const InputIt &I, const InputIt &E)
Definition DenseMap.h:968
SmallDenseMap(std::initializer_list< typename BaseT::value_type > Vals)
Definition DenseMap.h:973
SmallDenseMap & operator=(const SmallDenseMap &other)
Definition DenseMap.h:1053
detail namespace with internal helper functions
Definition input_adapters.h:32
Implement std::hash so that hash_code can be used in STL containers.
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
Foonathan namespace.
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:1347
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:729
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:749
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