Home | History | Annotate | Line # | Download | only in hash
hash_func.c revision 1.1
      1  1.1  cgd /*-
      2  1.1  cgd  * Copyright (c) 1990, 1993
      3  1.1  cgd  *	The Regents of the University of California.  All rights reserved.
      4  1.1  cgd  *
      5  1.1  cgd  * This code is derived from software contributed to Berkeley by
      6  1.1  cgd  * Margo Seltzer.
      7  1.1  cgd  *
      8  1.1  cgd  * Redistribution and use in source and binary forms, with or without
      9  1.1  cgd  * modification, are permitted provided that the following conditions
     10  1.1  cgd  * are met:
     11  1.1  cgd  * 1. Redistributions of source code must retain the above copyright
     12  1.1  cgd  *    notice, this list of conditions and the following disclaimer.
     13  1.1  cgd  * 2. Redistributions in binary form must reproduce the above copyright
     14  1.1  cgd  *    notice, this list of conditions and the following disclaimer in the
     15  1.1  cgd  *    documentation and/or other materials provided with the distribution.
     16  1.1  cgd  * 3. All advertising materials mentioning features or use of this software
     17  1.1  cgd  *    must display the following acknowledgement:
     18  1.1  cgd  *	This product includes software developed by the University of
     19  1.1  cgd  *	California, Berkeley and its contributors.
     20  1.1  cgd  * 4. Neither the name of the University nor the names of its contributors
     21  1.1  cgd  *    may be used to endorse or promote products derived from this software
     22  1.1  cgd  *    without specific prior written permission.
     23  1.1  cgd  *
     24  1.1  cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     25  1.1  cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     26  1.1  cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27  1.1  cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     28  1.1  cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29  1.1  cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30  1.1  cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31  1.1  cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32  1.1  cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33  1.1  cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34  1.1  cgd  * SUCH DAMAGE.
     35  1.1  cgd  */
     36  1.1  cgd 
     37  1.1  cgd #if defined(LIBC_SCCS) && !defined(lint)
     38  1.1  cgd static char sccsid[] = "@(#)hash_func.c	8.1 (Berkeley) 6/4/93";
     39  1.1  cgd #endif /* LIBC_SCCS and not lint */
     40  1.1  cgd 
     41  1.1  cgd #include <sys/types.h>
     42  1.1  cgd 
     43  1.1  cgd #include <db.h>
     44  1.1  cgd #include "hash.h"
     45  1.1  cgd #include "page.h"
     46  1.1  cgd #include "extern.h"
     47  1.1  cgd 
     48  1.1  cgd static int hash1 __P((u_char *, int));
     49  1.1  cgd static int hash2 __P((u_char *, int));
     50  1.1  cgd static int hash3 __P((u_char *, int));
     51  1.1  cgd static int hash4 __P((u_char *, int));
     52  1.1  cgd 
     53  1.1  cgd /* Global default hash function */
     54  1.1  cgd int (*__default_hash) __P((u_char *, int)) = hash4;
     55  1.1  cgd 
     56  1.1  cgd /******************************* HASH FUNCTIONS **************************/
     57  1.1  cgd /*
     58  1.1  cgd  * Assume that we've already split the bucket to which this key hashes,
     59  1.1  cgd  * calculate that bucket, and check that in fact we did already split it.
     60  1.1  cgd  *
     61  1.1  cgd  * This came from ejb's hsearch.
     62  1.1  cgd  */
     63  1.1  cgd 
     64  1.1  cgd #define PRIME1		37
     65  1.1  cgd #define PRIME2		1048583
     66  1.1  cgd 
     67  1.1  cgd static int
     68  1.1  cgd hash1(key, len)
     69  1.1  cgd 	register u_char *key;
     70  1.1  cgd 	register int len;
     71  1.1  cgd {
     72  1.1  cgd 	register int h;
     73  1.1  cgd 
     74  1.1  cgd 	h = 0;
     75  1.1  cgd 	/* Convert string to integer */
     76  1.1  cgd 	while (len--)
     77  1.1  cgd 		h = h * PRIME1 ^ (*key++ - ' ');
     78  1.1  cgd 	h %= PRIME2;
     79  1.1  cgd 	return (h);
     80  1.1  cgd }
     81  1.1  cgd 
     82  1.1  cgd /*
     83  1.1  cgd  * Phong's linear congruential hash
     84  1.1  cgd  */
     85  1.1  cgd #define dcharhash(h, c)	((h) = 0x63c63cd9*(h) + 0x9c39c33d + (c))
     86  1.1  cgd 
     87  1.1  cgd static int
     88  1.1  cgd hash2(key, len)
     89  1.1  cgd 	register u_char *key;
     90  1.1  cgd 	int len;
     91  1.1  cgd {
     92  1.1  cgd 	register u_char *e, c;
     93  1.1  cgd 	register int h;
     94  1.1  cgd 
     95  1.1  cgd 	e = key + len;
     96  1.1  cgd 	for (h = 0; key != e;) {
     97  1.1  cgd 		c = *key++;
     98  1.1  cgd 		if (!c && key > e)
     99  1.1  cgd 			break;
    100  1.1  cgd 		dcharhash(h, c);
    101  1.1  cgd 	}
    102  1.1  cgd 	return (h);
    103  1.1  cgd }
    104  1.1  cgd 
    105  1.1  cgd /*
    106  1.1  cgd  * This is INCREDIBLY ugly, but fast.  We break the string up into 8 byte
    107  1.1  cgd  * units.  On the first time through the loop we get the "leftover bytes"
    108  1.1  cgd  * (strlen % 8).  On every other iteration, we perform 8 HASHC's so we handle
    109  1.1  cgd  * all 8 bytes.  Essentially, this saves us 7 cmp & branch instructions.  If
    110  1.1  cgd  * this routine is heavily used enough, it's worth the ugly coding.
    111  1.1  cgd  *
    112  1.1  cgd  * OZ's original sdbm hash
    113  1.1  cgd  */
    114  1.1  cgd static int
    115  1.1  cgd hash3(key, len)
    116  1.1  cgd 	register u_char *key;
    117  1.1  cgd 	register int len;
    118  1.1  cgd {
    119  1.1  cgd 	register int n, loop;
    120  1.1  cgd 
    121  1.1  cgd #define HASHC   n = *key++ + 65599 * n
    122  1.1  cgd 
    123  1.1  cgd 	n = 0;
    124  1.1  cgd 	if (len > 0) {
    125  1.1  cgd 		loop = (len + 8 - 1) >> 3;
    126  1.1  cgd 
    127  1.1  cgd 		switch (len & (8 - 1)) {
    128  1.1  cgd 		case 0:
    129  1.1  cgd 			do {	/* All fall throughs */
    130  1.1  cgd 				HASHC;
    131  1.1  cgd 		case 7:
    132  1.1  cgd 				HASHC;
    133  1.1  cgd 		case 6:
    134  1.1  cgd 				HASHC;
    135  1.1  cgd 		case 5:
    136  1.1  cgd 				HASHC;
    137  1.1  cgd 		case 4:
    138  1.1  cgd 				HASHC;
    139  1.1  cgd 		case 3:
    140  1.1  cgd 				HASHC;
    141  1.1  cgd 		case 2:
    142  1.1  cgd 				HASHC;
    143  1.1  cgd 		case 1:
    144  1.1  cgd 				HASHC;
    145  1.1  cgd 			} while (--loop);
    146  1.1  cgd 		}
    147  1.1  cgd 
    148  1.1  cgd 	}
    149  1.1  cgd 	return (n);
    150  1.1  cgd }
    151  1.1  cgd 
    152  1.1  cgd /* Hash function from Chris Torek. */
    153  1.1  cgd static int
    154  1.1  cgd hash4(key, len)
    155  1.1  cgd 	register u_char *key;
    156  1.1  cgd 	register int len;
    157  1.1  cgd {
    158  1.1  cgd 	register int h, loop;
    159  1.1  cgd 
    160  1.1  cgd #define HASH4a   h = (h << 5) - h + *key++;
    161  1.1  cgd #define HASH4b   h = (h << 5) + h + *key++;
    162  1.1  cgd #define HASH4 HASH4b
    163  1.1  cgd 
    164  1.1  cgd 	h = 0;
    165  1.1  cgd 	if (len > 0) {
    166  1.1  cgd 		loop = (len + 8 - 1) >> 3;
    167  1.1  cgd 
    168  1.1  cgd 		switch (len & (8 - 1)) {
    169  1.1  cgd 		case 0:
    170  1.1  cgd 			do {	/* All fall throughs */
    171  1.1  cgd 				HASH4;
    172  1.1  cgd 		case 7:
    173  1.1  cgd 				HASH4;
    174  1.1  cgd 		case 6:
    175  1.1  cgd 				HASH4;
    176  1.1  cgd 		case 5:
    177  1.1  cgd 				HASH4;
    178  1.1  cgd 		case 4:
    179  1.1  cgd 				HASH4;
    180  1.1  cgd 		case 3:
    181  1.1  cgd 				HASH4;
    182  1.1  cgd 		case 2:
    183  1.1  cgd 				HASH4;
    184  1.1  cgd 		case 1:
    185  1.1  cgd 				HASH4;
    186  1.1  cgd 			} while (--loop);
    187  1.1  cgd 		}
    188  1.1  cgd 
    189  1.1  cgd 	}
    190  1.1  cgd 	return (h);
    191  1.1  cgd }
    192