From e18b054779766269a4b9ca68729c380d24c0535d Mon Sep 17 00:00:00 2001 From: Steven Le Rouzic Date: Mon, 6 Jan 2025 22:25:09 +0100 Subject: Some more work on hashing --- asl/hash.hpp | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) (limited to 'asl/hash.hpp') diff --git a/asl/hash.hpp b/asl/hash.hpp index 5eb64f6..fd3c116 100644 --- a/asl/hash.hpp +++ b/asl/hash.hpp @@ -2,7 +2,7 @@ #include "asl/integers.hpp" #include "asl/meta.hpp" -#include "asl/utility.hpp" +#include "asl/span.hpp" namespace asl::city_hash { @@ -68,6 +68,15 @@ struct HashState constexpr HashState() = default; explicit constexpr HashState(uint128_t s) : state{s} {} + static HashState combine_bytes(HashState h, span bytes) + { + auto hashed = city_hash::CityHash128WithSeed( + reinterpret_cast(bytes.data()), + static_cast(bytes.size()), + h.state); + return HashState{hashed}; + } + static constexpr HashState combine(HashState h) { return h; @@ -86,8 +95,20 @@ concept hashable = hashable_generic; template constexpr H AslHashValue(H h, const T& value) { - auto hashed = city_hash::CityHash128WithSeed(reinterpret_cast(&value), size_of, h.state); - return HashState{hashed}; + return H::combine_bytes(h, as_bytes(span{&value, 1})); +} + +template +constexpr H AslHashValue(H h, bool value) +{ + return AslHashValue(h, value ? 1 : 0); +} + +template +constexpr uint64_t hash_value(const T& value) +{ + auto result = AslHashValue(HashState{}, value).state; + return city_hash::Hash128to64(result); } } // namespace asl -- cgit