mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-12-28 13:43:45 +03:00
AK: Add HashTable::remove_all_matching(predicate)
This removes all matching entries from a table in a single pass.
This commit is contained in:
parent
365bd8a0c3
commit
54cf42fac1
Notes:
sideshowbarker
2024-07-17 21:35:27 +09:00
Author: https://github.com/awesomekling Commit: https://github.com/SerenityOS/serenity/commit/54cf42fac1c
@ -384,7 +384,7 @@ public:
|
||||
return false;
|
||||
}
|
||||
|
||||
void remove(Iterator iterator)
|
||||
Iterator remove(Iterator iterator)
|
||||
{
|
||||
VERIFY(iterator.m_bucket);
|
||||
auto& bucket = *iterator.m_bucket;
|
||||
@ -394,6 +394,9 @@ public:
|
||||
if constexpr (!IsOrdered)
|
||||
VERIFY(!bucket.end);
|
||||
|
||||
auto next_iterator = iterator;
|
||||
++next_iterator;
|
||||
|
||||
bucket.slot()->~T();
|
||||
bucket.used = false;
|
||||
bucket.deleted = true;
|
||||
@ -411,6 +414,19 @@ public:
|
||||
else
|
||||
m_collection_data.tail = bucket.previous;
|
||||
}
|
||||
|
||||
return next_iterator;
|
||||
}
|
||||
|
||||
template<typename TUnaryPredicate>
|
||||
void remove_all_matching(TUnaryPredicate predicate)
|
||||
{
|
||||
for (auto it = begin(); it != end();) {
|
||||
if (predicate(*it))
|
||||
it = remove(it);
|
||||
else
|
||||
++it;
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
|
@ -84,6 +84,26 @@ TEST_CASE(table_remove)
|
||||
EXPECT(strings.find("Two") != strings.end());
|
||||
}
|
||||
|
||||
TEST_CASE(table_remove_all_matching)
|
||||
{
|
||||
HashTable<int> ints;
|
||||
|
||||
ints.set(1);
|
||||
ints.set(2);
|
||||
ints.set(3);
|
||||
ints.set(4);
|
||||
|
||||
EXPECT_EQ(ints.size(), 4u);
|
||||
|
||||
ints.remove_all_matching([&](int value) { return value > 2; });
|
||||
|
||||
EXPECT_EQ(ints.size(), 2u);
|
||||
|
||||
ints.remove_all_matching([&](int) { return true; });
|
||||
|
||||
EXPECT(ints.is_empty());
|
||||
}
|
||||
|
||||
TEST_CASE(case_insensitive)
|
||||
{
|
||||
HashTable<String, CaseInsensitiveStringTraits> casetable;
|
||||
|
Loading…
Reference in New Issue
Block a user