mirror of
https://github.com/zebrajr/ladybird.git
synced 2025-12-06 00:19:53 +01: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
|
|
@ -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…
Reference in New Issue
Block a user