summaryrefslogtreecommitdiff
path: root/asl
diff options
context:
space:
mode:
authorSteven Le Rouzic <steven.lerouzic@gmail.com>2025-01-16 00:25:49 +0100
committerSteven Le Rouzic <steven.lerouzic@gmail.com>2025-01-16 00:25:49 +0100
commit41454a09c6d73fcecffc1f7d6e3754c60cc49e31 (patch)
tree9515fa1fdb4d223bb00978b9778cdbbf4d4cb21b /asl
parent0ee5725793f62e6b0386b66d21aee5eebfd7be13 (diff)
Start work on hash_map
Diffstat (limited to 'asl')
-rw-r--r--asl/hash_map.hpp105
1 files changed, 105 insertions, 0 deletions
diff --git a/asl/hash_map.hpp b/asl/hash_map.hpp
new file mode 100644
index 0000000..310b532
--- /dev/null
+++ b/asl/hash_map.hpp
@@ -0,0 +1,105 @@
+#pragma once
+
+#include "asl/annotations.hpp"
+#include "asl/meta.hpp"
+#include "asl/utility.hpp"
+#include "asl/maybe_uninit.hpp"
+#include "asl/hash.hpp"
+#include "asl/allocator.hpp"
+#include "asl/memory.hpp"
+#include "asl/hash_set.hpp"
+
+namespace asl
+{
+
+namespace hash_map_internal
+{
+
+template<typename K, typename V>
+struct Slot
+{
+ K key;
+ V value;
+};
+
+template<hashable K, typename V, key_hasher<K> KeyHasher>
+struct SlotHasher : public KeyHasher
+{
+ using KeyHasher::hash;
+
+ constexpr static uint64_t hash(const Slot<K, V>& slot)
+ {
+ return KeyHasher::hash(slot.key);
+ }
+};
+
+template<equality_comparable K, typename V, key_comparator<K> KeyComparator>
+struct SlotComparator : public KeyComparator
+{
+ using KeyComparator::eq;
+
+ constexpr static bool eq(const Slot<K, V>& a, const Slot<K, V>& b)
+ {
+ return KeyComparator::eq(a.key, b.key);
+ }
+
+ constexpr static bool eq(const Slot<K, V>& a, const K& b)
+ {
+ return KeyComparator::eq(a.key, b);
+ }
+};
+
+} // namespace hash_map_internal
+
+template<
+ is_object K,
+ is_object V,
+ allocator Allocator = DefaultAllocator,
+ key_hasher<K> KeyHasher = default_key_hasher<K>,
+ key_comparator<K> KeyComparator = default_key_comparator<K>
+>
+requires moveable<K> && moveable<V>
+class hash_map : hash_set<
+ hash_map_internal::Slot<K, V>,
+ Allocator,
+ hash_map_internal::SlotHasher<K, V, KeyHasher>,
+ hash_map_internal::SlotComparator<K, V, KeyComparator>>
+{
+ using Base =
+ hash_set<
+ hash_map_internal::Slot<K, V>,
+ Allocator,
+ hash_map_internal::SlotHasher<K, V, KeyHasher>,
+ hash_map_internal::SlotComparator<K, V, KeyComparator>>;
+
+public:
+ constexpr hash_map() requires default_constructible<Allocator> = default;
+
+ explicit constexpr hash_map(Allocator allocator)
+ : Base{ASL_MOVE(allocator)}
+ {}
+
+ hash_map(const hash_map&) requires copyable<K> && copyable<V> = default;
+
+ hash_map& operator=(const hash_map&) requires copyable<K> && copyable<V> = default;
+
+ hash_map(hash_map&&) = default;
+
+ hash_map& operator=(hash_map&&) = default;
+
+ ~hash_map() = default;
+
+ using Base::destroy;
+
+ using Base::clear;
+
+ using Base::size;
+
+ // @Todo insert
+ // @Todo contains
+ // @Todo remove
+ // @Todo get
+ // @Todo tests
+};
+
+} // namespace asl