Skip to content

Commit fddb8fe

Browse files
[ADT] Simplify SmallPtrSetIterator with std::reverse_iterator (NFC) (#160643)
SmallPtrSetIterator has two tasks: - iterate the buckets in the requested direction - skip the empty and tombstone buckets These tasks are intertwined in the current implementation. This patch separates them. A new private iterator type, BucketItTy, now handles the iteration direction. This is an alias for a raw pointer for forward iteration and std::reverse_iterator<pointer> for reverse iteration. The user-facing iterator now focuses solely on advancing BucketItTy while skipping invalid (empty or tombstone) buckets. AdvanceIfNotValid now works transparently for both directions. operator++ on BucketItTy does the right thing whether it's a raw pointer or a std::reverse_iterator. This simplification removes RetreatIfNotValid and the reverse-iteration logic in operator*() and operator++().
1 parent 5953233 commit fddb8fe

File tree

1 file changed

+7
-23
lines changed

1 file changed

+7
-23
lines changed

llvm/include/llvm/ADT/SmallPtrSet.h

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -281,16 +281,17 @@ class SmallPtrSetImplBase : public DebugEpochBase {
281281
/// instances of SmallPtrSetIterator.
282282
class SmallPtrSetIteratorImpl {
283283
protected:
284-
const void *const *Bucket;
285-
const void *const *End;
284+
using BucketItTy =
285+
std::conditional_t<shouldReverseIterate(),
286+
std::reverse_iterator<const void *const *>,
287+
const void *const *>;
288+
289+
BucketItTy Bucket;
290+
BucketItTy End;
286291

287292
public:
288293
explicit SmallPtrSetIteratorImpl(const void *const *BP, const void *const *E)
289294
: Bucket(BP), End(E) {
290-
if (shouldReverseIterate()) {
291-
RetreatIfNotValid();
292-
return;
293-
}
294295
AdvanceIfNotValid();
295296
}
296297

@@ -312,14 +313,6 @@ class SmallPtrSetIteratorImpl {
312313
*Bucket == SmallPtrSetImplBase::getTombstoneMarker()))
313314
++Bucket;
314315
}
315-
void RetreatIfNotValid() {
316-
assert(Bucket >= End);
317-
while (Bucket != End &&
318-
(Bucket[-1] == SmallPtrSetImplBase::getEmptyMarker() ||
319-
Bucket[-1] == SmallPtrSetImplBase::getTombstoneMarker())) {
320-
--Bucket;
321-
}
322-
}
323316
};
324317

325318
/// SmallPtrSetIterator - This implements a const_iterator for SmallPtrSet.
@@ -344,21 +337,12 @@ class LLVM_DEBUGEPOCHBASE_HANDLEBASE_EMPTYBASE SmallPtrSetIterator
344337

345338
const PtrTy operator*() const {
346339
assert(isHandleInSync() && "invalid iterator access!");
347-
if (shouldReverseIterate()) {
348-
assert(Bucket > End);
349-
return PtrTraits::getFromVoidPointer(const_cast<void *>(Bucket[-1]));
350-
}
351340
assert(Bucket < End);
352341
return PtrTraits::getFromVoidPointer(const_cast<void *>(*Bucket));
353342
}
354343

355344
inline SmallPtrSetIterator &operator++() { // Preincrement
356345
assert(isHandleInSync() && "invalid iterator access!");
357-
if (shouldReverseIterate()) {
358-
--Bucket;
359-
RetreatIfNotValid();
360-
return *this;
361-
}
362346
++Bucket;
363347
AdvanceIfNotValid();
364348
return *this;

0 commit comments

Comments
 (0)