更新使用ELF哈希算法

master
brisk 2014-03-28 01:25:34 +08:00
parent 025c96bf4f
commit 8fa4d1ac1d
1 changed files with 13 additions and 15 deletions

View File

@ -1,8 +1,8 @@
#include "hash.h"
/*两个计算哈希值的哈希函数*/
int hash_func1(const char *key);
int hash_func2(const char *key);
unsigned int hash_func1(const char *key);
unsigned int hash_func2(const char *key);
int conf_value_insert(CONF_ARG *arg)
{
@ -15,7 +15,7 @@ CONF_VALUE **conf_value_get_all(CONF *conf)
{}
/*BKDR 哈希算法*/
int hash_func1(const char *key)
unsigned int hash_func1(const char *key)
{
unsigned int seed=131;
unsigned int hash=0;
@ -30,23 +30,21 @@ int hash_func1(const char *key)
return hash&0x7FFFFFFF;
}
/*AP 哈希算法*/
int hash_func2(const char *key)
/*ELF 哈希算法*/
unsigned int hash_func2(const char *key)
{
unsigned int hash=0;
int i;
unsigned int x=0;
for(i=0;key[i];++i)
while(*key)
{
if((i & 1) == 0)
hash=(hash << 4)+*key;
++key;
if((x = hash & 0xF0000000L) != 0)
{
hash^=((hash << 7)^*key^(hash >> 3));
++key;
}
else
{
hash^=(~((hash << 11)^*key^(hash >> 5)));
++key;
hash^=(x>>24);
hash&=~x;
}
}