summaryrefslogtreecommitdiff
path: root/AK/HashMap.h
diff options
context:
space:
mode:
authorAndreas Kling <kling@serenityos.org>2022-01-05 16:53:24 +0100
committerAndreas Kling <kling@serenityos.org>2022-01-05 18:57:14 +0100
commit376e5ef9125fcc7c17c2b96f14cb4b64b28e191a (patch)
tree502bc2b191f68b5b9a79c92da56b2558f78ffeda /AK/HashMap.h
parente08d3251249b034044b1a8336ae08815b3c49c82 (diff)
downloadserenity-376e5ef9125fcc7c17c2b96f14cb4b64b28e191a.zip
AK: Add HashMap::remove_all_matching(predicate)
This removes all matching entries from a hash map in a single pass.
Diffstat (limited to 'AK/HashMap.h')
-rw-r--r--AK/HashMap.h15
1 files changed, 13 insertions, 2 deletions
diff --git a/AK/HashMap.h b/AK/HashMap.h
index 33251fde72..8a7d2f3160 100644
--- a/AK/HashMap.h
+++ b/AK/HashMap.h
@@ -74,6 +74,17 @@ public:
return false;
}
+ template<typename TUnaryPredicate>
+ void remove_all_matching(TUnaryPredicate predicate)
+ {
+ for (auto it = begin(); it != end();) {
+ if (predicate(it->key, it->value))
+ it = remove(it);
+ else
+ ++it;
+ }
+ }
+
using HashTableType = HashTable<Entry, EntryTraits, IsOrdered>;
using IteratorType = typename HashTableType::Iterator;
using ConstIteratorType = typename HashTableType::ConstIterator;
@@ -180,9 +191,9 @@ public:
return find(value) != end();
}
- void remove(IteratorType it)
+ IteratorType remove(IteratorType it)
{
- m_table.remove(it);
+ return m_table.remove(it);
}
V& ensure(const K& key)