Module hash4j

Interface ElementHashProvider


public interface ElementHashProvider
An element hash provider.
  • Method Details

    • getElementHash

      long getElementHash(int elementIndex)
      Returns a 64-bit hash value of the element with given index.

      The function must be defined for all nonnegative indices less than getNumberOfElements(). Multiple calls with the same element index must always return the same hash value. The hash values must have a high quality, meaning that they can be considered as uniformly distributed and mutually independent in practice.

      Parameters:
      elementIndex - the element index
      Returns:
      the hash value
    • getNumberOfElements

      int getNumberOfElements()
      Returns the number of elements.

      The number of elements must be positive.

      Returns:
      the number of elements
    • ofValues

      static ElementHashProvider ofValues(long... elementHashes)
      Creates an ElementHashProvider given a non-empty list of element hashes.
      Parameters:
      elementHashes - list of element hashes
      Returns:
      an element hash provider
      Throws:
      IllegalArgumentException - if the list of element hashes is empty
    • ofFunction

      static ElementHashProvider ofFunction(IntToLongFunction elementIndexToHash, int numberOfElements)
      Creates an ElementHashProvider given a function that maps an element index to its corresponding hash value for a given number of elements.

      The function must always return the same hash value for the same element index and must be defined for all nonnegative indices smaller than the given number of elements.

      Parameters:
      elementIndexToHash - function that maps an element index to its corresponding hash value
      numberOfElements - the number of elements
      Returns:
      an element hash provider
      Throws:
      IllegalArgumentException - if the provided function is null or the number of elements is not positive.
    • ofCollection

      static <T> ElementHashProvider ofCollection(Collection<T> collection, ToLongFunction<? super T> elementHashFunction)
      Creates an ElementHashProvider given a collection of elements and a function that maps an element to a 64-bit hash value.
      Type Parameters:
      T - the element type
      Parameters:
      collection - a collection of elements
      elementHashFunction - a function that maps an element to a 64-bit hash value
      Returns:
      an element hash provider
      Throws:
      IllegalArgumentException - if any argument is null or the collection is empty