Luna/libluna/include/luna/HashMap.h

60 lines
1.2 KiB
C
Raw Normal View History

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;
}
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, {} });
}
void clear()
{
m_table.clear();
}
2023-06-16 22:07:43 +00:00
private:
HashTable<HashPair<K, V>> m_table;
};