Skip to content

Commit

Permalink
Simplify hash function
Browse files Browse the repository at this point in the history
  • Loading branch information
r-barnes committed Jul 6, 2023
1 parent c5a183a commit 02985ca
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions include/richdem/depressions/depression_hierarchy.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,12 +147,16 @@ struct Outlet {
template <class elev_t>
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<dh_label_t, uint32_t>);

// 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<uint64_t>(out.depa) << 32) | static_cast<uint64_t>(out.depb);
}
};

Expand Down

0 comments on commit 02985ca

Please sign in to comment.