protobuf/src/google/protobuf/stubs/hash.h
Go to the documentation of this file.
1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc. All rights reserved.
3 // https://developers.google.com/protocol-buffers/
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 // * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer.
11 // * Redistributions in binary form must reproduce the above
12 // copyright notice, this list of conditions and the following disclaimer
13 // in the documentation and/or other materials provided with the
14 // distribution.
15 // * Neither the name of Google Inc. nor the names of its
16 // contributors may be used to endorse or promote products derived from
17 // this software without specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 
31 // Author: kenton@google.com (Kenton Varda)
32 
33 #ifndef GOOGLE_PROTOBUF_STUBS_HASH_H__
34 #define GOOGLE_PROTOBUF_STUBS_HASH_H__
35 
36 #include <cstring>
37 #include <string>
38 #include <unordered_map>
39 #include <unordered_set>
40 
41 # define GOOGLE_PROTOBUF_HASH_NAMESPACE_DECLARATION_START \
42  namespace google { \
43  namespace protobuf {
44 # define GOOGLE_PROTOBUF_HASH_NAMESPACE_DECLARATION_END }}
45 
46 namespace google {
47 namespace protobuf {
48 
49 template <typename Key>
50 struct hash : public std::hash<Key> {};
51 
52 template <typename Key>
53 struct hash<const Key*> {
54  inline size_t operator()(const Key* key) const {
55  return reinterpret_cast<size_t>(key);
56  }
57 };
58 
59 // Unlike the old SGI version, the TR1 "hash" does not special-case char*. So,
60 // we go ahead and provide our own implementation.
61 template <>
62 struct hash<const char*> {
63  inline size_t operator()(const char* str) const {
64  size_t result = 0;
65  for (; *str != '\0'; str++) {
66  result = 5 * result + static_cast<size_t>(*str);
67  }
68  return result;
69  }
70 };
71 
72 template<>
73 struct hash<bool> {
74  size_t operator()(bool x) const {
75  return static_cast<size_t>(x);
76  }
77 };
78 
79 template <>
80 struct hash<std::string> {
81  inline size_t operator()(const std::string& key) const {
82  return hash<const char*>()(key.c_str());
83  }
84 
85  static const size_t bucket_size = 4;
86  static const size_t min_buckets = 8;
87  inline bool operator()(const std::string& a, const std::string& b) const {
88  return a < b;
89  }
90 };
91 
92 template <typename First, typename Second>
93 struct hash<std::pair<First, Second> > {
94  inline size_t operator()(const std::pair<First, Second>& key) const {
95  size_t first_hash = hash<First>()(key.first);
96  size_t second_hash = hash<Second>()(key.second);
97 
98  // FIXME(kenton): What is the best way to compute this hash? I have
99  // no idea! This seems a bit better than an XOR.
100  return first_hash * ((1 << 16) - 1) + second_hash;
101  }
102 
103  static const size_t bucket_size = 4;
104  static const size_t min_buckets = 8;
105  inline bool operator()(const std::pair<First, Second>& a,
106  const std::pair<First, Second>& b) const {
107  return a < b;
108  }
109 };
110 
111 } // namespace protobuf
112 } // namespace google
113 
114 #endif // GOOGLE_PROTOBUF_STUBS_HASH_H__
xds_interop_client.str
str
Definition: xds_interop_client.py:487
google::protobuf::hash< const Key * >::operator()
size_t operator()(const Key *key) const
Definition: protobuf/src/google/protobuf/stubs/hash.h:54
_gevent_test_main.result
result
Definition: _gevent_test_main.py:96
google::protobuf::hash
Definition: bloaty/third_party/protobuf/src/google/protobuf/stubs/hash.h:51
const
#define const
Definition: bloaty/third_party/zlib/zconf.h:230
bool
bool
Definition: setup_once.h:312
google::protobuf::hash< std::string >::operator()
size_t operator()(const std::string &key) const
Definition: protobuf/src/google/protobuf/stubs/hash.h:81
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
google::protobuf::hash< bool >::operator()
size_t operator()(bool x) const
Definition: protobuf/src/google/protobuf/stubs/hash.h:74
google::protobuf
Definition: bloaty/third_party/protobuf/benchmarks/util/data_proto2_to_proto3_util.h:12
a
int a
Definition: abseil-cpp/absl/container/internal/hash_policy_traits_test.cc:88
google::protobuf::hash< const char * >::operator()
size_t operator()(const char *str) const
Definition: protobuf/src/google/protobuf/stubs/hash.h:63
google::protobuf::hash< std::pair< First, Second > >::operator()
size_t operator()(const std::pair< First, Second > &key) const
Definition: protobuf/src/google/protobuf/stubs/hash.h:94
hash
uint64_t hash
Definition: ring_hash.cc:284
google::protobuf::hash< std::string >::operator()
bool operator()(const std::string &a, const std::string &b) const
Definition: protobuf/src/google/protobuf/stubs/hash.h:87
google::protobuf::hash< std::pair< First, Second > >::operator()
bool operator()(const std::pair< First, Second > &a, const std::pair< First, Second > &b) const
Definition: protobuf/src/google/protobuf/stubs/hash.h:105
x
int x
Definition: bloaty/third_party/googletest/googlemock/test/gmock-matchers_test.cc:3610
b
uint64_t b
Definition: abseil-cpp/absl/container/internal/layout_test.cc:53
key
const char * key
Definition: hpack_parser_table.cc:164
google::protobuf::hash< const char * >
Definition: bloaty/third_party/protobuf/src/google/protobuf/stubs/hash.h:63
testing::Key
internal::KeyMatcher< M > Key(M inner_matcher)
Definition: cares/cares/test/gmock-1.8.0/gmock/gmock.h:9141
std
Definition: grpcpp/impl/codegen/async_unary_call.h:407
pair
std::pair< std::string, std::string > pair
Definition: abseil-cpp/absl/container/internal/raw_hash_set_benchmark.cc:78
google
Definition: bloaty/third_party/protobuf/benchmarks/util/data_proto2_to_proto3_util.h:11


grpc
Author(s):
autogenerated on Fri May 16 2025 02:59:00