/*
* A simple string hash algorithm
*
* (Java is using the same algorithm)
*
* */
unsigned int string_hash_calculate(char *key)
{
char *p = key;
unsigned int h = 0;
printf("using string hash algo!\n");
while(*p) {
h = (h << 5) - h + (unsigned int) (*p); // h = h * 31 + val
p++;
}
return h;
}
elf hash算法
/*
* elf hash algorithm
*
* Another good hash algorithm
*/
unsigned int elf_hash_calculate(char *key)
{
unsigned int h = 0, g;
char *p = key;
printf("using elf hash algo!\n");
while(*p) {
h = (h << 4) + *p++; //core calculate
if ((g = (h & 0xF0000000U)) != 0) //handle overflow case (which means top 4 bits are used)
h ^= (g >> 24); //XOR top 4 bits and low 4 bits
h &= ~g; //clean top 4 bits for next calculate
}
return h;
}