Package squidpony.squidmath
Class HashCommon
java.lang.Object
squidpony.squidmath.HashCommon
public class HashCommon extends Object
Code used internally for hashing OrderedMap, OrderedSet, IntDoubleOrderedMap, Arrangement, and so on.
Has some methods and constants that may be useful in other kinds of code.
Created by Tommy Ettinger on 7/28/2017.
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
HashCommon.EnumHasher
-
Field Summary
Fields Modifier and Type Field Description static HashCommon.EnumHasher
enumHasher
static int
INT_PHI
232 · φ, φ = (√5 − 1)/2.static int
INV_INT_PHI
The reciprocal ofINT_PHI
modulo 232.static long
INV_LONG_PHI
The reciprocal ofLONG_PHI
modulo 264.static long
LONG_PHI
264 · φ, φ = (√5 − 1)/2. -
Method Summary
Modifier and Type Method Description static int
mix(int n)
Thoroughly mixes the bits of an integer.static long
mix(long x)
Quickly mixes the bits of a long integer.static int
mixOther(int x)
Thoroughly mixes the bits of an integer.static int
nextPowerOfTwo(int x)
Return the least power of two greater than or equal to the specified value.static long
nextPowerOfTwo(long x)
Return the least power of two greater than or equal to the specified value.
-
Field Details
-
enumHasher
-
INT_PHI
232 · φ, φ = (√5 − 1)/2.- See Also:
- Constant Field Values
-
INV_INT_PHI
The reciprocal ofINT_PHI
modulo 232.- See Also:
- Constant Field Values
-
LONG_PHI
264 · φ, φ = (√5 − 1)/2.- See Also:
- Constant Field Values
-
INV_LONG_PHI
The reciprocal ofLONG_PHI
modulo 264.- See Also:
- Constant Field Values
-
-
Method Details
-
mixOther
Thoroughly mixes the bits of an integer.
This method mixes the bits of the argument using a multiplication by a smaller int (19 bits) followed by XOR with two different rotations of the earlier product; it should work well even on GWT, where overflow can't be relied on without bitwise operations being used. The previous mix(int) method would lose precision rather than overflowing on GWT, which could have serious effects on the performance of a hash table (where lost precision means more collisions). This is a little slower thanmix(int)
, and does almost exactly as well at avoiding collisions on most input, so mix() is preferred.- Parameters:
x
- an integer.- Returns:
- a hash value obtained by mixing the bits of
x
.
-
mix
Thoroughly mixes the bits of an integer.
This method acts likemixOriginal(int)
(which is used by Koloboke and Fastutil), but doesn't multiply by any too-large numbers to ease compatibility with GWT. Specifically, it multipliesn
by0x9E375
(found using the golden ratio times 2 to the 20, minus 2 to improve quality slightly) then xors that with itself unsigned-right-shifted by 16 before returning. It tends to have less pathologically-bad cases than using an unmixed integer in a hash table, but will still often have more collisions than an unmixed integer if that hash table is filled with numbers that vary in their lower bits. The value of this is that when ints are given that only differ in their upper bits, if you didn't mix a hash code you would have 95% or higher collision rates in some cases. This acts as a safeguard for that kind of scenario.
This replacesmixOther(int)
, which is also GWT-compatible but is a little slower without offering any improvement in collision rates.
This is used inIntDoubleOrderedMap
andIntIntOrderedMap
, at the least. The algorithm this uses is also used byCrossHash.defaultHasher
.- Parameters:
n
- an integer.- Returns:
- a hash value obtained by mixing the bits of
x
.
-
mix
Quickly mixes the bits of a long integer.
This method mixes the bits of the argument by multiplying by the golden ratio and xorshifting twice the result. It is borrowed from Koloboke, and it has slightly worse behaviour than MurmurHash3 (in open-addressing hash tables the average number of probes is slightly larger), but it's much faster.- Parameters:
x
- a long integer.- Returns:
- a hash value obtained by mixing the bits of
x
. - See Also:
invMix(long)
-
nextPowerOfTwo
Return the least power of two greater than or equal to the specified value.
Note that this function will return 1 when the argument is 0.
This is a cleaned-up Java version of this C code.- Parameters:
x
- a non-negative int.- Returns:
- the least power of two greater than or equal to the specified value.
-
nextPowerOfTwo
Return the least power of two greater than or equal to the specified value.
Note that this function will return 1 when the argument is 0.
This is a cleaned-up Java version of this C code.- Parameters:
x
- a non-negative long.- Returns:
- the least power of two greater than or equal to the specified value.
-