From 9f2f619a21ea1a04e346426368839f189b5e11f3 Mon Sep 17 00:00:00 2001 From: Orange Date: Tue, 14 Oct 2025 13:00:28 +0300 Subject: [PATCH] Adds hash support for Vector2, Vector3, and Vector4 Implements `std::hash` specialization for `omath::Vector2`, `omath::Vector3`, and `omath::Vector4` with float components. This enables usage of these vector types as keys in hash-based containers, such as `std::unordered_map` and `std::unordered_set`. --- include/omath/linear_algebra/vector2.hpp | 15 +++++++++++++++ include/omath/linear_algebra/vector3.hpp | 1 + include/omath/linear_algebra/vector4.hpp | 16 ++++++++++++++++ 3 files changed, 32 insertions(+) diff --git a/include/omath/linear_algebra/vector2.hpp b/include/omath/linear_algebra/vector2.hpp index 4dc96c7..2994144 100644 --- a/include/omath/linear_algebra/vector2.hpp +++ b/include/omath/linear_algebra/vector2.hpp @@ -234,6 +234,21 @@ namespace omath }; } // namespace omath +template<> struct std::hash> +{ + [[nodiscard]] + std::size_t operator()(const omath::Vector2& vec) const noexcept + { + std::size_t hash = 0; + constexpr std::hash hasher; + + hash ^= hasher(vec.x) + 0x9e3779b9 + (hash << 6) + (hash >> 2); + hash ^= hasher(vec.y) + 0x9e3779b9 + (hash << 6) + (hash >> 2); + + return hash; + } +}; + template struct std::formatter> // NOLINT(*-dcl58-cpp) { diff --git a/include/omath/linear_algebra/vector3.hpp b/include/omath/linear_algebra/vector3.hpp index 7e5838f..77d60cf 100644 --- a/include/omath/linear_algebra/vector3.hpp +++ b/include/omath/linear_algebra/vector3.hpp @@ -273,6 +273,7 @@ namespace omath template<> struct std::hash> { + [[nodiscard]] std::size_t operator()(const omath::Vector3& vec) const noexcept { std::size_t hash = 0; diff --git a/include/omath/linear_algebra/vector4.hpp b/include/omath/linear_algebra/vector4.hpp index d1e1377..25d6a6d 100644 --- a/include/omath/linear_algebra/vector4.hpp +++ b/include/omath/linear_algebra/vector4.hpp @@ -202,6 +202,22 @@ namespace omath }; } // namespace omath +template<> struct std::hash> +{ + [[nodiscard]] + std::size_t operator()(const omath::Vector4& vec) const noexcept + { + std::size_t hash = 0; + constexpr std::hash hasher; + + hash ^= hasher(vec.x) + 0x9e3779b9 + (hash << 6) + (hash >> 2); + hash ^= hasher(vec.y) + 0x9e3779b9 + (hash << 6) + (hash >> 2); + hash ^= hasher(vec.z) + 0x9e3779b9 + (hash << 6) + (hash >> 2); + hash ^= hasher(vec.w) + 0x9e3779b9 + (hash << 6) + (hash >> 2); + return hash; + } +}; + template struct std::formatter> // NOLINT(*-dcl58-cpp) {