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> {
95
template <typename Key,
96
typename HashFcn = hash<Key>,
97
typename EqualKey = int >
98
class hash_set : public std::set<Key, HashFcn> {
101
#elif defined(_MSC_VER) && !defined(_STLPORT_VERSION)
103
template <typename Key>
104
struct hash : public HASH_NAMESPACE::hash_compare<Key> {
107
// MSVC's hash_compare<const char*> hashes based on the string contents but
108
// compares based on the string pointer. WTF?
111
inline bool operator()(const char* a, const char* b) const {
112
return strcmp(a, b) < 0;
117
struct hash<const char*>
118
: public HASH_NAMESPACE::hash_compare<const char*, CstringLess> {
121
template <typename Key, typename Data,
122
typename HashFcn = hash<Key>,
123
typename EqualKey = int >
124
class hash_map : public HASH_NAMESPACE::hash_map<
125
Key, Data, HashFcn> {
128
template <typename Key,
129
typename HashFcn = hash<Key>,
130
typename EqualKey = int >
131
class hash_set : public HASH_NAMESPACE::hash_set<
137
template <typename Key>
138
struct hash : public HASH_NAMESPACE::hash<Key> {
141
template <typename Key>
142
struct hash<const Key*> {
143
inline size_t operator()(const Key* key) const {
144
return reinterpret_cast<size_t>(key);
148
// Unlike the old SGI version, the TR1 "hash" does not special-case char*. So,
149
// we go ahead and provide our own implementation.
151
struct hash<const char*> {
152
inline size_t operator()(const char* str) const {
154
for (; *str != '\0'; str++) {
155
result = 5 * result + *str;
161
template <typename Key, typename Data,
162
typename HashFcn = hash<Key>,
163
typename EqualKey = std::equal_to<Key> >
164
class hash_map : public HASH_NAMESPACE::HASH_MAP_CLASS<
165
Key, Data, HashFcn, EqualKey> {
168
template <typename Key,
169
typename HashFcn = hash<Key>,
170
typename EqualKey = std::equal_to<Key> >
171
class hash_set : public HASH_NAMESPACE::HASH_SET_CLASS<
172
Key, HashFcn, EqualKey> {
178
struct hash<string> {
179
inline size_t operator()(const string& key) const {
180
return hash<const char*>()(key.c_str());
183
static const size_t bucket_size = 4;
184
static const size_t min_buckets = 8;
185
inline size_t operator()(const string& a, const string& b) const {
190
template <typename First, typename Second>
191
struct hash<pair<First, Second> > {
192
inline size_t operator()(const pair<First, Second>& key) const {
193
size_t first_hash = hash<First>()(key.first);
194
size_t second_hash = hash<Second>()(key.second);
196
// FIXME(kenton): What is the best way to compute this hash? I have
197
// no idea! This seems a bit better than an XOR.
198
return first_hash * ((1 << 16) - 1) + second_hash;
201
static const size_t bucket_size = 4;
202
static const size_t min_buckets = 8;
203
inline size_t operator()(const pair<First, Second>& a,
204
const pair<First, Second>& b) const {
209
// Used by GCC/SGI STL only. (Why isn't this provided by the standard
212
inline bool operator()(const char* a, const char* b) const {
213
return strcmp(a, b) == 0;
217
} // namespace protobuf
218
} // namespace google
220
#endif // GOOGLE_PROTOBUF_STUBS_HASH_H__