MurmurHash2.cs
1.93 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
using System.Runtime.InteropServices;
namespace Pole.Core.Utils
{
public class MurmurHash2
{
public static uint Hash(byte[] data)
{
return Hash(data, 0xc58f1a7b);
}
const uint m = 0x5bd1e995;
const int r = 24;
[StructLayout(LayoutKind.Explicit)]
struct BytetouintConverter
{
[FieldOffset(0)]
public byte[] Bytes;
[FieldOffset(0)]
public uint[] UInts;
}
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;
uint[] hackArray = new BytetouintConverter { Bytes = data }.UInts;
while (length >= 4)
{
uint k = hackArray[currentIndex++];
k *= m;
k ^= k >> r;
k *= m;
h *= m;
h ^= k;
length -= 4;
}
currentIndex *= 4; // fix the length
switch (length)
{
case 3:
h ^= (ushort)(data[currentIndex++] | data[currentIndex++] << 8);
h ^= (uint)data[currentIndex] << 16;
h *= m;
break;
case 2:
h ^= (ushort)(data[currentIndex++] | data[currentIndex] << 8);
h *= m;
break;
case 1:
h ^= data[currentIndex];
h *= m;
break;
default:
break;
}
// Do a few final mixes of the hash to ensure the last few
// bytes are well-incorporated.
h ^= h >> 13;
h *= m;
h ^= h >> 15;
return h;
}
}
}