From c4127a721f498ad417b4db603e643822305cb11e Mon Sep 17 00:00:00 2001 From: Abseil Team Date: Mon, 3 Apr 2023 14:02:48 -0700 Subject: Add heterogeneous lookup support for wstring/u16string/u32string. PiperOrigin-RevId: 521556211 Change-Id: I1e1812eb11bfc5772abbf38ce348580c3afa5612 --- absl/container/internal/hash_function_defaults.h | 46 ++++++ .../internal/hash_function_defaults_test.cc | 166 +++++++++++++++++++++ 2 files changed, 212 insertions(+) (limited to 'absl/container') diff --git a/absl/container/internal/hash_function_defaults.h b/absl/container/internal/hash_function_defaults.h index 250e662c..a3613b4b 100644 --- a/absl/container/internal/hash_function_defaults.h +++ b/absl/container/internal/hash_function_defaults.h @@ -56,6 +56,10 @@ #include "absl/strings/cord.h" #include "absl/strings/string_view.h" +#ifdef ABSL_HAVE_STD_STRING_VIEW +#include +#endif + namespace absl { ABSL_NAMESPACE_BEGIN namespace container_internal { @@ -107,6 +111,48 @@ struct HashEq : StringHashEq {}; template <> struct HashEq : StringHashEq {}; +#ifdef ABSL_HAVE_STD_STRING_VIEW + +template +struct BasicStringHash { + using is_transparent = void; + + size_t operator()(std::basic_string_view v) const { + return absl::Hash>{}(v); + } +}; + +template +struct BasicStringEq { + using is_transparent = void; + bool operator()(std::basic_string_view lhs, + std::basic_string_view rhs) const { + return lhs == rhs; + } +}; + +// Supports heterogeneous lookup for w/u16/u32 string + string_view + char*. +template +struct BasicStringHashEq { + using Hash = BasicStringHash; + using Eq = BasicStringEq; +}; + +template <> +struct HashEq : BasicStringHashEq {}; +template <> +struct HashEq : BasicStringHashEq {}; +template <> +struct HashEq : BasicStringHashEq {}; +template <> +struct HashEq : BasicStringHashEq {}; +template <> +struct HashEq : BasicStringHashEq {}; +template <> +struct HashEq : BasicStringHashEq {}; + +#endif // ABSL_HAVE_STD_STRING_VIEW + // Supports heterogeneous lookup for pointers and smart pointers. template struct HashEq { diff --git a/absl/container/internal/hash_function_defaults_test.cc b/absl/container/internal/hash_function_defaults_test.cc index 9f0a4c72..c31af3be 100644 --- a/absl/container/internal/hash_function_defaults_test.cc +++ b/absl/container/internal/hash_function_defaults_test.cc @@ -24,6 +24,10 @@ #include "absl/strings/cord_test_helpers.h" #include "absl/strings/string_view.h" +#ifdef ABSL_HAVE_STD_STRING_VIEW +#include +#endif + namespace absl { ABSL_NAMESPACE_BEGIN namespace container_internal { @@ -109,6 +113,168 @@ TYPED_TEST(HashString, Works) { EXPECT_NE(h, hash(std::string("b"))); } +TEST(BasicStringViewTest, WStringEqWorks) { +#ifndef ABSL_HAVE_STD_STRING_VIEW + GTEST_SKIP(); +#else + hash_default_eq eq; + EXPECT_TRUE(eq(L"a", L"a")); + EXPECT_TRUE(eq(L"a", std::wstring_view(L"a"))); + EXPECT_TRUE(eq(L"a", std::wstring(L"a"))); + EXPECT_FALSE(eq(L"a", L"b")); + EXPECT_FALSE(eq(L"a", std::wstring_view(L"b"))); + EXPECT_FALSE(eq(L"a", std::wstring(L"b"))); +#endif +} + +TEST(BasicStringViewTest, WStringViewEqWorks) { +#ifndef ABSL_HAVE_STD_STRING_VIEW + GTEST_SKIP(); +#else + hash_default_eq eq; + EXPECT_TRUE(eq(L"a", L"a")); + EXPECT_TRUE(eq(L"a", std::wstring_view(L"a"))); + EXPECT_TRUE(eq(L"a", std::wstring(L"a"))); + EXPECT_FALSE(eq(L"a", L"b")); + EXPECT_FALSE(eq(L"a", std::wstring_view(L"b"))); + EXPECT_FALSE(eq(L"a", std::wstring(L"b"))); +#endif +} + +TEST(BasicStringViewTest, U16StringEqWorks) { +#ifndef ABSL_HAVE_STD_STRING_VIEW + GTEST_SKIP(); +#else + hash_default_eq eq; + EXPECT_TRUE(eq(u"a", u"a")); + EXPECT_TRUE(eq(u"a", std::u16string_view(u"a"))); + EXPECT_TRUE(eq(u"a", std::u16string(u"a"))); + EXPECT_FALSE(eq(u"a", u"b")); + EXPECT_FALSE(eq(u"a", std::u16string_view(u"b"))); + EXPECT_FALSE(eq(u"a", std::u16string(u"b"))); +#endif +} + +TEST(BasicStringViewTest, U16StringViewEqWorks) { +#ifndef ABSL_HAVE_STD_STRING_VIEW + GTEST_SKIP(); +#else + hash_default_eq eq; + EXPECT_TRUE(eq(u"a", u"a")); + EXPECT_TRUE(eq(u"a", std::u16string_view(u"a"))); + EXPECT_TRUE(eq(u"a", std::u16string(u"a"))); + EXPECT_FALSE(eq(u"a", u"b")); + EXPECT_FALSE(eq(u"a", std::u16string_view(u"b"))); + EXPECT_FALSE(eq(u"a", std::u16string(u"b"))); +#endif +} + +TEST(BasicStringViewTest, U32StringEqWorks) { +#ifndef ABSL_HAVE_STD_STRING_VIEW + GTEST_SKIP(); +#else + hash_default_eq eq; + EXPECT_TRUE(eq(U"a", U"a")); + EXPECT_TRUE(eq(U"a", std::u32string_view(U"a"))); + EXPECT_TRUE(eq(U"a", std::u32string(U"a"))); + EXPECT_FALSE(eq(U"a", U"b")); + EXPECT_FALSE(eq(U"a", std::u32string_view(U"b"))); + EXPECT_FALSE(eq(U"a", std::u32string(U"b"))); +#endif +} + +TEST(BasicStringViewTest, U32StringViewEqWorks) { +#ifndef ABSL_HAVE_STD_STRING_VIEW + GTEST_SKIP(); +#else + hash_default_eq eq; + EXPECT_TRUE(eq(U"a", U"a")); + EXPECT_TRUE(eq(U"a", std::u32string_view(U"a"))); + EXPECT_TRUE(eq(U"a", std::u32string(U"a"))); + EXPECT_FALSE(eq(U"a", U"b")); + EXPECT_FALSE(eq(U"a", std::u32string_view(U"b"))); + EXPECT_FALSE(eq(U"a", std::u32string(U"b"))); +#endif +} + +TEST(BasicStringViewTest, WStringHashWorks) { +#ifndef ABSL_HAVE_STD_STRING_VIEW + GTEST_SKIP(); +#else + hash_default_hash hash; + auto h = hash(L"a"); + EXPECT_EQ(h, hash(std::wstring_view(L"a"))); + EXPECT_EQ(h, hash(std::wstring(L"a"))); + EXPECT_NE(h, hash(std::wstring_view(L"b"))); + EXPECT_NE(h, hash(std::wstring(L"b"))); +#endif +} + +TEST(BasicStringViewTest, WStringViewHashWorks) { +#ifndef ABSL_HAVE_STD_STRING_VIEW + GTEST_SKIP(); +#else + hash_default_hash hash; + auto h = hash(L"a"); + EXPECT_EQ(h, hash(std::wstring_view(L"a"))); + EXPECT_EQ(h, hash(std::wstring(L"a"))); + EXPECT_NE(h, hash(std::wstring_view(L"b"))); + EXPECT_NE(h, hash(std::wstring(L"b"))); +#endif +} + +TEST(BasicStringViewTest, U16StringHashWorks) { +#ifndef ABSL_HAVE_STD_STRING_VIEW + GTEST_SKIP(); +#else + hash_default_hash hash; + auto h = hash(u"a"); + EXPECT_EQ(h, hash(std::u16string_view(u"a"))); + EXPECT_EQ(h, hash(std::u16string(u"a"))); + EXPECT_NE(h, hash(std::u16string_view(u"b"))); + EXPECT_NE(h, hash(std::u16string(u"b"))); +#endif +} + +TEST(BasicStringViewTest, U16StringViewHashWorks) { +#ifndef ABSL_HAVE_STD_STRING_VIEW + GTEST_SKIP(); +#else + hash_default_hash hash; + auto h = hash(u"a"); + EXPECT_EQ(h, hash(std::u16string_view(u"a"))); + EXPECT_EQ(h, hash(std::u16string(u"a"))); + EXPECT_NE(h, hash(std::u16string_view(u"b"))); + EXPECT_NE(h, hash(std::u16string(u"b"))); +#endif +} + +TEST(BasicStringViewTest, U32StringHashWorks) { +#ifndef ABSL_HAVE_STD_STRING_VIEW + GTEST_SKIP(); +#else + hash_default_hash hash; + auto h = hash(U"a"); + EXPECT_EQ(h, hash(std::u32string_view(U"a"))); + EXPECT_EQ(h, hash(std::u32string(U"a"))); + EXPECT_NE(h, hash(std::u32string_view(U"b"))); + EXPECT_NE(h, hash(std::u32string(U"b"))); +#endif +} + +TEST(BasicStringViewTest, U32StringViewHashWorks) { +#ifndef ABSL_HAVE_STD_STRING_VIEW + GTEST_SKIP(); +#else + hash_default_hash hash; + auto h = hash(U"a"); + EXPECT_EQ(h, hash(std::u32string_view(U"a"))); + EXPECT_EQ(h, hash(std::u32string(U"a"))); + EXPECT_NE(h, hash(std::u32string_view(U"b"))); + EXPECT_NE(h, hash(std::u32string(U"b"))); +#endif +} + struct NoDeleter { template void operator()(const T* ptr) const {} -- cgit v1.2.3