Make hash_combine accept a configurable hash function
authorDavid Vickrey <davidvickrey@fb.com>
Mon, 11 Mar 2013 22:34:18 +0000 (15:34 -0700)
committerJordan DeLong <jdelong@fb.com>
Tue, 19 Mar 2013 00:09:06 +0000 (17:09 -0700)
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

folly/Hash.h
folly/test/HashTest.cpp

index 1cbeda6a81e5723786760c0d77a52b208d0b8ca1..132ad846edda98a359107bc8cadd3703d2074a32 100644 (file)
@@ -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.
 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
@@ -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...);
+}
+
 //////////////////////////////////////////////////////////////////////
 
 /*
index dc3d403c690f4d0bee23cf815db885bd12da2432..aef8f226e7b2fc4539bad1ee41e9ed1d68909ad0 100644 (file)
@@ -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.
@@ -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) {