com.hazelcast.util

## Class HashUtil

- java.lang.Object
- - com.hazelcast.util.HashUtil

-     public final class HashUtil
      extends Object

  Utility methods related to hash tables.

- - ### Method Summary

    All Methods Static Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `static int` | `computePerturbationValue(int capacity)` Computes the key perturbation value applied before hashing.
    `static int` | `fastIntMix(int k)` Hash function based on Knuth's multiplicative method.
    `static long` | `fastLongMix(long k)` Hash function based on Knuth's multiplicative method.
    `static int` | `hashCode(Object... objects)` Hash code for multiple objects using `Arrays.hashCode(Object[])`.
    `static int` | `hashToIndex(int hash, int length)` A function that calculates the index (e.g.
    `static int` | `MurmurHash3_fmix(int k)`
    `static long` | `MurmurHash3_fmix(long k)`
    `static long` | `MurmurHash3_x64_64_direct(long base, int offset, int len)`
    `static long` | `MurmurHash3_x64_64_direct(com.hazelcast.internal.memory.MemoryAccessor mem, long base, int offset, int len)` Returns the `MurmurHash3_x64_64` hash of a memory block accessed by the provided `MemoryAccessor`.
    `static long` | `MurmurHash3_x64_64(byte[] data, int offset, int len)` Returns the MurmurHash3_x86_32 hash of a block inside a byte array.
    `static int` | `MurmurHash3_x86_32_direct(long base, int offset, int len)`
    `static int` | `MurmurHash3_x86_32_direct(com.hazelcast.internal.memory.MemoryAccessor mem, long base, int offset, int len)` Returns the `MurmurHash3_x86_32` hash of a memory block accessed by the provided `MemoryAccessor`.
    `static int` | `MurmurHash3_x86_32(byte[] data, int offset, int len)` Returns the MurmurHash3_x86_32 hash of a block inside a byte array.

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Method Detail

    - #### MurmurHash3_x86_32

          public static int MurmurHash3_x86_32(byte[] data,
                                               int offset,
                                               int len)

      Returns the MurmurHash3_x86_32 hash of a block inside a byte array.

    - #### MurmurHash3_x86_32_direct

          public static int MurmurHash3_x86_32_direct(long base,
                                                      int offset,
                                                      int len)

    - #### MurmurHash3_x86_32_direct

          public static int MurmurHash3_x86_32_direct(com.hazelcast.internal.memory.MemoryAccessor mem,
                                                      long base,
                                                      int offset,
                                                      int len)

      Returns the `MurmurHash3_x86_32` hash of a memory block accessed by the provided `MemoryAccessor`. The `MemoryAccessor` will be used to access `int`-sized data at addresses `(base + offset)`, `(base + offset + 4)`, etc. The caller must ensure that the `MemoryAccessor` supports it, especially when `(base + offset)` is not guaranteed to be 4 byte-aligned.

    - #### MurmurHash3_x64_64

          public static long MurmurHash3_x64_64(byte[] data,
                                                int offset,
                                                int len)

      Returns the MurmurHash3_x86_32 hash of a block inside a byte array.

    - #### MurmurHash3_x64_64_direct

          public static long MurmurHash3_x64_64_direct(long base,
                                                       int offset,
                                                       int len)

    - #### MurmurHash3_x64_64_direct

          public static long MurmurHash3_x64_64_direct(com.hazelcast.internal.memory.MemoryAccessor mem,
                                                       long base,
                                                       int offset,
                                                       int len)

      Returns the `MurmurHash3_x64_64` hash of a memory block accessed by the provided `MemoryAccessor`. The `MemoryAccessor` will be used to access `long`-sized data at addresses `(base + offset)`, `(base + offset + 8)`, etc. The caller must ensure that the `MemoryAccessor` supports it, especially when `(base + offset)` is not guaranteed to be 8 byte-aligned.

    - #### MurmurHash3_fmix

          public static int MurmurHash3_fmix(int k)

    - #### MurmurHash3_fmix

          public static long MurmurHash3_fmix(long k)

    - #### fastLongMix

          public static long fastLongMix(long k)

      Hash function based on Knuth's multiplicative method. This version is faster than using Murmur hash but provides acceptable behavior.
      Parameters:
        `k` - the long for which the hash will be calculated
      Returns:
        the hash

    - #### fastIntMix

          public static int fastIntMix(int k)

      Hash function based on Knuth's multiplicative method. This version is faster than using Murmur hash but provides acceptable behavior.
      Parameters:
        `k` - the integer for which the hash will be calculated
      Returns:
        the hash

    - #### hashCode

          public static int hashCode(Object... objects)

      Hash code for multiple objects using `Arrays.hashCode(Object[])`.

    - #### hashToIndex

          public static int hashToIndex(int hash,
                                        int length)

      A function that calculates the index (e.g. to be used in an array/list) for a given hash. The returned value will always be equal or larger than 0 and will always be smaller than 'length'. The reason this function exists is to deal correctly with negative and especially the Integer.MIN_VALUE; since that can't be used safely with a Math.abs function.
      Parameters:
        `length` - the length of the array/list
      Returns:
        the mod of the hash
      Throws:
        `IllegalArgumentException` - if mod smaller than 1.

    - #### computePerturbationValue

          public static int computePerturbationValue(int capacity)

      Computes the key perturbation value applied before hashing. The returned value should be non-zero and different for each capacity. This matters because keys are nearly ordered by hashcode so when adding one container's values to the other, the number of collisions can skyrocket into the worst case possible.

      If it is known that hash containers will not be added to each other (will be used for counting only, for example) then some speed can be gained by not perturbing keys before hashing and returning a value of zero for all possible capacities. The speed gain is a result of faster rehash operation (keys are mostly in order).

Copyright © 2019. All rights reserved.