summaryrefslogtreecommitdiff
path: root/unproto/hash.c
blob: 153f6b7df9904214e40b944742260dbec8c3d29a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/*++
/* NAME
/*	hash 3
/* SUMMARY
/*	compute hash value for string
/* SYNOPSIS
/*	int hash(string, size)
/*	char *string;
/*	int size;
/* DESCRIPTION
/*	This function computes for the given null-terminated string an
/*	integer hash value in the range 0..size-1.
/* SEE ALSO
/* .fi
/*	Alfred V. Aho, Ravi Sethi and Jeffrey D. Ullman: Compilers: 
/*	principles, techniques and tools; Addison-Wesley, Amsterdam, 1986.
/* AUTHOR(S)
/*	Wietse Venema
/*	Eindhoven University of Technology
/*	Department of Mathematics and Computer Science
/*	Den Dolech 2, P.O. Box 513, 5600 MB Eindhoven, The Netherlands
/*
/*	Originally written by: P. J. Weinberger at Bell Labs.
/* LAST MODIFICATION
/*	92/01/15 21:53:12
/* VERSION/RELEASE
/*	%I
/*--*/

static char hash_sccsid[] = "@(#) hash.c 1.1 92/01/15 21:53:12";

/* hash - hash a string; original author: P. J. Weinberger at Bell Labs. */

int     hash(s, size)
register char *s;
unsigned size;
{
    register unsigned long h = 0;
    register unsigned long g;

    /*
     * For a performance comparison with the hash function presented in K&R,
     * first edition, see the "Dragon" book by Aho, Sethi and Ullman.
     */

    while (*s) {
	h = (h << 4) + *s++;
	if (g = (h & 0xf0000000)) {
	    h ^= (g >> 24);
	    h ^= g;
	}
    }
    return (h % size);
}