File tree Expand file tree Collapse file tree 2 files changed +13
-4
lines changed Expand file tree Collapse file tree 2 files changed +13
-4
lines changed Original file line number Diff line number Diff line change @@ -357,9 +357,11 @@ class SmallPtrSetImpl : public SmallPtrSetImplBase {
357
357
return erase_imp (PtrTraits::getAsVoidPointer (Ptr));
358
358
}
359
359
360
- // / Remove elements that match the given predicate.
360
+ // / Remove elements that match the given predicate. Returns whether anything
361
+ // / was removed.
361
362
template <typename UnaryPredicate>
362
- void remove_if (UnaryPredicate P) {
363
+ bool remove_if (UnaryPredicate P) {
364
+ bool Removed = false ;
363
365
for (const void **APtr = CurArray, **E = EndPointer (); APtr != E; ++APtr) {
364
366
const void *Value = *APtr;
365
367
if (Value == getTombstoneMarker () || Value == getEmptyMarker ())
@@ -368,8 +370,10 @@ class SmallPtrSetImpl : public SmallPtrSetImplBase {
368
370
if (P (Ptr)) {
369
371
*APtr = getTombstoneMarker ();
370
372
++NumTombstones;
373
+ Removed = true ;
371
374
}
372
375
}
376
+ return Removed;
373
377
}
374
378
375
379
// / count - Return 1 if the specified pointer is in the set, 0 otherwise.
Original file line number Diff line number Diff line change @@ -422,8 +422,9 @@ TEST(SmallPtrSetTest, RemoveIf) {
422
422
Set.erase (&Vals[0 ]); // Leave a tombstone.
423
423
424
424
// 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 ; });
426
426
// We should only have element 2 left now.
427
+ EXPECT_TRUE (Removed);
427
428
EXPECT_EQ (Set.size (), 1u );
428
429
EXPECT_TRUE (Set.contains (&Vals[2 ]));
429
430
@@ -436,9 +437,13 @@ TEST(SmallPtrSetTest, RemoveIf) {
436
437
Set.erase (&Vals[0 ]); // Leave a tombstone.
437
438
438
439
// Remove odd elements.
439
- Set.remove_if ([](int *Ptr) { return *Ptr % 2 != 0 ; });
440
+ Removed = Set.remove_if ([](int *Ptr) { return *Ptr % 2 != 0 ; });
440
441
// We should only have elements 2 and 4 left now.
442
+ EXPECT_TRUE (Removed);
441
443
EXPECT_EQ (Set.size (), 2u );
442
444
EXPECT_TRUE (Set.contains (&Vals[2 ]));
443
445
EXPECT_TRUE (Set.contains (&Vals[4 ]));
446
+
447
+ Removed = Set.remove_if ([](int *Ptr) { return false ; });
448
+ EXPECT_FALSE (Removed);
444
449
}
You can’t perform that action at this time.
0 commit comments