From: Giuseppe Ottaviano Date: Sat, 12 Sep 2015 00:37:20 +0000 (-0700) Subject: Add support for std::string in folly::Hash X-Git-Tag: deprecate-dynamic-initializer~418 X-Git-Url: http://plrg.eecs.uci.edu/git/?a=commitdiff_plain;h=c1be26e5844b71e2e2ce197fa71b88cd36376761;p=folly.git Add support for std::string in folly::Hash Reviewed By: @​liviu Differential Revision: D2436705 --- diff --git a/folly/Hash.h b/folly/Hash.h index f40f4ec2..07eb5571 100644 --- a/folly/Hash.h +++ b/folly/Hash.h @@ -375,6 +375,12 @@ template<> struct hasher { } }; +template<> struct hasher { + size_t operator()(const std::string& key) const { + return hash::SpookyHashV2::Hash64(key.data(), key.size(), 0); + } +}; + template struct hasher::value, void>::type> { size_t operator()(T key) const { diff --git a/folly/test/HashTest.cpp b/folly/test/HashTest.cpp index bbae4f6f..de12b1e0 100644 --- a/folly/test/HashTest.cpp +++ b/folly/test/HashTest.cpp @@ -303,7 +303,7 @@ TEST(Hash, std_tuple_different_hash) { std::hash()(t3)); } -TEST(Range, Hash) { +TEST(Hash, Strings) { using namespace folly; StringPiece a1 = "10050517", b1 = "51107032", @@ -329,4 +329,10 @@ TEST(Range, Hash) { EXPECT_NE(h2(w1), h2(w2)); EXPECT_NE(h2(w1), h2(w3)); EXPECT_NE(h2(w2), h2(w4)); + + // Check compatibility with std::string. + EXPECT_EQ(h2(a1), h2(a1.str())); + EXPECT_EQ(h2(a2), h2(a2.str())); + EXPECT_EQ(h2(a3), h2(a3.str())); + EXPECT_EQ(h2(a4), h2(a4.str())); }