1
0
mirror of https://github.com/SerenityOS/serenity synced 2024-07-09 09:00:46 +00:00

AK: Add a try_ensure() method to HashMap

This commit is contained in:
Eli Youngs 2022-12-10 18:03:02 -08:00 committed by Andrew Kaster
parent a2024cfb69
commit 950b36f95d

View File

@ -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;