From 02985ca4cbfe0f223f531cb4652ff46481a81056 Mon Sep 17 00:00:00 2001 From: Richard Barnes Date: Thu, 6 Jul 2023 18:59:29 -0400 Subject: [PATCH] Simplify hash function --- include/richdem/depressions/depression_hierarchy.hpp | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/include/richdem/depressions/depression_hierarchy.hpp b/include/richdem/depressions/depression_hierarchy.hpp index 101fc9db..0b309634 100644 --- a/include/richdem/depressions/depression_hierarchy.hpp +++ b/include/richdem/depressions/depression_hierarchy.hpp @@ -147,12 +147,16 @@ struct Outlet { template struct OutletLinkHash { std::size_t operator()(const OutletLink& out) const { + // This hash function depends on the data type of `dh_label_t` being + // `uint32_t` so that we can bit-shift without having to convert and without + // losing bits + static_assert(std::is_same_v); + // Since depa and depb are sorted on construction, we don't have to worry // about which order the invoking code called them in and our hash function - // doesn't need to be symmetric with respect to depa and depb. - - // Boost hash combine function: https://stackoverflow.com/a/27952689/752843 - return out.depa ^ (out.depb + 0x9e3779b9 + (out.depa << 6) + (out.depa >> 2)); + // doesn't need to be symmetric with respect to depa and depb. A simple + // bit-shift is sufficient. + return (static_cast(out.depa) << 32) | static_cast(out.depb); } };