Skip to content

[ADT] Use range-based for loops in SmallPtrSet.h (NFC) #152821

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
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
38 changes: 20 additions & 18 deletions llvm/include/llvm/ADT/SmallPtrSet.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "llvm/ADT/ADL.h"
#include "llvm/ADT/EpochTracker.h"
#include "llvm/ADT/STLForwardCompat.h"
#include "llvm/ADT/iterator_range.h"
#include "llvm/Support/Compiler.h"
#include "llvm/Support/MathExtras.h"
#include "llvm/Support/ReverseIteration.h"
Expand Down Expand Up @@ -147,17 +148,23 @@ class SmallPtrSetImplBase : public DebugEpochBase {
return isSmall() ? CurArray + NumNonEmpty : CurArray + CurArraySize;
}

iterator_range<const void **> small_buckets() {
return make_range(CurArray, CurArray + NumNonEmpty);
}

iterator_range<const void *const *> small_buckets() const {
return {CurArray, CurArray + NumNonEmpty};
}

/// 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.
std::pair<const void *const *, bool> insert_imp(const void *Ptr) {
if (isSmall()) {
// Check to see if it is already in the set.
for (const void **APtr = CurArray, **E = CurArray + NumNonEmpty;
APtr != E; ++APtr) {
const void *Value = *APtr;
if (Value == Ptr)
return std::make_pair(APtr, false);
for (const void *&Bucket : small_buckets()) {
if (Bucket == Ptr)
return std::make_pair(&Bucket, false);
}

// Nope, there isn't. If we stay small, just 'pushback' now.
Expand All @@ -177,10 +184,9 @@ class SmallPtrSetImplBase : public DebugEpochBase {
/// in.
bool erase_imp(const void * Ptr) {
if (isSmall()) {
for (const void **APtr = CurArray, **E = CurArray + NumNonEmpty;
APtr != E; ++APtr) {
if (*APtr == Ptr) {
*APtr = CurArray[--NumNonEmpty];
for (const void *&Bucket : small_buckets()) {
if (Bucket == Ptr) {
Bucket = CurArray[--NumNonEmpty];
incrementEpoch();
return true;
}
Expand All @@ -206,11 +212,9 @@ class SmallPtrSetImplBase : public DebugEpochBase {
const void *const * find_imp(const void * Ptr) const {
if (isSmall()) {
// Linear search for the item.
for (const void *const *APtr = CurArray, *const *E =
CurArray + NumNonEmpty;
APtr != E; ++APtr)
if (*APtr == Ptr)
return APtr;
for (const void *const &Bucket : small_buckets())
if (Bucket == Ptr)
return &Bucket;
return EndPointer();
}

Expand All @@ -223,10 +227,8 @@ class SmallPtrSetImplBase : public DebugEpochBase {
bool contains_imp(const void *Ptr) const {
if (isSmall()) {
// Linear search for the item.
const void *const *APtr = CurArray;
const void *const *E = CurArray + NumNonEmpty;
for (; APtr != E; ++APtr)
if (*APtr == Ptr)
for (const void *const &Bucket : small_buckets())
if (Bucket == Ptr)
return true;
return false;
}
Expand Down