Class BinarySearch

java.lang.Object
org.monarchinitiative.lirical.core.util.BinarySearch

public class BinarySearch extends Object
Static utility class with the binary search implementation for arrays of items with custom key extractor function.
  • Method Details

    • binarySearch

      public static <T, U extends Comparable<U>> Optional<T> binarySearch(T[] haystack, Function<T,U> keyExtractor, U needle)
      Perform a binary search on an array of sorted BinarySearchs using the keyExtractor function for extracting the key for comparison.

      The array must be sorted by the keyExtractor function. Otherwise, the behavior is undefined.

      Type Parameters:
      T - type of the array items
      U - type of the comparison key
      Parameters:
      haystack - an array of items sorted by keyExtractor function.
      keyExtractor - a function for extracting a key with natural comparison order.
      needle - the item we are searching for.
      Returns:
      an Optional with the found item or an empty optional if the item is not present in the array.
    • binarySearch

      public static <T, U> Optional<T> binarySearch(T[] haystack, Function<T,U> keyExtractor, Comparator<? super U> comparator, U needle)
      Perform a binary search on an array of sorted BinarySearchs using the keyExtractor function for extracting the key for comparison.

      The array must be sorted by the keyExtractor and comparator functions. Otherwise, the behavior is undefined.

      Type Parameters:
      T - type of the array items
      U - type of the comparison key
      Parameters:
      haystack - an array of items sorted by keyExtractor function.
      keyExtractor - a function for extracting a key with natural comparison order.
      comparator - a function for comparing the key instances.
      needle - the item we are searching for.
      Returns:
      an Optional with the found item or an empty optional if the item is not present in the array.