您的位置:首页 > 其它

Hash算法--[murmurhash2算法 和 DJB Hash算法]

2016-06-14 00:00 876 查看
摘要: murmurhash2算法 和 DJB Hash算法是目前最流行的hash算法

1. DJB hash算法

[code=language-cpp]/* the famous DJB Hash Function for strings */
unsigned int DJBHash(char *str)
{
unsigned int hash = 5381;

while (*str){
hash = ((hash << 5) + hash) + (*str++); /* times 33 */
}
hash &= ~(1 << 31); /* strip the highest bit */
return hash;
}


2.murmurhash2

(摘自nginx系统源码)

[code=plain]uint32_t
murmur_hash2(char *data, size_t len)
{
uint32_t  h, k;

h = 0 ^ len;

while (len >= 4) {
k  = data[0];
k |= data[1] << 8;
k |= data[2] << 16;
k |= data[3] << 24;

k *= 0x5bd1e995;
k ^= k >> 24;
k *= 0x5bd1e995;

h *= 0x5bd1e995;
h ^= k;

data += 4;
len -= 4;
}

switch (len) {
case 3:
h ^= data[2] << 16;
case 2:
h ^= data[1] << 8;
case 1:
h ^= data[0];
h *= 0x5bd1e995;
}

h ^= h >> 13;
h *= 0x5bd1e995;
h ^= h >> 15;

return h;
}

目前比较流行的两种HASH算法,冲突概率较小
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: