]>
git.saurik.com Git - apple/libc.git/blob - db.subproj/hash.subproj/hash_func.c
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
23 * Copyright (c) 1990, 1993
24 * The Regents of the University of California. All rights reserved.
26 * This code is derived from software contributed to Berkeley by
29 * Redistribution and use in source and binary forms, with or without
30 * modification, are permitted provided that the following conditions
32 * 1. Redistributions of source code must retain the above copyright
33 * notice, this list of conditions and the following disclaimer.
34 * 2. Redistributions in binary form must reproduce the above copyright
35 * notice, this list of conditions and the following disclaimer in the
36 * documentation and/or other materials provided with the distribution.
37 * 3. All advertising materials mentioning features or use of this software
38 * must display the following acknowledgement:
39 * This product includes software developed by the University of
40 * California, Berkeley and its contributors.
41 * 4. Neither the name of the University nor the names of its contributors
42 * may be used to endorse or promote products derived from this software
43 * without specific prior written permission.
45 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
46 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
49 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 #include <sys/types.h>
66 static u_int32_t hash1
__P((const void *, size_t));
67 static u_int32_t hash2
__P((const void *, size_t));
68 static u_int32_t hash3
__P((const void *, size_t));
69 static u_int32_t hash4
__P((const void *, size_t));
71 /* Global default hash function */
72 u_int32_t (*__default_hash
) __P((const void *, size_t)) = hash4
;
77 * Assume that we've already split the bucket to which this key hashes,
78 * calculate that bucket, and check that in fact we did already split it.
80 * This came from ejb's hsearch.
84 #define PRIME2 1048583
91 register const u_char
*key
;
94 /* Convert string to integer */
95 for (key
= keyarg
, h
= 0; len
--;)
96 h
= h
* PRIME1
^ (*key
++ - ' ');
102 * Phong's linear congruential hash
104 #define dcharhash(h, c) ((h) = 0x63c63cd9*(h) + 0x9c39c33d + (c))
111 register const u_char
*e
, *key
;
112 register u_int32_t h
;
117 for (h
= 0; key
!= e
;) {
127 * This is INCREDIBLY ugly, but fast. We break the string up into 8 byte
128 * units. On the first time through the loop we get the "leftover bytes"
129 * (strlen % 8). On every other iteration, we perform 8 HASHC's so we handle
130 * all 8 bytes. Essentially, this saves us 7 cmp & branch instructions. If
131 * this routine is heavily used enough, it's worth the ugly coding.
133 * OZ's original sdbm hash
140 register const u_char
*key
;
141 register size_t loop
;
142 register u_int32_t h
;
144 #define HASHC h = *key++ + 65599 * h
149 loop
= (len
+ 8 - 1) >> 3;
151 switch (len
& (8 - 1)) {
182 /* Hash function from Chris Torek. */
188 register const u_char
*key
;
189 register size_t loop
;
190 register u_int32_t h
;
192 #define HASH4a h = (h << 5) - h + *key++;
193 #define HASH4b h = (h << 5) + h + *key++;
199 loop
= (len
+ 8 - 1) >> 3;
201 switch (len
& (8 - 1)) {