Commit e47bc9b4 authored by Yedidya Feldblum's avatar Yedidya Feldblum Committed by Facebook GitHub Bot

add hasher<string_view>

Summary: Add a specialization of `folly::hasher<std::string_view>` parallel to the specialilzation of `folly::hasher<std::string>`.

Reviewed By: luciang

Differential Revision: D28921343

fbshipit-source-id: 77213d9ff66cec57b6d36b214c41e479e1d9455d
parent b3427e71
......@@ -33,6 +33,10 @@
#include <folly/hash/SpookyHashV2.h>
#include <folly/lang/Bits.h>
#if FOLLY_HAS_STRING_VIEW
#include <string_view>
#endif
namespace folly {
namespace hash {
......@@ -561,6 +565,20 @@ struct hasher<std::string> {
template <typename K>
struct IsAvalanchingHasher<hasher<std::string>, K> : std::true_type {};
#if FOLLY_HAS_STRING_VIEW
template <>
struct hasher<std::string_view> {
using folly_is_avalanching = std::true_type;
size_t operator()(const std::string_view& key) const {
return static_cast<size_t>(
hash::SpookyHashV2::Hash64(key.data(), key.size(), 0));
}
};
template <typename K>
struct IsAvalanchingHasher<hasher<std::string_view>, K> : std::true_type {};
#endif
template <typename T>
struct hasher<T, std::enable_if_t<std::is_enum<T>::value>> {
size_t operator()(T key) const noexcept { return Hash()(to_underlying(key)); }
......
......@@ -628,6 +628,12 @@ TEST(Hash, Strings) {
EXPECT_EQ(h2(a2), h2(a2.str()));
EXPECT_EQ(h2(a3), h2(a3.str()));
EXPECT_EQ(h2(a4), h2(a4.str()));
// Check compatibility with std::string_view.
EXPECT_EQ(h2(a1), h2(std::string_view{a1}));
EXPECT_EQ(h2(a2), h2(std::string_view{a2}));
EXPECT_EQ(h2(a3), h2(std::string_view{a3}));
EXPECT_EQ(h2(a4), h2(std::string_view{a4}));
}
namespace {
......
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