/*
- * 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.
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
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...);
+}
+
//////////////////////////////////////////////////////////////////////
/*
/*
- * 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.
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) {