Class BinarySearch
java.lang.Object
org.monarchinitiative.lirical.core.util.BinarySearch
Static utility class with the binary search implementation for arrays of items with custom key extractor function.
-
Method Summary
Modifier and TypeMethodDescriptionstatic <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 sortedBinarySearch
s using thekeyExtractor
function for extracting the key for comparison.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 sortedBinarySearch
s using thekeyExtractor
function for extracting the key for comparison.
-
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 sortedBinarySearch
s using thekeyExtractor
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 itemsU
- type of the comparison key- Parameters:
haystack
- an array of items sorted bykeyExtractor
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 sortedBinarySearch
s using thekeyExtractor
function for extracting the key for comparison.The array must be sorted by the
keyExtractor
andcomparator
functions. Otherwise, the behavior is undefined.- Type Parameters:
T
- type of the array itemsU
- type of the comparison key- Parameters:
haystack
- an array of items sorted bykeyExtractor
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.
-