/***********************************************************************1* *2* This software is part of the ast package *3* Copyright (c) 1985-2011 AT&T Intellectual Property *4* and is licensed under the *5* Eclipse Public License, Version 1.0 *6* by AT&T Intellectual Property *7* *8* A copy of the License is available at *9* http://www.eclipse.org/org/documents/epl-v10.html *10* (with md5 checksum b35adb5213ca9657e911e9befb180842) *11* *12* Information and Software Systems Research *13* AT&T Research *14* Florham Park NJ *15* *16* Glenn Fowler <[email protected]> *17* David Korn <[email protected]> *18* Phong Vo <[email protected]> *19* *20***********************************************************************/21#include "dthdr.h"2223/* Hashing a string into an unsigned integer.24** The basic method is to continuingly accumulate bytes and multiply25** with some given prime. The length n of the string is added last.26** The recurrent equation is like this:27** h[k] = (h[k-1] + bytes)*prime for 0 <= k < n28** h[n] = (h[n-1] + n)*prime29** The prime is chosen to have a good distribution of 1-bits so that30** the multiplication will distribute the bits in the accumulator well.31** The below code accumulates 2 bytes at a time for speed.32**33** Written by Kiem-Phong Vo (02/28/03)34*/3536#if __STD_C37uint dtstrhash(uint h, Void_t* args, ssize_t n)38#else39uint dtstrhash(h,args,n)40reg uint h;41Void_t* args;42ssize_t n;43#endif44{45unsigned char *s = (unsigned char*)args;4647if(n <= 0)48{ for(; *s != 0; s += s[1] ? 2 : 1)49h = (h + (s[0]<<8) + s[1])*DT_PRIME;50n = s - (unsigned char*)args;51}52else53{ unsigned char* ends;54for(ends = s+n-1; s < ends; s += 2)55h = (h + (s[0]<<8) + s[1])*DT_PRIME;56if(s <= ends)57h = (h + (s[0]<<8))*DT_PRIME;58}59return (h+n)*DT_PRIME;60}616263