murmurhash2算法 和 DJB Hash算法是目前最流行的hash算法

murmurhash2算法 和 DJB Hash算法是目前最流行的hash算法

1.DJB HASH算法

1
2
3
4
5
6
7
8
9
10
11
/* 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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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;
}
 
 
 
 
http://daodaoliang.com/c-%E5%AD%A6%E4%B9%A0/2017/03/08/c-%E5%AD%A6%E4%B9%A0-2017-03-08-HashAlgorithm/
posted @ 2017-06-13 18:33  findumars  Views(5936)  Comments(0Edit  收藏  举报