2
* See the file LICENSE for redistribution information.
4
* Copyright (c) 1996-2002
5
* Sleepycat Software. All rights reserved.
8
* Copyright (c) 1990, 1993
9
* Margo Seltzer. All rights reserved.
12
* Copyright (c) 1990, 1993
13
* The Regents of the University of California. All rights reserved.
15
* This code is derived from software contributed to Berkeley by
18
* Redistribution and use in source and binary forms, with or without
19
* modification, are permitted provided that the following conditions
21
* 1. Redistributions of source code must retain the above copyright
22
* notice, this list of conditions and the following disclaimer.
23
* 2. Redistributions in binary form must reproduce the above copyright
24
* notice, this list of conditions and the following disclaimer in the
25
* documentation and/or other materials provided with the distribution.
26
* 3. Neither the name of the University nor the names of its contributors
27
* may be used to endorse or promote products derived from this software
28
* without specific prior written permission.
30
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
31
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
32
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
34
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
35
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
36
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
37
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
38
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
39
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
43
#include "db_config.h"
46
static const char revid[] = "$Id$";
49
#ifndef NO_SYSTEM_INCLUDES
50
#include <sys/types.h>
57
* Phong Vo's linear congruential hash.
59
* PUBLIC: u_int32_t __ham_func2 __P((DB *, const void *, u_int32_t));
61
#define DCHARHASH(h, c) ((h) = 0x63c63cd9*(h) + 0x9c39c33d + (c))
64
__ham_func2(dbp, key, len)
69
const u_int8_t *e, *k;
78
for (h = 0; k != e;) {
89
* Ozan Yigit's original sdbm hash.
91
* Ugly, but fast. Break the string up into 8 byte units. On the first time
92
* through the loop get the "leftover bytes" (strlen % 8). On every other
93
* iteration, perform 8 HASHC's so we handle all 8 bytes. Essentially, this
94
* saves us 7 cmp & branch instructions.
96
* PUBLIC: u_int32_t __ham_func3 __P((DB *, const void *, u_int32_t));
99
__ham_func3(dbp, key, len)
108
COMPQUIET(dbp, NULL);
113
#define HASHC n = *k++ + 65599 * n
117
loop = (len + 8 - 1) >> 3;
118
switch (len & (8 - 1)) {
143
* Chris Torek's hash function. Although this function performs only
144
* slightly worse than __ham_func5 on strings, it performs horribly on
147
* PUBLIC: u_int32_t __ham_func4 __P((DB *, const void *, u_int32_t));
150
__ham_func4(dbp, key, len)
159
COMPQUIET(dbp, NULL);
164
#define HASH4a h = (h << 5) - h + *k++;
165
#define HASH4b h = (h << 5) + h + *k++;
170
loop = (len + 8 - 1) >> 3;
171
switch (len & (8 - 1)) {
195
* Fowler/Noll/Vo hash
197
* The basis of the hash algorithm was taken from an idea sent by email to the
198
* IEEE Posix P1003.2 mailing list from Phong Vo (kpv@research.att.com) and
199
* Glenn Fowler (gsf@research.att.com). Landon Curt Noll (chongo@toad.com)
200
* later improved on their algorithm.
202
* The magic is in the interesting relationship between the special prime
203
* 16777619 (2^24 + 403) and 2^32 and 2^8.
205
* This hash produces the fewest collisions of any function that we've seen so
206
* far, and works well on both numbers and strings.
208
* PUBLIC: u_int32_t __ham_func5 __P((DB *, const void *, u_int32_t));
211
__ham_func5(dbp, key, len)
216
const u_int8_t *k, *e;
220
COMPQUIET(dbp, NULL);
224
for (h = 0; k < e; ++k) {
234
* PUBLIC: u_int32_t __ham_test __P((DB *, const void *, u_int32_t));
237
__ham_test(dbp, key, len)
242
COMPQUIET(dbp, NULL);
244
return ((u_int32_t)*(char *)key);