1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
4
* Copyright (C) 2009 Sun Microsystems
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; version 2 of the License.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, write to the Free Software
17
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20
// Protocol Buffers - Google's data interchange format
21
// Copyright 2008 Google Inc. All rights reserved.
22
// http://code.google.com/p/protobuf/
24
// Redistribution and use in source and binary forms, with or without
25
// modification, are permitted provided that the following conditions are
28
// * Redistributions of source code must retain the above copyright
29
// notice, this list of conditions and the following disclaimer.
30
// * Redistributions in binary form must reproduce the above
31
// copyright notice, this list of conditions and the following disclaimer
32
// in the documentation and/or other materials provided with the
34
// * Neither the name of Google Inc. nor the names of its
35
// contributors may be used to endorse or promote products derived from
36
// this software without specific prior written permission.
38
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
39
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
40
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
41
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
42
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
43
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
44
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
45
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
46
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
47
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
48
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
50
// Author: kenton@google.com (Kenton Varda)
52
// Deals with the fact that hash_map is not defined everywhere.
54
#ifndef DRIZZLED_HASH_H
55
#define DRIZZLED_HASH_H
60
#if defined(HAVE_HASH_MAP) && defined(HAVE_HASH_SET)
73
// This system doesn't have hash_map or hash_set. Emulate them using map and
76
// Make hash<T> be the same as less<T>. Note that everywhere where custom
77
// hash functions are defined in the protobuf code, they are also defined such
78
// that they can be used as "less" functions, which is required by MSVC anyway.
79
template <typename Key>
81
// Dummy, just to make derivative hash functions compile.
82
int operator()(const Key& key) {
87
inline bool operator()(const Key& a, const Key& b) const {
92
// Make sure char* is compared by value.
94
struct hash<const char*> {
95
// Dummy, just to make derivative hash functions compile.
96
int operator()(const char*) {
101
inline bool operator()(const char* a, const char* b) const {
102
return strcmp(a, b) < 0;
106
template <typename Key, typename Data,
107
typename HashFcn = hash<Key>,
108
typename EqualKey = int >
109
class hash_map : public std::map<Key, Data, HashFcn> {
111
void rehash(size_t buckets_in)
117
template <typename Key,
118
typename HashFcn = hash<Key>,
119
typename EqualKey = int >
120
class hash_set : public std::set<Key, HashFcn> {
121
void rehash(size_t buckets_in)
127
#elif defined(_MSC_VER) && !defined(_STLPORT_VERSION)
129
template <typename Key>
130
struct hash : public HASH_NAMESPACE::hash_compare<Key> {
133
// MSVC's hash_compare<const char*> hashes based on the string contents but
134
// compares based on the string pointer. WTF?
137
inline bool operator()(const char* a, const char* b) const {
138
return strcmp(a, b) < 0;
143
struct hash<const char*>
144
: public HASH_NAMESPACE::hash_compare<const char*, CstringLess> {
147
template <typename Key, typename Data,
148
typename HashFcn = hash<Key>,
149
typename EqualKey = int >
150
class hash_map : public HASH_NAMESPACE::HASH_MAP_CLASS<
151
Key, Data, HashFcn> {
152
#if !defined(HASH_MAP_HAS_REHASH)
154
void rehash(size_t buckets_in)
156
# if defined(HASH_MAP_HAS_RESIZE)
158
# endif /* HASH_MAP_HAS_RESIZE */
160
#endif /* HASH_MAP_HAS_REHASH */
163
template <typename Key,
164
typename HashFcn = hash<Key>,
165
typename EqualKey = int >
166
class hash_set : public HASH_NAMESPACE::HASH_SET_CLASS<
168
#if !defined(HASH_MAP_HAS_REHASH)
170
void rehash(size_t buckets_in)
172
# if defined(HASH_MAP_HAS_RESIZE)
174
# endif /* HASH_MAP_HAS_RESIZE */
176
#endif /* HASH_MAP_HAS_REHASH */
181
template <typename Key>
182
struct hash : public HASH_NAMESPACE::hash<Key> {
185
template <typename Key>
186
struct hash<const Key*> {
187
inline size_t operator()(const Key* key) const {
188
return reinterpret_cast<size_t>(key);
193
struct hash<const char*> : public HASH_NAMESPACE::hash<const char*> {
196
template <typename Key, typename Data,
197
typename HashFcn = hash<Key>,
198
typename EqualKey = std::equal_to<Key> >
199
class hash_map : public HASH_NAMESPACE::HASH_MAP_CLASS<
200
Key, Data, HashFcn, EqualKey> {
201
#if !defined(HASH_MAP_HAS_REHASH)
203
void rehash(size_t buckets_in)
205
# if defined(HASH_MAP_HAS_RESIZE)
206
HASH_NAMESPACE::HASH_MAP_CLASS<Key, Data, HashFcn, EqualKey>::resize(buckets_in);
207
# endif /* HASH_MAP_HAS_RESIZE */
209
#endif /* HASH_MAP_HAS_REHASH */
212
template <typename Key,
213
typename HashFcn = hash<Key>,
214
typename EqualKey = std::equal_to<Key> >
215
class hash_set : public HASH_NAMESPACE::HASH_SET_CLASS<
216
Key, HashFcn, EqualKey> {
217
#if !defined(HASH_MAP_HAS_REHASH)
219
void rehash(size_t buckets_in)
221
# if defined(HASH_MAP_HAS_RESIZE)
222
HASH_NAMESPACE::HASH_SET_CLASS<Key, HashFcn, EqualKey>::resize(buckets_in);
223
# endif /* HASH_MAP_HAS_RESIZE */
225
#endif /* HASH_MAP_HAS_REHASH */
230
#if defined(REDEFINE_HASH_STRING)
232
struct hash<std::string> {
233
inline size_t operator()(const std::string& key) const {
234
return hash<const char*>()(key.c_str());
237
static const size_t bucket_size = 4;
238
static const size_t min_buckets = 8;
239
inline size_t operator()(const std::string& a, const std::string& b) const {
245
template <typename First, typename Second>
246
struct hash<std::pair<First, Second> > {
247
inline size_t operator()(const std::pair<First, Second>& key) const {
248
size_t first_hash = hash<First>()(key.first);
249
size_t second_hash = hash<Second>()(key.second);
251
// FIXME(kenton): What is the best way to compute this hash? I have
252
// no idea! This seems a bit better than an XOR.
253
return first_hash * ((1 << 16) - 1) + second_hash;
256
static const size_t bucket_size = 4;
257
static const size_t min_buckets = 8;
258
inline size_t operator()(const std::pair<First, Second>& a,
259
const std::pair<First, Second>& b) const {
264
// Used by GCC/SGI STL only. (Why isn't this provided by the standard
267
inline bool operator()(const char* a, const char* b) const {
268
return strcmp(a, b) == 0;
272
} /* namespace drizzled */
274
#endif /* DRIZZLED_HASH_H */