Skip to content

[ADT] Use range-based for loops in SmallPtrSet (NFC) #152882

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
Show file tree
Hide file tree
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
13 changes: 8 additions & 5 deletions llvm/include/llvm/ADT/SmallPtrSet.h
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,10 @@ class SmallPtrSetImplBase : public DebugEpochBase {
return {CurArray, CurArray + NumNonEmpty};
}

iterator_range<const void **> buckets() {
return make_range(CurArray, EndPointer());
}

/// insert_imp - This returns true if the pointer was new to the set, false if
/// it was already in the set. This is hidden from the client so that the
/// derived class can check that the right type of pointer is passed in.
Expand Down Expand Up @@ -441,13 +445,12 @@ class SmallPtrSetImpl : public SmallPtrSetImplBase {
return Removed;
}

for (const void **APtr = CurArray, **E = EndPointer(); APtr != E; ++APtr) {
const void *Value = *APtr;
if (Value == getTombstoneMarker() || Value == getEmptyMarker())
for (const void *&Bucket : buckets()) {
if (Bucket == getTombstoneMarker() || Bucket == getEmptyMarker())
continue;
PtrType Ptr = PtrTraits::getFromVoidPointer(const_cast<void *>(Value));
PtrType Ptr = PtrTraits::getFromVoidPointer(const_cast<void *>(Bucket));
if (P(Ptr)) {
*APtr = getTombstoneMarker();
Bucket = getTombstoneMarker();
++NumTombstones;
incrementEpoch();
Removed = true;
Expand Down
12 changes: 5 additions & 7 deletions llvm/lib/Support/SmallPtrSet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,7 @@ const void *const *SmallPtrSetImplBase::FindBucketFor(const void *Ptr) const {
/// Grow - Allocate a larger backing store for the buckets and move it over.
///
void SmallPtrSetImplBase::Grow(unsigned NewSize) {
const void **OldBuckets = CurArray;
const void **OldEnd = EndPointer();
auto OldBuckets = buckets();
bool WasSmall = isSmall();

// Install the new array. Clear all the buckets to empty.
Expand All @@ -123,15 +122,14 @@ void SmallPtrSetImplBase::Grow(unsigned NewSize) {
memset(CurArray, -1, NewSize*sizeof(void*));

// Copy over all valid entries.
for (const void **BucketPtr = OldBuckets; BucketPtr != OldEnd; ++BucketPtr) {
for (const void *&Bucket : OldBuckets) {
// Copy over the element if it is valid.
const void *Elt = *BucketPtr;
if (Elt != getTombstoneMarker() && Elt != getEmptyMarker())
*const_cast<void**>(FindBucketFor(Elt)) = const_cast<void*>(Elt);
if (Bucket != getTombstoneMarker() && Bucket != getEmptyMarker())
*const_cast<void **>(FindBucketFor(Bucket)) = const_cast<void *>(Bucket);
}

if (!WasSmall)
free(OldBuckets);
free(OldBuckets.begin());
NumNonEmpty -= NumTombstones;
NumTombstones = 0;
IsSmall = false;
Expand Down