现在的位置: 首页 > 综合 > 正文

记部分HASH函数

2013年07月17日 ⁄ 综合 ⁄ 共 467字 ⁄ 字号 评论关闭

据说使折叠法:

int hashcode(int *v, int k) {
int i, p = 0;
for(i = 0; i < k; ++i) {
p = ((p << 2) + (v[i] >> 4))^(v[i] << 10);
}
p %= MOD;
if(p < 0) p += MOD;
return p;
}

 

ELFhash UNIX系统处理字符串使用的哈希

 

//UNIX系统使用的哈希

int ELFhash(char *key) {
unsigned long h = 0;
while (*key) {
h = (h << 4) + *key++;
unsigned long g = h & 0xf0000000L; // 1个f,7个0
if (g) h ^= g >> 24;
h &= ~g;
}
return (h+M) % M; // M is Prime
}

//把数字的转换为字符串 ,进行哈希,以后就不用费心思设计哈希了

int hashcode(const L&a) {
char str[32*8];
char *s = (char*)(&a.d[1]);
int i;
for (i = 0; i < k-1; i++){
str[i] = 'a' + s[i];
}
str[i] = 0;
return ELFhash(str);
}

抱歉!评论已关闭.