~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/hash.h

Added code necessary for building plugins dynamically.
Merged in changes from lifeless to allow autoreconf to work.
Touching plugin.ini files now triggers a rebuid - so config/autorun.sh is no
longer required to be run after touching those.
Removed the duplicate plugin names - also removed the issue that getting them
different would silently fail weirdly later.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
 
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
 
3
 *
 
4
 *  Copyright (C) 2009 Sun Microsystems
 
5
 *
 
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.
 
9
 *
 
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.
 
14
 *
 
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
 
18
 */
 
19
 
 
20
// Protocol Buffers - Google's data interchange format
 
21
// Copyright 2008 Google Inc.  All rights reserved.
 
22
// http://code.google.com/p/protobuf/
 
23
//
 
24
// Redistribution and use in source and binary forms, with or without
 
25
// modification, are permitted provided that the following conditions are
 
26
// met:
 
27
//
 
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
 
33
// distribution.
 
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.
 
37
//
 
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.
 
49
 
 
50
// Author: kenton@google.com (Kenton Varda)
 
51
//
 
52
// Deals with the fact that hash_map is not defined everywhere.
 
53
 
 
54
#ifndef DRIZZLED_HASH_H
 
55
#define DRIZZLED_HASH_H
 
56
 
 
57
#include <cstring>
 
58
#include <string>
 
59
 
 
60
#if defined(HAVE_HASH_MAP) && defined(HAVE_HASH_SET)
 
61
#include HASH_MAP_H
 
62
#include HASH_SET_H
 
63
#else
 
64
#define MISSING_HASH
 
65
#include <map>
 
66
#include <set>
 
67
#endif
 
68
 
 
69
namespace drizzled {
 
70
 
 
71
#ifdef MISSING_HASH
 
72
 
 
73
// This system doesn't have hash_map or hash_set.  Emulate them using map and
 
74
// set.
 
75
 
 
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>
 
80
struct hash {
 
81
  // Dummy, just to make derivative hash functions compile.
 
82
  int operator()(const Key& key) {
 
83
    assert(0);
 
84
    return 0;
 
85
  }
 
86
 
 
87
  inline bool operator()(const Key& a, const Key& b) const {
 
88
    return a < b;
 
89
  }
 
90
};
 
91
 
 
92
// Make sure char* is compared by value.
 
93
template <>
 
94
struct hash<const char*> {
 
95
  // Dummy, just to make derivative hash functions compile.
 
96
  int operator()(const char* key) {
 
97
    assert(0);
 
98
    return 0;
 
99
  }
 
100
 
 
101
  inline bool operator()(const char* a, const char* b) const {
 
102
    return strcmp(a, b) < 0;
 
103
  }
 
104
};
 
105
 
 
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> {
 
110
};
 
111
 
 
112
template <typename Key,
 
113
          typename HashFcn = hash<Key>,
 
114
          typename EqualKey = int >
 
115
class hash_set : public std::set<Key, HashFcn> {
 
116
};
 
117
 
 
118
#elif defined(_MSC_VER) && !defined(_STLPORT_VERSION)
 
119
 
 
120
template <typename Key>
 
121
struct hash : public HASH_NAMESPACE::hash_compare<Key> {
 
122
};
 
123
 
 
124
// MSVC's hash_compare<const char*> hashes based on the string contents but
 
125
// compares based on the string pointer.  WTF?
 
126
class CstringLess {
 
127
 public:
 
128
  inline bool operator()(const char* a, const char* b) const {
 
129
    return strcmp(a, b) < 0;
 
130
  }
 
131
};
 
132
 
 
133
template <>
 
134
struct hash<const char*>
 
135
  : public HASH_NAMESPACE::hash_compare<const char*, CstringLess> {
 
136
};
 
137
 
 
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> {
 
143
};
 
144
 
 
145
template <typename Key,
 
146
          typename HashFcn = hash<Key>,
 
147
          typename EqualKey = int >
 
148
class hash_set : public HASH_NAMESPACE::HASH_SET_CLASS<
 
149
    Key, HashFcn> {
 
150
};
 
151
 
 
152
#else
 
153
 
 
154
template <typename Key>
 
155
struct hash : public HASH_NAMESPACE::hash<Key> {
 
156
};
 
157
 
 
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);
 
162
  }
 
163
};
 
164
 
 
165
template <>
 
166
struct hash<const char*> : public HASH_NAMESPACE::hash<const char*> {
 
167
};
 
168
 
 
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> {
 
174
};
 
175
 
 
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> {
 
181
};
 
182
 
 
183
#endif
 
184
 
 
185
#if defined(REDEFINE_HASH_STRING)
 
186
template <>
 
187
struct hash<std::string> {
 
188
  inline size_t operator()(const std::string& key) const {
 
189
    return hash<const char*>()(key.c_str());
 
190
  }
 
191
 
 
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 {
 
195
    return a < b;
 
196
  }
 
197
};
 
198
#endif
 
199
 
 
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);
 
205
 
 
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;
 
209
  }
 
210
 
 
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 {
 
215
    return a < b;
 
216
  }
 
217
};
 
218
 
 
219
// Used by GCC/SGI STL only.  (Why isn't this provided by the standard
 
220
// library?  :( )
 
221
struct streq {
 
222
  inline bool operator()(const char* a, const char* b) const {
 
223
    return strcmp(a, b) == 0;
 
224
  }
 
225
};
 
226
 
 
227
}  /* namespace drizzled */
 
228
 
 
229
#endif /* DRIZZLED_HASH_H */