-
Notifications
You must be signed in to change notification settings - Fork 9
/
mmh2.py
38 lines (27 loc) · 797 Bytes
/
mmh2.py
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
import numpy
# MurmurHash2 (MurmurHash64A()) by Austin Appleby. Public domain.
def _hash64(key, seed=0):
aligned = (len(key) // 8) * 8
m = numpy.uint64(0xc6a4a7935bd1e995)
r = numpy.uint64(47)
h = numpy.uint64(seed) ^ numpy.uint64((numpy.uint64(len(key)) * m))
data = numpy.frombuffer(key[:aligned], dtype=numpy.uint64)
for k in data:
k *= m
k ^= k >> r
k *= m
h ^= k
h *= m
left = len(key) - aligned
if left:
data2 = key[aligned:]
for i in reversed(range(left)):
h ^= numpy.uint64(data2[i]) << numpy.uint64(i * 8)
h *= m
h ^= h >> r
h *= m
h ^= h >> r
return h
def hash64(key, seed=0):
with numpy.errstate(over='ignore'):
return _hash64(key, seed)