Merge pull request #218 from jpcima/numeric-hash

Hashing function for numbers
This commit is contained in:
JP Cimalando 2020-05-07 12:27:28 +02:00 committed by GitHub
commit dda4162a01
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -76,3 +76,15 @@ constexpr uint64_t hashNoAmpersand(absl::string_view s, uint64_t h = Fnv1aBasis)
: hashNoAmpersand( { s.data() + 1, s.length() - 1 }, (h ^ s.front()) * Fnv1aPrime )
);
}
/**
* @brief Run-time hashing function for numbers, useful for example to
* create hash functions for keys which depend on numeric values.
*/
template <class Int>
uint64_t hashNumber(Int i, uint64_t h = Fnv1aBasis)
{
static_assert(std::is_arithmetic<Int>::value,
"The hashed object must be of arithmetic type");
return hash(absl::string_view(reinterpret_cast<const char*>(&i), sizeof(i)), h);
}