Skip to content

Commit

Permalink
Rename LongHashFunction.xx_r39() to simply xx() and remove mentions o…
Browse files Browse the repository at this point in the history
…f particular xxHash releases, because it seems that if xxHash algorithm evolves, it will be given a different name
  • Loading branch information
leventov committed Feb 27, 2017
1 parent 4b7ba74 commit 658079a
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 41 deletions.
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,10 @@ in different byte order and obtain consistent results, only moderately compromis

Currently `long`-valued hash function interface is defined, with a plenty of shipped
implementations:
- **[xxHash](https://github.com/Cyan4973/xxHash), r39** (latest; r40-r42 are maintenance releases
without algorithm changes).
- **[xxHash](https://github.com/Cyan4973/xxHash)**.

- Two algorithms from **[FarmHash](https://github.com/google/farmhash)**: `farmhashna` (introduced
in FarmHash 1.0) and `farmhashuo` (introduced in FarmHash 1.1)
in FarmHash 1.0) and `farmhashuo` (introduced in FarmHash 1.1).

- **[CityHash](https://code.google.com/p/cityhash/), version 1.1** (latest; 1.1.1 is a C++
language-specific maintenance release).
Expand Down Expand Up @@ -87,7 +86,7 @@ Or Maven:

In Java:
```java
long hash = LongHashFunction.xx_r39().hashChars("hello");
long hash = LongHashFunction.xx().hashChars("hello");
```

See **[JavaDocs](http://openhft.github.io/Zero-Allocation-Hashing/apidocs/)** for more information.
Expand Down
23 changes: 9 additions & 14 deletions src/main/java/net/openhft/hashing/LongHashFunction.java
Original file line number Diff line number Diff line change
Expand Up @@ -265,34 +265,29 @@ public static LongHashFunction murmur_3(long seed) {

/**
* Returns a hash function implementing
* <a href="https://github.com/Cyan4973/xxHash/releases/tag/r39">xxHash
* algorithm, release 39</a> without seed value (0 is used as default seed value).
* <a href="https://github.com/Cyan4973/xxHash">xxHash algorithm</a> without seed value (0 is used
* as default 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.
*
* <p>Note: implementation is fully compatible with xxHash releases at least up to r42.
*
* @see #xx_r39(long)
* @see #xx(long)
*/
public static LongHashFunction xx_r39() {
return XxHash_r39.asLongHashFunctionWithoutSeed();
public static LongHashFunction xx() {
return XxHash.asLongHashFunctionWithoutSeed();
}

/**
* Returns a hash function implementing
* <a href="https://github.com/Cyan4973/xxHash/releases/tag/r39">xxHash
* algorithm, release 39</a> with the given seed value.
* <a href="https://github.com/Cyan4973/xxHash">xxHash algorithm</a> with 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.
*
* <p>Note: implementation is fully compatible with xxHash releases at least up to r42.
*
* @see #xx_r39()
* @see #xx()
*/
public static LongHashFunction xx_r39(long seed) {
return XxHash_r39.asLongHashFunctionWithSeed(seed);
public static LongHashFunction xx(long seed) {
return XxHash.asLongHashFunctionWithSeed(seed);
}

private static StringHash stringHash;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,13 @@
import static net.openhft.hashing.LongHashFunction.NATIVE_LITTLE_ENDIAN;

/**
* Adapted version of xxHash implementation from https://github.com/Cyan4973/xxHash/releases/tag/r39, which
* is fully compatible with r40 though.
* Adapted version of xxHash implementation from https://github.com/Cyan4973/xxHash.
* This implementation provides endian-independant hash values, but it's slower on big-endian platforms.
*/
class XxHash_r39 {
private static final XxHash_r39 INSTANCE = new XxHash_r39();
private static final XxHash_r39 NATIVE_XX = NATIVE_LITTLE_ENDIAN ?
XxHash_r39.INSTANCE : BigEndian.INSTANCE;
class XxHash {
private static final XxHash INSTANCE = new XxHash();
private static final XxHash NATIVE_XX = NATIVE_LITTLE_ENDIAN ?
XxHash.INSTANCE : BigEndian.INSTANCE;

// Primes if treated as unsigned
private static final long P1 = -7046029288634856825L;
Expand All @@ -36,7 +35,7 @@ class XxHash_r39 {
private static final long P4 = -8796714831421723037L;
private static final long P5 = 2870177450012600261L;

private XxHash_r39() {}
private XxHash() {}

<T> long fetch64(Access<T> access, T in, long off) {
return access.getLong(in, off);
Expand Down Expand Up @@ -166,7 +165,7 @@ private static long finalize(long hash) {
return hash;
}

private static class BigEndian extends XxHash_r39 {
private static class BigEndian extends XxHash {
private static final BigEndian INSTANCE = new BigEndian();

private BigEndian() {}
Expand Down Expand Up @@ -224,7 +223,7 @@ public long hashLong(long input) {
input *= P1;
hash ^= input;
hash = Long.rotateLeft(hash, 27) * P1 + P4;
return XxHash_r39.finalize(hash);
return XxHash.finalize(hash);
}

@Override
Expand All @@ -233,7 +232,7 @@ public long hashInt(int input) {
long hash = seed() + P5 + 4;
hash ^= Primitives.unsignedInt(input) * P1;
hash = Long.rotateLeft(hash, 23) * P2 + P3;
return XxHash_r39.finalize(hash);
return XxHash.finalize(hash);
}

@Override
Expand All @@ -244,7 +243,7 @@ public long hashShort(short input) {
hash = Long.rotateLeft(hash, 11) * P1;
hash ^= Primitives.unsignedByte(input >> 8) * P5;
hash = Long.rotateLeft(hash, 11) * P1;
return XxHash_r39.finalize(hash);
return XxHash.finalize(hash);
}

@Override
Expand All @@ -257,19 +256,19 @@ public long hashByte(byte input) {
long hash = seed() + P5 + 1;
hash ^= Primitives.unsignedByte(input) * P5;
hash = Long.rotateLeft(hash, 11) * P1;
return XxHash_r39.finalize(hash);
return XxHash.finalize(hash);
}

@Override
public long hashVoid() {
return XxHash_r39.finalize(P5);
return XxHash.finalize(P5);
}

@Override
public <T> long hash(T input, Access<T> access, long off, long len) {
long seed = seed();
if (access.byteOrder(input) == LITTLE_ENDIAN) {
return XxHash_r39.INSTANCE.xxHash64(seed, input, access, off, len);
return XxHash.INSTANCE.xxHash64(seed, input, access, off, len);
} else {
return BigEndian.INSTANCE.xxHash64(seed, input, access, off, len);
}
Expand All @@ -286,7 +285,7 @@ private static class AsLongHashFunctionSeeded extends AsLongHashFunction {

private AsLongHashFunctionSeeded(long seed) {
this.seed = seed;
voidHash = XxHash_r39.finalize(seed + P5);
voidHash = XxHash.finalize(seed + P5);
}

@Override
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/net/openhft/hashing/package-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
* <li>{@code long}-valued functions: see {@link net.openhft.hashing.LongHashFunction}
* <ul>
* <li>
* {@linkplain net.openhft.hashing.LongHashFunction#xx_r39() xxHash r39 without seed} and
* {@linkplain net.openhft.hashing.LongHashFunction#xx_r39(long) with a seed}.
* {@linkplain net.openhft.hashing.LongHashFunction#xx() xxHash without seed} and
* {@linkplain net.openhft.hashing.LongHashFunction#xx(long) with a seed}.
* </li>
* <li>
* {@linkplain net.openhft.hashing.LongHashFunction#farmUo() FarmHash 1.1 (farmhashuo)
Expand Down
6 changes: 3 additions & 3 deletions src/test/java/net/openhft/hashing/XxHashCollisionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,17 @@ public void xxHashCollisionTest() {
sequence.putLong(0, 1);
sequence.putLong(16, 42);
sequence.putLong(32, 2);
long h1 = LongHashFunction.xx_r39().hashBytes(sequence);
long h1 = LongHashFunction.xx().hashBytes(sequence);

sequence.putLong(0, 1 + 0xBA79078168D4BAFL);
sequence.putLong(32, 2 + 0x9C90005B80000000L);
long h2 = LongHashFunction.xx_r39().hashBytes(sequence);
long h2 = LongHashFunction.xx().hashBytes(sequence);
assertEquals(h1, h2);

sequence.putLong(0, 1 + 0xBA79078168D4BAFL * 2);
sequence.putLong(32, 2 + 0x9C90005B80000000L * 2);

long h3 = LongHashFunction.xx_r39().hashBytes(sequence);
long h3 = LongHashFunction.xx().hashBytes(sequence);
assertEquals(h2, h3);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import java.util.Collection;

@RunWith(Parameterized.class)
public class XxHash_r39_Test {
public class XxHashTest {

@Parameterized.Parameters
public static Collection<Object[]> data() {
Expand All @@ -40,12 +40,12 @@ public static Collection<Object[]> data() {

@Test
public void testCityWithoutSeeds() {
test(LongHashFunction.xx_r39(), HASHES_OF_LOOPING_BYTES_WITHOUT_SEED);
test(LongHashFunction.xx(), HASHES_OF_LOOPING_BYTES_WITHOUT_SEED);
}

@Test
public void testCityWithOneSeed() {
test(LongHashFunction.xx_r39(42L), HASHES_OF_LOOPING_BYTES_WITH_SEED_42);
test(LongHashFunction.xx(42L), HASHES_OF_LOOPING_BYTES_WITH_SEED_42);
}


Expand Down

0 comments on commit 658079a

Please sign in to comment.