From 88f1ca95fd7509cc07b2e99f93cb149713f72134 Mon Sep 17 00:00:00 2001 From: BlackMATov Date: Wed, 25 Nov 2020 23:10:52 +0700 Subject: [PATCH] std hash support --- headers/vmath.hpp/vmath_ext.hpp | 10 ++++++++++ headers/vmath.hpp/vmath_mat_ext.hpp | 10 ++++++++++ headers/vmath.hpp/vmath_vec_ext.hpp | 10 ++++++++++ untests/vmath_mat_ext_tests.cpp | 5 +++++ untests/vmath_vec_ext_tests.cpp | 11 +++++++++++ 5 files changed, 46 insertions(+) diff --git a/headers/vmath.hpp/vmath_ext.hpp b/headers/vmath.hpp/vmath_ext.hpp index 1dd9b72..a5a0c02 100644 --- a/headers/vmath.hpp/vmath_ext.hpp +++ b/headers/vmath.hpp/vmath_ext.hpp @@ -10,6 +10,16 @@ #include "vmath_fun.hpp" +namespace vmath_hpp::detail +{ + struct hash_combiner { + template < typename T > + std::size_t operator()(std::size_t seed, const T& x) noexcept { + return (seed ^= std::hash{}(x) + 0x9e3779b9 + (seed << 6) + ( seed >> 2)); + } + }; +} + namespace vmath_hpp { template < typename To, typename From > diff --git a/headers/vmath.hpp/vmath_mat_ext.hpp b/headers/vmath.hpp/vmath_mat_ext.hpp index 6639501..bf6494d 100644 --- a/headers/vmath.hpp/vmath_mat_ext.hpp +++ b/headers/vmath.hpp/vmath_mat_ext.hpp @@ -18,6 +18,16 @@ #include "vmath_mat.hpp" #include "vmath_mat_fun.hpp" +namespace std +{ + template < typename T, size_t Size > + struct hash> { + size_t operator()(const vmath_hpp::mat& m) const noexcept { + return vmath_hpp::detail::fold(vmath_hpp::detail::hash_combiner{}, size_t{}, m); + } + }; +} + namespace vmath_hpp { // cast_to diff --git a/headers/vmath.hpp/vmath_vec_ext.hpp b/headers/vmath.hpp/vmath_vec_ext.hpp index 84dd79d..75b3019 100644 --- a/headers/vmath.hpp/vmath_vec_ext.hpp +++ b/headers/vmath.hpp/vmath_vec_ext.hpp @@ -14,6 +14,16 @@ #include "vmath_vec.hpp" #include "vmath_vec_fun.hpp" +namespace std +{ + template < typename T, size_t Size > + struct hash> { + size_t operator()(const vmath_hpp::vec& v) const noexcept { + return vmath_hpp::detail::fold(vmath_hpp::detail::hash_combiner{}, size_t{}, v); + } + }; +} + namespace vmath_hpp { // cast_to diff --git a/untests/vmath_mat_ext_tests.cpp b/untests/vmath_mat_ext_tests.cpp index 342ffd3..9cceae5 100644 --- a/untests/vmath_mat_ext_tests.cpp +++ b/untests/vmath_mat_ext_tests.cpp @@ -18,6 +18,11 @@ namespace } TEST_CASE("vmath/mat_ext") { + SECTION("hash") { + REQUIRE(std::hash{}({1,2,3,4}) == std::hash{}({1,2,3,4})); + REQUIRE_FALSE(std::hash{}({1,2,3,4}) == std::hash{}({1,2,4,3})); + } + SECTION("cast_to") { constexpr auto m = cast_to(mat2f{1.5f}); STATIC_REQUIRE(m == mat2i(1)); diff --git a/untests/vmath_vec_ext_tests.cpp b/untests/vmath_vec_ext_tests.cpp index a013df2..0db5f33 100644 --- a/untests/vmath_vec_ext_tests.cpp +++ b/untests/vmath_vec_ext_tests.cpp @@ -18,6 +18,17 @@ namespace } TEST_CASE("vmath/vec_ext") { + SECTION("hash") { + REQUIRE(std::hash{}({1,2}) == std::hash{}({1,2})); + REQUIRE_FALSE(std::hash{}({1,2}) == std::hash{}({2,1})); + + REQUIRE(std::hash{}({1,2,3}) == std::hash{}({1,2,3})); + REQUIRE_FALSE(std::hash{}({1,2,3}) == std::hash{}({3,2,1})); + + REQUIRE(std::hash{}({1,2,3,4}) == std::hash{}({1,2,3,4})); + REQUIRE_FALSE(std::hash{}({1,2,3,4}) == std::hash{}({3,2,1,4})); + } + SECTION("cast_to") { constexpr auto v = cast_to(vec2f{1.5f}); STATIC_REQUIRE(v == vec2i(1));