hash.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. // Author: kenton@google.com (Kenton Varda)
  31. #ifndef GOOGLE_PROTOBUF_STUBS_HASH_H__
  32. #define GOOGLE_PROTOBUF_STUBS_HASH_H__
  33. #include <cstring>
  34. #include <string>
  35. #include <unordered_map>
  36. #include <unordered_set>
  37. # define GOOGLE_PROTOBUF_HASH_NAMESPACE_DECLARATION_START \
  38. namespace google { \
  39. namespace protobuf {
  40. # define GOOGLE_PROTOBUF_HASH_NAMESPACE_DECLARATION_END }}
  41. namespace google {
  42. namespace protobuf {
  43. template <typename Key>
  44. struct hash : public std::hash<Key> {};
  45. template <typename Key>
  46. struct hash<const Key*> {
  47. inline size_t operator()(const Key* key) const {
  48. return reinterpret_cast<size_t>(key);
  49. }
  50. };
  51. // Unlike the old SGI version, the TR1 "hash" does not special-case char*. So,
  52. // we go ahead and provide our own implementation.
  53. template <>
  54. struct hash<const char*> {
  55. inline size_t operator()(const char* str) const {
  56. size_t result = 0;
  57. for (; *str != '\0'; str++) {
  58. result = 5 * result + static_cast<size_t>(*str);
  59. }
  60. return result;
  61. }
  62. };
  63. template<>
  64. struct hash<bool> {
  65. size_t operator()(bool x) const {
  66. return static_cast<size_t>(x);
  67. }
  68. };
  69. template <>
  70. struct hash<std::string> {
  71. inline size_t operator()(const std::string& key) const {
  72. return hash<const char*>()(key.c_str());
  73. }
  74. static const size_t bucket_size = 4;
  75. static const size_t min_buckets = 8;
  76. inline bool operator()(const std::string& a, const std::string& b) const {
  77. return a < b;
  78. }
  79. };
  80. template <typename First, typename Second>
  81. struct hash<std::pair<First, Second> > {
  82. inline size_t operator()(const std::pair<First, Second>& key) const {
  83. size_t first_hash = hash<First>()(key.first);
  84. size_t second_hash = hash<Second>()(key.second);
  85. // FIXME(kenton): What is the best way to compute this hash? I have
  86. // no idea! This seems a bit better than an XOR.
  87. return first_hash * ((1 << 16) - 1) + second_hash;
  88. }
  89. static const size_t bucket_size = 4;
  90. static const size_t min_buckets = 8;
  91. inline bool operator()(const std::pair<First, Second>& a,
  92. const std::pair<First, Second>& b) const {
  93. return a < b;
  94. }
  95. };
  96. } // namespace protobuf
  97. } // namespace google
  98. #endif // GOOGLE_PROTOBUF_STUBS_HASH_H__