Class MurmurHash3Adaptor


  • public final class MurmurHash3Adaptor
    extends Object
    A general purpose wrapper for the MurmurHash3.
    • Inputs can be long, long[], int[], char[], byte[], double or String.
    • Returns null if arrays or String is null or empty.
    • Provides methods for returning the 128-bit result as either an array of 2 longs or as a byte array of 16 bytes.
    • Provides modulo, asDouble and asInt functions.
    Author:
    Lee Rhodes
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static double asDouble​(long[] hash)
      Returns a uniform random double with a minimum inclusive value of zero and a maximum exclusive value of 1.0.
      static int asInt​(byte[] data, int n)
      Returns a deterministic uniform random integer between zero (inclusive) and n (exclusive) given the input data.
      static int asInt​(double datum, int n)
      Returns a deterministic uniform random integer between zero (inclusive) and n (exclusive) given the input double.
      static int asInt​(int[] data, int n)
      Returns a deterministic uniform random integer between zero (inclusive) and n (exclusive) given the input data.
      static int asInt​(long[] data, int n)
      Returns a deterministic uniform random integer between zero (inclusive) and n (exclusive) given the input data.
      static int asInt​(long datum, int n)
      Returns a deterministic uniform random integer between zero (inclusive) and n (exclusive) given the input datum.
      static int asInt​(String datum, int n)
      Returns a deterministic uniform random integer between zero (inclusive) and n (exclusive) given the input datum.
      static byte[] hashToBytes​(byte[] data, long seed)
      Hash a byte[] and long seed.
      static byte[] hashToBytes​(char[] data, long seed)
      Hash a char[] and long seed.
      static byte[] hashToBytes​(double datum, long seed)
      Hash a double and long seed.
      static byte[] hashToBytes​(int[] data, long seed)
      Hash an int[] and long seed.
      static byte[] hashToBytes​(long[] data, long seed)
      Hash a long[] and long seed.
      static byte[] hashToBytes​(long datum, long seed)
      Hash a long and long seed.
      static byte[] hashToBytes​(String datum, long seed)
      Hash a String and long seed.
      static long[] hashToLongs​(byte[] data, long seed)
      Hash a byte[] and long seed.
      static long[] hashToLongs​(char[] data, long seed)
      Hash a char[] and long seed.
      static long[] hashToLongs​(double datum, long seed)
      Hash a double and long seed.
      static long[] hashToLongs​(int[] data, long seed)
      Hash a int[] and long seed.
      static long[] hashToLongs​(long[] data, long seed)
      Hash a long[] and long seed.
      static long[] hashToLongs​(long datum, long seed)
      Hash a long and long seed.
      static long[] hashToLongs​(String datum, long seed)
      Hash a String and long seed.
      static int modulo​(long[] hash, int divisor)
      Returns the remainder from the modulo division of the 128-bit output of the murmurHash3 by the divisor.
      static int modulo​(long h0, long h1, int divisor)
      Returns the remainder from the modulo division of the 128-bit output of the murmurHash3 by the divisor.
    • Method Detail

      • hashToBytes

        public static byte[] hashToBytes​(long datum,
                                         long seed)
        Hash a long and long seed.
        Parameters:
        datum - the input long value
        seed - A long valued seed.
        Returns:
        The 128-bit hash as a byte[16] in Big Endian order from 2 64-bit longs.
      • hashToBytes

        public static byte[] hashToBytes​(long[] data,
                                         long seed)
        Hash a long[] and long seed.
        Parameters:
        data - the input long array
        seed - A long valued seed.
        Returns:
        The 128-bit hash as a byte[16] in Big Endian order from 2 64-bit longs.
      • hashToBytes

        public static byte[] hashToBytes​(int[] data,
                                         long seed)
        Hash an int[] and long seed.
        Parameters:
        data - the input int array
        seed - A long valued seed.
        Returns:
        The 128-bit hash as a byte[16] in Big Endian order from 2 64-bit longs.
      • hashToBytes

        public static byte[] hashToBytes​(char[] data,
                                         long seed)
        Hash a char[] and long seed.
        Parameters:
        data - the input char array
        seed - A long valued seed.
        Returns:
        The 128-bit hash as a byte[16] in Big Endian order from 2 64-bit longs.
      • hashToBytes

        public static byte[] hashToBytes​(byte[] data,
                                         long seed)
        Hash a byte[] and long seed.
        Parameters:
        data - the input byte array
        seed - A long valued seed.
        Returns:
        The 128-bit hash as a byte[16] in Big Endian order from 2 64-bit longs.
      • hashToBytes

        public static byte[] hashToBytes​(double datum,
                                         long seed)
        Hash a double and long seed.
        Parameters:
        datum - the input double
        seed - A long valued seed.
        Returns:
        The 128-bit hash as a byte[16] in Big Endian order from 2 64-bit longs.
      • hashToBytes

        public static byte[] hashToBytes​(String datum,
                                         long seed)
        Hash a String and long seed.
        Parameters:
        datum - the input String
        seed - A long valued seed.
        Returns:
        The 128-bit hash as a byte[16] in Big Endian order from 2 64-bit longs.
      • hashToLongs

        public static long[] hashToLongs​(long datum,
                                         long seed)
        Hash a long and long seed.
        Parameters:
        datum - the input long
        seed - A long valued seed.
        Returns:
        The 128-bit hash as a long[2].
      • hashToLongs

        public static long[] hashToLongs​(long[] data,
                                         long seed)
        Hash a long[] and long seed.
        Parameters:
        data - the input long array.
        seed - A long valued seed.
        Returns:
        The 128-bit hash as a long[2].
      • hashToLongs

        public static long[] hashToLongs​(int[] data,
                                         long seed)
        Hash a int[] and long seed.
        Parameters:
        data - the input int array.
        seed - A long valued seed.
        Returns:
        The 128-bit hash as a long[2].
      • hashToLongs

        public static long[] hashToLongs​(char[] data,
                                         long seed)
        Hash a char[] and long seed.
        Parameters:
        data - the input char array.
        seed - A long valued seed.
        Returns:
        The 128-bit hash as a long[2].
      • hashToLongs

        public static long[] hashToLongs​(byte[] data,
                                         long seed)
        Hash a byte[] and long seed.
        Parameters:
        data - the input byte array.
        seed - A long valued seed.
        Returns:
        The 128-bit hash as a long[2].
      • hashToLongs

        public static long[] hashToLongs​(double datum,
                                         long seed)
        Hash a double and long seed.
        Parameters:
        datum - the input double.
        seed - A long valued seed.
        Returns:
        The 128-bit hash as a long[2].
      • hashToLongs

        public static long[] hashToLongs​(String datum,
                                         long seed)
        Hash a String and long seed.
        Parameters:
        datum - the input String.
        seed - A long valued seed.
        Returns:
        The 128-bit hash as a long[2].
      • asInt

        public static int asInt​(long[] data,
                                int n)
        Returns a deterministic uniform random integer between zero (inclusive) and n (exclusive) given the input data.
        Parameters:
        data - the input long array.
        n - The upper exclusive bound of the integers produced. Must be > 1.
        Returns:
        deterministic uniform random integer
      • asInt

        public static int asInt​(int[] data,
                                int n)
        Returns a deterministic uniform random integer between zero (inclusive) and n (exclusive) given the input data.
        Parameters:
        data - the input int array.
        n - The upper exclusive bound of the integers produced. Must be > 1.
        Returns:
        deterministic uniform random integer
      • asInt

        public static int asInt​(byte[] data,
                                int n)
        Returns a deterministic uniform random integer between zero (inclusive) and n (exclusive) given the input data.
        Parameters:
        data - the input byte array.
        n - The upper exclusive bound of the integers produced. Must be > 1.
        Returns:
        deterministic uniform random integer.
      • asInt

        public static int asInt​(long datum,
                                int n)
        Returns a deterministic uniform random integer between zero (inclusive) and n (exclusive) given the input datum.
        Parameters:
        datum - the input long
        n - The upper exclusive bound of the integers produced. Must be > 1.
        Returns:
        deterministic uniform random integer
      • asInt

        public static int asInt​(double datum,
                                int n)
        Returns a deterministic uniform random integer between zero (inclusive) and n (exclusive) given the input double.
        Parameters:
        datum - the given double.
        n - The upper exclusive bound of the integers produced. Must be > 1.
        Returns:
        deterministic uniform random integer
      • asInt

        public static int asInt​(String datum,
                                int n)
        Returns a deterministic uniform random integer between zero (inclusive) and n (exclusive) given the input datum.
        Parameters:
        datum - the given String.
        n - The upper exclusive bound of the integers produced. Must be > 1.
        Returns:
        deterministic uniform random integer
      • asDouble

        public static double asDouble​(long[] hash)
        Returns a uniform random double with a minimum inclusive value of zero and a maximum exclusive value of 1.0.

        The double values produced are only as random as the MurmurHash3 algorithm, which may be adequate for many applications. However, if you are looking for high guarantees of randomness you should turn to more sophisticated random generators such as Mersenne Twister or Well algorithms.

        Parameters:
        hash - The output of the MurmurHash3.
        Returns:
        the uniform random double.
      • modulo

        public static int modulo​(long h0,
                                 long h1,
                                 int divisor)
        Returns the remainder from the modulo division of the 128-bit output of the murmurHash3 by the divisor.
        Parameters:
        h0 - The lower 64-bits of the 128-bit MurmurHash3 hash.
        h1 - The upper 64-bits of the 128-bit MurmurHash3 hash.
        divisor - Must be positive and greater than zero.
        Returns:
        the modulo result.
      • modulo

        public static int modulo​(long[] hash,
                                 int divisor)
        Returns the remainder from the modulo division of the 128-bit output of the murmurHash3 by the divisor.
        Parameters:
        hash - The size 2 long array from the MurmurHash3.
        divisor - Must be positive and greater than zero.
        Returns:
        the modulo result