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/


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM