Skip to content

Commit 92164fa

Browse files
[ADT] Use range-based for loops in SmallPtrSet.h (NFC) (llvm#152821)
This patch defines a couple of helper functions so that we can convert four loops to range-based for loops.
1 parent 97f0ff0 commit 92164fa

File tree

1 file changed

+20
-18
lines changed

1 file changed

+20
-18
lines changed

llvm/include/llvm/ADT/SmallPtrSet.h

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "llvm/ADT/ADL.h"
1919
#include "llvm/ADT/EpochTracker.h"
2020
#include "llvm/ADT/STLForwardCompat.h"
21+
#include "llvm/ADT/iterator_range.h"
2122
#include "llvm/Support/Compiler.h"
2223
#include "llvm/Support/MathExtras.h"
2324
#include "llvm/Support/ReverseIteration.h"
@@ -147,17 +148,23 @@ class SmallPtrSetImplBase : public DebugEpochBase {
147148
return isSmall() ? CurArray + NumNonEmpty : CurArray + CurArraySize;
148149
}
149150

151+
iterator_range<const void **> small_buckets() {
152+
return make_range(CurArray, CurArray + NumNonEmpty);
153+
}
154+
155+
iterator_range<const void *const *> small_buckets() const {
156+
return {CurArray, CurArray + NumNonEmpty};
157+
}
158+
150159
/// insert_imp - This returns true if the pointer was new to the set, false if
151160
/// it was already in the set. This is hidden from the client so that the
152161
/// derived class can check that the right type of pointer is passed in.
153162
std::pair<const void *const *, bool> insert_imp(const void *Ptr) {
154163
if (isSmall()) {
155164
// Check to see if it is already in the set.
156-
for (const void **APtr = CurArray, **E = CurArray + NumNonEmpty;
157-
APtr != E; ++APtr) {
158-
const void *Value = *APtr;
159-
if (Value == Ptr)
160-
return std::make_pair(APtr, false);
165+
for (const void *&Bucket : small_buckets()) {
166+
if (Bucket == Ptr)
167+
return std::make_pair(&Bucket, false);
161168
}
162169

163170
// Nope, there isn't. If we stay small, just 'pushback' now.
@@ -177,10 +184,9 @@ class SmallPtrSetImplBase : public DebugEpochBase {
177184
/// in.
178185
bool erase_imp(const void * Ptr) {
179186
if (isSmall()) {
180-
for (const void **APtr = CurArray, **E = CurArray + NumNonEmpty;
181-
APtr != E; ++APtr) {
182-
if (*APtr == Ptr) {
183-
*APtr = CurArray[--NumNonEmpty];
187+
for (const void *&Bucket : small_buckets()) {
188+
if (Bucket == Ptr) {
189+
Bucket = CurArray[--NumNonEmpty];
184190
incrementEpoch();
185191
return true;
186192
}
@@ -206,11 +212,9 @@ class SmallPtrSetImplBase : public DebugEpochBase {
206212
const void *const * find_imp(const void * Ptr) const {
207213
if (isSmall()) {
208214
// Linear search for the item.
209-
for (const void *const *APtr = CurArray, *const *E =
210-
CurArray + NumNonEmpty;
211-
APtr != E; ++APtr)
212-
if (*APtr == Ptr)
213-
return APtr;
215+
for (const void *const &Bucket : small_buckets())
216+
if (Bucket == Ptr)
217+
return &Bucket;
214218
return EndPointer();
215219
}
216220

@@ -223,10 +227,8 @@ class SmallPtrSetImplBase : public DebugEpochBase {
223227
bool contains_imp(const void *Ptr) const {
224228
if (isSmall()) {
225229
// Linear search for the item.
226-
const void *const *APtr = CurArray;
227-
const void *const *E = CurArray + NumNonEmpty;
228-
for (; APtr != E; ++APtr)
229-
if (*APtr == Ptr)
230+
for (const void *const &Bucket : small_buckets())
231+
if (Bucket == Ptr)
230232
return true;
231233
return false;
232234
}

0 commit comments

Comments
 (0)