Add support for std::string in folly::Hash
authorGiuseppe Ottaviano <ott@fb.com>
Sat, 12 Sep 2015 00:37:20 +0000 (17:37 -0700)
committerfacebook-github-bot-1 <folly-bot@fb.com>
Sat, 12 Sep 2015 01:20:17 +0000 (18:20 -0700)
Reviewed By: @​liviu

Differential Revision: D2436705

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

index f40f4ec27d620512239a7d85fa6f7cac83600863..07eb5571eb5a891f9d1907b399dcc64343cd2c06 100644 (file)
@@ -375,6 +375,12 @@ template<> struct hasher<uint64_t> {
   }
 };
 
+template<> struct hasher<std::string> {
+  size_t operator()(const std::string& key) const {
+    return hash::SpookyHashV2::Hash64(key.data(), key.size(), 0);
+  }
+};
+
 template <class T>
 struct hasher<T, typename std::enable_if<std::is_enum<T>::value, void>::type> {
   size_t operator()(T key) const {
index bbae4f6f29fcb07e9820141e2955ded2484b1f94..de12b1e02ae2b58ef55a12145803882efb58101d 100644 (file)
@@ -303,7 +303,7 @@ TEST(Hash, std_tuple_different_hash) {
             std::hash<tuple3>()(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()));
 }