mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-04-20 19:45:12 +00:00
AK: Add a try_ensure() method to HashMap
This commit is contained in:
parent
a2024cfb69
commit
950b36f95d
Notes:
sideshowbarker
2024-07-17 18:38:54 +09:00
Author: https://github.com/water-ghosts Commit: https://github.com/SerenityOS/serenity/commit/950b36f95d Pull-request: https://github.com/SerenityOS/serenity/pull/16388 Reviewed-by: https://github.com/ADKaster ✅ Reviewed-by: https://github.com/Zaggy1024 ✅ Reviewed-by: https://github.com/sppmacd
1 changed files with 11 additions and 0 deletions
11
AK/HashMap.h
11
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;
|
||||
|
|
Loading…
Add table
Reference in a new issue