From 48f7e22d9cc93989173afe2550fcc2f082c61773 Mon Sep 17 00:00:00 2001 From: Steven Le Rouzic Date: Mon, 6 Jan 2025 00:33:50 +0100 Subject: Start work on hashing --- asl/hash.hpp | 45 +++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 43 insertions(+), 2 deletions(-) (limited to 'asl/hash.hpp') diff --git a/asl/hash.hpp b/asl/hash.hpp index 6190e8f..5eb64f6 100644 --- a/asl/hash.hpp +++ b/asl/hash.hpp @@ -1,12 +1,12 @@ #pragma once #include "asl/integers.hpp" +#include "asl/meta.hpp" +#include "asl/utility.hpp" namespace asl::city_hash { -// All CityHash stuff below this point - // Hash function for a byte array. uint64_t CityHash64(const char *s, size_t len); @@ -51,3 +51,44 @@ constexpr uint64_t Hash128to64(const uint128_t& x) } } // namespace asl::city_hash + +namespace asl +{ + +template +concept hashable_generic = requires(const T& value, H h) +{ + { AslHashValue(h, value) } -> same_as; +}; + +struct HashState +{ + uint128_t state{}; + + constexpr HashState() = default; + explicit constexpr HashState(uint128_t s) : state{s} {} + + static constexpr HashState combine(HashState h) + { + return h; + } + + template Arg, hashable_generic... Remaining> + static constexpr HashState combine(HashState h, const Arg& arg, const Remaining&... remaining) + { + return combine(AslHashValue(h, arg), remaining...); + } +}; + +template +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}; +} + +} // namespace asl + -- cgit