From 55d5dfd9a36157bf9fb59cc2a59f12218d7506f7 Mon Sep 17 00:00:00 2001 From: BlackMATov Date: Mon, 25 Jan 2021 06:10:53 +0700 Subject: [PATCH] hash and cast_to qua functions --- headers/vmath.hpp/vmath_ext.hpp | 18 ++++++++ untests/vmath_ext_tests.cpp | 73 +++++++++++++++++++++++++++++++-- 2 files changed, 88 insertions(+), 3 deletions(-) diff --git a/headers/vmath.hpp/vmath_ext.hpp b/headers/vmath.hpp/vmath_ext.hpp index eb89ef2..de68b97 100644 --- a/headers/vmath.hpp/vmath_ext.hpp +++ b/headers/vmath.hpp/vmath_ext.hpp @@ -11,6 +11,7 @@ #include "vmath_fun.hpp" #include "vmath_vec_fun.hpp" #include "vmath_mat_fun.hpp" +#include "vmath_qua_fun.hpp" // // Units @@ -73,6 +74,11 @@ namespace vmath_hpp::detail [[nodiscard]] std::size_t hash(const mat& m) noexcept { return fold_join(hash_combiner{}, std::size_t{}, m); } + + template < typename T > + [[nodiscard]] std::size_t hash(const qua& q) noexcept { + return fold_join(hash_combiner{}, std::size_t{}, q); + } } namespace std @@ -90,6 +96,13 @@ namespace std return vmath_hpp::detail::hash(m); } }; + + template < typename T > + struct hash> { + size_t operator()(const vmath_hpp::qua& q) const noexcept { + return vmath_hpp::detail::hash(q); + } + }; } // @@ -115,6 +128,11 @@ namespace vmath_hpp [[nodiscard]] constexpr mat cast_to(const mat& m) { return detail::map_join([](const vec& v){ return cast_to(v); }, m); } + + template < typename To, typename From > + [[nodiscard]] constexpr qua cast_to(const qua& q) { + return detail::map_join([](From x){ return cast_to(x); }, q); + } } // diff --git a/untests/vmath_ext_tests.cpp b/untests/vmath_ext_tests.cpp index db3ea47..47014c0 100644 --- a/untests/vmath_ext_tests.cpp +++ b/untests/vmath_ext_tests.cpp @@ -52,7 +52,7 @@ TEST_CASE("vmath/ext") { STATIC_REQUIRE(identity4x4 == int4x4()); } - SUBCASE("hash") { + SUBCASE("vector hash") { REQUIRE(std::hash{}({1,2}) == std::hash{}({1,2})); REQUIRE_FALSE(std::hash{}({1,2}) == std::hash{}({2,1})); @@ -62,8 +62,6 @@ TEST_CASE("vmath/ext") { 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})); - 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})); { std::set s; s.insert(int2(1,2)); @@ -92,6 +90,70 @@ TEST_CASE("vmath/ext") { } } + SUBCASE("matrix 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})); + + { + std::set s; + s.insert(int2x2(1,2,3,4)); + REQUIRE(s.count(int2x2(1,2,3,4)) > 0); + REQUIRE_FALSE(s.count(int2x2(1,1,1,1)) > 0); + } + { + std::map s; + s.emplace(int2x2(1,2,3,4),3); + s.emplace(int2x2(2,3,4,5),5); + REQUIRE(s[int2x2(1,2,3,4)] == 3); + REQUIRE(s[int2x2(2,3,4,5)] == 5); + } + { + std::unordered_set s; + s.insert(int2x2(1,2,3,4)); + REQUIRE(s.count(int2x2(1,2,3,4)) > 0); + REQUIRE_FALSE(s.count(int2x2(1,1,1,1)) > 0); + } + { + std::unordered_map s; + s.emplace(int2x2(1,2,3,4),3); + s.emplace(int2x2(2,3,4,5),5); + REQUIRE(s[int2x2(1,2,3,4)] == 3); + REQUIRE(s[int2x2(2,3,4,5)] == 5); + } + } + + SUBCASE("quaternion hash") { + 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})); + + { + std::set> s; + s.insert(qua(1,2,3,4)); + REQUIRE(s.count(qua(1,2,3,4)) > 0); + REQUIRE_FALSE(s.count(qua(1,1,1,1)) > 0); + } + { + std::map, int> s; + s.emplace(qua(1,2,3,4),3); + s.emplace(qua(2,3,4,5),5); + REQUIRE(s[qua(1,2,3,4)] == 3); + REQUIRE(s[qua(2,3,4,5)] == 5); + } + { + std::unordered_set> s; + s.insert(qua(1,2,3,4)); + REQUIRE(s.count(qua(1,2,3,4)) > 0); + REQUIRE_FALSE(s.count(qua(1,1,1,1)) > 0); + } + { + std::unordered_map, int> s; + s.emplace(qua(1,2,3,4),3); + s.emplace(qua(2,3,4,5),5); + REQUIRE(s[qua(1,2,3,4)] == 3); + REQUIRE(s[qua(2,3,4,5)] == 5); + } + } + SUBCASE("cast_to") { { constexpr auto i = cast_to(1.5f); @@ -108,6 +170,11 @@ TEST_CASE("vmath/ext") { STATIC_REQUIRE(m == int2x2(1)); STATIC_REQUIRE(std::is_same_v); } + { + constexpr auto v = cast_to(fqua{1.5f, 2.2f, 3.6f, 4.5f}); + STATIC_REQUIRE(v == qua(1,2,3,4)); + STATIC_REQUIRE(std::is_same_v); + } } SUBCASE("component") {