From b94a42b978251c4cdb4eb0be2a2e8d9dc8949eba Mon Sep 17 00:00:00 2001 From: Steven Le Rouzic Date: Tue, 7 Jan 2025 23:17:50 +0100 Subject: More work on hashing --- asl/hash.hpp | 34 +++++++++++++++++++++++++--------- 1 file changed, 25 insertions(+), 9 deletions(-) (limited to 'asl/hash.hpp') diff --git a/asl/hash.hpp b/asl/hash.hpp index fd3c116..5f81e92 100644 --- a/asl/hash.hpp +++ b/asl/hash.hpp @@ -68,13 +68,26 @@ struct HashState constexpr HashState() = default; explicit constexpr HashState(uint128_t s) : state{s} {} - static HashState combine_bytes(HashState h, span bytes) + template + static HashState combine_contiguous(HashState h, span s) { - auto hashed = city_hash::CityHash128WithSeed( - reinterpret_cast(bytes.data()), - static_cast(bytes.size()), - h.state); - return HashState{hashed}; + if constexpr (uniquely_represented) + { + auto bytes = as_bytes(s); + auto hashed = city_hash::CityHash128WithSeed( + reinterpret_cast(bytes.data()), + static_cast(bytes.size()), + h.state); + return HashState{hashed}; + } + else + { + for (const auto& value: s) + { + h = AslHashValue(ASL_MOVE(h), value); + } + return h; + } } static constexpr HashState combine(HashState h) @@ -85,7 +98,7 @@ struct HashState template Arg, hashable_generic... Remaining> static constexpr HashState combine(HashState h, const Arg& arg, const Remaining&... remaining) { - return combine(AslHashValue(h, arg), remaining...); + return combine(AslHashValue(ASL_MOVE(h), arg), remaining...); } }; @@ -95,15 +108,18 @@ concept hashable = hashable_generic; template constexpr H AslHashValue(H h, const T& value) { - return H::combine_bytes(h, as_bytes(span{&value, 1})); + return H::combine_contiguous(ASL_MOVE(h), span{&value, 1}); } template constexpr H AslHashValue(H h, bool value) { - return AslHashValue(h, value ? 1 : 0); + return AslHashValue(ASL_MOVE(h), value ? 1 : 0); } +template +constexpr void AslHashValue(H h, T*); // Don't hash pointers + template constexpr uint64_t hash_value(const T& value) { -- cgit