Class MurmurHash3


  • public final class MurmurHash3
    extends java.lang.Object
    MurmurHash3 yields a 32-bit or 128-bit value. MurmurHash is a non-cryptographic hash function suitable for general hash-based lookup. The name comes from two basic operations, multiply (MU) and rotate (R), used in its inner loop. Unlike cryptographic hash functions, it is not specifically designed to be difficult to reverse by an adversary, making it unsuitable for cryptographic purposes. 32-bit Java port of https://code.google.com/p/smhasher/source/browse/trunk/MurmurHash3.cpp#94 128-bit Java port of https://code.google.com/p/smhasher/source/browse/trunk/MurmurHash3.cpp#255 This is a public domain code with no copyrights. From homepage of MurmurHash (https://code.google.com/p/smhasher/), "All MurmurHash versions are public domain software, and the author disclaims all copyright to their code." Copied from Apache Hive: https://github.com/apache/hive/blob/master/storage-api/src/java/org/apache/hive/common/util/Murmur3.java
    Since:
    1.13
    See Also:
    MurmurHash
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private static long C1  
      private static int C1_32  
      private static long C2  
      private static int C2_32  
      static int DEFAULT_SEED  
      (package private) static int INTEGER_BYTES
      TODO Replace on Java 8 with Integer.BYTES.
      (package private) static int LONG_BYTES
      TODO Replace on Java 8 with Long.BYTES.
      private static int M  
      private static int M_32  
      private static int N_32  
      private static int N1  
      private static int N2  
      static long NULL_HASHCODE  
      private static int R1  
      private static int R1_32  
      private static int R2  
      private static int R2_32  
      private static int R3  
      (package private) static int SHORT_BYTES
      TODO Replace on Java 8 with Short.BYTES.
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private MurmurHash3()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      private static int fmix32​(int length, int hash)  
      private static long fmix64​(long h)  
      static long[] hash128​(byte[] data)
      Murmur3 128-bit variant.
      static long[] hash128​(byte[] data, int offset, int length, int seed)
      Murmur3 128-bit variant.
      static long[] hash128​(java.lang.String data)
      Murmur3 128-bit variant.
      static int hash32​(byte[] data)
      Generates 32 bit hash from byte array with the default seed.
      static int hash32​(byte[] data, int length)
      Generates 32 bit hash from byte array with the default seed.
      static int hash32​(byte[] data, int length, int seed)
      Generates 32 bit hash from byte array with the given length and seed.
      static int hash32​(byte[] data, int offset, int length, int seed)
      Generates 32 bit hash from byte array with the given length, offset and seed.
      static int hash32​(long l0)
      Generates 32 bit hash from a long with default seed value.
      static int hash32​(long l0, int seed)
      Generates 32 bit hash from a long with the given seed.
      static int hash32​(long l0, long l1)
      Generates 32 bit hash from two longs with default seed value.
      static int hash32​(long l0, long l1, int seed)
      Generates 32 bit hash from two longs with the given seed.
      static int hash32​(java.lang.String data)
      Generates 32 bit hash from a string with the default seed.
      static long hash64​(byte[] data)
      Murmur3 64-bit variant.
      static long hash64​(byte[] data, int offset, int length)
      Generates 64 bit hash from byte array with the given length, offset and default seed.
      static long hash64​(byte[] data, int offset, int length, int seed)
      Generates 64 bit hash from byte array with the given length, offset and seed.
      static long hash64​(int data)
      Murmur3 64-bit variant.
      static long hash64​(long data)
      Murmur3 64-bit variant.
      static long hash64​(short data)
      Murmur3 64-bit variant.
      private static int mix32​(int k, int hash)  
      private static int orBytes​(byte b1, byte b2, byte b3, byte b4)  
      • Methods inherited from class java.lang.Object

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

      • MurmurHash3

        private MurmurHash3()
    • Method Detail

      • hash32

        public static int hash32​(long l0,
                                 long l1)
        Generates 32 bit hash from two longs with default seed value.
        Parameters:
        l0 - long to hash
        l1 - long to hash
        Returns:
        32 bit hash
      • hash32

        public static int hash32​(long l0)
        Generates 32 bit hash from a long with default seed value.
        Parameters:
        l0 - long to hash
        Returns:
        32 bit hash
      • hash32

        public static int hash32​(long l0,
                                 int seed)
        Generates 32 bit hash from a long with the given seed.
        Parameters:
        l0 - long to hash
        seed - initial seed value
        Returns:
        32 bit hash
      • hash32

        public static int hash32​(long l0,
                                 long l1,
                                 int seed)
        Generates 32 bit hash from two longs with the given seed.
        Parameters:
        l0 - long to hash
        l1 - long to hash
        seed - initial seed value
        Returns:
        32 bit hash
      • hash32

        public static int hash32​(byte[] data)
        Generates 32 bit hash from byte array with the default seed.
        Parameters:
        data - - input byte array
        Returns:
        32 bit hash
      • hash32

        public static int hash32​(java.lang.String data)
        Generates 32 bit hash from a string with the default seed.
        Parameters:
        data - - input string
        Returns:
        32 bit hash
      • hash32

        public static int hash32​(byte[] data,
                                 int length)
        Generates 32 bit hash from byte array with the default seed.
        Parameters:
        data - - input byte array
        length - - length of array
        Returns:
        32 bit hash
      • hash32

        public static int hash32​(byte[] data,
                                 int length,
                                 int seed)
        Generates 32 bit hash from byte array with the given length and seed.
        Parameters:
        data - - input byte array
        length - - length of array
        seed - - seed. (default 0)
        Returns:
        32 bit hash
      • hash32

        public static int hash32​(byte[] data,
                                 int offset,
                                 int length,
                                 int seed)
        Generates 32 bit hash from byte array with the given length, offset and seed.
        Parameters:
        data - - input byte array
        offset - - offset of data
        length - - length of array
        seed - - seed. (default 0)
        Returns:
        32 bit hash
      • hash64

        public static long hash64​(byte[] data)
        Murmur3 64-bit variant. This is essentially MSB 8 bytes of Murmur3 128-bit variant.
        Parameters:
        data - - input byte array
        Returns:
        64 bit hash
      • hash64

        public static long hash64​(long data)
        Murmur3 64-bit variant. This is essentially MSB 8 bytes of Murmur3 128-bit variant.
        Parameters:
        data - - input long
        Returns:
        64 bit hash
      • hash64

        public static long hash64​(int data)
        Murmur3 64-bit variant. This is essentially MSB 8 bytes of Murmur3 128-bit variant.
        Parameters:
        data - - input int
        Returns:
        64 bit hash
      • hash64

        public static long hash64​(short data)
        Murmur3 64-bit variant. This is essentially MSB 8 bytes of Murmur3 128-bit variant.
        Parameters:
        data - - input short
        Returns:
        64 bit hash
      • hash64

        public static long hash64​(byte[] data,
                                  int offset,
                                  int length)
        Generates 64 bit hash from byte array with the given length, offset and default seed.
        Parameters:
        data - - input byte array
        offset - - offset of data
        length - - length of array
        Returns:
        64 bit hash
      • hash64

        public static long hash64​(byte[] data,
                                  int offset,
                                  int length,
                                  int seed)
        Generates 64 bit hash from byte array with the given length, offset and seed.
        Parameters:
        data - - input byte array
        offset - - offset of data
        length - - length of array
        seed - - seed. (default 0)
        Returns:
        64 bit hash
      • hash128

        public static long[] hash128​(byte[] data)
        Murmur3 128-bit variant.
        Parameters:
        data - - input byte array
        Returns:
        - 128 bit hash (2 longs)
      • hash128

        public static long[] hash128​(java.lang.String data)
        Murmur3 128-bit variant.
        Parameters:
        data - - input String
        Returns:
        - 128 bit hash (2 longs)
      • hash128

        public static long[] hash128​(byte[] data,
                                     int offset,
                                     int length,
                                     int seed)
        Murmur3 128-bit variant.
        Parameters:
        data - - input byte array
        offset - - the first element of array
        length - - length of array
        seed - - seed. (default is 0)
        Returns:
        - 128 bit hash (2 longs)
      • mix32

        private static int mix32​(int k,
                                 int hash)
      • fmix32

        private static int fmix32​(int length,
                                  int hash)
      • fmix64

        private static long fmix64​(long h)
      • orBytes

        private static int orBytes​(byte b1,
                                   byte b2,
                                   byte b3,
                                   byte b4)