1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 23:17:45 +00:00

AK: Allow HashMap to be used with non-default-constructible values.

Solve this by adding find() overloads to HashTable and SinglyLinkedList
that take a templated functor for comparing the values.

This allows HashMap to call HashTable::find() without having to create
a temporary Entry for use as the table key. :^)
This commit is contained in:
Andreas Kling 2019-06-29 21:09:40 +02:00
parent d5bb98acbc
commit 6e95b11395
3 changed files with 67 additions and 51 deletions

View file

@ -37,7 +37,12 @@ public:
void set(const K& key, const V& value) { m_table.set({ key, value }); }
void set(const K& key, V&& value) { m_table.set({ key, move(value) }); }
void remove(const K& key) { m_table.remove({ key, V() }); }
void remove(const K& key)
{
auto it = find(key);
if (it != end())
m_table.remove(it);
}
void remove_one_randomly() { m_table.remove(m_table.begin()); }
typedef HashTable<Entry, EntryTraits> HashTableType;
@ -46,11 +51,11 @@ public:
IteratorType begin() { return m_table.begin(); }
IteratorType end() { return m_table.end(); }
IteratorType find(const K& key) { return m_table.find({ key, V() }); }
IteratorType find(const K& key) { return m_table.find(Traits<K>::hash(key), [&](auto& entry) { return key == entry.key; }); }
ConstIteratorType begin() const { return m_table.begin(); }
ConstIteratorType end() const { return m_table.end(); }
ConstIteratorType find(const K& key) const { return m_table.find({ key, V() }); }
ConstIteratorType find(const K& key) const { return m_table.find(Traits<K>::hash(key), [&](auto& entry) { return key == entry.key; }); }
void ensure_capacity(int capacity) { m_table.ensure_capacity(capacity); }