std hash support

This commit is contained in:
BlackMATov
2020-11-25 23:10:52 +07:00
parent 65699f6b0f
commit 88f1ca95fd
5 changed files with 46 additions and 0 deletions

View File

@@ -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<T>{}(x) + 0x9e3779b9 + (seed << 6) + ( seed >> 2));
}
};
}
namespace vmath_hpp
{
template < typename To, typename From >

View File

@@ -18,6 +18,16 @@
#include "vmath_mat.hpp"
#include "vmath_mat_fun.hpp"
namespace std
{
template < typename T, size_t Size >
struct hash<vmath_hpp::mat<T, Size>> {
size_t operator()(const vmath_hpp::mat<T, Size>& m) const noexcept {
return vmath_hpp::detail::fold(vmath_hpp::detail::hash_combiner{}, size_t{}, m);
}
};
}
namespace vmath_hpp
{
// cast_to

View File

@@ -14,6 +14,16 @@
#include "vmath_vec.hpp"
#include "vmath_vec_fun.hpp"
namespace std
{
template < typename T, size_t Size >
struct hash<vmath_hpp::vec<T, Size>> {
size_t operator()(const vmath_hpp::vec<T, Size>& v) const noexcept {
return vmath_hpp::detail::fold(vmath_hpp::detail::hash_combiner{}, size_t{}, v);
}
};
}
namespace vmath_hpp
{
// cast_to

View File

@@ -18,6 +18,11 @@ namespace
}
TEST_CASE("vmath/mat_ext") {
SECTION("hash") {
REQUIRE(std::hash<mat2i>{}({1,2,3,4}) == std::hash<mat2i>{}({1,2,3,4}));
REQUIRE_FALSE(std::hash<mat2i>{}({1,2,3,4}) == std::hash<mat2i>{}({1,2,4,3}));
}
SECTION("cast_to") {
constexpr auto m = cast_to<int>(mat2f{1.5f});
STATIC_REQUIRE(m == mat2i(1));

View File

@@ -18,6 +18,17 @@ namespace
}
TEST_CASE("vmath/vec_ext") {
SECTION("hash") {
REQUIRE(std::hash<vec2i>{}({1,2}) == std::hash<vec2i>{}({1,2}));
REQUIRE_FALSE(std::hash<vec2i>{}({1,2}) == std::hash<vec2i>{}({2,1}));
REQUIRE(std::hash<vec3i>{}({1,2,3}) == std::hash<vec3i>{}({1,2,3}));
REQUIRE_FALSE(std::hash<vec3i>{}({1,2,3}) == std::hash<vec3i>{}({3,2,1}));
REQUIRE(std::hash<vec4i>{}({1,2,3,4}) == std::hash<vec4i>{}({1,2,3,4}));
REQUIRE_FALSE(std::hash<vec4i>{}({1,2,3,4}) == std::hash<vec4i>{}({3,2,1,4}));
}
SECTION("cast_to") {
constexpr auto v = cast_to<int>(vec2f{1.5f});
STATIC_REQUIRE(v == vec2i(1));