Add SpookyHashV2-based hash to StringPiece
authorTom Jackson <tjackson@fb.com>
Fri, 8 May 2015 07:43:26 +0000 (00:43 -0700)
committerPraveen Kumar Ramakrishnan <praveenr@fb.com>
Tue, 12 May 2015 00:02:43 +0000 (17:02 -0700)
Summary: Since the old one is weak and slow.

Test Plan: Unit tests

Reviewed By: ott@fb.com

Subscribers: trunkagent, maxime, folly-diffs@, yfeldblum, chalfant

FB internal diff: D2052630

Tasks: 6998080

Signature: t1:2052630:1431015271:bc90ccf99941902cd4bd43a0980238c616e66abf

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

index 124019f6a36b51e1f0a2a8b59014589402e15a5e..566bad05afed814ec77e4698711d250ff518f643 100644 (file)
@@ -338,6 +338,13 @@ inline uint32_t hsieh_hash32_str(const std::string& str) {
 template<class Key>
 struct hasher;
 
+struct Hash {
+  template <class T>
+  size_t operator()(const T& v) const {
+    return hasher<T>()(v);
+  }
+};
+
 template<> struct hasher<int32_t> {
   size_t operator()(int32_t key) const {
     return hash::jenkins_rev_mix32(uint32_t(key));
index 7401af6bbe1b4c754a4eb9398d3e38c307be2c95..2942ea98af1177c2206e25f5966fe5c6d84aed14 100644 (file)
@@ -22,6 +22,8 @@
 
 #include <folly/Portability.h>
 #include <folly/FBString.h>
+#include <folly/SpookyHashV2.h>
+
 #include <algorithm>
 #include <boost/operators.hpp>
 #include <climits>
@@ -1121,6 +1123,16 @@ inline size_t qfind_first_of(const Range<const unsigned char*>& haystack,
   return detail::qfind_first_byte_of(StringPiece(haystack),
                                      StringPiece(needles));
 }
+
+template<class Key>
+struct hasher;
+
+template <class T> struct hasher<folly::Range<T*>> {
+  size_t operator()(folly::Range<T*> r) const {
+    return hash::SpookyHashV2::Hash64(r.begin(), r.size() * sizeof(T), 0);
+  }
+};
+
 }  // !namespace folly
 
 #pragma GCC diagnostic pop
index 948135e6beaba68cf6c9e8345a4508eea6e6d868..435352b7ea92e6cc04197fd04983f2696cfd0dc4 100644 (file)
@@ -262,3 +262,31 @@ TEST(Hash, std_tuple_different_hash) {
   EXPECT_NE(std::hash<tuple3>()(t1),
             std::hash<tuple3>()(t3));
 }
+
+TEST(Range, Hash) {
+  using namespace folly;
+
+  StringPiece a1 = "10050517", b1 = "51107032",
+              a2 = "10050518", b2 = "51107033",
+              a3 = "10050519", b3 = "51107034",
+              a4 = "10050525", b4 = "51107040";
+  Range<const wchar_t*> w1 = range(L"10050517"), w2 = range(L"51107032"),
+                        w3 = range(L"10050518"), w4 = range(L"51107033");
+  StringPieceHash h1;
+  Hash h2;
+  EXPECT_EQ(h1(a1), h1(b1));
+  EXPECT_EQ(h1(a2), h1(b2));
+  EXPECT_EQ(h1(a3), h1(b3));
+  EXPECT_EQ(h1(a4), h1(b4));
+  EXPECT_NE(h2(a1), h2(b1));
+  EXPECT_NE(h2(a1), h2(b1));
+  EXPECT_NE(h2(a2), h2(b2));
+  EXPECT_NE(h2(a3), h2(b3));
+  EXPECT_NE(h2(ByteRange(a1)), h2(ByteRange(b1)));
+  EXPECT_NE(h2(ByteRange(a2)), h2(ByteRange(b2)));
+  EXPECT_NE(h2(ByteRange(a3)), h2(ByteRange(b3)));
+  EXPECT_NE(h2(ByteRange(a4)), h2(ByteRange(b4)));
+  EXPECT_NE(h2(w1), h2(w2));
+  EXPECT_NE(h2(w1), h2(w3));
+  EXPECT_NE(h2(w2), h2(w4));
+}