mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-12-08 06:09:58 +00:00
AK: Add HashMap::update()
This updates a HashMap by copying another HashMap's keys and values.
This commit is contained in:
parent
76ee1ce04c
commit
0b96690f0c
Notes:
github-actions[bot]
2025-07-25 14:23:37 +00:00
Author: https://github.com/gmta
Commit: 0b96690f0c
Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/5600
Reviewed-by: https://github.com/alimpfard ✅
2 changed files with 32 additions and 0 deletions
|
|
@ -64,6 +64,12 @@ public:
|
|||
ErrorOr<HashSetResult> try_set(K const& key, V&& value, HashSetExistingEntryBehavior existing_entry_behavior = HashSetExistingEntryBehavior::Replace) { return m_table.try_set({ key, move(value) }, existing_entry_behavior); }
|
||||
ErrorOr<HashSetResult> try_set(K&& key, V&& value, HashSetExistingEntryBehavior existing_entry_behavior = HashSetExistingEntryBehavior::Replace) { return m_table.try_set({ move(key), move(value) }, existing_entry_behavior); }
|
||||
|
||||
void update(HashMap const& other)
|
||||
{
|
||||
for (auto const& [key, value] : other)
|
||||
set(key, value);
|
||||
}
|
||||
|
||||
bool remove(K const& key)
|
||||
{
|
||||
auto it = find(key);
|
||||
|
|
|
|||
|
|
@ -327,3 +327,29 @@ TEST_CASE(move_assign)
|
|||
EXPECT_EQ(second.size(), static_cast<size_t>(3));
|
||||
EXPECT_EQ(second.get(2), Optional<int>(20));
|
||||
}
|
||||
|
||||
TEST_CASE(update)
|
||||
{
|
||||
HashMap<int, int> first;
|
||||
HashMap<int, int> second;
|
||||
|
||||
first.set(1, 10);
|
||||
first.set(2, 20);
|
||||
|
||||
second.set(1, 9);
|
||||
second.set(3, 30);
|
||||
second.set(4, 40);
|
||||
|
||||
first.update(second);
|
||||
|
||||
EXPECT_EQ(4u, first.size());
|
||||
EXPECT_EQ(3u, second.size());
|
||||
|
||||
EXPECT_EQ(9, first.get(1));
|
||||
EXPECT_EQ(20, first.get(2));
|
||||
EXPECT_EQ(30, first.get(3));
|
||||
EXPECT_EQ(40, first.get(4));
|
||||
|
||||
second.update(first);
|
||||
EXPECT_EQ(4u, second.size());
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue