2018-10-10 12:53:07 +03:00
|
|
|
#pragma once
|
|
|
|
|
2019-04-15 03:22:08 +03:00
|
|
|
#include <AK/HashTable.h>
|
2019-07-24 11:25:43 +03:00
|
|
|
#include <AK/Optional.h>
|
2019-04-15 03:22:08 +03:00
|
|
|
#include <AK/StdLibExtras.h>
|
|
|
|
#include <AK/Vector.h>
|
|
|
|
#include <AK/kstdio.h>
|
2018-10-10 12:53:07 +03:00
|
|
|
|
|
|
|
namespace AK {
|
|
|
|
|
2019-07-13 12:00:29 +03:00
|
|
|
template<typename K, typename V, typename KeyTraits = Traits<K>>
|
2018-10-10 12:53:07 +03:00
|
|
|
class HashMap {
|
|
|
|
private:
|
|
|
|
struct Entry {
|
|
|
|
K key;
|
|
|
|
V value;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct EntryTraits {
|
2019-07-13 12:00:29 +03:00
|
|
|
static unsigned hash(const Entry& entry) { return KeyTraits::hash(entry.key); }
|
|
|
|
static bool equals(const Entry& a, const Entry& b) { return KeyTraits::equals(a.key, b.key); }
|
2018-10-10 12:53:07 +03:00
|
|
|
static void dump(const Entry& entry)
|
|
|
|
{
|
2018-10-17 11:55:43 +03:00
|
|
|
kprintf("key=");
|
2019-07-13 12:00:29 +03:00
|
|
|
KeyTraits::dump(entry.key);
|
2018-10-17 11:55:43 +03:00
|
|
|
kprintf(" value=");
|
2018-10-10 12:53:07 +03:00
|
|
|
Traits<V>::dump(entry.value);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
public:
|
2019-05-28 12:53:16 +03:00
|
|
|
HashMap() {}
|
2018-10-10 12:53:07 +03:00
|
|
|
|
2018-12-21 04:10:45 +03:00
|
|
|
bool is_empty() const { return m_table.is_empty(); }
|
2019-05-06 14:28:52 +03:00
|
|
|
int size() const { return m_table.size(); }
|
|
|
|
int capacity() const { return m_table.capacity(); }
|
2018-10-25 13:35:49 +03:00
|
|
|
void clear() { m_table.clear(); }
|
2018-10-10 12:53:07 +03:00
|
|
|
|
2019-06-27 15:19:50 +03:00
|
|
|
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) }); }
|
2019-06-29 22:09:40 +03:00
|
|
|
void remove(const K& key)
|
|
|
|
{
|
|
|
|
auto it = find(key);
|
|
|
|
if (it != end())
|
|
|
|
m_table.remove(it);
|
|
|
|
}
|
2019-01-31 19:31:23 +03:00
|
|
|
void remove_one_randomly() { m_table.remove(m_table.begin()); }
|
2018-10-10 12:53:07 +03: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(); }
|
2019-07-24 11:25:43 +03:00
|
|
|
IteratorType find(const K& key)
|
|
|
|
{
|
|
|
|
return m_table.find(KeyTraits::hash(key), [&](auto& entry) { return KeyTraits::equals(key, entry.key); });
|
|
|
|
}
|
2019-08-24 23:29:05 +03:00
|
|
|
template<typename Finder>
|
|
|
|
IteratorType find(unsigned hash, Finder finder)
|
|
|
|
{
|
|
|
|
return m_table.find(hash, finder);
|
|
|
|
}
|
2018-10-10 12:53:07 +03:00
|
|
|
|
|
|
|
ConstIteratorType begin() const { return m_table.begin(); }
|
|
|
|
ConstIteratorType end() const { return m_table.end(); }
|
2019-07-24 11:25:43 +03:00
|
|
|
ConstIteratorType find(const K& key) const
|
|
|
|
{
|
|
|
|
return m_table.find(KeyTraits::hash(key), [&](auto& entry) { return KeyTraits::equals(key, entry.key); });
|
|
|
|
}
|
2019-08-24 23:29:05 +03:00
|
|
|
template<typename Finder>
|
|
|
|
ConstIteratorType find(unsigned hash, Finder finder) const
|
|
|
|
{
|
|
|
|
return m_table.find(hash, finder);
|
|
|
|
}
|
2018-10-10 12:53:07 +03:00
|
|
|
|
2019-05-27 14:07:20 +03:00
|
|
|
void ensure_capacity(int capacity) { m_table.ensure_capacity(capacity); }
|
|
|
|
|
2018-10-10 12:53:07 +03:00
|
|
|
void dump() const { m_table.dump(); }
|
|
|
|
|
2019-08-14 12:47:38 +03:00
|
|
|
Optional<typename Traits<V>::PeekType> get(const K& key) const
|
2018-12-31 17:10:12 +03:00
|
|
|
{
|
|
|
|
auto it = find(key);
|
|
|
|
if (it == end())
|
2019-07-24 11:25:43 +03:00
|
|
|
return {};
|
2018-12-31 17:10:12 +03:00
|
|
|
return (*it).value;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool contains(const K& key) const
|
|
|
|
{
|
|
|
|
return find(key) != end();
|
|
|
|
}
|
|
|
|
|
2019-02-01 05:50:06 +03:00
|
|
|
void remove(IteratorType it)
|
|
|
|
{
|
|
|
|
m_table.remove(it);
|
|
|
|
}
|
|
|
|
|
2019-04-15 03:22:08 +03: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 12:53:07 +03:00
|
|
|
private:
|
2019-06-27 15:19:50 +03:00
|
|
|
HashTableType m_table;
|
2018-10-10 12:53:07 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
using AK::HashMap;
|