1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 04:37:34 +00:00

AK: Define HashMap::take to find and remove a value from the map

This commit is contained in:
Timothy Flynn 2023-02-02 10:55:50 -05:00 committed by Linus Groh
parent f31bd190b0
commit 2af7447dfd
2 changed files with 57 additions and 0 deletions

View file

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