Commit c1be26e5 authored by Giuseppe Ottaviano's avatar Giuseppe Ottaviano Committed by facebook-github-bot-1

Add support for std::string in folly::Hash

Reviewed By: @​liviu

Differential Revision: D2436705
parent 2e6f9c9b
...@@ -375,6 +375,12 @@ template<> struct hasher<uint64_t> { ...@@ -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> template <class T>
struct hasher<T, typename std::enable_if<std::is_enum<T>::value, void>::type> { struct hasher<T, typename std::enable_if<std::is_enum<T>::value, void>::type> {
size_t operator()(T key) const { size_t operator()(T key) const {
......
...@@ -303,7 +303,7 @@ TEST(Hash, std_tuple_different_hash) { ...@@ -303,7 +303,7 @@ TEST(Hash, std_tuple_different_hash) {
std::hash<tuple3>()(t3)); std::hash<tuple3>()(t3));
} }
TEST(Range, Hash) { TEST(Hash, Strings) {
using namespace folly; using namespace folly;
StringPiece a1 = "10050517", b1 = "51107032", StringPiece a1 = "10050517", b1 = "51107032",
...@@ -329,4 +329,10 @@ TEST(Range, Hash) { ...@@ -329,4 +329,10 @@ TEST(Range, Hash) {
EXPECT_NE(h2(w1), h2(w2)); EXPECT_NE(h2(w1), h2(w2));
EXPECT_NE(h2(w1), h2(w3)); EXPECT_NE(h2(w1), h2(w3));
EXPECT_NE(h2(w2), h2(w4)); 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()));
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment