14#ifndef WPIUTIL_WPI_DENSEMAP_H
15#define WPIUTIL_WPI_DENSEMAP_H
29#include <initializer_list>
41template <
typename KeyT,
typename ValueT>
43 using std::pair<KeyT, ValueT>::pair;
47 ValueT &
getSecond() {
return std::pair<KeyT, ValueT>::second; }
48 const ValueT &
getSecond()
const {
return std::pair<KeyT, ValueT>::second; }
53template <
typename KeyT,
typename ValueT,
54 typename KeyInfoT = DenseMapInfo<KeyT>,
57class DenseMapIterator;
59template <
typename DerivedT,
typename KeyT,
typename ValueT,
typename KeyInfoT,
80 if (shouldReverseIterate<KeyT>())
81 return makeIterator(getBucketsEnd() - 1, getBuckets(), *
this);
82 return makeIterator(getBuckets(), getBucketsEnd(), *
this);
85 return makeIterator(getBucketsEnd(), getBucketsEnd(), *
this,
true);
90 if (shouldReverseIterate<KeyT>())
91 return makeConstIterator(getBucketsEnd() - 1, getBuckets(), *
this);
92 return makeConstIterator(getBuckets(), getBucketsEnd(), *
this);
95 return makeConstIterator(getBucketsEnd(), getBucketsEnd(), *
this,
true);
99 return getNumEntries() == 0;
101 unsigned size()
const {
return getNumEntries(); }
108 if (NumBuckets > getNumBuckets())
114 if (getNumEntries() == 0 && getNumTombstones() == 0)
return;
118 if (getNumEntries() * 4 < getNumBuckets() && getNumBuckets() > 64) {
124 if (std::is_trivially_destructible<ValueT>::value) {
126 for (BucketT *P = getBuckets(), *E = getBucketsEnd(); P != E; ++P)
127 P->getFirst() = EmptyKey;
129 [[maybe_unused]]
unsigned NumEntries = getNumEntries();
130 for (BucketT *P = getBuckets(), *E = getBucketsEnd(); P != E; ++P) {
131 if (!KeyInfoT::isEqual(P->getFirst(), EmptyKey)) {
132 if (!KeyInfoT::isEqual(P->getFirst(), TombstoneKey)) {
133 P->getSecond().~ValueT();
136 P->getFirst() = EmptyKey;
139 assert(NumEntries == 0 &&
"Node count imbalance!");
147 const BucketT *TheBucket;
148 return LookupBucketFor(Val, TheBucket) ? 1 : 0;
153 if (LookupBucketFor(Val, TheBucket))
154 return makeIterator(TheBucket,
155 shouldReverseIterate<KeyT>() ? getBuckets()
161 const BucketT *TheBucket;
162 if (LookupBucketFor(Val, TheBucket))
163 return makeConstIterator(TheBucket,
164 shouldReverseIterate<KeyT>() ? getBuckets()
175 template<
class LookupKeyT>
178 if (LookupBucketFor(Val, TheBucket))
179 return makeIterator(TheBucket,
180 shouldReverseIterate<KeyT>() ? getBuckets()
185 template<
class LookupKeyT>
187 const BucketT *TheBucket;
188 if (LookupBucketFor(Val, TheBucket))
189 return makeConstIterator(TheBucket,
190 shouldReverseIterate<KeyT>() ? getBuckets()
198 ValueT
lookup(const_arg_type_t<KeyT> Val)
const {
199 const BucketT *TheBucket;
200 if (LookupBucketFor(Val, TheBucket))
201 return TheBucket->getSecond();
208 std::pair<iterator, bool>
insert(
const std::pair<KeyT, ValueT> &KV) {
215 std::pair<iterator, bool>
insert(std::pair<KeyT, ValueT> &&KV) {
216 return try_emplace(std::move(KV.first), std::move(KV.second));
222 template <
typename... Ts>
223 std::pair<iterator, bool>
try_emplace(KeyT &&Key, Ts &&... Args) {
225 if (LookupBucketFor(Key, TheBucket))
226 return std::make_pair(makeIterator(TheBucket,
227 shouldReverseIterate<KeyT>()
235 InsertIntoBucket(TheBucket, std::move(Key), std::forward<Ts>(Args)...);
236 return std::make_pair(makeIterator(TheBucket,
237 shouldReverseIterate<KeyT>()
247 template <
typename... Ts>
248 std::pair<iterator, bool>
try_emplace(
const KeyT &Key, Ts &&... Args) {
250 if (LookupBucketFor(Key, TheBucket))
251 return std::make_pair(makeIterator(TheBucket,
252 shouldReverseIterate<KeyT>()
259 TheBucket = InsertIntoBucket(TheBucket, Key, std::forward<Ts>(Args)...);
260 return std::make_pair(makeIterator(TheBucket,
261 shouldReverseIterate<KeyT>()
273 template <
typename LookupKeyT>
274 std::pair<iterator, bool>
insert_as(std::pair<KeyT, ValueT> &&KV,
275 const LookupKeyT &Val) {
277 if (LookupBucketFor(Val, TheBucket))
278 return std::make_pair(makeIterator(TheBucket,
279 shouldReverseIterate<KeyT>()
286 TheBucket = InsertIntoBucketWithLookup(TheBucket, std::move(KV.first),
287 std::move(KV.second), Val);
288 return std::make_pair(makeIterator(TheBucket,
289 shouldReverseIterate<KeyT>()
297 template<
typename InputIt>
305 if (!LookupBucketFor(Val, TheBucket))
308 TheBucket->getSecond().~ValueT();
310 decrementNumEntries();
311 incrementNumTombstones();
315 BucketT *TheBucket = &*I;
316 TheBucket->getSecond().~ValueT();
318 decrementNumEntries();
319 incrementNumTombstones();
324 if (LookupBucketFor(Key, TheBucket))
327 return *InsertIntoBucket(TheBucket, Key);
336 if (LookupBucketFor(Key, TheBucket))
339 return *InsertIntoBucket(TheBucket, std::move(Key));
350 return Ptr >= getBuckets() && Ptr < getBucketsEnd();
362 if (getNumBuckets() == 0)
366 for (BucketT *P = getBuckets(), *E = getBucketsEnd(); P != E; ++P) {
367 if (!KeyInfoT::isEqual(P->getFirst(), EmptyKey) &&
368 !KeyInfoT::isEqual(P->getFirst(), TombstoneKey))
369 P->getSecond().~ValueT();
370 P->getFirst().~KeyT();
378 assert((getNumBuckets() & (getNumBuckets()-1)) == 0 &&
379 "# initial buckets must be a power of two!");
381 for (BucketT *B = getBuckets(), *E = getBucketsEnd(); B != E; ++B)
382 ::new (&B->getFirst()) KeyT(EmptyKey);
393 return static_cast<unsigned>(
NextPowerOf2(NumEntries * 4 / 3 + 1));
402 for (BucketT *B = OldBucketsBegin, *E = OldBucketsEnd; B != E; ++B) {
403 if (!KeyInfoT::isEqual(B->getFirst(), EmptyKey) &&
404 !KeyInfoT::isEqual(B->getFirst(), TombstoneKey)) {
407 bool FoundVal = LookupBucketFor(B->getFirst(), DestBucket);
409 assert(!FoundVal &&
"Key already in new map?");
410 DestBucket->getFirst() = std::move(B->getFirst());
411 ::new (&DestBucket->getSecond()) ValueT(std::move(B->getSecond()));
412 incrementNumEntries();
415 B->getSecond().~ValueT();
417 B->getFirst().~KeyT();
421 template <
typename OtherBaseT>
424 assert(&other !=
this);
425 assert(getNumBuckets() == other.getNumBuckets());
427 setNumEntries(other.getNumEntries());
428 setNumTombstones(other.getNumTombstones());
430 if (std::is_trivially_copyable<KeyT>::value &&
431 std::is_trivially_copyable<ValueT>::value)
432 memcpy(
reinterpret_cast<void *
>(getBuckets()), other.getBuckets(),
433 getNumBuckets() *
sizeof(BucketT));
435 for (
size_t i = 0; i < getNumBuckets(); ++i) {
436 ::new (&getBuckets()[i].getFirst())
437 KeyT(other.getBuckets()[i].getFirst());
438 if (!KeyInfoT::isEqual(getBuckets()[i].getFirst(),
getEmptyKey()) &&
440 ::new (&getBuckets()[i].getSecond())
441 ValueT(other.getBuckets()[i].getSecond());
446 return KeyInfoT::getHashValue(Val);
449 template<
typename LookupKeyT>
451 return KeyInfoT::getHashValue(Val);
455 static_assert(std::is_base_of<DenseMapBase, DerivedT>::value,
456 "Must pass the derived type to this template!");
457 return KeyInfoT::getEmptyKey();
461 return KeyInfoT::getTombstoneKey();
465 iterator makeIterator(BucketT *P, BucketT *E,
467 bool NoAdvance=
false) {
468 if (shouldReverseIterate<KeyT>()) {
469 BucketT *B = P == getBucketsEnd() ? getBuckets() : P + 1;
470 return iterator(B, E, Epoch, NoAdvance);
472 return iterator(P, E, Epoch, NoAdvance);
475 const_iterator makeConstIterator(
const BucketT *P,
const BucketT *E,
477 const bool NoAdvance=
false)
const {
478 if (shouldReverseIterate<KeyT>()) {
479 const BucketT *B = P == getBucketsEnd() ? getBuckets() : P + 1;
485 unsigned getNumEntries()
const {
486 return static_cast<const DerivedT *
>(
this)->getNumEntries();
489 void setNumEntries(
unsigned Num) {
490 static_cast<DerivedT *
>(
this)->setNumEntries(Num);
493 void incrementNumEntries() {
494 setNumEntries(getNumEntries() + 1);
497 void decrementNumEntries() {
498 setNumEntries(getNumEntries() - 1);
501 unsigned getNumTombstones()
const {
502 return static_cast<const DerivedT *
>(
this)->getNumTombstones();
505 void setNumTombstones(
unsigned Num) {
506 static_cast<DerivedT *
>(
this)->setNumTombstones(Num);
509 void incrementNumTombstones() {
510 setNumTombstones(getNumTombstones() + 1);
513 void decrementNumTombstones() {
514 setNumTombstones(getNumTombstones() - 1);
517 const BucketT *getBuckets()
const {
518 return static_cast<const DerivedT *
>(
this)->getBuckets();
521 BucketT *getBuckets() {
522 return static_cast<DerivedT *
>(
this)->getBuckets();
525 unsigned getNumBuckets()
const {
526 return static_cast<const DerivedT *
>(
this)->getNumBuckets();
529 BucketT *getBucketsEnd() {
530 return getBuckets() + getNumBuckets();
533 const BucketT *getBucketsEnd()
const {
534 return getBuckets() + getNumBuckets();
537 void grow(
unsigned AtLeast) {
538 static_cast<DerivedT *
>(
this)->grow(AtLeast);
541 void shrink_and_clear() {
542 static_cast<DerivedT *
>(
this)->shrink_and_clear();
545 template <
typename KeyArg,
typename... ValueArgs>
546 BucketT *InsertIntoBucket(BucketT *TheBucket, KeyArg &&Key,
547 ValueArgs &&... Values) {
548 TheBucket = InsertIntoBucketImpl(Key, Key, TheBucket);
550 TheBucket->getFirst() = std::forward<KeyArg>(Key);
551 ::new (&TheBucket->getSecond()) ValueT(
std::forward<ValueArgs>(Values)...);
555 template <typename LookupKeyT>
556 BucketT *InsertIntoBucketWithLookup(BucketT *TheBucket, KeyT &&Key,
557 ValueT &&Value, LookupKeyT &Lookup) {
558 TheBucket = InsertIntoBucketImpl(Key, Lookup, TheBucket);
560 TheBucket->getFirst() = std::move(Key);
561 ::new (&TheBucket->getSecond()) ValueT(
std::move(Value));
565 template <typename LookupKeyT>
566 BucketT *InsertIntoBucketImpl(const KeyT &Key, const LookupKeyT &Lookup,
567 BucketT *TheBucket) {
579 unsigned NewNumEntries = getNumEntries() + 1;
580 unsigned NumBuckets = getNumBuckets();
582 this->grow(NumBuckets * 2);
583 LookupBucketFor(Lookup, TheBucket);
584 NumBuckets = getNumBuckets();
585 }
else if (
LLVM_UNLIKELY(NumBuckets-(NewNumEntries+getNumTombstones()) <=
587 this->grow(NumBuckets);
588 LookupBucketFor(Lookup, TheBucket);
594 incrementNumEntries();
598 if (!KeyInfoT::isEqual(TheBucket->getFirst(), EmptyKey))
599 decrementNumTombstones();
608 template<
typename LookupKeyT>
609 bool LookupBucketFor(
const LookupKeyT &Val,
610 const BucketT *&FoundBucket)
const {
611 const BucketT *BucketsPtr = getBuckets();
612 const unsigned NumBuckets = getNumBuckets();
614 if (NumBuckets == 0) {
615 FoundBucket =
nullptr;
620 const BucketT *FoundTombstone =
nullptr;
623 assert(!KeyInfoT::isEqual(Val, EmptyKey) &&
624 !KeyInfoT::isEqual(Val, TombstoneKey) &&
625 "Empty/Tombstone value shouldn't be inserted into map!");
628 unsigned ProbeAmt = 1;
630 const BucketT *ThisBucket = BucketsPtr + BucketNo;
632 if (
LLVM_LIKELY(KeyInfoT::isEqual(Val, ThisBucket->getFirst()))) {
633 FoundBucket = ThisBucket;
639 if (
LLVM_LIKELY(KeyInfoT::isEqual(ThisBucket->getFirst(), EmptyKey))) {
642 FoundBucket = FoundTombstone ? FoundTombstone : ThisBucket;
648 if (KeyInfoT::isEqual(ThisBucket->getFirst(), TombstoneKey) &&
650 FoundTombstone = ThisBucket;
654 BucketNo += ProbeAmt++;
655 BucketNo &= (NumBuckets-1);
659 template <
typename LookupKeyT>
660 bool LookupBucketFor(
const LookupKeyT &Val, BucketT *&FoundBucket) {
661 const BucketT *ConstFoundBucket;
663 ->LookupBucketFor(Val, ConstFoundBucket);
664 FoundBucket =
const_cast<BucketT *
>(ConstFoundBucket);
674 return getNumBuckets() *
sizeof(BucketT);
684template <
typename DerivedT,
typename KeyT,
typename ValueT,
typename KeyInfoT,
692 for (
auto &KV : LHS) {
693 auto I = RHS.
find(KV.first);
694 if (I == RHS.
end() || I->second != KV.second)
704template <
typename DerivedT,
typename KeyT,
typename ValueT,
typename KeyInfoT,
709 return !(LHS == RHS);
712template <
typename KeyT,
typename ValueT,
713 typename KeyInfoT = DenseMapInfo<KeyT>,
716 KeyT, ValueT, KeyInfoT, BucketT> {
725 unsigned NumTombstones;
731 explicit DenseMap(
unsigned InitialReserve = 0) { init(InitialReserve); }
743 template<
typename InputIt>
745 init(std::distance(I, E));
749 DenseMap(std::initializer_list<typename BaseT::value_type> Vals) {
751 this->
insert(Vals.begin(), Vals.end());
764 std::swap(NumTombstones, RHS.NumTombstones);
785 if (allocateBuckets(other.NumBuckets)) {
786 this->BaseT::copyFrom(other);
793 void init(
unsigned InitNumEntries) {
794 auto InitBuckets = BaseT::getMinBucketToReserveForEntries(InitNumEntries);
795 if (allocateBuckets(InitBuckets)) {
796 this->BaseT::initEmpty();
804 unsigned OldNumBuckets = NumBuckets;
805 BucketT *OldBuckets = Buckets;
807 allocateBuckets(std::max<unsigned>(64,
static_cast<unsigned>(
NextPowerOf2(AtLeast-1))));
810 this->BaseT::initEmpty();
822 unsigned OldNumBuckets = NumBuckets;
823 unsigned OldNumEntries = NumEntries;
827 unsigned NewNumBuckets = 0;
830 if (NewNumBuckets == NumBuckets) {
831 this->BaseT::initEmpty();
841 unsigned getNumEntries()
const {
845 void setNumEntries(
unsigned Num) {
849 unsigned getNumTombstones()
const {
850 return NumTombstones;
853 void setNumTombstones(
unsigned Num) {
857 BucketT *getBuckets()
const {
861 unsigned getNumBuckets()
const {
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 init(NumInitBuckets);
922 template<
typename InputIt>
937 unsigned TmpNumEntries = RHS.NumEntries;
938 RHS.NumEntries = NumEntries;
939 NumEntries = TmpNumEntries;
940 std::swap(NumTombstones, RHS.NumTombstones);
944 if (
Small && RHS.Small) {
949 for (
unsigned i = 0,
e = InlineBuckets; i !=
e; ++i) {
950 BucketT *LHSB = &getInlineBuckets()[i],
951 *RHSB = &RHS.getInlineBuckets()[i];
952 bool hasLHSValue = (!KeyInfoT::isEqual(LHSB->getFirst(), EmptyKey) &&
953 !KeyInfoT::isEqual(LHSB->getFirst(), TombstoneKey));
954 bool hasRHSValue = (!KeyInfoT::isEqual(RHSB->getFirst(), EmptyKey) &&
955 !KeyInfoT::isEqual(RHSB->getFirst(), TombstoneKey));
956 if (hasLHSValue && hasRHSValue) {
962 std::swap(LHSB->getFirst(), RHSB->getFirst());
964 ::new (&RHSB->getSecond()) ValueT(std::move(LHSB->getSecond()));
965 LHSB->getSecond().~ValueT();
966 }
else if (hasRHSValue) {
967 ::new (&LHSB->getSecond()) ValueT(std::move(RHSB->getSecond()));
968 RHSB->getSecond().~ValueT();
973 if (!
Small && !RHS.Small) {
974 std::swap(getLargeRep()->Buckets, RHS.getLargeRep()->Buckets);
975 std::swap(getLargeRep()->NumBuckets, RHS.getLargeRep()->NumBuckets);
983 LargeRep TmpRep = std::move(*LargeSide.getLargeRep());
984 LargeSide.getLargeRep()->~LargeRep();
985 LargeSide.Small =
true;
990 for (
unsigned i = 0,
e = InlineBuckets; i !=
e; ++i) {
991 BucketT *NewB = &LargeSide.getInlineBuckets()[i],
992 *OldB = &SmallSide.getInlineBuckets()[i];
993 ::new (&NewB->getFirst()) KeyT(std::move(OldB->getFirst()));
994 OldB->getFirst().~KeyT();
995 if (!KeyInfoT::isEqual(NewB->getFirst(), EmptyKey) &&
996 !KeyInfoT::isEqual(NewB->getFirst(), TombstoneKey)) {
997 ::new (&NewB->getSecond()) ValueT(std::move(OldB->getSecond()));
998 OldB->getSecond().~ValueT();
1004 SmallSide.Small =
false;
1005 new (SmallSide.getLargeRep()) LargeRep(std::move(TmpRep));
1016 deallocateBuckets();
1024 deallocateBuckets();
1026 if (other.getNumBuckets() > InlineBuckets) {
1028 new (getLargeRep()) LargeRep(allocateBuckets(other.getNumBuckets()));
1030 this->BaseT::copyFrom(other);
1035 if (InitBuckets > InlineBuckets) {
1037 new (getLargeRep()) LargeRep(allocateBuckets(InitBuckets));
1039 this->BaseT::initEmpty();
1043 if (AtLeast > InlineBuckets)
1044 AtLeast = std::max<unsigned>(64,
NextPowerOf2(AtLeast-1));
1049 BucketT *TmpBegin =
reinterpret_cast<BucketT *
>(&TmpStorage);
1050 BucketT *TmpEnd = TmpBegin;
1056 for (BucketT *P = getBuckets(), *E = P + InlineBuckets; P != E; ++P) {
1057 if (!KeyInfoT::isEqual(P->getFirst(), EmptyKey) &&
1058 !KeyInfoT::isEqual(P->getFirst(), TombstoneKey)) {
1059 assert(
size_t(TmpEnd - TmpBegin) < InlineBuckets &&
1060 "Too many inline buckets!");
1061 ::new (&TmpEnd->getFirst()) KeyT(std::move(P->getFirst()));
1062 ::new (&TmpEnd->getSecond()) ValueT(std::move(P->getSecond()));
1064 P->getSecond().~ValueT();
1066 P->getFirst().~KeyT();
1072 if (AtLeast > InlineBuckets) {
1074 new (getLargeRep()) LargeRep(allocateBuckets(AtLeast));
1080 LargeRep OldRep = std::move(*getLargeRep());
1081 getLargeRep()->~LargeRep();
1082 if (AtLeast <= InlineBuckets) {
1085 new (getLargeRep()) LargeRep(allocateBuckets(AtLeast));
1096 unsigned OldSize = this->
size();
1100 unsigned NewNumBuckets = 0;
1103 if (NewNumBuckets > InlineBuckets && NewNumBuckets < 64u)
1106 if ((
Small && NewNumBuckets <= InlineBuckets) ||
1107 (!
Small && NewNumBuckets == getLargeRep()->NumBuckets)) {
1108 this->BaseT::initEmpty();
1112 deallocateBuckets();
1113 init(NewNumBuckets);
1117 unsigned getNumEntries()
const {
1121 void setNumEntries(
unsigned Num) {
1123 assert(Num < (1U << 31) &&
"Cannot support more than 1<<31 entries");
1127 unsigned getNumTombstones()
const {
1128 return NumTombstones;
1131 void setNumTombstones(
unsigned Num) {
1132 NumTombstones = Num;
1135 const BucketT *getInlineBuckets()
const {
1140 return reinterpret_cast<const BucketT *
>(&storage);
1143 BucketT *getInlineBuckets() {
1144 return const_cast<BucketT *
>(
1145 const_cast<const SmallDenseMap *
>(
this)->getInlineBuckets());
1148 const LargeRep *getLargeRep()
const {
1151 return reinterpret_cast<const LargeRep *
>(&storage);
1154 LargeRep *getLargeRep() {
1155 return const_cast<LargeRep *
>(
1156 const_cast<const SmallDenseMap *
>(
this)->getLargeRep());
1159 const BucketT *getBuckets()
const {
1160 return Small ? getInlineBuckets() : getLargeRep()->Buckets;
1163 BucketT *getBuckets() {
1164 return const_cast<BucketT *
>(
1165 const_cast<const SmallDenseMap *
>(
this)->getBuckets());
1168 unsigned getNumBuckets()
const {
1169 return Small ? InlineBuckets : getLargeRep()->NumBuckets;
1172 void deallocateBuckets() {
1177 sizeof(BucketT) * getLargeRep()->NumBuckets,
1179 getLargeRep()->~LargeRep();
1182 LargeRep allocateBuckets(
unsigned Num) {
1183 assert(Num > InlineBuckets &&
"Must allocate more buckets than are inline");
1185 sizeof(BucketT) * Num,
alignof(BucketT))),
1191template <
typename KeyT,
typename ValueT,
typename KeyInfoT,
typename Bucket,
1213 bool NoAdvance =
false)
1215 assert(isHandleInSync() &&
"invalid construction!");
1217 if (NoAdvance)
return;
1218 if (shouldReverseIterate<KeyT>()) {
1219 RetreatPastEmptyBuckets();
1222 AdvancePastEmptyBuckets();
1228 template <
bool IsConstSrc,
1229 typename = std::enable_if_t<!IsConstSrc && IsConst>>
1235 assert(isHandleInSync() &&
"invalid iterator access!");
1236 assert(Ptr != End &&
"dereferencing end() iterator");
1237 if (shouldReverseIterate<KeyT>())
1242 assert(isHandleInSync() &&
"invalid iterator access!");
1243 assert(Ptr != End &&
"dereferencing end() iterator");
1244 if (shouldReverseIterate<KeyT>())
1251 assert((!LHS.Ptr || LHS.isHandleInSync()) &&
"handle not in sync!");
1252 assert((!RHS.Ptr || RHS.isHandleInSync()) &&
"handle not in sync!");
1253 assert(LHS.getEpochAddress() == RHS.getEpochAddress() &&
1254 "comparing incomparable iterators!");
1255 return LHS.Ptr == RHS.Ptr;
1260 return !(LHS == RHS);
1264 assert(isHandleInSync() &&
"invalid iterator access!");
1265 assert(Ptr != End &&
"incrementing end() iterator");
1266 if (shouldReverseIterate<KeyT>()) {
1268 RetreatPastEmptyBuckets();
1272 AdvancePastEmptyBuckets();
1276 assert(isHandleInSync() &&
"invalid iterator access!");
1281 void AdvancePastEmptyBuckets() {
1283 const KeyT
Empty = KeyInfoT::getEmptyKey();
1284 const KeyT Tombstone = KeyInfoT::getTombstoneKey();
1286 while (Ptr != End && (KeyInfoT::isEqual(Ptr->getFirst(),
Empty) ||
1287 KeyInfoT::isEqual(Ptr->getFirst(), Tombstone)))
1291 void RetreatPastEmptyBuckets() {
1293 const KeyT
Empty = KeyInfoT::getEmptyKey();
1294 const KeyT Tombstone = KeyInfoT::getTombstoneKey();
1296 while (Ptr != End && (KeyInfoT::isEqual(Ptr[-1].getFirst(),
Empty) ||
1297 KeyInfoT::isEqual(Ptr[-1].getFirst(), Tombstone)))
1302template <
typename KeyT,
typename ValueT,
typename KeyInfoT>
#define LLVM_UNLIKELY(EXPR)
Definition: Compiler.h:250
#define LLVM_NODISCARD
LLVM_NODISCARD - Warn if a type or return value is discarded.
Definition: Compiler.h:177
#define LLVM_LIKELY(EXPR)
Definition: Compiler.h:249
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:57
A base class for data structure classes wishing to make iterators ("handles") pointing into themselve...
Definition: EpochTracker.h:35
void incrementEpoch()
Calling incrementEpoch invalidates all handles pointing into the calling instance.
Definition: EpochTracker.h:43
DebugEpochBase()
Definition: EpochTracker.h:39
Definition: DenseMap.h:61
void copyFrom(const DenseMapBase< OtherBaseT, KeyT, ValueT, KeyInfoT, BucketT > &other)
Definition: DenseMap.h:422
bool isPointerIntoBucketsArray(const void *Ptr) const
isPointerIntoBucketsArray - Return true if the specified pointer points somewhere into the DenseMap's...
Definition: DenseMap.h:349
LLVM_NODISCARD bool empty() const
Definition: DenseMap.h:98
const_iterator find_as(const LookupKeyT &Val) const
Definition: DenseMap.h:186
DenseMapIterator< KeyT, ValueT, KeyInfoT, BucketT > iterator
Definition: DenseMap.h:71
std::pair< iterator, bool > try_emplace(KeyT &&Key, Ts &&... Args)
Definition: DenseMap.h:223
iterator end()
Definition: DenseMap.h:84
std::pair< iterator, bool > insert(std::pair< KeyT, ValueT > &&KV)
Definition: DenseMap.h:215
static const KeyT getEmptyKey()
Definition: DenseMap.h:454
bool erase(const KeyT &Val)
Definition: DenseMap.h:303
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:146
ValueT mapped_type
Definition: DenseMap.h:68
void erase(iterator I)
Definition: DenseMap.h:314
void initEmpty()
Definition: DenseMap.h:374
unsigned size() const
Definition: DenseMap.h:101
static unsigned getHashValue(const KeyT &Val)
Definition: DenseMap.h:445
void reserve(size_type NumEntries)
Grow the densemap so that it can contain at least NumEntries items before resizing again.
Definition: DenseMap.h:105
void insert(InputIt I, InputIt E)
insert - Range insertion of pairs.
Definition: DenseMap.h:298
BucketT value_type
Definition: DenseMap.h:69
const void * getPointerIntoBucketsArray() const
getPointerIntoBucketsArray() - Return an opaque pointer into the buckets array.
Definition: DenseMap.h:356
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Definition: DenseMap.h:208
const_iterator end() const
Definition: DenseMap.h:94
KeyT key_type
Definition: DenseMap.h:67
value_type & FindAndConstruct(const KeyT &Key)
Definition: DenseMap.h:322
void clear()
Definition: DenseMap.h:112
void destroyAll()
Definition: DenseMap.h:361
DenseMapIterator< KeyT, ValueT, KeyInfoT, BucketT, true > const_iterator
Definition: DenseMap.h:73
iterator begin()
Definition: DenseMap.h:75
ValueT & operator[](const KeyT &Key)
Definition: DenseMap.h:330
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:198
static unsigned getHashValue(const LookupKeyT &Val)
Definition: DenseMap.h:450
unsigned size_type
Definition: DenseMap.h:66
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:274
unsigned getMinBucketToReserveForEntries(unsigned NumEntries)
Returns the number of buckets to allocate to ensure that the DenseMap can accommodate NumEntries with...
Definition: DenseMap.h:387
void moveFromOldBuckets(BucketT *OldBucketsBegin, BucketT *OldBucketsEnd)
Definition: DenseMap.h:396
const_iterator find(const_arg_type_t< KeyT > Val) const
Definition: DenseMap.h:160
static const KeyT getTombstoneKey()
Definition: DenseMap.h:460
ValueT & operator[](KeyT &&Key)
Definition: DenseMap.h:342
iterator find(const_arg_type_t< KeyT > Val)
Definition: DenseMap.h:151
value_type & FindAndConstruct(KeyT &&Key)
Definition: DenseMap.h:334
iterator find_as(const LookupKeyT &Val)
Alternate version of find() which allows a different, and possibly less expensive,...
Definition: DenseMap.h:176
size_t getMemorySize() const
Return the approximate size (in bytes) of the actual map.
Definition: DenseMap.h:673
const_iterator begin() const
Definition: DenseMap.h:87
std::pair< iterator, bool > try_emplace(const KeyT &Key, Ts &&... Args)
Definition: DenseMap.h:248
Definition: DenseMap.h:716
void init(unsigned InitNumEntries)
Definition: DenseMap.h:793
void copyFrom(const DenseMap &other)
Definition: DenseMap.h:782
DenseMap & operator=(DenseMap &&other)
Definition: DenseMap.h:774
DenseMap & operator=(const DenseMap &other)
Definition: DenseMap.h:768
DenseMap(const DenseMap &other)
Definition: DenseMap.h:733
DenseMap(std::initializer_list< typename BaseT::value_type > Vals)
Definition: DenseMap.h:749
void grow(unsigned AtLeast)
Definition: DenseMap.h:803
void shrink_and_clear()
Definition: DenseMap.h:821
DenseMap(const InputIt &I, const InputIt &E)
Definition: DenseMap.h:744
void swap(DenseMap &RHS)
Definition: DenseMap.h:759
DenseMap(DenseMap &&other)
Definition: DenseMap.h:738
DenseMap(unsigned InitialReserve=0)
Create a DenseMap with an optional InitialReserve that guarantee that this number of elements can be ...
Definition: DenseMap.h:731
~DenseMap()
Definition: DenseMap.h:754
Definition: DenseMap.h:1193
DenseMapIterator & operator++()
Definition: DenseMap.h:1263
friend bool operator!=(const DenseMapIterator &LHS, const DenseMapIterator &RHS)
Definition: DenseMap.h:1258
DenseMapIterator(const DenseMapIterator< KeyT, ValueT, KeyInfoT, Bucket, IsConstSrc > &I)
Definition: DenseMap.h:1230
typename std::conditional< IsConst, const Bucket, Bucket >::type value_type
Definition: DenseMap.h:1200
reference operator*() const
Definition: DenseMap.h:1234
pointer operator->() const
Definition: DenseMap.h:1241
DenseMapIterator()=default
DenseMapIterator(pointer Pos, pointer E, const DebugEpochBase &Epoch, bool NoAdvance=false)
Definition: DenseMap.h:1212
std::forward_iterator_tag iterator_category
Definition: DenseMap.h:1203
ptrdiff_t difference_type
Definition: DenseMap.h:1198
value_type * pointer
Definition: DenseMap.h:1201
DenseMapIterator operator++(int)
Definition: DenseMap.h:1275
friend bool operator==(const DenseMapIterator &LHS, const DenseMapIterator &RHS)
Definition: DenseMap.h:1249
value_type & reference
Definition: DenseMap.h:1202
Definition: DenseMap.h:884
~SmallDenseMap()
Definition: DenseMap.h:931
SmallDenseMap(unsigned NumInitBuckets=0)
Definition: DenseMap.h:908
void swap(SmallDenseMap &RHS)
Definition: DenseMap.h:936
void copyFrom(const SmallDenseMap &other)
Definition: DenseMap.h:1022
void init(unsigned InitBuckets)
Definition: DenseMap.h:1033
SmallDenseMap & operator=(SmallDenseMap &&other)
Definition: DenseMap.h:1014
SmallDenseMap(const SmallDenseMap &other)
Definition: DenseMap.h:912
SmallDenseMap(SmallDenseMap &&other)
Definition: DenseMap.h:917
void shrink_and_clear()
Definition: DenseMap.h:1095
void grow(unsigned AtLeast)
Definition: DenseMap.h:1042
SmallDenseMap(const InputIt &I, const InputIt &E)
Definition: DenseMap.h:923
SmallDenseMap(std::initializer_list< typename BaseT::value_type > Vals)
Definition: DenseMap.h:928
SmallDenseMap & operator=(const SmallDenseMap &other)
Definition: DenseMap.h:1008
type
Definition: core.h:575
constexpr common_t< T1, T2 > max(const T1 x, const T2 y) noexcept
Compile-time pairwise maximum function.
Definition: max.hpp:35
EIGEN_CONSTEXPR Index first(const T &x) EIGEN_NOEXCEPT
Definition: IndexedViewHelper.h:81
EIGEN_STRONG_INLINE void swap(T &a, T &b)
Definition: Meta.h:766
@ Small
Definition: GeneralProduct.h:18
Definition: format-inl.h:32
const int Empty
Definition: Eigen_Colamd.h:116
Definition: BFloat16.h:88
void swap(wpi::SmallPtrSet< T, N > &LHS, wpi::SmallPtrSet< T, N > &RHS)
Implement std::swap in terms of SmallPtrSet swap.
Definition: SmallPtrSet.h:512
static constexpr const charge::coulomb_t e(1.6021766208e-19)
elementary charge.
Definition: AprilTagFieldLayout.h:18
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:582
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:1303
uint64_t NextPowerOf2(uint64_t A)
Returns the next power of two (in 64-bits) that is strictly greater than A.
Definition: MathExtras.h:656
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:686
constexpr bool isPowerOf2_64(uint64_t Value)
Return true if the argument is a power of two > 0 (64 bit edition.)
Definition: MathExtras.h:469
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:706
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:42
KeyT & getFirst()
Definition: DenseMap.h:45
ValueT & getSecond()
Definition: DenseMap.h:47
const KeyT & getFirst() const
Definition: DenseMap.h:46
const ValueT & getSecond() const
Definition: DenseMap.h:48