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;
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,
81 if (shouldReverseIterate<KeyT>())
82 return makeIterator(getBucketsEnd() - 1, getBuckets(), *
this);
83 return makeIterator(getBuckets(), getBucketsEnd(), *
this);
86 return makeIterator(getBucketsEnd(), getBucketsEnd(), *
this,
true);
91 if (shouldReverseIterate<KeyT>())
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,
160 shouldReverseIterate<KeyT>() ? getBuckets()
166 const BucketT *TheBucket;
167 if (LookupBucketFor(Val, TheBucket))
168 return makeConstIterator(TheBucket,
169 shouldReverseIterate<KeyT>() ? getBuckets()
180 template<
class LookupKeyT>
183 if (LookupBucketFor(Val, TheBucket))
184 return makeIterator(TheBucket,
185 shouldReverseIterate<KeyT>() ? getBuckets()
190 template<
class LookupKeyT>
192 const BucketT *TheBucket;
193 if (LookupBucketFor(Val, TheBucket))
194 return makeConstIterator(TheBucket,
195 shouldReverseIterate<KeyT>() ? getBuckets()
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,
240 shouldReverseIterate<KeyT>()
248 InsertIntoBucket(TheBucket, std::move(Key), std::forward<Ts>(Args)...);
249 return std::make_pair(makeIterator(TheBucket,
250 shouldReverseIterate<KeyT>()
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,
265 shouldReverseIterate<KeyT>()
272 TheBucket = InsertIntoBucket(TheBucket, Key, std::forward<Ts>(Args)...);
273 return std::make_pair(makeIterator(TheBucket,
274 shouldReverseIterate<KeyT>()
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,
292 shouldReverseIterate<KeyT>()
299 TheBucket = InsertIntoBucketWithLookup(TheBucket, std::move(KV.first),
300 std::move(KV.second), Val);
301 return std::make_pair(makeIterator(TheBucket,
302 shouldReverseIterate<KeyT>()
310 template<
typename InputIt>
332 if (!LookupBucketFor(Val, TheBucket))
335 TheBucket->getSecond().~ValueT();
337 decrementNumEntries();
338 incrementNumTombstones();
342 BucketT *TheBucket = &*I;
343 TheBucket->getSecond().~ValueT();
345 decrementNumEntries();
346 incrementNumTombstones();
351 if (LookupBucketFor(Key, TheBucket))
354 return *InsertIntoBucket(TheBucket, Key);
363 if (LookupBucketFor(Key, TheBucket))
366 return *InsertIntoBucket(TheBucket, std::move(Key));
377 return Ptr >= getBuckets() && Ptr < getBucketsEnd();
389 if (getNumBuckets() == 0)
393 for (BucketT *P = getBuckets(), *E = getBucketsEnd(); P != E; ++P) {
394 if (!KeyInfoT::isEqual(P->getFirst(), EmptyKey) &&
395 !KeyInfoT::isEqual(P->getFirst(), TombstoneKey))
396 P->getSecond().~ValueT();
397 P->getFirst().~KeyT();
405 assert((getNumBuckets() & (getNumBuckets()-1)) == 0 &&
406 "# initial buckets must be a power of two!");
408 for (BucketT *B = getBuckets(), *E = getBucketsEnd(); B != E; ++B)
409 ::new (&B->getFirst()) KeyT(EmptyKey);
420 return static_cast<unsigned>(
NextPowerOf2(NumEntries * 4 / 3 + 1));
429 for (BucketT *B = OldBucketsBegin, *E = OldBucketsEnd; B != E; ++B) {
430 if (!KeyInfoT::isEqual(B->getFirst(), EmptyKey) &&
431 !KeyInfoT::isEqual(B->getFirst(), TombstoneKey)) {
434 bool FoundVal = LookupBucketFor(B->getFirst(), DestBucket);
436 assert(!FoundVal &&
"Key already in new map?");
437 DestBucket->getFirst() = std::move(B->getFirst());
438 ::new (&DestBucket->getSecond()) ValueT(std::move(B->getSecond()));
439 incrementNumEntries();
442 B->getSecond().~ValueT();
444 B->getFirst().~KeyT();
448 template <
typename OtherBaseT>
451 assert(&other !=
this);
452 assert(getNumBuckets() == other.getNumBuckets());
454 setNumEntries(other.getNumEntries());
455 setNumTombstones(other.getNumTombstones());
457 if (std::is_trivially_copyable<KeyT>::value &&
458 std::is_trivially_copyable<ValueT>::value)
459 memcpy(
reinterpret_cast<void *
>(getBuckets()), other.getBuckets(),
460 getNumBuckets() *
sizeof(BucketT));
462 for (
size_t i = 0; i < getNumBuckets(); ++i) {
463 ::new (&getBuckets()[i].getFirst())
464 KeyT(other.getBuckets()[i].getFirst());
465 if (!KeyInfoT::isEqual(getBuckets()[i].getFirst(),
getEmptyKey()) &&
467 ::new (&getBuckets()[i].getSecond())
468 ValueT(other.getBuckets()[i].getSecond());
473 return KeyInfoT::getHashValue(Val);
476 template<
typename LookupKeyT>
478 return KeyInfoT::getHashValue(Val);
482 static_assert(std::is_base_of<DenseMapBase, DerivedT>::value,
483 "Must pass the derived type to this template!");
484 return KeyInfoT::getEmptyKey();
488 return KeyInfoT::getTombstoneKey();
492 iterator makeIterator(BucketT *P, BucketT *E,
494 bool NoAdvance=
false) {
495 if (shouldReverseIterate<KeyT>()) {
496 BucketT *B = P == getBucketsEnd() ? getBuckets() : P + 1;
497 return iterator(B, E, Epoch, NoAdvance);
499 return iterator(P, E, Epoch, NoAdvance);
502 const_iterator makeConstIterator(
const BucketT *P,
const BucketT *E,
504 const bool NoAdvance=
false)
const {
505 if (shouldReverseIterate<KeyT>()) {
506 const BucketT *B = P == getBucketsEnd() ? getBuckets() : P + 1;
512 unsigned getNumEntries()
const {
513 return static_cast<const DerivedT *
>(
this)->getNumEntries();
516 void setNumEntries(
unsigned Num) {
517 static_cast<DerivedT *
>(
this)->setNumEntries(Num);
520 void incrementNumEntries() {
521 setNumEntries(getNumEntries() + 1);
524 void decrementNumEntries() {
525 setNumEntries(getNumEntries() - 1);
528 unsigned getNumTombstones()
const {
529 return static_cast<const DerivedT *
>(
this)->getNumTombstones();
532 void setNumTombstones(
unsigned Num) {
533 static_cast<DerivedT *
>(
this)->setNumTombstones(Num);
536 void incrementNumTombstones() {
537 setNumTombstones(getNumTombstones() + 1);
540 void decrementNumTombstones() {
541 setNumTombstones(getNumTombstones() - 1);
544 const BucketT *getBuckets()
const {
545 return static_cast<const DerivedT *
>(
this)->getBuckets();
548 BucketT *getBuckets() {
549 return static_cast<DerivedT *
>(
this)->getBuckets();
552 unsigned getNumBuckets()
const {
553 return static_cast<const DerivedT *
>(
this)->getNumBuckets();
556 BucketT *getBucketsEnd() {
557 return getBuckets() + getNumBuckets();
560 const BucketT *getBucketsEnd()
const {
561 return getBuckets() + getNumBuckets();
564 void grow(
unsigned AtLeast) {
565 static_cast<DerivedT *
>(
this)->grow(AtLeast);
568 void shrink_and_clear() {
569 static_cast<DerivedT *
>(
this)->shrink_and_clear();
572 template <
typename KeyArg,
typename... ValueArgs>
573 BucketT *InsertIntoBucket(BucketT *TheBucket, KeyArg &&Key,
574 ValueArgs &&... Values) {
575 TheBucket = InsertIntoBucketImpl(Key, Key, TheBucket);
577 TheBucket->getFirst() = std::forward<KeyArg>(Key);
578 ::new (&TheBucket->getSecond()) ValueT(
std::forward<ValueArgs>(Values)...);
582 template <typename LookupKeyT>
583 BucketT *InsertIntoBucketWithLookup(BucketT *TheBucket, KeyT &&Key,
584 ValueT &&Value, LookupKeyT &Lookup) {
585 TheBucket = InsertIntoBucketImpl(Key, Lookup, TheBucket);
587 TheBucket->getFirst() = std::move(Key);
588 ::new (&TheBucket->getSecond()) ValueT(
std::move(Value));
592 template <typename LookupKeyT>
593 BucketT *InsertIntoBucketImpl(const KeyT &Key, const LookupKeyT &Lookup,
594 BucketT *TheBucket) {
606 unsigned NewNumEntries = getNumEntries() + 1;
607 unsigned NumBuckets = getNumBuckets();
609 this->grow(NumBuckets * 2);
610 LookupBucketFor(Lookup, TheBucket);
611 NumBuckets = getNumBuckets();
612 }
else if (
LLVM_UNLIKELY(NumBuckets-(NewNumEntries+getNumTombstones()) <=
614 this->grow(NumBuckets);
615 LookupBucketFor(Lookup, TheBucket);
621 incrementNumEntries();
625 if (!KeyInfoT::isEqual(TheBucket->getFirst(), EmptyKey))
626 decrementNumTombstones();
635 template<
typename LookupKeyT>
636 bool LookupBucketFor(
const LookupKeyT &Val,
637 const BucketT *&FoundBucket)
const {
638 const BucketT *BucketsPtr = getBuckets();
639 const unsigned NumBuckets = getNumBuckets();
641 if (NumBuckets == 0) {
642 FoundBucket =
nullptr;
647 const BucketT *FoundTombstone =
nullptr;
650 assert(!KeyInfoT::isEqual(Val, EmptyKey) &&
651 !KeyInfoT::isEqual(Val, TombstoneKey) &&
652 "Empty/Tombstone value shouldn't be inserted into map!");
655 unsigned ProbeAmt = 1;
657 const BucketT *ThisBucket = BucketsPtr + BucketNo;
659 if (
LLVM_LIKELY(KeyInfoT::isEqual(Val, ThisBucket->getFirst()))) {
660 FoundBucket = ThisBucket;
666 if (
LLVM_LIKELY(KeyInfoT::isEqual(ThisBucket->getFirst(), EmptyKey))) {
669 FoundBucket = FoundTombstone ? FoundTombstone : ThisBucket;
675 if (KeyInfoT::isEqual(ThisBucket->getFirst(), TombstoneKey) &&
677 FoundTombstone = ThisBucket;
681 BucketNo += ProbeAmt++;
682 BucketNo &= (NumBuckets-1);
686 template <
typename LookupKeyT>
687 bool LookupBucketFor(
const LookupKeyT &Val, BucketT *&FoundBucket) {
688 const BucketT *ConstFoundBucket;
690 ->LookupBucketFor(Val, ConstFoundBucket);
691 FoundBucket =
const_cast<BucketT *
>(ConstFoundBucket);
701 return getNumBuckets() *
sizeof(BucketT);
711template <
typename DerivedT,
typename KeyT,
typename ValueT,
typename KeyInfoT,
719 for (
auto &KV : LHS) {
720 auto I = RHS.
find(KV.first);
721 if (I == RHS.
end() || I->second != KV.second)
731template <
typename DerivedT,
typename KeyT,
typename ValueT,
typename KeyInfoT,
736 return !(LHS == RHS);
739template <
typename KeyT,
typename ValueT,
740 typename KeyInfoT = DenseMapInfo<KeyT>,
743 KeyT, ValueT, KeyInfoT, BucketT> {
752 unsigned NumTombstones;
758 explicit DenseMap(
unsigned InitialReserve = 0) { init(InitialReserve); }
770 template<
typename InputIt>
772 init(std::distance(I, E));
776 DenseMap(std::initializer_list<typename BaseT::value_type> Vals) {
778 this->
insert(Vals.begin(), Vals.end());
791 std::swap(NumTombstones, RHS.NumTombstones);
812 if (allocateBuckets(other.NumBuckets)) {
813 this->BaseT::copyFrom(other);
820 void init(
unsigned InitNumEntries) {
821 auto InitBuckets = BaseT::getMinBucketToReserveForEntries(InitNumEntries);
822 if (allocateBuckets(InitBuckets)) {
823 this->BaseT::initEmpty();
831 unsigned OldNumBuckets = NumBuckets;
832 BucketT *OldBuckets = Buckets;
834 allocateBuckets(std::max<unsigned>(64,
static_cast<unsigned>(
NextPowerOf2(AtLeast-1))));
837 this->BaseT::initEmpty();
849 unsigned OldNumBuckets = NumBuckets;
850 unsigned OldNumEntries = NumEntries;
854 unsigned NewNumBuckets = 0;
857 if (NewNumBuckets == NumBuckets) {
858 this->BaseT::initEmpty();
868 unsigned getNumEntries()
const {
872 void setNumEntries(
unsigned Num) {
876 unsigned getNumTombstones()
const {
877 return NumTombstones;
880 void setNumTombstones(
unsigned Num) {
884 BucketT *getBuckets()
const {
888 unsigned getNumBuckets()
const {
892 bool allocateBuckets(
unsigned Num) {
894 if (NumBuckets == 0) {
899 Buckets =
static_cast<BucketT *
>(
905template <
typename KeyT,
typename ValueT,
unsigned InlineBuckets = 4,
906 typename KeyInfoT = DenseMapInfo<KeyT>,
910 SmallDenseMap<KeyT, ValueT, InlineBuckets, KeyInfoT, BucketT>, KeyT,
911 ValueT, KeyInfoT, BucketT> {
919 "InlineBuckets must be a power of 2.");
922 unsigned NumEntries : 31;
923 unsigned NumTombstones;
936 if (NumInitBuckets > InlineBuckets)
937 NumInitBuckets = std::bit_ceil(NumInitBuckets);
938 init(NumInitBuckets);
951 template<
typename InputIt>
966 unsigned TmpNumEntries = RHS.NumEntries;
967 RHS.NumEntries = NumEntries;
968 NumEntries = TmpNumEntries;
969 std::swap(NumTombstones, RHS.NumTombstones);
973 if (Small && RHS.Small) {
978 for (
unsigned i = 0,
e = InlineBuckets; i !=
e; ++i) {
979 BucketT *LHSB = &getInlineBuckets()[i],
980 *RHSB = &RHS.getInlineBuckets()[i];
981 bool hasLHSValue = (!KeyInfoT::isEqual(LHSB->getFirst(), EmptyKey) &&
982 !KeyInfoT::isEqual(LHSB->getFirst(), TombstoneKey));
983 bool hasRHSValue = (!KeyInfoT::isEqual(RHSB->getFirst(), EmptyKey) &&
984 !KeyInfoT::isEqual(RHSB->getFirst(), TombstoneKey));
985 if (hasLHSValue && hasRHSValue) {
991 std::swap(LHSB->getFirst(), RHSB->getFirst());
993 ::new (&RHSB->getSecond()) ValueT(std::move(LHSB->getSecond()));
994 LHSB->getSecond().~ValueT();
995 }
else if (hasRHSValue) {
996 ::new (&LHSB->getSecond()) ValueT(std::move(RHSB->getSecond()));
997 RHSB->getSecond().~ValueT();
1002 if (!Small && !RHS.Small) {
1003 std::swap(getLargeRep()->Buckets, RHS.getLargeRep()->Buckets);
1004 std::swap(getLargeRep()->NumBuckets, RHS.getLargeRep()->NumBuckets);
1012 LargeRep TmpRep = std::move(*LargeSide.getLargeRep());
1013 LargeSide.getLargeRep()->~LargeRep();
1014 LargeSide.Small =
true;
1019 for (
unsigned i = 0,
e = InlineBuckets; i !=
e; ++i) {
1020 BucketT *NewB = &LargeSide.getInlineBuckets()[i],
1021 *OldB = &SmallSide.getInlineBuckets()[i];
1022 ::new (&NewB->getFirst()) KeyT(std::move(OldB->getFirst()));
1023 OldB->getFirst().~KeyT();
1024 if (!KeyInfoT::isEqual(NewB->getFirst(), EmptyKey) &&
1025 !KeyInfoT::isEqual(NewB->getFirst(), TombstoneKey)) {
1026 ::new (&NewB->getSecond()) ValueT(std::move(OldB->getSecond()));
1027 OldB->getSecond().~ValueT();
1033 SmallSide.Small =
false;
1034 new (SmallSide.getLargeRep()) LargeRep(std::move(TmpRep));
1045 deallocateBuckets();
1053 deallocateBuckets();
1055 if (other.getNumBuckets() > InlineBuckets) {
1057 new (getLargeRep()) LargeRep(allocateBuckets(other.getNumBuckets()));
1059 this->BaseT::copyFrom(other);
1064 if (InitBuckets > InlineBuckets) {
1066 new (getLargeRep()) LargeRep(allocateBuckets(InitBuckets));
1068 this->BaseT::initEmpty();
1072 if (AtLeast > InlineBuckets)
1073 AtLeast = std::max<unsigned>(64,
NextPowerOf2(AtLeast-1));
1078 BucketT *TmpBegin =
reinterpret_cast<BucketT *
>(&TmpStorage);
1079 BucketT *TmpEnd = TmpBegin;
1085 for (BucketT *P = getBuckets(), *E = P + InlineBuckets; P != E; ++P) {
1086 if (!KeyInfoT::isEqual(P->getFirst(), EmptyKey) &&
1087 !KeyInfoT::isEqual(P->getFirst(), TombstoneKey)) {
1088 assert(
size_t(TmpEnd - TmpBegin) < InlineBuckets &&
1089 "Too many inline buckets!");
1090 ::new (&TmpEnd->getFirst()) KeyT(std::move(P->getFirst()));
1091 ::new (&TmpEnd->getSecond()) ValueT(std::move(P->getSecond()));
1093 P->getSecond().~ValueT();
1095 P->getFirst().~KeyT();
1101 if (AtLeast > InlineBuckets) {
1103 new (getLargeRep()) LargeRep(allocateBuckets(AtLeast));
1109 LargeRep OldRep = std::move(*getLargeRep());
1110 getLargeRep()->~LargeRep();
1111 if (AtLeast <= InlineBuckets) {
1114 new (getLargeRep()) LargeRep(allocateBuckets(AtLeast));
1125 unsigned OldSize = this->
size();
1129 unsigned NewNumBuckets = 0;
1132 if (NewNumBuckets > InlineBuckets && NewNumBuckets < 64u)
1135 if ((Small && NewNumBuckets <= InlineBuckets) ||
1136 (!Small && NewNumBuckets == getLargeRep()->NumBuckets)) {
1137 this->BaseT::initEmpty();
1141 deallocateBuckets();
1142 init(NewNumBuckets);
1146 unsigned getNumEntries()
const {
1150 void setNumEntries(
unsigned Num) {
1152 assert(Num < (1U << 31) &&
"Cannot support more than 1<<31 entries");
1156 unsigned getNumTombstones()
const {
1157 return NumTombstones;
1160 void setNumTombstones(
unsigned Num) {
1161 NumTombstones = Num;
1164 const BucketT *getInlineBuckets()
const {
1169 return reinterpret_cast<const BucketT *
>(&storage);
1172 BucketT *getInlineBuckets() {
1173 return const_cast<BucketT *
>(
1174 const_cast<const SmallDenseMap *
>(
this)->getInlineBuckets());
1177 const LargeRep *getLargeRep()
const {
1180 return reinterpret_cast<const LargeRep *
>(&storage);
1183 LargeRep *getLargeRep() {
1184 return const_cast<LargeRep *
>(
1185 const_cast<const SmallDenseMap *
>(
this)->getLargeRep());
1188 const BucketT *getBuckets()
const {
1189 return Small ? getInlineBuckets() : getLargeRep()->Buckets;
1192 BucketT *getBuckets() {
1193 return const_cast<BucketT *
>(
1194 const_cast<const SmallDenseMap *
>(
this)->getBuckets());
1197 unsigned getNumBuckets()
const {
1198 return Small ? InlineBuckets : getLargeRep()->NumBuckets;
1201 void deallocateBuckets() {
1206 sizeof(BucketT) * getLargeRep()->NumBuckets,
1208 getLargeRep()->~LargeRep();
1211 LargeRep allocateBuckets(
unsigned Num) {
1212 assert(Num > InlineBuckets &&
"Must allocate more buckets than are inline");
1214 sizeof(BucketT) * Num,
alignof(BucketT))),
1220template <
typename KeyT,
typename ValueT,
typename KeyInfoT,
typename Bucket,
1228 using value_type = std::conditional_t<IsConst, const Bucket, Bucket>;
1241 bool NoAdvance =
false)
1243 assert(isHandleInSync() &&
"invalid construction!");
1245 if (NoAdvance)
return;
1246 if (shouldReverseIterate<KeyT>()) {
1247 RetreatPastEmptyBuckets();
1250 AdvancePastEmptyBuckets();
1256 template <
bool IsConstSrc,
1257 typename = std::enable_if_t<!IsConstSrc && IsConst>>
1263 assert(isHandleInSync() &&
"invalid iterator access!");
1264 assert(Ptr != End &&
"dereferencing end() iterator");
1265 if (shouldReverseIterate<KeyT>())
1270 assert(isHandleInSync() &&
"invalid iterator access!");
1271 assert(Ptr != End &&
"dereferencing end() iterator");
1272 if (shouldReverseIterate<KeyT>())
1279 assert((!LHS.Ptr || LHS.isHandleInSync()) &&
"handle not in sync!");
1280 assert((!RHS.Ptr || RHS.isHandleInSync()) &&
"handle not in sync!");
1281 assert(LHS.getEpochAddress() == RHS.getEpochAddress() &&
1282 "comparing incomparable iterators!");
1283 return LHS.Ptr == RHS.Ptr;
1288 return !(LHS == RHS);
1292 assert(isHandleInSync() &&
"invalid iterator access!");
1293 assert(Ptr != End &&
"incrementing end() iterator");
1294 if (shouldReverseIterate<KeyT>()) {
1296 RetreatPastEmptyBuckets();
1300 AdvancePastEmptyBuckets();
1304 assert(isHandleInSync() &&
"invalid iterator access!");
1309 void AdvancePastEmptyBuckets() {
1311 const KeyT Empty = KeyInfoT::getEmptyKey();
1312 const KeyT Tombstone = KeyInfoT::getTombstoneKey();
1314 while (Ptr != End && (KeyInfoT::isEqual(Ptr->getFirst(), Empty) ||
1315 KeyInfoT::isEqual(Ptr->getFirst(), Tombstone)))
1319 void RetreatPastEmptyBuckets() {
1321 const KeyT Empty = KeyInfoT::getEmptyKey();
1322 const KeyT Tombstone = KeyInfoT::getTombstoneKey();
1324 while (Ptr != End && (KeyInfoT::isEqual(Ptr[-1].getFirst(), Empty) ||
1325 KeyInfoT::isEqual(Ptr[-1].getFirst(), Tombstone)))
1330template <
typename KeyT,
typename ValueT,
typename KeyInfoT>
#define LLVM_UNLIKELY(EXPR)
Definition: Compiler.h:234
#define LLVM_LIKELY(EXPR)
Definition: Compiler.h:233
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
Definition: DenseMap.h:62
void copyFrom(const DenseMapBase< OtherBaseT, KeyT, ValueT, KeyInfoT, BucketT > &other)
Definition: DenseMap.h:449
bool isPointerIntoBucketsArray(const void *Ptr) const
isPointerIntoBucketsArray - Return true if the specified pointer points somewhere into the DenseMap's...
Definition: DenseMap.h:376
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:481
bool erase(const KeyT &Val)
Definition: DenseMap.h:330
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:341
void initEmpty()
Definition: DenseMap.h:401
unsigned size() const
Definition: DenseMap.h:100
static unsigned getHashValue(const KeyT &Val)
Definition: DenseMap.h:472
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:319
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:326
BucketT value_type
Definition: DenseMap.h:70
const void * getPointerIntoBucketsArray() const
getPointerIntoBucketsArray() - Return an opaque pointer into the buckets array.
Definition: DenseMap.h:383
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
value_type & FindAndConstruct(const KeyT &Key)
Definition: DenseMap.h:349
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:388
DenseMapIterator< KeyT, ValueT, KeyInfoT, BucketT, true > const_iterator
Definition: DenseMap.h:74
iterator begin()
Definition: DenseMap.h:76
ValueT & operator[](const KeyT &Key)
Definition: DenseMap.h:357
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:477
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:414
void moveFromOldBuckets(BucketT *OldBucketsBegin, BucketT *OldBucketsEnd)
Definition: DenseMap.h:423
const_iterator find(const_arg_type_t< KeyT > Val) const
Definition: DenseMap.h:165
static const KeyT getTombstoneKey()
Definition: DenseMap.h:487
ValueT & operator[](KeyT &&Key)
Definition: DenseMap.h:369
iterator find(const_arg_type_t< KeyT > Val)
Definition: DenseMap.h:156
value_type & FindAndConstruct(KeyT &&Key)
Definition: DenseMap.h:361
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:700
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:743
void init(unsigned InitNumEntries)
Definition: DenseMap.h:820
void copyFrom(const DenseMap &other)
Definition: DenseMap.h:809
DenseMap & operator=(DenseMap &&other)
Definition: DenseMap.h:801
DenseMap & operator=(const DenseMap &other)
Definition: DenseMap.h:795
DenseMap(const DenseMap &other)
Definition: DenseMap.h:760
DenseMap(std::initializer_list< typename BaseT::value_type > Vals)
Definition: DenseMap.h:776
void grow(unsigned AtLeast)
Definition: DenseMap.h:830
void shrink_and_clear()
Definition: DenseMap.h:848
DenseMap(const InputIt &I, const InputIt &E)
Definition: DenseMap.h:771
void swap(DenseMap &RHS)
Definition: DenseMap.h:786
DenseMap(DenseMap &&other)
Definition: DenseMap.h:765
DenseMap(unsigned InitialReserve=0)
Create a DenseMap with an optional InitialReserve that guarantee that this number of elements can be ...
Definition: DenseMap.h:758
~DenseMap()
Definition: DenseMap.h:781
Definition: DenseMap.h:1222
DenseMapIterator & operator++()
Definition: DenseMap.h:1291
friend bool operator!=(const DenseMapIterator &LHS, const DenseMapIterator &RHS)
Definition: DenseMap.h:1286
DenseMapIterator(const DenseMapIterator< KeyT, ValueT, KeyInfoT, Bucket, IsConstSrc > &I)
Definition: DenseMap.h:1258
reference operator*() const
Definition: DenseMap.h:1262
pointer operator->() const
Definition: DenseMap.h:1269
DenseMapIterator()=default
DenseMapIterator(pointer Pos, pointer E, const DebugEpochBase &Epoch, bool NoAdvance=false)
Definition: DenseMap.h:1240
std::forward_iterator_tag iterator_category
Definition: DenseMap.h:1231
ptrdiff_t difference_type
Definition: DenseMap.h:1227
std::conditional_t< IsConst, const Bucket, Bucket > value_type
Definition: DenseMap.h:1228
value_type * pointer
Definition: DenseMap.h:1229
DenseMapIterator operator++(int)
Definition: DenseMap.h:1303
friend bool operator==(const DenseMapIterator &LHS, const DenseMapIterator &RHS)
Definition: DenseMap.h:1277
value_type & reference
Definition: DenseMap.h:1230
Definition: DenseMap.h:911
~SmallDenseMap()
Definition: DenseMap.h:960
SmallDenseMap(unsigned NumInitBuckets=0)
Definition: DenseMap.h:935
void swap(SmallDenseMap &RHS)
Definition: DenseMap.h:965
void copyFrom(const SmallDenseMap &other)
Definition: DenseMap.h:1051
void init(unsigned InitBuckets)
Definition: DenseMap.h:1062
SmallDenseMap & operator=(SmallDenseMap &&other)
Definition: DenseMap.h:1043
SmallDenseMap(const SmallDenseMap &other)
Definition: DenseMap.h:941
SmallDenseMap(SmallDenseMap &&other)
Definition: DenseMap.h:946
void shrink_and_clear()
Definition: DenseMap.h:1124
void grow(unsigned AtLeast)
Definition: DenseMap.h:1071
SmallDenseMap(const InputIt &I, const InputIt &E)
Definition: DenseMap.h:952
SmallDenseMap(std::initializer_list< typename BaseT::value_type > Vals)
Definition: DenseMap.h:957
SmallDenseMap & operator=(const SmallDenseMap &other)
Definition: DenseMap.h:1037
detail namespace with internal helper functions
Definition: xchar.h:20
const T & first(const T &value, const Tail &...)
Definition: compile.h:60
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) is_nothrow_move_constructible< wpi::WPI_BASIC_JSON_TPL >::value &&//NOLINT(misc-redundant-expression) is_nothrow_move_assignable< wpi::WPI_BASIC_JSON_TPL >::value)
exchanges the values of two JSON objects
Definition: json.h:5219
static constexpr const charge::coulomb_t e(1.6021766208e-19)
elementary charge.
UnitTypeLhs() max(const UnitTypeLhs &lhs, const UnitTypeRhs &rhs)
Definition: base.h:3417
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:300
void swap(expected< T, E > &lhs, expected< T, E > &rhs) noexcept(noexcept(lhs.swap(rhs)))
Definition: expected:2438
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:1331
constexpr bool isPowerOf2_64(uint64_t Value)
Return true if the argument is a power of two > 0 (64 bit edition.)
Definition: MathExtras.h:243
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:323
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.
constexpr bool operator==(const unexpected< E > &lhs, const unexpected< E > &rhs)
Definition: expected:176
constexpr bool operator!=(const unexpected< E > &lhs, const unexpected< E > &rhs)
Definition: expected:180
A suitably aligned and sized character array member which can hold elements of any type.
Definition: AlignOf.h:27
const T & type
Definition: type_traits.h:64
Definition: DenseMap.h:43
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