Skip to content

Commit 317f782

Browse files
committed
[ADT] Return bool from SmallPtrSet::remove_if()
Return whether anything was removed. This matches the API of SetVector::remove_if() and is convenient for some future uses.
1 parent 86860be commit 317f782

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

llvm/include/llvm/ADT/SmallPtrSet.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -357,9 +357,11 @@ class SmallPtrSetImpl : public SmallPtrSetImplBase {
357357
return erase_imp(PtrTraits::getAsVoidPointer(Ptr));
358358
}
359359

360-
/// Remove elements that match the given predicate.
360+
/// Remove elements that match the given predicate. Returns whether anything
361+
/// was removed.
361362
template <typename UnaryPredicate>
362-
void remove_if(UnaryPredicate P) {
363+
bool remove_if(UnaryPredicate P) {
364+
bool Removed = false;
363365
for (const void **APtr = CurArray, **E = EndPointer(); APtr != E; ++APtr) {
364366
const void *Value = *APtr;
365367
if (Value == getTombstoneMarker() || Value == getEmptyMarker())
@@ -368,8 +370,10 @@ class SmallPtrSetImpl : public SmallPtrSetImplBase {
368370
if (P(Ptr)) {
369371
*APtr = getTombstoneMarker();
370372
++NumTombstones;
373+
Removed = true;
371374
}
372375
}
376+
return Removed;
373377
}
374378

375379
/// count - Return 1 if the specified pointer is in the set, 0 otherwise.

llvm/unittests/ADT/SmallPtrSetTest.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -422,8 +422,9 @@ TEST(SmallPtrSetTest, RemoveIf) {
422422
Set.erase(&Vals[0]); // Leave a tombstone.
423423

424424
// Remove odd elements.
425-
Set.remove_if([](int *Ptr) { return *Ptr % 2 != 0; });
425+
bool Removed = Set.remove_if([](int *Ptr) { return *Ptr % 2 != 0; });
426426
// We should only have element 2 left now.
427+
EXPECT_TRUE(Removed);
427428
EXPECT_EQ(Set.size(), 1u);
428429
EXPECT_TRUE(Set.contains(&Vals[2]));
429430

@@ -436,9 +437,13 @@ TEST(SmallPtrSetTest, RemoveIf) {
436437
Set.erase(&Vals[0]); // Leave a tombstone.
437438

438439
// Remove odd elements.
439-
Set.remove_if([](int *Ptr) { return *Ptr % 2 != 0; });
440+
Removed = Set.remove_if([](int *Ptr) { return *Ptr % 2 != 0; });
440441
// We should only have elements 2 and 4 left now.
442+
EXPECT_TRUE(Removed);
441443
EXPECT_EQ(Set.size(), 2u);
442444
EXPECT_TRUE(Set.contains(&Vals[2]));
443445
EXPECT_TRUE(Set.contains(&Vals[4]));
446+
447+
Removed = Set.remove_if([](int *Ptr) { return false; });
448+
EXPECT_FALSE(Removed);
444449
}

0 commit comments

Comments
 (0)