Skip to content

[ADT] Use a range-based for loop in DenseMap.h (NFC) #152438

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 7, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 16 additions & 13 deletions llvm/include/llvm/ADT/DenseMap.h
Original file line number Diff line number Diff line change
Expand Up @@ -454,28 +454,28 @@ class DenseMapBase : public DebugEpochBase {
return NextPowerOf2(NumEntries * 4 / 3 + 1);
}

void moveFromOldBuckets(BucketT *OldBucketsBegin, BucketT *OldBucketsEnd) {
void moveFromOldBuckets(iterator_range<BucketT *> OldBuckets) {
initEmpty();

// Insert all the old elements.
const KeyT EmptyKey = getEmptyKey();
const KeyT TombstoneKey = getTombstoneKey();
for (BucketT *B = OldBucketsBegin, *E = OldBucketsEnd; B != E; ++B) {
if (!KeyInfoT::isEqual(B->getFirst(), EmptyKey) &&
!KeyInfoT::isEqual(B->getFirst(), TombstoneKey)) {
for (BucketT &B : OldBuckets) {
if (!KeyInfoT::isEqual(B.getFirst(), EmptyKey) &&
!KeyInfoT::isEqual(B.getFirst(), TombstoneKey)) {
// Insert the key/value into the new table.
BucketT *DestBucket;
bool FoundVal = LookupBucketFor(B->getFirst(), DestBucket);
bool FoundVal = LookupBucketFor(B.getFirst(), DestBucket);
(void)FoundVal; // silence warning.
assert(!FoundVal && "Key already in new map?");
DestBucket->getFirst() = std::move(B->getFirst());
::new (&DestBucket->getSecond()) ValueT(std::move(B->getSecond()));
DestBucket->getFirst() = std::move(B.getFirst());
::new (&DestBucket->getSecond()) ValueT(std::move(B.getSecond()));
incrementNumEntries();

// Free the value.
B->getSecond().~ValueT();
B.getSecond().~ValueT();
}
B->getFirst().~KeyT();
B.getFirst().~KeyT();
}
}

Expand Down Expand Up @@ -867,7 +867,8 @@ class DenseMap : public DenseMapBase<DenseMap<KeyT, ValueT, KeyInfoT, BucketT>,
return;
}

this->moveFromOldBuckets(OldBuckets, OldBuckets + OldNumBuckets);
this->moveFromOldBuckets(
llvm::make_range(OldBuckets, OldBuckets + OldNumBuckets));

// Free the old table.
deallocate_buffer(OldBuckets, sizeof(BucketT) * OldNumBuckets,
Expand Down Expand Up @@ -952,6 +953,9 @@ class SmallDenseMap
struct LargeRep {
BucketT *Buckets;
unsigned NumBuckets;
iterator_range<BucketT *> buckets() {
return llvm::make_range(Buckets, Buckets + NumBuckets);
}
};

/// A "union" of an inline bucket array and the struct representing
Expand Down Expand Up @@ -1129,7 +1133,7 @@ class SmallDenseMap
Small = false;
new (getLargeRep()) LargeRep(allocateBuckets(AtLeast));
}
this->moveFromOldBuckets(TmpBegin, TmpEnd);
this->moveFromOldBuckets(llvm::make_range(TmpBegin, TmpEnd));
return;
}

Expand All @@ -1141,8 +1145,7 @@ class SmallDenseMap
new (getLargeRep()) LargeRep(allocateBuckets(AtLeast));
}

this->moveFromOldBuckets(OldRep.Buckets,
OldRep.Buckets + OldRep.NumBuckets);
this->moveFromOldBuckets(OldRep.buckets());

// Free the old table.
deallocate_buffer(OldRep.Buckets, sizeof(BucketT) * OldRep.NumBuckets,
Expand Down