Skip to content

Commit

Permalink
Automated rollback of commit 888810a.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 504428590
  • Loading branch information
mkruskal-google authored and copybara-github committed Jan 25, 2023
1 parent 888810a commit 5bbc6fc
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 15 deletions.
1 change: 0 additions & 1 deletion src/google/protobuf/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,6 @@ cc_library(
"//src/google/protobuf/stubs:lite",
"@com_google_absl//absl/container:btree",
"@com_google_absl//absl/container:flat_hash_set",
"@com_google_absl//absl/hash",
"@com_google_absl//absl/meta:type_traits",
"@com_google_absl//absl/numeric:bits",
"@com_google_absl//absl/strings:internal",
Expand Down
31 changes: 18 additions & 13 deletions src/google/protobuf/map.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,17 @@
#include <type_traits>
#include <utility>

#if defined(__cpp_lib_string_view)
#include <string_view>
#endif // defined(__cpp_lib_string_view)

#if !defined(GOOGLE_PROTOBUF_NO_RDTSC) && defined(__APPLE__)
#include <mach/mach_time.h>
#endif

#include "google/protobuf/stubs/common.h"
#include "absl/container/btree_map.h"
#include "absl/hash/hash.h"
#include "absl/meta/type_traits.h"
#include "absl/strings/string_view.h"
#include "google/protobuf/arena.h"
#include "google/protobuf/generated_enum_util.h"
#include "google/protobuf/map_type_handler.h"
Expand Down Expand Up @@ -227,31 +229,33 @@ struct TransparentSupport {
using key_arg = key_type;
};

// We add transparent support for std::string keys. We use
// std::hash<absl::string_view> as it supports the input types we care about.
// The lookup functions accept arbitrary `K`. This will include any key type
// that is convertible to absl::string_view.
#if defined(__cpp_lib_string_view)
// If std::string_view is available, we add transparent support for std::string
// keys. We use std::hash<std::string_view> as it supports the input types we
// care about. The lookup functions accept arbitrary `K`. This will include any
// key type that is convertible to std::string_view.
template <>
struct TransparentSupport<std::string> {
static absl::string_view ImplicitConvert(absl::string_view str) {
return str;
}
// If the element is not convertible to absl::string_view, try to convert to
static std::string_view ImplicitConvert(std::string_view str) { return str; }
// If the element is not convertible to std::string_view, try to convert to
// std::string first.
// The template makes this overload lose resolution when both have the same
// rank otherwise.
template <typename = void>
static absl::string_view ImplicitConvert(const std::string& str) {
static std::string_view ImplicitConvert(const std::string& str) {
return str;
}

struct hash : public absl::Hash<absl::string_view> {
struct hash : private std::hash<std::string_view> {
using is_transparent = void;

template <typename T>
size_t operator()(const T& str) const {
return absl::Hash<absl::string_view>::operator()(ImplicitConvert(str));
return base()(ImplicitConvert(str));
}

private:
const std::hash<std::string_view>& base() const { return *this; }
};
struct less {
using is_transparent = void;
Expand All @@ -270,6 +274,7 @@ struct TransparentSupport<std::string> {
template <typename K>
using key_arg = K;
};
#endif // defined(__cpp_lib_string_view)

struct NodeBase {
// Align the node to allow KeyNode to predict the location of the key.
Expand Down
4 changes: 3 additions & 1 deletion src/google/protobuf/map_test.inc
Original file line number Diff line number Diff line change
Expand Up @@ -1404,7 +1404,9 @@ void TestTransparent(const Key& key, const Key& miss_key) {
TEST_F(MapImplTest, TransparentLookupForString) {
TestTransparent("ABC", "LKJ");
TestTransparent(std::string("ABC"), std::string("LKJ"));
TestTransparent(absl::string_view("ABC"), absl::string_view("LKJ"));
#if defined(__cpp_lib_string_view)
TestTransparent(std::string_view("ABC"), std::string_view("LKJ"));
#endif // defined(__cpp_lib_string_view)

// std::reference_wrapper
std::string abc = "ABC", lkj = "LKJ";
Expand Down

0 comments on commit 5bbc6fc

Please sign in to comment.