aboutsummaryrefslogtreecommitdiff
path: root/core/src/Utils
diff options
context:
space:
mode:
Diffstat (limited to 'core/src/Utils')
-rw-r--r--core/src/Utils/Hash.hpp15
-rw-r--r--core/src/Utils/Vector.hpp6
-rw-r--r--core/src/Utils/VectorHash.hpp46
3 files changed, 67 insertions, 0 deletions
diff --git a/core/src/Utils/Hash.hpp b/core/src/Utils/Hash.hpp
new file mode 100644
index 0000000..cf7713a
--- /dev/null
+++ b/core/src/Utils/Hash.hpp
@@ -0,0 +1,15 @@
+#pragma once
+
+#include <cstddef>
+#include <functional>
+
+namespace HashUtils {
+
+template <class T>
+void Combine(size_t& seed, const T& v)
+{
+ std::hash<T> hasher;
+ seed ^= hasher(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
+}
+
+} // namespace HashUtils
diff --git a/core/src/Utils/Vector.hpp b/core/src/Utils/Vector.hpp
index bf75fd1..f49965e 100644
--- a/core/src/Utils/Vector.hpp
+++ b/core/src/Utils/Vector.hpp
@@ -6,6 +6,8 @@ struct Vec2
T x = 0;
T y = 0;
+ friend constexpr bool operator==(const Vec2& a, const Vec2& b) = default;
+
friend constexpr Vec2 operator+(const Vec2& a, const Vec2& b) { return { a.x + b.x, a.y + b.y }; }
friend constexpr Vec2 operator-(const Vec2& a, const Vec2& b) { return { a.x - b.x, a.y - b.y }; }
friend constexpr Vec2 operator*(const Vec2& a, const Vec2& b) { return { a.x * b.x, a.y * b.y }; }
@@ -27,6 +29,8 @@ struct Vec3
T y = 0;
T z = 0;
+ friend constexpr bool operator==(const Vec3& a, const Vec3& b) = default;
+
friend constexpr Vec3 operator+(const Vec3& a, const Vec3& b) { return { a.x + b.x, a.y + b.y, a.z + b.z }; }
friend constexpr Vec3 operator-(const Vec3& a, const Vec3& b) { return { a.x - b.x, a.y - b.y, a.z - b.z }; }
friend constexpr Vec3 operator*(const Vec3& a, const Vec3& b) { return { a.x * b.x, a.y * b.y, a.z * b.z }; }
@@ -49,6 +53,8 @@ struct Vec4
T z = 0;
T w = 0;
+ friend constexpr bool operator==(const Vec4& a, const Vec4& b) = default;
+
friend constexpr Vec4 operator+(const Vec4& a, const Vec4& b) { return { a.x + b.x, a.y + b.y, a.z + b.z, a.w + b.w }; }
friend constexpr Vec4 operator-(const Vec4& a, const Vec4& b) { return { a.x - b.x, a.y - b.y, a.z - b.z, a.w - b.w }; }
friend constexpr Vec4 operator*(const Vec4& a, const Vec4& b) { return { a.x * b.x, a.y * b.y, a.z * b.z, a.w * b.w }; }
diff --git a/core/src/Utils/VectorHash.hpp b/core/src/Utils/VectorHash.hpp
new file mode 100644
index 0000000..7df9c35
--- /dev/null
+++ b/core/src/Utils/VectorHash.hpp
@@ -0,0 +1,46 @@
+#pragma once
+
+#include "Utils/Hash.hpp"
+#include "Utils/Vector.hpp"
+
+#include <cstddef>
+#include <functional>
+
+template <class T>
+struct std::hash<Vec2<T>>
+{
+ size_t operator()(const Vec2<T>& vec) const
+ {
+ size_t result;
+ HashUtils::Combine(result, vec.x);
+ HashUtils::Combine(result, vec.y);
+ return result;
+ }
+};
+
+template <class T>
+struct std::hash<Vec3<T>>
+{
+ size_t operator()(const Vec3<T>& vec) const
+ {
+ size_t result;
+ HashUtils::Combine(result, vec.x);
+ HashUtils::Combine(result, vec.y);
+ HashUtils::Combine(result, vec.z);
+ return result;
+ }
+};
+
+template <class T>
+struct std::hash<Vec4<T>>
+{
+ size_t operator()(const Vec4<T>& vec) const
+ {
+ size_t result;
+ HashUtils::Combine(result, vec.x);
+ HashUtils::Combine(result, vec.y);
+ HashUtils::Combine(result, vec.z);
+ HashUtils::Combine(result, vec.w);
+ return result;
+ }
+};