Skip to content

Commit 97aa932

Browse files
kazutakahiratamahesh-attarde
authored andcommitted
[ADT] Refactor SmallPtrSetIterator (NFC) (llvm#160814)
SmallPtrSetIterator and its base class SmallPtrSetIteratorImpl collectively have the following responsibilities: - type-safe user-facing iterator interface - type-erased iterator increment/dereference core - DebugEpochBase via inheritance This patch refactors the two classes so that SmallPtrSetIteratorImpl implements everything except the type-safe user-facing interface. Benefits: - DebugEpochBase::HandleBase is now part of the type-erased class. - AdvanceIfNotValid is now private in SmallPtrSetIteratorImpl. - SmallPtrSetIterator is a very thin wrapper around SmallPtrSetIteratorImpl and should generate very little code on its own.
1 parent d553d3b commit 97aa932

File tree

1 file changed

+30
-25
lines changed

1 file changed

+30
-25
lines changed

llvm/include/llvm/ADT/SmallPtrSet.h

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -279,19 +279,12 @@ class SmallPtrSetImplBase : public DebugEpochBase {
279279

280280
/// SmallPtrSetIteratorImpl - This is the common base class shared between all
281281
/// instances of SmallPtrSetIterator.
282-
class SmallPtrSetIteratorImpl {
283-
protected:
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;
291-
282+
class LLVM_DEBUGEPOCHBASE_HANDLEBASE_EMPTYBASE SmallPtrSetIteratorImpl
283+
: public DebugEpochBase::HandleBase {
292284
public:
293-
explicit SmallPtrSetIteratorImpl(const void *const *BP, const void *const *E)
294-
: Bucket(BP), End(E) {
285+
explicit SmallPtrSetIteratorImpl(const void *const *BP, const void *const *E,
286+
const DebugEpochBase &Epoch)
287+
: DebugEpochBase::HandleBase(&Epoch), Bucket(BP), End(E) {
295288
AdvanceIfNotValid();
296289
}
297290

@@ -303,6 +296,18 @@ class SmallPtrSetIteratorImpl {
303296
}
304297

305298
protected:
299+
void *dereference() const {
300+
assert(isHandleInSync() && "invalid iterator access!");
301+
assert(Bucket < End);
302+
return const_cast<void *>(*Bucket);
303+
}
304+
void increment() {
305+
assert(isHandleInSync() && "invalid iterator access!");
306+
++Bucket;
307+
AdvanceIfNotValid();
308+
}
309+
310+
private:
306311
/// AdvanceIfNotValid - If the current bucket isn't valid, advance to a bucket
307312
/// that is. This is guaranteed to stop because the end() bucket is marked
308313
/// valid.
@@ -313,13 +318,19 @@ class SmallPtrSetIteratorImpl {
313318
*Bucket == SmallPtrSetImplBase::getTombstoneMarker()))
314319
++Bucket;
315320
}
321+
322+
using BucketItTy =
323+
std::conditional_t<shouldReverseIterate(),
324+
std::reverse_iterator<const void *const *>,
325+
const void *const *>;
326+
327+
BucketItTy Bucket;
328+
BucketItTy End;
316329
};
317330

318331
/// SmallPtrSetIterator - This implements a const_iterator for SmallPtrSet.
319332
template <typename PtrTy>
320-
class LLVM_DEBUGEPOCHBASE_HANDLEBASE_EMPTYBASE SmallPtrSetIterator
321-
: public SmallPtrSetIteratorImpl,
322-
DebugEpochBase::HandleBase {
333+
class SmallPtrSetIterator : public SmallPtrSetIteratorImpl {
323334
using PtrTraits = PointerLikeTypeTraits<PtrTy>;
324335

325336
public:
@@ -329,28 +340,22 @@ class LLVM_DEBUGEPOCHBASE_HANDLEBASE_EMPTYBASE SmallPtrSetIterator
329340
using difference_type = std::ptrdiff_t;
330341
using iterator_category = std::forward_iterator_tag;
331342

332-
explicit SmallPtrSetIterator(const void *const *BP, const void *const *E,
333-
const DebugEpochBase &Epoch)
334-
: SmallPtrSetIteratorImpl(BP, E), DebugEpochBase::HandleBase(&Epoch) {}
343+
using SmallPtrSetIteratorImpl::SmallPtrSetIteratorImpl;
335344

336345
// Most methods are provided by the base class.
337346

338347
const PtrTy operator*() const {
339-
assert(isHandleInSync() && "invalid iterator access!");
340-
assert(Bucket < End);
341-
return PtrTraits::getFromVoidPointer(const_cast<void *>(*Bucket));
348+
return PtrTraits::getFromVoidPointer(dereference());
342349
}
343350

344351
inline SmallPtrSetIterator &operator++() { // Preincrement
345-
assert(isHandleInSync() && "invalid iterator access!");
346-
++Bucket;
347-
AdvanceIfNotValid();
352+
increment();
348353
return *this;
349354
}
350355

351356
SmallPtrSetIterator operator++(int) { // Postincrement
352357
SmallPtrSetIterator tmp = *this;
353-
++*this;
358+
increment();
354359
return tmp;
355360
}
356361
};

0 commit comments

Comments
 (0)