diff options
author | Andreas Kling <kling@serenityos.org> | 2022-01-05 16:45:42 +0100 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2022-01-05 18:57:14 +0100 |
commit | 54cf42fac1cf3d89c91c5a241b6c98fc3cbf413f (patch) | |
tree | a14bd9441b90cddca59846bbd255203206e1b62b /AK | |
parent | 365bd8a0c356facc9b781a085778e10e70efe900 (diff) | |
download | serenity-54cf42fac1cf3d89c91c5a241b6c98fc3cbf413f.zip |
AK: Add HashTable::remove_all_matching(predicate)
This removes all matching entries from a table in a single pass.
Diffstat (limited to 'AK')
-rw-r--r-- | AK/HashTable.h | 18 |
1 files changed, 17 insertions, 1 deletions
diff --git a/AK/HashTable.h b/AK/HashTable.h index cacd4bda42..bb4d08e341 100644 --- a/AK/HashTable.h +++ b/AK/HashTable.h @@ -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: |