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* key) {
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> {
112
template <typename Key,
113
typename HashFcn = hash<Key>,
114
typename EqualKey = int >
115
class hash_set : public std::set<Key, HashFcn> {
118
#elif defined(_MSC_VER) && !defined(_STLPORT_VERSION)
120
template <typename Key>
121
struct hash : public HASH_NAMESPACE::hash_compare<Key> {
124
// MSVC's hash_compare<const char*> hashes based on the string contents but
125
// compares based on the string pointer. WTF?
128
inline bool operator()(const char* a, const char* b) const {
129
return strcmp(a, b) < 0;
134
struct hash<const char*>
135
: public HASH_NAMESPACE::hash_compare<const char*, CstringLess> {
138
template <typename Key, typename Data,
139
typename HashFcn = hash<Key>,
140
typename EqualKey = int >
141
class hash_map : public HASH_NAMESPACE::HASH_MAP_CLASS<
142
Key, Data, HashFcn> {
145
template <typename Key,
146
typename HashFcn = hash<Key>,
147
typename EqualKey = int >
148
class hash_set : public HASH_NAMESPACE::HASH_SET_CLASS<
154
template <typename Key>
155
struct hash : public HASH_NAMESPACE::hash<Key> {
158
template <typename Key>
159
struct hash<const Key*> {
160
inline size_t operator()(const Key* key) const {
161
return reinterpret_cast<size_t>(key);
166
struct hash<const char*> : public HASH_NAMESPACE::hash<const char*> {
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> {
176
template <typename Key,
177
typename HashFcn = hash<Key>,
178
typename EqualKey = std::equal_to<Key> >
179
class hash_set : public HASH_NAMESPACE::HASH_SET_CLASS<
180
Key, HashFcn, EqualKey> {
185
#if defined(REDEFINE_HASH_STRING)
187
struct hash<std::string> {
188
inline size_t operator()(const std::string& key) const {
189
return hash<const char*>()(key.c_str());
192
static const size_t bucket_size = 4;
193
static const size_t min_buckets = 8;
194
inline size_t operator()(const std::string& a, const std::string& b) const {
200
template <typename First, typename Second>
201
struct hash<std::pair<First, Second> > {
202
inline size_t operator()(const std::pair<First, Second>& key) const {
203
size_t first_hash = hash<First>()(key.first);
204
size_t second_hash = hash<Second>()(key.second);
206
// FIXME(kenton): What is the best way to compute this hash? I have
207
// no idea! This seems a bit better than an XOR.
208
return first_hash * ((1 << 16) - 1) + second_hash;
211
static const size_t bucket_size = 4;
212
static const size_t min_buckets = 8;
213
inline size_t operator()(const std::pair<First, Second>& a,
214
const std::pair<First, Second>& b) const {
219
// Used by GCC/SGI STL only. (Why isn't this provided by the standard
222
inline bool operator()(const char* a, const char* b) const {
223
return strcmp(a, b) == 0;
227
} /* namespace drizzled */
229
#endif /* DRIZZLED_HASH_H */