mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 06:37:43 +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:
parent
d5bb98acbc
commit
6e95b11395
3 changed files with 67 additions and 51 deletions
11
AK/HashMap.h
11
AK/HashMap.h
|
@ -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); }
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue