/** * Returns a hash function implementing * <a href="https://code.google.com/p/cityhash/source/browse/trunk/src/city.cc?r=10"> * CityHash64 algorithm, version 1.1</a> using the given seed value. This implementation produce * equal results for equal input on platforms with different {@link ByteOrder}, but is slower * on big-endian platforms than on little-endian. * * @see #city_1_1() * @see #city_1_1(long, long) */ public static LongHashFunction city_1_1(long seed) { return CityAndFarmHash_1_1.asLongHashFunctionWithSeed(seed); }
/** * Returns a hash function implementing * <a href="https://code.google.com/p/cityhash/source/browse/trunk/src/city.cc?r=10"> * CityHash64 algorithm, version 1.1</a> using the two given seed values. This implementation * produce equal results for equal input on platforms with different {@link ByteOrder}, but * is slower on big-endian platforms than on little-endian. * * @see #city_1_1() * @see #city_1_1(long) */ public static LongHashFunction city_1_1(long seed0, long seed1) { return CityAndFarmHash_1_1.asLongHashFunctionWithTwoSeeds(seed0, seed1); }
/** * Returns a hash function implementing * <a href="https://code.google.com/p/cityhash/source/browse/trunk/src/city.cc?r=10"> * CityHash64 algorithm, version 1.1</a> without seed values. This implementation produce * equal results for equal input on platforms with different {@link ByteOrder}, but is slower * on big-endian platforms than on little-endian. * * @see #city_1_1(long) * @see #city_1_1(long, long) */ public static LongHashFunction city_1_1() { return CityAndFarmHash_1_1.asLongHashFunctionWithoutSeed(); }
@Override public long hashLong(long input) { input = NATIVE_CITY.toLittleEndian(input); long hash = hash8To16Bytes(8L, input, input); return finalize(hash); }
<T> long hashLen17To32(Access<T> access, T in, long off, long len) { long mul = mul(len); long a = fetch64(access, in, off) * K1; long b = fetch64(access, in, off + 8L); long c = fetch64(access, in, off + len - 8L) * mul; long d = fetch64(access, in, off + len - 16L) * K2; return hashLen16(rotateRight(a + b, 43) + rotateRight(c, 30) + d, a + rotateRight(b + K2, 18) + c, mul); }
<T> long cityHash64(Access<T> access, T in, long off, long len) { if (len <= 32L) { if (len <= 16L) { return hashLen0To16(access, in, off, len); } else { return hashLen17To32(access, in, off, len); return cityHashLen33To64(access, in, off, len); long x = fetch64(access, in, off + len - 40L); long y = fetch64(access, in, off + len - 16L) + fetch64(access, in, off + len - 56L); long z = hashLen16(fetch64(access, in, off + len - 48L) + len, fetch64(access, in, off + len - 24L)); long w4 = fetch64(access, in, off + len - 64L); long x4 = fetch64(access, in, off + len - 64L + 8L); long y4 = fetch64(access, in, off + len - 64L + 16L); long z4 = fetch64(access, in, off + len - 64L + 24L); a3 += w4; b3 = rotateRight(b3 + a3 + z4, 21); long w3 = fetch64(access, in, off + len - 32L); long x3 = fetch64(access, in, off + len - 32L + 8L); long y3 = fetch64(access, in, off + len - 32L + 16L); long z3 = fetch64(access, in, off + len - 32L + 24L); a2 += w3; b2 = rotateRight(b2 + a2 + z3, 21); x = x * K1 + fetch64(access, in, off);
if (len <= 32) { if (len <= 16) { return hashLen0To16(access, in, off, len); } else { return hashLen17To32(access, in, off, len); return naHashLen33To64(access, in, off, len); long z = shiftMix(y * K2 + 113) * K2; long v1 = 0, v2 = 0; long w1 = 0, w2 = 0; x = x * K2 + fetch64(access, in, off); x = rotateRight(x + y + v1 + fetch64(access, in, off + 8), 37) * K1; y = rotateRight(y + v2 + fetch64(access, in, off + 48), 42) * K1; x ^= w2; y += v1 + fetch64(access, in, off + 40); z = rotateRight(z + w1, 33) * K1; long a = v2 * K1; long b = x + w1; long z1 = fetch64(access, in, off + 24); a += fetch64(access, in, off); b = rotateRight(b + a + z1, 21); long c = a; a += fetch64(access, in, off + 8); a += fetch64(access, in, off + 16); b += rotateRight(a, 44); v1 = a + z1; v2 = b + c;
<T> long hashLen0To16(Access<T> access, T in, long off, long len) { if (len >= 8L) { long a = fetch64(access, in, off); long b = fetch64(access, in, off + len - 8L); return hash8To16Bytes(len, a, b); } else if (len >= 4L) { long a = Primitives.unsignedInt(fetch32(access, in, off)); long b = Primitives.unsignedInt(fetch32(access, in, off + len - 4L)); return hash4To7Bytes(len, a, b); } else if (len > 0L) { int a = access.getUnsignedByte(in, off); int b = access.getUnsignedByte(in, off + (len >> 1)); int c = access.getUnsignedByte(in, off + len - 1L); return hash1To3Bytes((int) len, a, b, c); } return K2; }
<T> long uoHash64WithSeeds(Access<T> access, T in, long off, long len, long seed0, long seed1) { if (len <= 64) { return naHash64WithSeeds(access, in, off, len, seed0, seed1); long z = shiftMix(y * K2) * K2; long v0 = seed0; long v1 = seed1; long a0 = fetch64(access, in, off); long a1 = fetch64(access, in, off + 8); long a2 = fetch64(access, in, off + 16); long a3 = fetch64(access, in, off + 24); long a4 = fetch64(access, in, off + 32); long a5 = fetch64(access, in, off + 40); long a6 = fetch64(access, in, off + 48); long a7 = fetch64(access, in, off + 56); x += a0 + a1; y += a2; u += y; y += u; x = rotateRight(y - x + v0 + fetch64(access, in, off + 8), 37) * mul; y = rotateRight(y ^ v1 ^ fetch64(access, in, off + 48), 42) * mul; x ^= w1 * 9; y += v0 + fetch64(access, in, off + 40); z = rotateRight(z + w0, 33) * mul; long z1 = fetch64(access, in, off + 24); a += fetch64(access, in, off); b = rotateRight(b + a + z1, 21);
private <T> long cityHashLen33To64(Access<T> access, T in, long off, long len) { long mul = mul(len); long a = fetch64(access, in, off) * K2; long b = fetch64(access, in, off + 8L); long c = fetch64(access, in, off + len - 24L); long d = fetch64(access, in, off + len - 32L); long e = fetch64(access, in, off + 16L) * K2; long f = fetch64(access, in, off + 24L) * 9L; long g = fetch64(access, in, off + len - 8L); long h = fetch64(access, in, off + len - 16L) * mul; long u = rotateRight(a + g, 43) + (rotateRight(b, 30) + c) * 9L; long v = ((a + g) ^ d) + f + 1L; long w = reverseBytes((u + v) * mul) + h; long x = rotateRight(e + f, 42) + c; long y = (reverseBytes((v + w) * mul) + g) * mul; long z = e + f + c; a = reverseBytes((x + z) * mul + y) + b; b = shiftMix((z + a) * mul + d + h) * mul; return b + x; }
@Override public long hashInt(int input) { input = NATIVE_CITY.toLittleEndian(input); long unsignedInt = Primitives.unsignedInt(input); long hash = hash4To7Bytes(4L, unsignedInt, unsignedInt); return finalize(hash); }
@Override public long hashChar(char input) { int unsignedInput = (int) input; int firstByte = (unsignedInput >> FIRST_SHORT_BYTE_SHIFT) & FIRST_SHORT_BYTE_MASK; int secondByte = (unsignedInput >> SECOND_SHORT_BYTE_SHIFT) & SECOND_SHORT_BYTE_MASK; long hash = hash1To3Bytes(2, firstByte, secondByte, secondByte); return finalize(hash); }
@Override <T> long fetch64(Access<T> access, T in, long off) { return reverseBytes(super.fetch64(access, in, off)); }
@Override <T> int fetch32(Access<T> access, T in, long off) { return Integer.reverseBytes(super.fetch32(access, in, off)); }
@Override public <T> long hash(T input, Access<T> access, long off, long len) { long hash; if (access.byteOrder(input) == LITTLE_ENDIAN) { hash = CityAndFarmHash_1_1.INSTANCE.cityHash64(access, input, off, len); } else { hash = BigEndian.INSTANCE.cityHash64(access, input, off, len); } return finalize(hash); }
private <T> long naHashLen33To64(Access<T> access, T in, long off, long len) { long mul = mul(len); long a = fetch64(access, in, off) * K2; long b = fetch64(access, in, off + 8L); long c = fetch64(access, in, off + len - 8) * mul; long d = fetch64(access, in, off + len - 16) * K2; long y = rotateRight(a + b, 43) + rotateRight(c, 30) + d; long z = hashLen16(y, a + rotateRight(b + K2, 18) + c, mul); long e = fetch64(access, in, off + 16) * mul; long f = fetch64(access, in, off + 24); long g = (y + fetch64(access, in, off + len - 32)) * mul; long h = (z + fetch64(access, in, off + len - 24)) * mul; return hashLen16(rotateRight(e + f, 43) + rotateRight(g, 30) + h, e + rotateRight(f + a, 18) + g, mul); }
@Override public long hashByte(byte input) { int unsignedByte = Primitives.unsignedByte(input); long hash = hash1To3Bytes(1, unsignedByte, unsignedByte, unsignedByte); return finalize(hash); }