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

View file

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