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:
parent
f31bd190b0
commit
2af7447dfd
2 changed files with 57 additions and 0 deletions
25
AK/HashMap.h
25
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);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue