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 // Used by GCC/SGI STL only. (Why isn't this provided by the standard 112 // library? :( ) 113 struct streq { 114 inline bool operator()(const char* a, const char* b) const { 115 return strcmp(a, b) == 0; 116 } 117 }; 118 119 } // namespace protobuf 120 } // namespace google 121 122 #endif // GOOGLE_PROTOBUF_STUBS_HASH_H__ 123