2N/A/*-
2N/A * Copyright (c) 1990, 1993
2N/A * The Regents of the University of California. All rights reserved.
2N/A *
2N/A * This code is derived from software contributed to Berkeley by
2N/A * Margo Seltzer.
2N/A *
2N/A * Redistribution and use in source and binary forms, with or without
2N/A * modification, are permitted provided that the following conditions
2N/A * are met:
2N/A * 1. Redistributions of source code must retain the above copyright
2N/A * notice, this list of conditions and the following disclaimer.
2N/A * 2. Redistributions in binary form must reproduce the above copyright
2N/A * notice, this list of conditions and the following disclaimer in the
2N/A * documentation and/or other materials provided with the distribution.
2N/A * 3. All advertising materials mentioning features or use of this software
2N/A * must display the following acknowledgement:
2N/A * This product includes software developed by the University of
2N/A * California, Berkeley and its contributors.
2N/A * 4. Neither the name of the University nor the names of its contributors
2N/A * may be used to endorse or promote products derived from this software
2N/A * without specific prior written permission.
2N/A *
2N/A * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2N/A * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2N/A * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2N/A * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2N/A * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2N/A * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2N/A * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2N/A * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2N/A * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2N/A * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2N/A * SUCH DAMAGE.
2N/A */
2N/A
2N/A#if defined(LIBC_SCCS) && !defined(lint)
2N/Astatic char sccsid[] = "@(#)hash_func.c 8.4 (Berkeley) 11/7/95";
2N/A#endif /* LIBC_SCCS and not lint */
2N/A
2N/A#include <sys/types.h>
2N/A
2N/A#include "db-int.h"
2N/A#include "hash.h"
2N/A#include "page.h"
2N/A#include "extern.h"
2N/A
2N/A#if 0
2N/Astatic u_int32_t hash1 __P((const void *, size_t));
2N/Astatic u_int32_t hash2 __P((const void *, size_t));
2N/Astatic u_int32_t hash3 __P((const void *, size_t));
2N/A#endif
2N/Astatic u_int32_t hash4 __P((const void *, size_t));
2N/A
2N/A/* Default hash function. */
2N/Au_int32_t (*__default_hash) __P((const void *, size_t)) = hash4;
2N/A
2N/A/*
2N/A * Assume that we've already split the bucket to which this key hashes,
2N/A * calculate that bucket, and check that in fact we did already split it.
2N/A *
2N/A * EJB's original hsearch hash.
2N/A */
2N/A#define PRIME1 37
2N/A#define PRIME2 1048583
2N/A
2N/A#if 0
2N/Astatic u_int32_t
2N/Ahash1(key, len)
2N/A const void *key;
2N/A size_t len;
2N/A{
2N/A u_int32_t h;
2N/A u_int8_t *k;
2N/A
2N/A h = 0;
2N/A k = (u_int8_t *)key;
2N/A /* Convert string to integer */
2N/A while (len--)
2N/A h = h * PRIME1 ^ (*k++ - ' ');
2N/A h %= PRIME2;
2N/A return (h);
2N/A}
2N/A
2N/A/*
2N/A * Phong Vo's linear congruential hash
2N/A */
2N/A#define dcharhash(h, c) ((h) = 0x63c63cd9*(h) + 0x9c39c33d + (c))
2N/A
2N/Astatic u_int32_t
2N/Ahash2(key, len)
2N/A const void *key;
2N/A size_t len;
2N/A{
2N/A u_int32_t h;
2N/A u_int8_t *e, c, *k;
2N/A
2N/A k = (u_int8_t *)key;
2N/A e = k + len;
2N/A for (h = 0; k != e;) {
2N/A c = *k++;
2N/A if (!c && k > e)
2N/A break;
2N/A dcharhash(h, c);
2N/A }
2N/A return (h);
2N/A}
2N/A
2N/A/*
2N/A * This is INCREDIBLY ugly, but fast. We break the string up into 8 byte
2N/A * units. On the first time through the loop we get the "leftover bytes"
2N/A * (strlen % 8). On every other iteration, we perform 8 HASHC's so we handle
2N/A * all 8 bytes. Essentially, this saves us 7 cmp & branch instructions. If
2N/A * this routine is heavily used enough, it's worth the ugly coding.
2N/A *
2N/A * Ozan Yigit's original sdbm hash.
2N/A */
2N/Astatic u_int32_t
2N/Ahash3(key, len)
2N/A const void *key;
2N/A size_t len;
2N/A{
2N/A u_int32_t n, loop;
2N/A u_int8_t *k;
2N/A
2N/A#define HASHC n = *k++ + 65599 * n
2N/A
2N/A n = 0;
2N/A k = (u_int8_t *)key;
2N/A if (len > 0) {
2N/A loop = (len + 8 - 1) >> 3;
2N/A
2N/A switch (len & (8 - 1)) {
2N/A case 0:
2N/A do { /* All fall throughs */
2N/A HASHC;
2N/A case 7:
2N/A HASHC;
2N/A case 6:
2N/A HASHC;
2N/A case 5:
2N/A HASHC;
2N/A case 4:
2N/A HASHC;
2N/A case 3:
2N/A HASHC;
2N/A case 2:
2N/A HASHC;
2N/A case 1:
2N/A HASHC;
2N/A } while (--loop);
2N/A }
2N/A
2N/A }
2N/A return (n);
2N/A}
2N/A#endif
2N/A
2N/A
2N/A/* Chris Torek's hash function. */
2N/Astatic u_int32_t
2N/Ahash4(key, len)
2N/A const void *key;
2N/A size_t len;
2N/A{
2N/A u_int32_t h, loop;
2N/A const u_int8_t *k;
2N/A
2N/A#define HASH4a h = (h << 5) - h + *k++;
2N/A#define HASH4b h = (h << 5) + h + *k++;
2N/A#define HASH4 HASH4b
2N/A
2N/A h = 0;
2N/A k = (const u_int8_t *)key;
2N/A if (len > 0) {
2N/A loop = (len + 8 - 1) >> 3;
2N/A
2N/A switch (len & (8 - 1)) {
2N/A case 0:
2N/A do { /* All fall throughs */
2N/A HASH4;
2N/A case 7:
2N/A HASH4;
2N/A case 6:
2N/A HASH4;
2N/A case 5:
2N/A HASH4;
2N/A case 4:
2N/A HASH4;
2N/A case 3:
2N/A HASH4;
2N/A case 2:
2N/A HASH4;
2N/A case 1:
2N/A HASH4;
2N/A } while (--loop);
2N/A }
2N/A
2N/A }
2N/A return (h);
2N/A}