AK: Make it possible to move and copy HashMap and HashTable.

Previously it was only possible to move them, but we should allow copying
as well, since it's gonna be useful for many things.
This commit is contained in:
Andreas Kling 2019-06-24 11:57:54 +02:00
parent 009b9bfd10
commit 2c1c4ab116
Notes: sideshowbarker 2024-07-19 13:29:42 +09:00
2 changed files with 17 additions and 14 deletions

View file

@ -34,19 +34,6 @@ private:
public:
HashMap() {}
HashMap(HashMap&& other)
: m_table(move(other.m_table))
{
}
HashMap& operator=(HashMap&& other)
{
if (this != &other) {
m_table = move(other.m_table);
}
return *this;
}
bool is_empty() const { return m_table.is_empty(); }
int size() const { return m_table.size(); }
int capacity() const { return m_table.capacity(); }

View file

@ -22,7 +22,23 @@ private:
public:
HashTable() {}
explicit HashTable(HashTable&& other)
HashTable(const HashTable& other)
{
ensure_capacity(other.size());
for (auto& it : other)
set(it);
}
HashTable& operator=(const HashTable& other)
{
if (this != &other) {
clear();
ensure_capacity(other.size());
for (auto& it : other)
set(it);
}
return *this;
}
HashTable(HashTable&& other)
: m_buckets(other.m_buckets)
, m_size(other.m_size)
, m_capacity(other.m_capacity)