Skip to content

Commit

Permalink
AK: Simplify HashMap a bit.
Browse files Browse the repository at this point in the history
  • Loading branch information
awesomekling committed Jun 27, 2019
1 parent 65e470c commit ebe108e
Showing 1 changed file with 7 additions and 40 deletions.
47 changes: 7 additions & 40 deletions AK/HashMap.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class HashMap {
K key;
V value;

bool operator==(const Entry& other)
bool operator==(const Entry& other) const
{
return key == other.key;
}
Expand All @@ -39,9 +39,9 @@ class HashMap {
int capacity() const { return m_table.capacity(); }
void clear() { m_table.clear(); }

void set(const K&, const V&);
void set(const K&, V&&);
void remove(const K&);
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_one_randomly() { m_table.remove(m_table.begin()); }

typedef HashTable<Entry, EntryTraits> HashTableType;
Expand All @@ -50,11 +50,11 @@ class HashMap {

IteratorType begin() { return m_table.begin(); }
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 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); }

Expand Down Expand Up @@ -96,42 +96,9 @@ class HashMap {
}

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;

0 comments on commit ebe108e

Please sign in to comment.