AK/HashMap: Use structured bindings when iterating over itself

This commit is contained in:
Hendiadyoin1 2024-03-01 14:23:03 +01:00 committed by Andrew Kaster
parent b0fc5bea91
commit 79fd8eb28d
Notes: sideshowbarker 2024-07-16 20:05:14 +09:00

View file

@ -39,8 +39,8 @@ public:
HashMap(std::initializer_list<Entry> list)
{
MUST(try_ensure_capacity(list.size()));
for (auto& item : list)
set(item.key, item.value);
for (auto& [key, value] : list)
set(key, value);
}
HashMap(HashMap const&) = default; // FIXME: Not OOM-safe! Use clone() instead.
@ -274,16 +274,16 @@ public:
{
Vector<K> list;
list.ensure_capacity(size());
for (auto& it : *this)
list.unchecked_append(it.key);
for (auto const& [key, _] : *this)
list.unchecked_append(key);
return list;
}
[[nodiscard]] u32 hash() const
{
u32 hash = 0;
for (auto& it : *this) {
auto entry_hash = pair_int_hash(it.key.hash(), it.value.hash());
for (auto const& [key, value] : *this) {
auto entry_hash = pair_int_hash(key.hash(), value.hash());
hash = pair_int_hash(hash, entry_hash);
}
return hash;
@ -293,8 +293,9 @@ public:
ErrorOr<HashMap<K, V, NewKeyTraits, NewValueTraits, NewIsOrdered>> clone() const
{
HashMap<K, V, NewKeyTraits, NewValueTraits, NewIsOrdered> hash_map_clone;
for (auto& it : *this)
TRY(hash_map_clone.try_set(it.key, it.value));
TRY(hash_map_clone.try_ensure_capacity(size()));
for (auto const& [key, value] : *this)
hash_map_clone.set(key, value);
return hash_map_clone;
}