Graphviz 2.29.20120208.0545
lib/cdt/dtstrhash.c
Go to the documentation of this file.
00001 #include        "dthdr.h"
00002 
00003 /* Hashing a string into an unsigned integer.
00004 ** The basic method is to continuingly accumulate bytes and multiply
00005 ** with some given prime. The length n of the string is added last.
00006 ** The recurrent equation is like this:
00007 **      h[k] = (h[k-1] + bytes)*prime   for 0 <= k < n
00008 **      h[n] = (h[n-1] + n)*prime
00009 ** The prime is chosen to have a good distribution of 1-bits so that
00010 ** the multiplication will distribute the bits in the accumulator well.
00011 ** The below code accumulates 2 bytes at a time for speed.
00012 **
00013 ** Written by Kiem-Phong Vo (02/28/03)
00014 */
00015 
00016 #if __STD_C
00017 uint dtstrhash(reg uint h, Void_t* args, reg int n)
00018 #else
00019 uint dtstrhash(h,args,n)
00020 reg uint        h;
00021 Void_t*         args;
00022 reg int         n;
00023 #endif
00024 {
00025         reg unsigned char*      s = (unsigned char*)args;
00026 
00027         if(n <= 0)
00028         {       for(; *s != 0; s += s[1] ? 2 : 1)
00029                         h = (h + (s[0]<<8) + s[1])*DT_PRIME;
00030                 n = s - (unsigned char*)args;
00031         }
00032         else
00033         {       reg unsigned char*      ends;
00034                 for(ends = s+n-1; s < ends; s += 2)
00035                         h = (h + (s[0]<<8) + s[1])*DT_PRIME;
00036                 if(s <= ends)
00037                         h = (h + (s[0]<<8))*DT_PRIME;
00038         }
00039         return (h+n)*DT_PRIME;
00040 }