From 5cc2f9945c169d3946963c6ee485e32eea407fac Mon Sep 17 00:00:00 2001 From: Tom Jackson Date: Fri, 8 May 2015 00:43:26 -0700 Subject: [PATCH] Add SpookyHashV2-based hash to StringPiece 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 | 7 +++++++ folly/Range.h | 12 ++++++++++++ folly/test/HashTest.cpp | 28 ++++++++++++++++++++++++++++ 3 files changed, 47 insertions(+) diff --git a/folly/Hash.h b/folly/Hash.h index 124019f6..566bad05 100644 --- a/folly/Hash.h +++ b/folly/Hash.h @@ -338,6 +338,13 @@ inline uint32_t hsieh_hash32_str(const std::string& str) { template struct hasher; +struct Hash { + template + size_t operator()(const T& v) const { + return hasher()(v); + } +}; + template<> struct hasher { size_t operator()(int32_t key) const { return hash::jenkins_rev_mix32(uint32_t(key)); diff --git a/folly/Range.h b/folly/Range.h index 7401af6b..2942ea98 100644 --- a/folly/Range.h +++ b/folly/Range.h @@ -22,6 +22,8 @@ #include #include +#include + #include #include #include @@ -1121,6 +1123,16 @@ inline size_t qfind_first_of(const Range& haystack, return detail::qfind_first_byte_of(StringPiece(haystack), StringPiece(needles)); } + +template +struct hasher; + +template struct hasher> { + size_t operator()(folly::Range r) const { + return hash::SpookyHashV2::Hash64(r.begin(), r.size() * sizeof(T), 0); + } +}; + } // !namespace folly #pragma GCC diagnostic pop diff --git a/folly/test/HashTest.cpp b/folly/test/HashTest.cpp index 948135e6..435352b7 100644 --- a/folly/test/HashTest.cpp +++ b/folly/test/HashTest.cpp @@ -262,3 +262,31 @@ TEST(Hash, std_tuple_different_hash) { EXPECT_NE(std::hash()(t1), std::hash()(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 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)); +} -- 2.34.1