mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-21 23:20:20 +00:00
AK/HashMap: Use structured bindings when iterating over itself
This commit is contained in:
parent
b0fc5bea91
commit
79fd8eb28d
Notes:
sideshowbarker
2024-07-16 20:05:14 +09:00
Author: https://github.com/Hendiadyoin1 Commit: https://github.com/SerenityOS/serenity/commit/79fd8eb28d Pull-request: https://github.com/SerenityOS/serenity/pull/23403 Reviewed-by: https://github.com/ADKaster ✅
1 changed files with 9 additions and 8 deletions
17
AK/HashMap.h
17
AK/HashMap.h
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue