2023-06-16 22:07:43 +00:00
|
|
|
#pragma once
|
|
|
|
#include <luna/HashTable.h>
|
|
|
|
|
|
|
|
template <typename K, typename V> struct HashPair
|
|
|
|
{
|
|
|
|
K key;
|
|
|
|
Option<V> value;
|
|
|
|
|
|
|
|
bool operator==(const HashPair<K, V>& other) const
|
|
|
|
{
|
|
|
|
return key == other.key;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename K, typename V> u64 hash(const HashPair<K, V>& value, u64 salt)
|
|
|
|
{
|
|
|
|
return hash(value.key, salt);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename K, typename V> struct HashMap
|
|
|
|
{
|
|
|
|
public:
|
2023-08-02 20:18:36 +00:00
|
|
|
Result<bool> try_set(const K& key, V&& value)
|
|
|
|
{
|
|
|
|
return m_table.try_set(HashPair<K, V> { key, move(value) });
|
|
|
|
}
|
|
|
|
|
2023-06-16 22:07:43 +00:00
|
|
|
Result<bool> try_set(const K& key, const V& value)
|
|
|
|
{
|
|
|
|
return m_table.try_set(HashPair<K, V> { key, value });
|
|
|
|
}
|
|
|
|
|
|
|
|
Option<V> try_get(const K& key)
|
|
|
|
{
|
|
|
|
auto* p = m_table.try_find(HashPair<K, V> { key, {} });
|
|
|
|
if (!p) return {};
|
|
|
|
return p->value;
|
|
|
|
}
|
|
|
|
|
2023-06-21 19:32:28 +00:00
|
|
|
V* try_get_ref(const K& key)
|
|
|
|
{
|
|
|
|
auto* p = m_table.try_find(HashPair<K, V> { key, {} });
|
|
|
|
if (!p) return nullptr;
|
|
|
|
return p->value.value_ptr();
|
|
|
|
}
|
|
|
|
|
2023-06-16 22:07:43 +00:00
|
|
|
bool try_remove(const K& key)
|
|
|
|
{
|
|
|
|
return m_table.try_remove(HashPair<K, V> { key, {} });
|
|
|
|
}
|
|
|
|
|
2023-08-17 18:14:33 +00:00
|
|
|
usize capacity() const
|
|
|
|
{
|
|
|
|
return m_table.capacity();
|
|
|
|
}
|
|
|
|
|
|
|
|
usize size() const
|
|
|
|
{
|
|
|
|
return m_table.size();
|
|
|
|
}
|
|
|
|
|
2023-08-16 12:54:13 +00:00
|
|
|
void clear()
|
|
|
|
{
|
|
|
|
m_table.clear();
|
|
|
|
}
|
|
|
|
|
2023-06-16 22:07:43 +00:00
|
|
|
private:
|
|
|
|
HashTable<HashPair<K, V>> m_table;
|
|
|
|
};
|