diff options
-rw-r--r-- | AK/Vector.h | 5 | ||||
-rw-r--r-- | Tests/AK/TestVector.cpp | 23 |
2 files changed, 27 insertions, 1 deletions
diff --git a/AK/Vector.h b/AK/Vector.h index d04b34f83d..4dcedf933a 100644 --- a/AK/Vector.h +++ b/AK/Vector.h @@ -405,15 +405,18 @@ public: } template<typename TUnaryPredicate> - void remove_all_matching(TUnaryPredicate predicate) + bool remove_all_matching(TUnaryPredicate predicate) { + bool something_was_removed = false; for (size_t i = 0; i < size();) { if (predicate(at(i))) { remove(i); + something_was_removed = true; } else { ++i; } } + return something_was_removed; } ALWAYS_INLINE T take_last() diff --git a/Tests/AK/TestVector.cpp b/Tests/AK/TestVector.cpp index bf3d807431..2289c24639 100644 --- a/Tests/AK/TestVector.cpp +++ b/Tests/AK/TestVector.cpp @@ -259,6 +259,29 @@ TEST_CASE(vector_remove) EXPECT_EQ(ints[0], 4); } +TEST_CASE(remove_all_matching) +{ + Vector<int> ints; + + ints.append(1); + ints.append(2); + ints.append(3); + ints.append(4); + + EXPECT_EQ(ints.size(), 4u); + + EXPECT_EQ(ints.remove_all_matching([&](int value) { return value > 2; }), true); + EXPECT_EQ(ints.remove_all_matching([&](int) { return false; }), false); + + EXPECT_EQ(ints.size(), 2u); + + EXPECT_EQ(ints.remove_all_matching([&](int) { return true; }), true); + + EXPECT(ints.is_empty()); + + EXPECT_EQ(ints.remove_all_matching([&](int) { return true; }), false); +} + TEST_CASE(nonnullownptrvector) { struct Object { |