Class HashMath


  • public final class HashMath
    extends java.lang.Object
    Routines which are useful for hashcode computation, among other things.
    Author:
    David M. Lloyd
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static int multiHashOrdered​(int accumulatedHash, int nextHash)
      A hash function which combines an accumulated hash with a next hash such that f(f(k, b), a) ≠ₙ f(f(k, a), b).
      static int multiHashOrdered​(int accumulatedHash, int prime, int nextHash)
      A hash function which combines an accumulated hash with a next hash such that f(f(k, p2, b), p1, a) ≠ₙ f(f(k, p1, a), p2, b).
      static int multiHashUnordered​(int accumulatedHash, int nextHash)
      A hash function which combines an accumulated hash with a next hash such that f(f(k, b), a) = f(f(k, a), b).
      static int multiHashUnordered​(int accumulatedHash, int prime, int nextHash)
      A hash function which combines an accumulated hash with a next hash such that f(f(k, p2, b), p1, a) = f(f(k, p1, a), p2, b).
      static int multiplyWrap​(int a, int b)
      Multiply two unsigned integers together.
      static int roundToPowerOfTwo​(int value)
      Round the given value up to the next positive power of two.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • roundToPowerOfTwo

        public static int roundToPowerOfTwo​(int value)
        Round the given value up to the next positive power of two.
        Parameters:
        value - the value (must not be negative and must be less than or equal to 2^31)
        Returns:
        the rounded power of two value
      • multiHashOrdered

        public static int multiHashOrdered​(int accumulatedHash,
                                           int prime,
                                           int nextHash)
        A hash function which combines an accumulated hash with a next hash such that f(f(k, p2, b), p1, a) ≠ₙ f(f(k, p1, a), p2, b). This function is suitable for object chains whose order affects the overall equality of the hash code.

        The exact algorithm is not specified and is therefore subject to change and should not be relied upon for hash codes that persist outside of the JVM process.

        Parameters:
        accumulatedHash - the accumulated hash code of the previous stage
        prime - a prime multiplier
        nextHash - the hash code of the next single item
        Returns:
        the new accumulated hash code
      • multiHashUnordered

        public static int multiHashUnordered​(int accumulatedHash,
                                             int prime,
                                             int nextHash)
        A hash function which combines an accumulated hash with a next hash such that f(f(k, p2, b), p1, a) = f(f(k, p1, a), p2, b). This function is suitable for object chains whose order does not affect the overall equality of the hash code.

        The exact algorithm is not specified and is therefore subject to change and should not be relied upon for hash codes that persist outside of the JVM process.

        Parameters:
        accumulatedHash - the accumulated hash code of the previous stage
        prime - a prime multiplier
        nextHash - the hash code of the next single item
        Returns:
        the new accumulated hash code
      • multiHashOrdered

        public static int multiHashOrdered​(int accumulatedHash,
                                           int nextHash)
        A hash function which combines an accumulated hash with a next hash such that f(f(k, b), a) ≠ₙ f(f(k, a), b). This function is suitable for object chains whose order affects the overall equality of the hash code.

        The exact algorithm is not specified and is therefore subject to change and should not be relied upon for hash codes that persist outside of the JVM process.

        Parameters:
        accumulatedHash - the accumulated hash code of the previous stage
        nextHash - the hash code of the next single item
        Returns:
        the new accumulated hash code
      • multiHashUnordered

        public static int multiHashUnordered​(int accumulatedHash,
                                             int nextHash)
        A hash function which combines an accumulated hash with a next hash such that f(f(k, b), a) = f(f(k, a), b). This function is suitable for object chains whose order does not affect the overall equality of the hash code.

        The exact algorithm is not specified and is therefore subject to change and should not be relied upon for hash codes that persist outside of the JVM process.

        Parameters:
        accumulatedHash - the accumulated hash code of the previous stage
        nextHash - the hash code of the next single item
        Returns:
        the new accumulated hash code
      • multiplyWrap

        public static int multiplyWrap​(int a,
                                       int b)
        Multiply two unsigned integers together. If the result overflows a 32-bit number, XOR the overflowed bits back into the result. This operation is commutative, i.e. if we designate the symbol to represent this operation, then a ⨰ b = b ⨰ a. This operation is not associative, i.e. (a ⨰ b) ⨰ c ≠ₙ a ⨰ (b ⨰ c) (the symbol ≠ₙ meaning "not necessarily equal to"), therefore this operation is suitable for ordered combinatorial hash functions.
        Parameters:
        a - the first number to multiply
        b - the second number to multiply
        Returns:
        the wrapped multiply result