summaryrefslogtreecommitdiff
path: root/AK/HashMap.h
diff options
context:
space:
mode:
authorTimothy Flynn <trflynn89@pm.me>2023-02-02 10:55:50 -0500
committerLinus Groh <mail@linusgroh.de>2023-02-02 19:14:00 +0000
commit2af7447dfd598bbcabf8666211b3d09173b3fe8f (patch)
tree3d245f3c91b4b756b77e53676ee34790e4ce97e0 /AK/HashMap.h
parentf31bd190b0f746a8e304a4eeb79ce9c002e656d0 (diff)
downloadserenity-2af7447dfd598bbcabf8666211b3d09173b3fe8f.zip
AK: Define HashMap::take to find and remove a value from the map
Diffstat (limited to 'AK/HashMap.h')
-rw-r--r--AK/HashMap.h25
1 files changed, 25 insertions, 0 deletions
diff --git a/AK/HashMap.h b/AK/HashMap.h
index ec8d946472..d3e1af9b63 100644
--- a/AK/HashMap.h
+++ b/AK/HashMap.h
@@ -199,6 +199,31 @@ public:
m_table.remove(it);
}
+ Optional<V> take(K const& key)
+ {
+ if (auto it = find(key); it != end()) {
+ auto value = move(it->value);
+ m_table.remove(it);
+
+ return value;
+ }
+
+ return {};
+ }
+
+ template<Concepts::HashCompatible<K> Key>
+ requires(IsSame<KeyTraits, Traits<K>>) Optional<V> take(Key const& key)
+ {
+ if (auto it = find(key); it != end()) {
+ auto value = move(it->value);
+ m_table.remove(it);
+
+ return value;
+ }
+
+ return {};
+ }
+
V& ensure(K const& key)
{
auto it = find(key);