1
// Protocol Buffers - Google's data interchange format
2
// Copyright 2008 Google Inc. All rights reserved.
3
// http://code.google.com/p/protobuf/
5
// Redistribution and use in source and binary forms, with or without
6
// modification, are permitted provided that the following conditions are
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
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.
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.
31
// Author: kenton@google.com (Kenton Varda)
33
// Deals with the fact that hash_map is not defined everywhere.
35
#ifndef GOOGLE_PROTOBUF_STUBS_HASH_H__
36
#define GOOGLE_PROTOBUF_STUBS_HASH_H__
39
#include <google/protobuf/stubs/common.h>
42
#if defined(HAVE_HASH_MAP) && defined(HAVE_HASH_SET)
56
// This system doesn't have hash_map or hash_set. Emulate them using map and
59
// Make hash<T> be the same as less<T>. Note that everywhere where custom
60
// hash functions are defined in the protobuf code, they are also defined such
61
// that they can be used as "less" functions, which is required by MSVC anyway.
62
template <typename Key>
64
// Dummy, just to make derivative hash functions compile.
65
int operator()(const Key& key) {
66
GOOGLE_LOG(FATAL) << "Should never be called.";
70
inline bool operator()(const Key& a, const Key& b) const {
75
// Make sure char* is compared by value.
77
struct hash<const char*> {
78
// Dummy, just to make derivative hash functions compile.
79
int operator()(const char* key) {
80
GOOGLE_LOG(FATAL) << "Should never be called.";
84
inline bool operator()(const char* a, const char* b) const {
85
return strcmp(a, b) < 0;
89
template <typename Key, typename Data,
90
typename HashFcn = hash<Key>,
91
typename EqualKey = int >
92
class hash_map : public std::map<Key, Data, HashFcn> {
97
template <typename Key,
98
typename HashFcn = hash<Key>,
99
typename EqualKey = int >
100
class hash_set : public std::set<Key, HashFcn> {
105
#elif defined(_MSC_VER) && !defined(_STLPORT_VERSION)
107
template <typename Key>
108
struct hash : public HASH_NAMESPACE::hash_compare<Key> {
111
// MSVC's hash_compare<const char*> hashes based on the string contents but
112
// compares based on the string pointer. WTF?
115
inline bool operator()(const char* a, const char* b) const {
116
return strcmp(a, b) < 0;
121
struct hash<const char*>
122
: public HASH_NAMESPACE::hash_compare<const char*, CstringLess> {
125
template <typename Key, typename Data,
126
typename HashFcn = hash<Key>,
127
typename EqualKey = int >
128
class hash_map : public HASH_NAMESPACE::hash_map<
129
Key, Data, HashFcn> {
134
template <typename Key,
135
typename HashFcn = hash<Key>,
136
typename EqualKey = int >
137
class hash_set : public HASH_NAMESPACE::hash_set<
145
template <typename Key>
146
struct hash : public HASH_NAMESPACE::hash<Key> {
149
template <typename Key>
150
struct hash<const Key*> {
151
inline size_t operator()(const Key* key) const {
152
return reinterpret_cast<size_t>(key);
156
// Unlike the old SGI version, the TR1 "hash" does not special-case char*. So,
157
// we go ahead and provide our own implementation.
159
struct hash<const char*> {
160
inline size_t operator()(const char* str) const {
162
for (; *str != '\0'; str++) {
163
result = 5 * result + *str;
169
template <typename Key, typename Data,
170
typename HashFcn = hash<Key>,
171
typename EqualKey = std::equal_to<Key> >
172
class hash_map : public HASH_NAMESPACE::HASH_MAP_CLASS<
173
Key, Data, HashFcn, EqualKey> {
178
template <typename Key,
179
typename HashFcn = hash<Key>,
180
typename EqualKey = std::equal_to<Key> >
181
class hash_set : public HASH_NAMESPACE::HASH_SET_CLASS<
182
Key, HashFcn, EqualKey> {
190
struct hash<string> {
191
inline size_t operator()(const string& key) const {
192
return hash<const char*>()(key.c_str());
195
static const size_t bucket_size = 4;
196
static const size_t min_buckets = 8;
197
inline size_t operator()(const string& a, const string& b) const {
202
template <typename First, typename Second>
203
struct hash<pair<First, Second> > {
204
inline size_t operator()(const pair<First, Second>& key) const {
205
size_t first_hash = hash<First>()(key.first);
206
size_t second_hash = hash<Second>()(key.second);
208
// FIXME(kenton): What is the best way to compute this hash? I have
209
// no idea! This seems a bit better than an XOR.
210
return first_hash * ((1 << 16) - 1) + second_hash;
213
static const size_t bucket_size = 4;
214
static const size_t min_buckets = 8;
215
inline size_t operator()(const pair<First, Second>& a,
216
const pair<First, Second>& b) const {
221
// Used by GCC/SGI STL only. (Why isn't this provided by the standard
224
inline bool operator()(const char* a, const char* b) const {
225
return strcmp(a, b) == 0;
229
} // namespace protobuf
230
} // namespace google
232
#endif // GOOGLE_PROTOBUF_STUBS_HASH_H__