mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-26 01:20:25 +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)
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue