/*++ Copyright (c) 2006 Microsoft Corporation Module Name: hash.cpp Abstract: Basic hash computation support. Author: Leonardo de Moura (leonardo) 2006-09-11. Revision History: --*/ #include"debug.h" #include"hash.h" // I'm using Bob Jenkin's hash function. // http://burtleburtle.net/bob/hash/doobs.html unsigned string_hash(const char * str, unsigned length, unsigned init_value) { register unsigned a, b, c, len; /* Set up the internal state */ len = length; a = b = 0x9e3779b9; /* the golden ratio; an arbitrary value */ c = init_value; /* the previous hash value */ /*---------------------------------------- handle most of the key */ while (len >= 12) { a += reinterpret_cast(str)[0]; b += reinterpret_cast(str)[1]; c += reinterpret_cast(str)[2]; mix(a,b,c); str += 12; len -= 12; } /*------------------------------------- handle the last 11 bytes */ c += length; switch(len) { /* all the case statements fall through */ case 11: c+=((unsigned)str[10]<<24); __fallthrough; case 10: c+=((unsigned)str[9]<<16); __fallthrough; case 9 : c+=((unsigned)str[8]<<8); __fallthrough; /* the first byte of c is reserved for the length */ case 8 : b+=((unsigned)str[7]<<24); __fallthrough; case 7 : b+=((unsigned)str[6]<<16); __fallthrough; case 6 : b+=((unsigned)str[5]<<8); __fallthrough; case 5 : b+=str[4]; __fallthrough; case 4 : a+=((unsigned)str[3]<<24); __fallthrough; case 3 : a+=((unsigned)str[2]<<16); __fallthrough; case 2 : a+=((unsigned)str[1]<<8); __fallthrough; case 1 : a+=str[0]; __fallthrough; /* case 0: nothing left to add */ } mix(a,b,c); /*-------------------------------------------- report the result */ return c; }