Skip to content

Commit

Permalink
Make hash_combine accept a configurable hash function
Browse files Browse the repository at this point in the history
Summary:
std::hash is not awesome and not configurable.  Typical cases you might want to customize are:
string: I happen to know that fnv isn't super awesome, for example, and that's what folly uses for std::hash fbstring.
pointers: you may want to hash the contents of the pointer instead of the address for certain types.

This is a very simple diff that lets you do that.  It provides StdHasher that passes through to std::hash and uses that for hash_combine, so this should be 100% backward compatible.

Test Plan: test_hash.  I will add another test for using a hasher besides StdHasher shortly.

Reviewed By: delong.j@fb.com

FB internal diff: D733899
  • Loading branch information
David Vickrey authored and jdelong committed Mar 19, 2013
1 parent 9fb46d1 commit e4f530f
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 13 deletions.
44 changes: 32 additions & 12 deletions folly/Hash.h
@@ -1,5 +1,5 @@
/*
* Copyright 2012 Facebook, Inc.
* Copyright 2013 Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -32,14 +32,11 @@
namespace folly { namespace hash {

// This is a general-purpose way to create a single hash from multiple
// hashable objects. It relies on std::hash<T> being available for all
// relevant types and combines those hashes in an order-dependent way
// to yield a new hash.
// hashable objects. hash_combine_generic takes a class Hasher implementing
// hash<T>; hash_combine uses a default hasher StdHasher that uses std::hash.
// hash_combine_generic hashes each argument and combines those hashes in
// an order-dependent way to yield a new hash.

// Never used, but gcc demands it.
inline size_t hash_combine() {
return 0;
}

// This is the Hash128to64 function from Google's cityhash (available
// under the MIT License). We use it to reduce multiple 64 bit hashes
Expand All @@ -55,16 +52,39 @@ inline size_t hash_128_to_64(const size_t upper, const size_t lower) {
return b;
}

template <typename T, typename... Ts>
size_t hash_combine(const T& t, const Ts&... ts) {
size_t seed = std::hash<T>()(t);
// Never used, but gcc demands it.
template <class Hasher>
inline size_t hash_combine_generic() {
return 0;
}

template <class Hasher, typename T, typename... Ts>
size_t hash_combine_generic(const T& t, const Ts&... ts) {
size_t seed = Hasher::hash(t);
if (sizeof...(ts) == 0) {
return seed;
}
size_t remainder = hash_combine(ts...);
size_t remainder = hash_combine_generic<Hasher>(ts...);
return hash_128_to_64(seed, remainder);
}

// Simply uses std::hash to hash. Note that std::hash is not guaranteed
// to be a very good hash function; provided std::hash doesn't collide on
// the individual inputs, you are fine, but that won't be true for, say,
// strings or pairs
class StdHasher {
public:
template <typename T>
static size_t hash(const 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...);
}

//////////////////////////////////////////////////////////////////////

/*
Expand Down
44 changes: 43 additions & 1 deletion folly/test/HashTest.cpp
@@ -1,5 +1,5 @@
/*
* Copyright 2012 Facebook, Inc.
* Copyright 2013 Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -173,14 +173,56 @@ TEST(Hash, hasher) {
EXPECT_EQ(get_default(m, 4), 5);
}

// Not a full hasher since only handles one type
class TestHasher {
public:
static size_t hash(const std::pair<int, int>& p) {
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...);
}

TEST(Hash, pair) {
auto a = std::make_pair(1, 2);
auto b = std::make_pair(3, 4);
auto c = std::make_pair(1, 2);
auto d = std::make_pair(2, 1);
EXPECT_EQ(hash_combine(a),
hash_combine(c));
EXPECT_NE(hash_combine(b),
hash_combine(c));
EXPECT_NE(hash_combine(d),
hash_combine(c));

// With composition
EXPECT_EQ(hash_combine(a, b),
hash_combine(c, b));
// Test order dependence
EXPECT_NE(hash_combine(a, b),
hash_combine(b, a));

// Test with custom hasher
EXPECT_EQ(hash_combine_test(a),
hash_combine_test(c));
// 3 + 4 != 1 + 2
EXPECT_NE(hash_combine_test(b),
hash_combine_test(c));
// This time, thanks to a terrible hash function, these are equal
EXPECT_EQ(hash_combine_test(d),
hash_combine_test(c));
// With composition
EXPECT_EQ(hash_combine_test(a, b),
hash_combine_test(c, b));
// Test order dependence
EXPECT_NE(hash_combine_test(a, b),
hash_combine_test(b, a));
// Again, 1 + 2 == 2 + 1
EXPECT_EQ(hash_combine_test(a, b),
hash_combine_test(d, b));
}

TEST(Hash, hash_combine) {
Expand Down

0 comments on commit e4f530f

Please sign in to comment.