public abstract class LongHashFunction extends Object implements Serializable
long
-valued result from byte sequences of any length and
a plenty of different sources which "feels like byte sequences". Except hashBytes(byte[])
, hashBytes(ByteBuffer)
(with their "sliced" versions) and
hashMemory(long, long)
methods, which actually accept byte sequences, notion of byte
sequence is defined as follows:
String
s and
StringBuilder
s, byte sequence is how the input's bytes are actually lay in memory.
native
byte order, or
equivalently, hashXxx(primitive)
has always the same result as hashXxxs(new xxx[] {primitive})
, where "xxx" is any Java primitive type name.hash(Object, Access, long, long)
method byte sequence abstraction
is defined by the given Access
strategy to the given object.Hash function implementation could either produce equal results for equal input on platforms
with different ByteOrder
, favoring one byte order in terms of performance, or different
results, but performing equally good. This choice should be explicitly documented for all
LongHashFunction
implementations.
hashVoid()
and hash(Object, Access, long, long)
should be implemented; other have default implementations which in the end delegate to
hash(Object, Access, long, long)
abstract method.
Notes about how exactly methods with default implementations are implemented in doc comments are given for information and could be changed at any moment. However, it could hardly cause any issues with subclassing, except probably little performance degradation. Methods documented as "shortcuts" could either delegate to the referenced method or delegate directly to the method to which the referenced method delegates.
LongHashFunction
implementations shouldn't assume that Access
strategies
do defensive checks, and access only bytes within the requested range.
Modifier | Constructor and Description |
---|---|
protected |
LongHashFunction()
Constructor for use in subclasses.
|
Modifier and Type | Method and Description |
---|---|
static LongHashFunction |
city_1_1()
Returns a hash function implementing
CityHash64 algorithm, version 1.1 without seed values.
|
static LongHashFunction |
city_1_1(long seed)
Returns a hash function implementing
CityHash64 algorithm, version 1.1 using the given seed value.
|
static LongHashFunction |
city_1_1(long seed0,
long seed1)
Returns a hash function implementing
CityHash64 algorithm, version 1.1 using the two given seed values.
|
static LongHashFunction |
farmNa()
Returns a hash function implementing so-called
farmhashna algorithm, without seed values.
|
static LongHashFunction |
farmNa(long seed)
Returns a hash function implementing so-called
farmhashna algorithm, using the given seed value.
|
static LongHashFunction |
farmNa(long seed0,
long seed1)
Returns a hash function implementing so-called
farmhashna algorithm, using the two given seed values.
|
static LongHashFunction |
farmUo()
Returns a hash function implementing so-called
farmhashuo algorithm without seed values.
|
static LongHashFunction |
farmUo(long seed)
Returns a hash function implementing so-called
farmhashuo algorithm with the given seed value.
|
static LongHashFunction |
farmUo(long seed0,
long seed1)
Returns a hash function implementing so-called
farmhashuo algorithm with the two given seed values.
|
abstract <T> long |
hash(T input,
Access<T> access,
long off,
long len)
Returns the hash code for
len continuous bytes of the given input object,
starting from the given offset. |
long |
hashBoolean(boolean input)
Shortcut for
hashBooleans(new boolean[] {input}) . |
long |
hashBooleans(boolean[] input)
Shortcut for
hashBooleans(input, 0, input.length) . |
long |
hashBooleans(boolean[] input,
int off,
int len)
Returns the hash code for the specified subsequence of the given
boolean array. |
abstract long |
hashByte(byte input)
Returns the hash code for the given
byte value. |
long |
hashBytes(byte[] input)
Shortcut for
hashBytes(input, 0, input.length) . |
long |
hashBytes(byte[] input,
int off,
int len)
Returns the hash code for the specified subsequence of the given
byte array. |
long |
hashBytes(ByteBuffer input)
Shortcut for
hashBytes(input, input.position(), input.remaining()) . |
long |
hashBytes(ByteBuffer input,
int off,
int len)
Returns the hash code for the specified subsequence of the given
ByteBuffer . |
abstract long |
hashChar(char input)
Returns the hash code for the given
char value; this method is consistent with
LongHashFunction methods that accept sequences of bytes, assuming the input
value is interpreted in native byte order. |
long |
hashChars(char[] input)
Shortcut for
hashChars(input, 0, input.length) . |
long |
hashChars(char[] input,
int off,
int len)
Returns the hash code for bytes, as they lay in memory, of the specified subsequence
of the given
char array. |
long |
hashChars(String input)
Shortcut for
hashChars(input, 0, input.length()) . |
long |
hashChars(StringBuilder input)
Shortcut for
hashChars(input, 0, input.length()) . |
long |
hashChars(StringBuilder input,
int off,
int len)
Returns the hash code for bytes of the specified subsequence of the given
StringBuilder 's underlying char array. |
long |
hashChars(String input,
int off,
int len)
Returns the hash code for bytes of the specified subsequence of the given
String 's
underlying char array. |
abstract long |
hashInt(int input)
Returns the hash code for the given
int value; this method is consistent with
LongHashFunction methods that accept sequences of bytes, assuming the input
value is interpreted in native byte order. |
long |
hashInts(int[] input)
Shortcut for
hashInts(input, 0, input.length) . |
long |
hashInts(int[] input,
int off,
int len)
Returns the hash code for bytes, as they lay in memory, of the specified subsequence
of the given
int array. |
abstract long |
hashLong(long input)
Returns the hash code for the given
long value; this method is consistent with
LongHashFunction methods that accept sequences of bytes, assuming the input
value is interpreted in native byte order. |
long |
hashLongs(long[] input)
Shortcut for
hashLongs(input, 0, input.length) . |
long |
hashLongs(long[] input,
int off,
int len)
Returns the hash code for bytes, as they lay in memory, of the specified subsequence
of the given
long array. |
long |
hashMemory(long address,
long len)
Returns the hash code of bytes of the wild memory from the given address.
|
abstract long |
hashShort(short input)
Returns the hash code for the given
short value; this method is consistent with
LongHashFunction methods that accept sequences of bytes, assuming the input
value is interpreted in native byte order. |
long |
hashShorts(short[] input)
Shortcut for
hashShorts(input, 0, input.length) . |
long |
hashShorts(short[] input,
int off,
int len)
Returns the hash code for bytes, as they lay in memory, of the specified subsequence
of the given
short array. |
abstract long |
hashVoid()
Returns the hash code for the empty (zero-length) bytes sequence,
for example
hashBytes(new byte[0]) . |
static LongHashFunction |
murmur_3()
Returns a hash function implementing
MurmurHash3
algorithm without seed values.
|
static LongHashFunction |
murmur_3(long seed)
Returns a hash function implementing
MurmurHash3
algorithm with the given seed value.
|
static LongHashFunction |
xx_r39()
Returns a hash function implementing
xxHash
algorithm, release 39 without seed value (0 is used as default seed value).
|
static LongHashFunction |
xx_r39(long seed)
Returns a hash function implementing
xxHash
algorithm, release 39 with the given seed value.
|
public static LongHashFunction city_1_1()
ByteOrder
, but is slower
on big-endian platforms than on little-endian.city_1_1(long)
,
city_1_1(long, long)
public static LongHashFunction city_1_1(long seed)
ByteOrder
, but is slower
on big-endian platforms than on little-endian.city_1_1()
,
city_1_1(long, long)
public static LongHashFunction city_1_1(long seed0, long seed1)
ByteOrder
, but
is slower on big-endian platforms than on little-endian.city_1_1()
,
city_1_1(long)
public static LongHashFunction farmNa()
ByteOrder
, but is slower on big-endian
platforms than on little-endian.
farmhashna
was introduced in FarmHash 1.0. For inputs shorter than 32 bytes it's
output is equivalent to city_1_1()
output.
farmNa(long)
,
farmNa(long, long)
public static LongHashFunction farmNa(long seed)
ByteOrder
, but is slower on
big-endian platforms than on little-endian.
farmhashna
was introduced in FarmHash 1.0. For inputs shorter than 32 bytes it's
output is equivalent to city_1_1(long)
output.
farmNa()
,
farmNa(long, long)
public static LongHashFunction farmNa(long seed0, long seed1)
ByteOrder
, but is slower on
big-endian platforms than on little-endian.
farmhashna
was introduced in FarmHash 1.0. For inputs shorter than 32 bytes it's
output is equivalent to city_1_1(long, long)
output.
farmNa()
,
farmNa(long)
public static LongHashFunction farmUo()
ByteOrder
, but is slower on big-endian
platforms than on little-endian.
farmhashuo
was introduced in FarmHash 1.1.
farmUo(long)
,
farmUo(long, long)
public static LongHashFunction farmUo(long seed)
ByteOrder
, but is slower on big-endian
platforms than on little-endian.
farmhashuo
was introduced in FarmHash 1.1.
farmUo()
,
farmUo(long, long)
public static LongHashFunction farmUo(long seed0, long seed1)
ByteOrder
, but is slower on
big-endian platforms than on little-endian.
farmhashuo
was introduced in FarmHash 1.1.
farmUo()
,
farmUo(long)
public static LongHashFunction murmur_3()
ByteOrder
, but is slower on big-endian platforms than on
little-endian.murmur_3(long)
public static LongHashFunction murmur_3(long seed)
ByteOrder
, but is slower on big-endian platforms
than on little-endian.murmur_3()
public static LongHashFunction xx_r39()
ByteOrder
, but is slower on big-endian platforms
than on little-endian.
Note: implementation is fully compatible with xxHash releases at least up to r42.
xx_r39(long)
public static LongHashFunction xx_r39(long seed)
ByteOrder
, but is slower on big-endian platforms
than on little-endian.
Note: implementation is fully compatible with xxHash releases at least up to r42.
xx_r39()
public abstract long hashLong(long input)
long
value; this method is consistent with
LongHashFunction
methods that accept sequences of bytes, assuming the input
value is interpreted in native byte order. For example,
the result of hashLong(v)
call is identical to the result of
hashLongs(new long[] {v})
call for any long
value.public abstract long hashInt(int input)
int
value; this method is consistent with
LongHashFunction
methods that accept sequences of bytes, assuming the input
value is interpreted in native byte order. For example,
the result of hashInt(v)
call is identical to the result of
hashInts(new int[] {v})
call for any int
value.public abstract long hashShort(short input)
short
value; this method is consistent with
LongHashFunction
methods that accept sequences of bytes, assuming the input
value is interpreted in native byte order. For example,
the result of hashShort(v)
call is identical to the result of
hashShorts(new short[] {v})
call for any short
value.
As a consequence, hashShort(v)
call produce always the same result as hashChar((char) v)
.public abstract long hashChar(char input)
char
value; this method is consistent with
LongHashFunction
methods that accept sequences of bytes, assuming the input
value is interpreted in native byte order. For example,
the result of hashChar(v)
call is identical to the result of
hashChars(new char[] {v})
call for any char
value.
As a consequence, hashChar(v)
call produce always the same result as hashShort((short) v)
.public abstract long hashByte(byte input)
byte
value. This method is consistent with
LongHashFunction
methods that accept sequences of bytes. For example, the result of
hashByte(v)
call is identical to the result of
hashBytes(new byte[] {v})
call for any byte
value.public abstract long hashVoid()
hashBytes(new byte[0])
.public abstract <T> long hash(T input, Access<T> access, long off, long len)
len
continuous bytes of the given input
object,
starting from the given offset. The abstraction of input as ordered byte sequence and
"offset within the input" is defined by the given access
strategy.
This method doesn't promise to throw a RuntimeException
if [off, off + len - 1]
subsequence exceeds the bounds of the bytes sequence, defined by access
strategy for the given input
, so use this method with caution.
T
- the type of the inputinput
- the object to read bytes fromaccess
- access which defines the abstraction of the given input
as ordered byte sequenceoff
- offset to the first byte of the subsequence to hashlen
- length of the subsequence to hashpublic long hashBoolean(boolean input)
hashBooleans(new boolean[] {input})
.
Note that this is not necessarily equal to hashByte(input ? (byte) 1 : (byte) 0)
,
because booleans could be stored differently in this JVM.public long hashBooleans(@NotNull boolean[] input)
hashBooleans(input, 0, input.length)
.public long hashBooleans(@NotNull boolean[] input, int off, int len)
boolean
array.
Default implementation delegates to hash(Object, Access, long, long)
method
using unsafe Access
.
input
- the array to read data fromoff
- index of the first boolean
in the subsequence to hashlen
- length of the subsequence to hashIndexOutOfBoundsException
- if off < 0
or off + len > input.length
or len < 0
public long hashBytes(@NotNull byte[] input)
hashBytes(input, 0, input.length)
.public long hashBytes(@NotNull byte[] input, int off, int len)
byte
array.
Default implementation delegates to hash(Object, Access, long, long)
method
using unsafe Access
.
input
- the array to read bytes fromoff
- index of the first byte
in the subsequence to hashlen
- length of the subsequence to hashIndexOutOfBoundsException
- if off < 0
or off + len > input.length
or len < 0
public long hashBytes(ByteBuffer input)
hashBytes(input, input.position(), input.remaining())
.public long hashBytes(@NotNull ByteBuffer input, int off, int len)
ByteBuffer
.
This method doesn't alter the state (mark, position, limit or order) of the given
ByteBuffer
.
Default implementation delegates to hash(Object, Access, long, long)
method
using Access.toByteBuffer()
.
input
- the buffer to read bytes fromoff
- index of the first byte
in the subsequence to hashlen
- length of the subsequence to hashIndexOutOfBoundsException
- if off < 0
or off + len > input.capacity()
or len < 0
public long hashMemory(long address, long len)
Default implementation delegates to hash(Object, Access, long, long)
method
using unsafe Access
.
address
- the address of the first byte to hashlen
- length of the byte sequence to hashpublic long hashChars(@NotNull char[] input)
hashChars(input, 0, input.length)
.public long hashChars(@NotNull char[] input, int off, int len)
char
array.
Default implementation delegates to hash(Object, Access, long, long)
method
using unsafe Access
.
input
- the array to read data fromoff
- index of the first char
in the subsequence to hashlen
- length of the subsequence to hash, in chars (i. e. the length of the bytes
sequence to hash is len * 2L
)IndexOutOfBoundsException
- if off < 0
or off + len > input.length
or len < 0
public long hashChars(@NotNull String input)
hashChars(input, 0, input.length())
.public long hashChars(@NotNull String input, int off, int len)
String
's
underlying char
array.
Default implementation could either delegate to hash(Object, Access, long, long)
using Access.toNativeCharSequence()
, or to hashChars(char[], int, int)
.
input
- the string which bytes to hashoff
- index of the first char
in the subsequence to hashlen
- length of the subsequence to hash, in chars (i. e. the length of the bytes
sequence to hash is len * 2L
)String
's bytesIndexOutOfBoundsException
- if off < 0
or off + len > input.length()
or len < 0
public long hashChars(@NotNull StringBuilder input)
hashChars(input, 0, input.length())
.public long hashChars(@NotNull StringBuilder input, int off, int len)
StringBuilder
's underlying char
array.
Default implementation could either delegate to hash(Object, Access, long, long)
using Access.toNativeCharSequence()
, or to hashChars(char[], int, int)
.
input
- the string builder which bytes to hashoff
- index of the first char
in the subsequence to hashlen
- length of the subsequence to hash, in chars (i. e. the length of the bytes
sequence to hash is len * 2L
)String
's bytesIndexOutOfBoundsException
- if off < 0
or off + len > input.length()
or len < 0
public long hashShorts(@NotNull short[] input)
hashShorts(input, 0, input.length)
.public long hashShorts(@NotNull short[] input, int off, int len)
short
array.
Default implementation delegates to hash(Object, Access, long, long)
method
using unsafe Access
.
input
- the array to read data fromoff
- index of the first short
in the subsequence to hashlen
- length of the subsequence to hash, in shorts (i. e. the length of the bytes
sequence to hash is len * 2L
)IndexOutOfBoundsException
- if off < 0
or off + len > input.length
or len < 0
public long hashInts(@NotNull int[] input)
hashInts(input, 0, input.length)
.public long hashInts(@NotNull int[] input, int off, int len)
int
array.
Default implementation delegates to hash(Object, Access, long, long)
method
using unsafe Access
.
input
- the array to read data fromoff
- index of the first int
in the subsequence to hashlen
- length of the subsequence to hash, in ints (i. e. the length of the bytes
sequence to hash is len * 4L
)IndexOutOfBoundsException
- if off < 0
or off + len > input.length
or len < 0
public long hashLongs(@NotNull long[] input)
hashLongs(input, 0, input.length)
.public long hashLongs(@NotNull long[] input, int off, int len)
long
array.
Default implementation delegates to hash(Object, Access, long, long)
method
using unsafe Access
.
input
- the array to read data fromoff
- index of the first long
in the subsequence to hashlen
- length of the subsequence to hash, in longs (i. e. the length of the bytes
sequence to hash is len * 8L
)IndexOutOfBoundsException
- if off < 0
or off + len > input.length
or len < 0
Copyright © 2014–2017. All rights reserved.