mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 02:07:35 +00:00
AK: Make Vector::remove_all_matching() return removal success
This matches the behavior of other remove_*_matching() functions.
This commit is contained in:
parent
5279a04c78
commit
558fb0a04a
2 changed files with 27 additions and 1 deletions
|
@ -405,15 +405,18 @@ public:
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename TUnaryPredicate>
|
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();) {
|
for (size_t i = 0; i < size();) {
|
||||||
if (predicate(at(i))) {
|
if (predicate(at(i))) {
|
||||||
remove(i);
|
remove(i);
|
||||||
|
something_was_removed = true;
|
||||||
} else {
|
} else {
|
||||||
++i;
|
++i;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return something_was_removed;
|
||||||
}
|
}
|
||||||
|
|
||||||
ALWAYS_INLINE T take_last()
|
ALWAYS_INLINE T take_last()
|
||||||
|
|
|
@ -259,6 +259,29 @@ TEST_CASE(vector_remove)
|
||||||
EXPECT_EQ(ints[0], 4);
|
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)
|
TEST_CASE(nonnullownptrvector)
|
||||||
{
|
{
|
||||||
struct Object {
|
struct Object {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue