public static int hash(Slice data, int offset, int length) { return hash(DEFAULT_SEED, data, offset, length); }
public static int hash(Slice data, int offset, int length) { return hash(DEFAULT_SEED, data, offset, length); }
public static int hash(Slice data) { return hash(data, 0, data.length()); }
/** * Special-purpose version for hashing a single long value. Value is treated as little-endian */ public static int hash(long input) { int low = (int) input; int high = (int) (input >>> 32); int k1 = mixK1(low); int h1 = mixH1(DEFAULT_SEED, k1); k1 = mixK1(high); h1 = mixH1(h1, k1); return fmix(h1, SizeOf.SIZE_OF_LONG); }
public static int hash(Slice data) { return hash(data, 0, data.length()); }
/** * Special-purpose version for hashing a single long value. Value is treated as little-endian */ public static int hash(long input) { int low = (int) input; int high = (int) (input >>> 32); int k1 = mixK1(low); int h1 = mixH1(DEFAULT_SEED, k1); k1 = mixK1(high); h1 = mixH1(h1, k1); return fmix(h1, SizeOf.SIZE_OF_LONG); }
@Test(invocationCount = 100) public void testSingleInt() throws Exception { int value = ThreadLocalRandom.current().nextInt(); Slice slice = Slices.allocate(4); slice.setInt(0, value); int expected = Murmur3Hash32.hash(slice); int actual = Murmur3Hash32.hash(value); assertEquals(actual, expected); }
@SuppressFBWarnings({"SF_SWITCH_NO_DEFAULT", "SF_SWITCH_FALLTHROUGH"}) public static int hash(int seed, Slice data, int offset, int length) { final int fastLimit = offset + length - SizeOf.SIZE_OF_INT + 1; int h1 = seed; int current = offset; while (current < fastLimit) { int k1 = mixK1(data.getInt(current)); current += SizeOf.SIZE_OF_INT; h1 = mixH1(h1, k1); } int k1 = 0; switch (length & 3) { case 3: k1 ^= ((int) data.getUnsignedByte(current + 2)) << 16; case 2: k1 ^= ((int) data.getUnsignedByte(current + 1)) << 8; case 1: k1 ^= ((int) data.getUnsignedByte(current + 0)) << 0; } h1 ^= mixK1(k1); return fmix(h1, length); }
@Test(invocationCount = 100) public void testSingleLong() throws Exception { long value = ThreadLocalRandom.current().nextLong(); Slice slice = Slices.allocate(8); slice.setLong(0, value); int expected = Murmur3Hash32.hash(slice); int actual = Murmur3Hash32.hash(value); assertEquals(actual, expected); }
@SuppressFBWarnings({"SF_SWITCH_NO_DEFAULT", "SF_SWITCH_FALLTHROUGH"}) public static int hash(int seed, Slice data, int offset, int length) { final int fastLimit = offset + length - SizeOf.SIZE_OF_INT + 1; int h1 = seed; int current = offset; while (current < fastLimit) { int k1 = mixK1(data.getInt(current)); current += SizeOf.SIZE_OF_INT; h1 = mixH1(h1, k1); } int k1 = 0; switch (length & 3) { case 3: k1 ^= ((int) data.getUnsignedByte(current + 2)) << 16; case 2: k1 ^= ((int) data.getUnsignedByte(current + 1)) << 8; case 1: k1 ^= ((int) data.getUnsignedByte(current + 0)) << 0; } h1 ^= mixK1(k1); return fmix(h1, length); }
@Test(invocationCount = 100) public void testSingleInt() throws Exception { int value = ThreadLocalRandom.current().nextInt(); Slice slice = Slices.allocate(4); slice.setInt(0, value); int expected = Murmur3Hash32.hash(slice); int actual = Murmur3Hash32.hash(value); assertEquals(actual, expected); }
@Test(invocationCount = 100) public void testSingleLong() throws Exception { long value = ThreadLocalRandom.current().nextLong(); Slice slice = Slices.allocate(8); slice.setLong(0, value); int expected = Murmur3Hash32.hash(slice); int actual = Murmur3Hash32.hash(value); assertEquals(actual, expected); }
@Test(invocationCount = 100) public void testLessThan4Bytes() throws Exception { byte[] data = randomBytes(ThreadLocalRandom.current().nextInt(4)); int expected = Hashing.murmur3_32().hashBytes(data).asInt(); int actual = Murmur3Hash32.hash(Slices.wrappedBuffer(data)); assertEquals(actual, expected); }
@Test(invocationCount = 100) public void testLessThan4Bytes() throws Exception { byte[] data = randomBytes(ThreadLocalRandom.current().nextInt(4)); int expected = Hashing.murmur3_32().hashBytes(data).asInt(); int actual = Murmur3Hash32.hash(Slices.wrappedBuffer(data)); assertEquals(actual, expected); }
@Test(invocationCount = 100) public void testMoreThan4Bytes() throws Exception { byte[] data = randomBytes(131); int expected = Hashing.murmur3_32().hashBytes(data).asInt(); int actual = Murmur3Hash32.hash(Slices.wrappedBuffer(data)); assertEquals(actual, expected); }
@Test(invocationCount = 100) public void testMoreThan4Bytes() throws Exception { byte[] data = randomBytes(131); int expected = Hashing.murmur3_32().hashBytes(data).asInt(); int actual = Murmur3Hash32.hash(Slices.wrappedBuffer(data)); assertEquals(actual, expected); }
@Test(invocationCount = 100) public void testNonDefaultSeed() throws Exception { byte[] data = randomBytes(131); int seed = 123456789; int expected = Hashing.murmur3_32(seed).hashBytes(data).asInt(); int actual = Murmur3Hash32.hash(seed, Slices.wrappedBuffer(data), 0, data.length); assertEquals(actual, expected); }
@Test(invocationCount = 100) public void testOffsetAndLength() throws Exception { byte[] data = randomBytes(131); int offset = 13; int length = 55; int expected = Hashing.murmur3_32().hashBytes(data, offset, length).asInt(); int actual = Murmur3Hash32.hash(Slices.wrappedBuffer(data), offset, length); assertEquals(actual, expected); }