From: David Vickrey Date: Mon, 11 Mar 2013 22:34:18 +0000 (-0700) Subject: Make hash_combine accept a configurable hash function X-Git-Tag: v0.22.0~1039 X-Git-Url: http://plrg.eecs.uci.edu/git/?a=commitdiff_plain;h=e4f530f84abb296327f2e8d986a38fce660dcd71;p=folly.git Make hash_combine accept a configurable hash function 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 --- diff --git a/folly/Hash.h b/folly/Hash.h index 1cbeda6a..132ad846 100644 --- a/folly/Hash.h +++ b/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. @@ -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 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; 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 -size_t hash_combine(const T& t, const Ts&... ts) { - size_t seed = std::hash()(t); +// Never used, but gcc demands it. +template +inline size_t hash_combine_generic() { + return 0; +} + +template +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(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 + static size_t hash(const T& t) { + return std::hash()(t); + } +}; + +template +size_t hash_combine(const T& t, const Ts&... ts) { + return hash_combine_generic(t, ts...); +} + ////////////////////////////////////////////////////////////////////// /* diff --git a/folly/test/HashTest.cpp b/folly/test/HashTest.cpp index dc3d403c..aef8f226 100644 --- a/folly/test/HashTest.cpp +++ b/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. @@ -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& p) { + return p.first + p.second; + } +}; + +template +size_t hash_combine_test(const T& t, const Ts&... ts) { + return hash_combine_generic(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) {