1 /***********************************************************************
3 * This software is part of the ast package *
4 * Copyright (c) 1985-2010 AT&T Intellectual Property *
5 * and is licensed under the *
6 * Common Public License, Version 1.0 *
7 * by AT&T Intellectual Property *
9 * A copy of the License is available at *
10 * http://www.opensource.org/licenses/cpl1.0.txt *
11 * (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) *
13 * Information and Software Systems Research *
17 * Glenn Fowler <gsf@research.att.com> *
18 * David Korn <dgk@research.att.com> *
19 * Phong Vo <kpv@research.att.com> *
21 ***********************************************************************/
24 /* Hashing a string into an unsigned integer.
25 ** The basic method is to continuingly accumulate bytes and multiply
26 ** with some given prime. The length n of the string is added last.
27 ** The recurrent equation is like this:
28 ** h[k] = (h[k-1] + bytes)*prime for 0 <= k < n
29 ** h[n] = (h[n-1] + n)*prime
30 ** The prime is chosen to have a good distribution of 1-bits so that
31 ** the multiplication will distribute the bits in the accumulator well.
32 ** The below code accumulates 2 bytes at a time for speed.
34 ** Written by Kiem-Phong Vo (02/28/03)
38 uint
dtstrhash(reg uint h
, Void_t
* args
, reg
int n
)
40 uint
dtstrhash(h
,args
,n
)
46 reg
unsigned char* s
= (unsigned char*)args
;
49 { for(; *s
!= 0; s
+= s
[1] ? 2 : 1)
50 h
= (h
+ (s
[0]<<8) + s
[1])*DT_PRIME
;
51 n
= s
- (unsigned char*)args
;
54 { reg
unsigned char* ends
;
55 for(ends
= s
+n
-1; s
< ends
; s
+= 2)
56 h
= (h
+ (s
[0]<<8) + s
[1])*DT_PRIME
;
58 h
= (h
+ (s
[0]<<8))*DT_PRIME
;
60 return (h
+n
)*DT_PRIME
;