aboutsummaryrefslogtreecommitdiff
path: root/src/util
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2022-03-07 20:20:18 +0100
committerHampusM <hampus@hampusmat.com>2022-06-13 17:56:55 +0200
commitf0824fdebc79fbf3843c2053522107c33e3ce2a3 (patch)
treee5bd34fa89cbe80cf8a30596766cf95098465aec /src/util
parent12fffa7df0685ef6d23ffe888a06695ae490df81 (diff)
refactor: move directions to vector2 & make vector2 hashable
Diffstat (limited to 'src/util')
-rw-r--r--src/util/hash.hpp10
-rw-r--r--src/util/hash.tpp22
2 files changed, 32 insertions, 0 deletions
diff --git a/src/util/hash.hpp b/src/util/hash.hpp
new file mode 100644
index 0000000..72e9b82
--- /dev/null
+++ b/src/util/hash.hpp
@@ -0,0 +1,10 @@
+#pragma once
+
+#include <cstddef>
+
+constexpr auto GOLDEN_RATIO = 0x9e3779b9;
+
+template <typename Value>
+void hash_combine(std::size_t &seed, const Value &value) noexcept;
+
+#include "hash.tpp"
diff --git a/src/util/hash.tpp b/src/util/hash.tpp
new file mode 100644
index 0000000..146cfa0
--- /dev/null
+++ b/src/util/hash.tpp
@@ -0,0 +1,22 @@
+#pragma once
+
+#include "hash.hpp"
+
+#include <cstdint>
+#include <functional>
+
+/**
+ * Combines the hash 'seed' with the hash of 'value'.
+ *
+ * @param seed A hash that will be mutated
+ * @param value A hashable value
+ */
+template <typename Value>
+void hash_combine(std::size_t &seed, const Value &value) noexcept
+{
+ constexpr uint32_t shift_left = 6;
+ constexpr uint32_t shift_right = 2;
+
+ seed ^= std::hash<Value>()(value) + GOLDEN_RATIO + (seed << shift_left) +
+ (seed >> shift_right);
+}