Commit 01ef9fbc authored by Yedidya Feldblum's avatar Yedidya Feldblum Committed by Facebook Github Bot

Let hash_combine_generic take hasher instances

Summary: [Folly] Let `hash_combine_generic` take hasher instances as parameters v.s. taking hasher types as template parameters. This would permit its use with non-default-constructible hasher types (if any exist).

Reviewed By: nbronson

Differential Revision: D9330419

fbshipit-source-id: 2fb306e16603c07812559c47e31d7a9e2edcb072
parent 35c96fe0
......@@ -62,7 +62,7 @@ inline uint64_t hash_128_to_64(
// Never used, but gcc demands it.
template <class Hasher>
inline size_t hash_combine_generic() {
inline size_t hash_combine_generic(const Hasher&) {
return 0;
}
......@@ -80,12 +80,12 @@ hash_range(Iter begin, Iter end, uint64_t hash = 0, Hash hasher = Hash()) {
inline uint32_t twang_32from64(uint64_t key) noexcept;
template <class Hasher, typename T, typename... Ts>
size_t hash_combine_generic(const T& t, const Ts&... ts) {
size_t seed = Hasher::hash(t);
size_t hash_combine_generic(const Hasher& h, const T& t, const Ts&... ts) {
size_t seed = h(t);
if (sizeof...(ts) == 0) {
return seed;
}
size_t remainder = hash_combine_generic<Hasher>(ts...);
size_t remainder = hash_combine_generic(h, ts...);
/* static */ if (sizeof(size_t) == sizeof(uint32_t)) {
return twang_32from64((uint64_t(seed) << 32) | remainder);
} else {
......@@ -103,14 +103,14 @@ class StdHasher {
// supplied by either the standard library or by users to be default
// constructible.
template <typename T>
static size_t hash(const T& t) noexcept(noexcept(std::hash<T>()(t))) {
size_t operator()(const T& t) const noexcept(noexcept(std::hash<T>()(t))) {
return std::hash<T>()(t);
}
};
template <typename T, typename... Ts>
size_t hash_combine(const T& t, const Ts&... ts) {
return hash_combine_generic<StdHasher>(t, ts...);
return hash_combine_generic(StdHasher{}, t, ts...);
}
//////////////////////////////////////////////////////////////////////
......
......@@ -409,14 +409,14 @@ TEST(Hash, float_types) {
// Not a full hasher since only handles one type
class TestHasher {
public:
static size_t hash(const std::pair<int, int>& p) {
size_t operator()(const std::pair<int, int>& p) const {
return p.first + p.second;
}
};
template <typename T, typename... Ts>
size_t hash_combine_test(const T& t, const Ts&... ts) {
return hash_combine_generic<TestHasher>(t, ts...);
return hash_combine_generic(TestHasher{}, t, ts...);
}
TEST(Hash, pair) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment