mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-21 23:20:20 +00:00
AK: Make Hash{Map,Table}::remove_all_matching() return removal success
These functions now return whether one or more entries were removed.
This commit is contained in:
parent
c7ac0c2c80
commit
5279a04c78
Notes:
sideshowbarker
2024-07-17 21:35:15 +09:00
Author: https://github.com/awesomekling Commit: https://github.com/SerenityOS/serenity/commit/5279a04c787
4 changed files with 24 additions and 12 deletions
10
AK/HashMap.h
10
AK/HashMap.h
|
@ -75,14 +75,18 @@ public:
|
|||
}
|
||||
|
||||
template<typename TUnaryPredicate>
|
||||
void remove_all_matching(TUnaryPredicate predicate)
|
||||
bool remove_all_matching(TUnaryPredicate predicate)
|
||||
{
|
||||
bool something_was_removed = false;
|
||||
for (auto it = begin(); it != end();) {
|
||||
if (predicate(it->key, it->value))
|
||||
if (predicate(it->key, it->value)) {
|
||||
it = remove(it);
|
||||
else
|
||||
something_was_removed = true;
|
||||
} else {
|
||||
++it;
|
||||
}
|
||||
}
|
||||
return something_was_removed;
|
||||
}
|
||||
|
||||
using HashTableType = HashTable<Entry, EntryTraits, IsOrdered>;
|
||||
|
|
|
@ -419,14 +419,18 @@ public:
|
|||
}
|
||||
|
||||
template<typename TUnaryPredicate>
|
||||
void remove_all_matching(TUnaryPredicate predicate)
|
||||
bool remove_all_matching(TUnaryPredicate predicate)
|
||||
{
|
||||
bool something_was_removed = false;
|
||||
for (auto it = begin(); it != end();) {
|
||||
if (predicate(*it))
|
||||
if (predicate(*it)) {
|
||||
it = remove(it);
|
||||
else
|
||||
something_was_removed = true;
|
||||
} else {
|
||||
++it;
|
||||
}
|
||||
}
|
||||
return something_was_removed;
|
||||
}
|
||||
|
||||
private:
|
||||
|
|
|
@ -82,17 +82,18 @@ TEST_CASE(remove_all_matching)
|
|||
|
||||
EXPECT_EQ(map.size(), 4u);
|
||||
|
||||
map.remove_all_matching([&](int key, String const& value) {
|
||||
return key == 1 || value == "Two";
|
||||
});
|
||||
EXPECT_EQ(map.remove_all_matching([&](int key, String const& value) { return key == 1 || value == "Two"; }), true);
|
||||
|
||||
EXPECT_EQ(map.size(), 2u);
|
||||
EXPECT(map.contains(3));
|
||||
EXPECT(map.contains(4));
|
||||
|
||||
map.remove_all_matching([&](int, String const&) { return true; });
|
||||
EXPECT_EQ(map.remove_all_matching([&](int, String const&) { return true; }), true);
|
||||
EXPECT_EQ(map.remove_all_matching([&](int, String const&) { return false; }), false);
|
||||
|
||||
EXPECT(map.is_empty());
|
||||
|
||||
EXPECT_EQ(map.remove_all_matching([&](int, String const&) { return true; }), false);
|
||||
}
|
||||
|
||||
TEST_CASE(case_insensitive)
|
||||
|
|
|
@ -95,13 +95,16 @@ TEST_CASE(table_remove_all_matching)
|
|||
|
||||
EXPECT_EQ(ints.size(), 4u);
|
||||
|
||||
ints.remove_all_matching([&](int value) { return value > 2; });
|
||||
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);
|
||||
|
||||
ints.remove_all_matching([&](int) { return true; });
|
||||
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(case_insensitive)
|
||||
|
|
Loading…
Reference in a new issue