2018-10-10 09:53:07 +00:00
|
|
|
#pragma once
|
|
|
|
|
2019-04-15 00:22:08 +00:00
|
|
|
#include <AK/HashTable.h>
|
|
|
|
#include <AK/StdLibExtras.h>
|
|
|
|
#include <AK/Vector.h>
|
|
|
|
#include <AK/kstdio.h>
|
2018-10-10 09:53:07 +00:00
|
|
|
|
|
|
|
namespace AK {
|
|
|
|
|
|
|
|
template<typename K, typename V>
|
|
|
|
class HashMap {
|
|
|
|
private:
|
|
|
|
struct Entry {
|
|
|
|
K key;
|
|
|
|
V value;
|
|
|
|
|
|
|
|
bool operator==(const Entry& other)
|
|
|
|
{
|
|
|
|
return key == other.key;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct EntryTraits {
|
|
|
|
static unsigned hash(const Entry& entry) { return Traits<K>::hash(entry.key); }
|
|
|
|
static void dump(const Entry& entry)
|
|
|
|
{
|
2018-10-17 08:55:43 +00:00
|
|
|
kprintf("key=");
|
2018-10-10 09:53:07 +00:00
|
|
|
Traits<K>::dump(entry.key);
|
2018-10-17 08:55:43 +00:00
|
|
|
kprintf(" value=");
|
2018-10-10 09:53:07 +00:00
|
|
|
Traits<V>::dump(entry.value);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
public:
|
2019-05-28 09:53:16 +00:00
|
|
|
HashMap() {}
|
2018-10-10 09:53:07 +00:00
|
|
|
|
2018-12-21 01:10:45 +00:00
|
|
|
bool is_empty() const { return m_table.is_empty(); }
|
2019-05-06 11:28:52 +00:00
|
|
|
int size() const { return m_table.size(); }
|
|
|
|
int capacity() const { return m_table.capacity(); }
|
2018-10-25 10:35:49 +00:00
|
|
|
void clear() { m_table.clear(); }
|
2018-10-10 09:53:07 +00:00
|
|
|
|
2018-10-25 10:35:49 +00:00
|
|
|
void set(const K&, const V&);
|
2018-10-10 09:53:07 +00:00
|
|
|
void set(const K&, V&&);
|
2018-10-13 12:23:47 +00:00
|
|
|
void remove(const K&);
|
2019-01-31 16:31:23 +00:00
|
|
|
void remove_one_randomly() { m_table.remove(m_table.begin()); }
|
2018-10-10 09:53:07 +00:00
|
|
|
|
|
|
|
typedef HashTable<Entry, EntryTraits> HashTableType;
|
|
|
|
typedef typename HashTableType::Iterator IteratorType;
|
|
|
|
typedef typename HashTableType::ConstIterator ConstIteratorType;
|
|
|
|
|
|
|
|
IteratorType begin() { return m_table.begin(); }
|
|
|
|
IteratorType end() { return m_table.end(); }
|
|
|
|
IteratorType find(const K&);
|
|
|
|
|
|
|
|
ConstIteratorType begin() const { return m_table.begin(); }
|
|
|
|
ConstIteratorType end() const { return m_table.end(); }
|
|
|
|
ConstIteratorType find(const K&) const;
|
|
|
|
|
2019-05-27 11:07:20 +00:00
|
|
|
void ensure_capacity(int capacity) { m_table.ensure_capacity(capacity); }
|
|
|
|
|
2018-10-10 09:53:07 +00:00
|
|
|
void dump() const { m_table.dump(); }
|
|
|
|
|
2018-12-31 14:10:12 +00:00
|
|
|
V get(const K& key) const
|
|
|
|
{
|
|
|
|
auto it = find(key);
|
|
|
|
if (it == end())
|
|
|
|
return V();
|
|
|
|
return (*it).value;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool contains(const K& key) const
|
|
|
|
{
|
|
|
|
return find(key) != end();
|
|
|
|
}
|
|
|
|
|
2019-02-01 02:50:06 +00:00
|
|
|
void remove(IteratorType it)
|
|
|
|
{
|
|
|
|
m_table.remove(it);
|
|
|
|
}
|
|
|
|
|
2019-04-15 00:22:08 +00:00
|
|
|
V& ensure(const K& key)
|
|
|
|
{
|
|
|
|
auto it = find(key);
|
|
|
|
if (it == end())
|
|
|
|
set(key, V());
|
|
|
|
return find(key)->value;
|
|
|
|
}
|
|
|
|
|
|
|
|
Vector<K> keys() const
|
|
|
|
{
|
|
|
|
Vector<K> list;
|
|
|
|
list.ensure_capacity(size());
|
|
|
|
for (auto& it : *this)
|
|
|
|
list.unchecked_append(it.key);
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
2018-10-10 09:53:07 +00:00
|
|
|
private:
|
|
|
|
HashTable<Entry, EntryTraits> m_table;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename K, typename V>
|
|
|
|
void HashMap<K, V>::set(const K& key, V&& value)
|
|
|
|
{
|
2019-05-28 09:53:16 +00:00
|
|
|
m_table.set(Entry { key, move(value) });
|
2018-10-10 09:53:07 +00:00
|
|
|
}
|
|
|
|
|
2018-10-25 10:35:49 +00:00
|
|
|
template<typename K, typename V>
|
|
|
|
void HashMap<K, V>::set(const K& key, const V& value)
|
|
|
|
{
|
2019-05-28 09:53:16 +00:00
|
|
|
m_table.set(Entry { key, value });
|
2018-10-25 10:35:49 +00:00
|
|
|
}
|
|
|
|
|
2018-10-13 12:23:47 +00:00
|
|
|
template<typename K, typename V>
|
|
|
|
void HashMap<K, V>::remove(const K& key)
|
|
|
|
{
|
|
|
|
Entry dummy { key, V() };
|
|
|
|
m_table.remove(dummy);
|
|
|
|
}
|
|
|
|
|
2018-10-10 09:53:07 +00:00
|
|
|
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;
|