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

AK: Simplify HashMap a bit.

This commit is contained in:
Andreas Kling 2019-06-27 14:19:50 +02:00
parent 65e470c90a
commit ebe108efa6

View file

@ -14,7 +14,7 @@ private:
K key; K key;
V value; V value;
bool operator==(const Entry& other) bool operator==(const Entry& other) const
{ {
return key == other.key; return key == other.key;
} }
@ -39,9 +39,9 @@ public:
int capacity() const { return m_table.capacity(); } int capacity() const { return m_table.capacity(); }
void clear() { m_table.clear(); } void clear() { m_table.clear(); }
void set(const K&, const V&); void set(const K& key, const V& value) { m_table.set({ key, value }); }
void set(const K&, V&&); void set(const K& key, V&& value) { m_table.set({ key, move(value) }); }
void remove(const K&); void remove(const K& key) { m_table.remove({ key, V() }); }
void remove_one_randomly() { m_table.remove(m_table.begin()); } void remove_one_randomly() { m_table.remove(m_table.begin()); }
typedef HashTable<Entry, EntryTraits> HashTableType; typedef HashTable<Entry, EntryTraits> HashTableType;
@ -50,11 +50,11 @@ public:
IteratorType begin() { return m_table.begin(); } IteratorType begin() { return m_table.begin(); }
IteratorType end() { return m_table.end(); } IteratorType end() { return m_table.end(); }
IteratorType find(const K&); IteratorType find(const K& key) { return m_table.find({ key, V() }); }
ConstIteratorType begin() const { return m_table.begin(); } ConstIteratorType begin() const { return m_table.begin(); }
ConstIteratorType end() const { return m_table.end(); } ConstIteratorType end() const { return m_table.end(); }
ConstIteratorType find(const K&) const; ConstIteratorType find(const K& key) const { return m_table.find({ key, V() }); }
void ensure_capacity(int capacity) { m_table.ensure_capacity(capacity); } void ensure_capacity(int capacity) { m_table.ensure_capacity(capacity); }
@ -96,42 +96,9 @@ public:
} }
private: private:
HashTable<Entry, EntryTraits> m_table; HashTableType m_table;
}; };
template<typename K, typename V>
void HashMap<K, V>::set(const K& key, V&& value)
{
m_table.set(Entry { key, move(value) });
}
template<typename K, typename V>
void HashMap<K, V>::set(const K& key, const V& value)
{
m_table.set(Entry { key, value });
}
template<typename K, typename V>
void HashMap<K, V>::remove(const K& key)
{
Entry dummy { key, V() };
m_table.remove(dummy);
}
template<typename K, typename V>
auto HashMap<K, V>::find(const K& key) -> IteratorType
{
Entry dummy { key, V() };
return m_table.find(dummy);
}
template<typename K, typename V>
auto HashMap<K, V>::find(const K& key) const -> ConstIteratorType
{
Entry dummy { key, V() };
return m_table.find(dummy);
}
} }
using AK::HashMap; using AK::HashMap;