-
Notifications
You must be signed in to change notification settings - Fork 5
/
MurmurHash.cs
58 lines (53 loc) · 1.05 KB
/
MurmurHash.cs
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
public class MurmurHash2
{
public static uint Hash(string data)
{
return Hash(System.Text.Encoding.UTF8.GetBytes(data));
}
public static uint Hash(byte[] data)
{
return Hash(data, 0xc58f1a7a);
}
const uint m = 0x5bd1e995;
const int r = 24;
public static uint Hash(byte[] data, uint seed)
{
int length = data.Length;
if (length == 0)
return 0;
uint h = seed ^ (uint)length;
int currentIndex = 0;
while (length >= 4)
{
uint k = (uint)(data[currentIndex++] | data[currentIndex++] << 8 | data[currentIndex++] << 16 | data[currentIndex++] << 24);
k *= m;
k ^= k >> r;
k *= m;
h *= m;
h ^= k;
length -= 4;
}
switch (length)
{
case 3:
h ^= (UInt16)(data[currentIndex++] | data[currentIndex++] << 8);
h ^= (uint)(data[currentIndex] << 16);
h *= m;
break;
case 2:
h ^= (UInt16)(data[currentIndex++] | data[currentIndex] << 8);
h *= m;
break;
case 1:
h ^= data[currentIndex];
h *= m;
break;
default:
break;
}
h ^= h >> 13;
h *= m;
h ^= h >> 15;
return h;
}
}