111 lines
4.0 KiB
Python
111 lines
4.0 KiB
Python
"""CityHash64 variant used by SCS HashFS; pure Python offline path hashing.
|
|
|
|
Port of ETS2LA/maps e853797b packages/clis/parser/cityhash/city.cc.
|
|
Copyright (c) 2011 Google, Inc.
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
in the Software without restriction, including without limitation the rights
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
furnished to do so, subject to the following conditions:
|
|
The above copyright notice and this permission notice shall be included in
|
|
all copies or substantial portions of the Software.
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
THE SOFTWARE.
|
|
"""
|
|
import struct
|
|
|
|
MASK = (1 << 64)-1
|
|
K0, K1, K2, K3 = 0xc3a5c85c97cb3127, 0xb492b66fbe98f273, 0x9ae16a3b2f90404f, 0xc949d7c7509e6557
|
|
|
|
|
|
def rotate(value, bits):
|
|
value &= MASK
|
|
return ((value >> bits) | (value << (64-bits))) & MASK
|
|
|
|
|
|
def mix(value):
|
|
value &= MASK
|
|
return value ^ (value >> 47)
|
|
|
|
|
|
def hash16(u, v):
|
|
mul = 0x9ddfea08eb382d69
|
|
a = ((u ^ v)*mul) & MASK
|
|
a ^= a >> 47
|
|
b = ((v ^ a)*mul) & MASK
|
|
b ^= b >> 47
|
|
return (b*mul) & MASK
|
|
|
|
|
|
def cityhash64(value):
|
|
s = value.encode('utf-8') if isinstance(value, str) else bytes(value)
|
|
n = len(s)
|
|
f = lambda i: struct.unpack_from('<Q', s, i)[0]
|
|
if n <= 16:
|
|
if n > 8:
|
|
a, b = f(0), f(n-8)
|
|
return hash16(a, rotate(b+n, n)) ^ b
|
|
if n >= 4:
|
|
a = struct.unpack_from('<I', s, 0)[0]
|
|
return hash16(n+(a << 3), struct.unpack_from('<I', s, n-4)[0])
|
|
if n:
|
|
y = s[0]+(s[n >> 1] << 8)
|
|
z = n+(s[n-1] << 2)
|
|
return (mix(((y*K2) & MASK) ^ ((z*K3) & MASK))*K2) & MASK
|
|
return K2
|
|
if n <= 32:
|
|
a, b, c, d = (f(0)*K1) & MASK, f(8), (f(n-8)*K2) & MASK, (f(n-16)*K0) & MASK
|
|
return hash16((rotate(a-b, 43)+rotate(c, 30)+d) & MASK,
|
|
(a+rotate(b ^ K3, 20)-c+n) & MASK)
|
|
if n <= 64:
|
|
z = f(24)
|
|
a = (f(0)+(n+f(n-16))*K0) & MASK
|
|
b, c = rotate(a+z, 52), rotate(a, 37)
|
|
a = (a+f(8)) & MASK
|
|
c = (c+rotate(a, 7)) & MASK
|
|
a = (a+f(16)) & MASK
|
|
vf, vs = (a+z) & MASK, (b+rotate(a, 31)+c) & MASK
|
|
a, z = (f(16)+f(n-32)) & MASK, f(n-8)
|
|
b, c = rotate(a+z, 52), rotate(a, 37)
|
|
a = (a+f(n-24)) & MASK
|
|
c = (c+rotate(a, 7)) & MASK
|
|
a = (a+f(n-16)) & MASK
|
|
wf, ws = (a+z) & MASK, (b+rotate(a, 31)+c) & MASK
|
|
r = mix((vf+ws)*K2+(wf+vs)*K0)
|
|
return (mix(r*K0+vs)*K2) & MASK
|
|
|
|
def weak(offset, a, b):
|
|
w, x, y, z = f(offset), f(offset+8), f(offset+16), f(offset+24)
|
|
a = (a+w) & MASK
|
|
b = rotate(b+a+z, 21)
|
|
c = a
|
|
a = (a+x+y) & MASK
|
|
b = (b+rotate(a, 44)) & MASK
|
|
return (a+z) & MASK, (b+c) & MASK
|
|
|
|
x = f(n-40)
|
|
y = (f(n-16)+f(n-56)) & MASK
|
|
z = hash16((f(n-48)+n) & MASK, f(n-24))
|
|
v, w = weak(n-64, n, z), weak(n-32, (y+K1) & MASK, x)
|
|
x = (x*K1+f(0)) & MASK
|
|
remaining, offset = (n-1) & ~63, 0
|
|
while remaining:
|
|
x = (rotate(x+y+v[0]+f(offset+8), 37)*K1) & MASK
|
|
y = (rotate(y+v[1]+f(offset+48), 42)*K1) & MASK
|
|
x ^= w[1]
|
|
y = (y+v[0]+f(offset+40)) & MASK
|
|
z = (rotate(z+w[0], 33)*K1) & MASK
|
|
v = weak(offset, (v[1]*K1) & MASK, (x+w[0]) & MASK)
|
|
w = weak(offset+32, (z+w[1]) & MASK, (y+f(offset+16)) & MASK)
|
|
z, x = x, z
|
|
offset += 64
|
|
remaining -= 64
|
|
return hash16((hash16(v[0], w[0])+mix(y)*K1+z) & MASK,
|
|
(hash16(v[1], w[1])+x) & MASK)
|