summaryrefslogtreecommitdiff
path: root/AK/HashMap.h
diff options
context:
space:
mode:
Diffstat (limited to 'AK/HashMap.h')
-rw-r--r--AK/HashMap.h11
1 files changed, 11 insertions, 0 deletions
diff --git a/AK/HashMap.h b/AK/HashMap.h
index 2a036cc5ae..d4d56968e2 100644
--- a/AK/HashMap.h
+++ b/AK/HashMap.h
@@ -222,6 +222,17 @@ public:
return find(key)->value;
}
+ template<typename Callback>
+ ErrorOr<V> try_ensure(K const& key, Callback initialization_callback)
+ {
+ auto it = find(key);
+ if (it != end())
+ return it->value;
+ auto result = TRY(try_set(key, initialization_callback()));
+ VERIFY(result == HashSetResult::InsertedNewEntry);
+ return find(key)->value;
+ }
+
[[nodiscard]] Vector<K> keys() const
{
Vector<K> list;