AK: Add HashMap::update()

This updates a HashMap by copying another HashMap's keys and values.
This commit is contained in:
Jelle Raaijmakers 2025-07-25 12:47:11 +02:00 committed by Jelle Raaijmakers
parent 76ee1ce04c
commit 0b96690f0c
2 changed files with 32 additions and 0 deletions

View File

@ -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);

View File

@ -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());
}