You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

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