Class GenericInequalitySearch

java.lang.Object
org.apache.datasketches.GenericInequalitySearch

public class GenericInequalitySearch
extends Object
This provides efficient, unique and unambiguous binary searching for inequalities for ordered arrays of values that may include duplicate values. These inequalities include <, ≤, ==, ≥, >. The same search method can be used for all these inequalities.

In order to make the searching unique and unambiguous, we modified the traditional binary search algorithm to search for adjacent pairs of values {A, B} in the values array instead of just a single value, where A and B are the array indicies of two adjacent values in the array. We then define the searching criteria, given an array of values arr[] and the search key value v, as follows:

  • LT: Find the highest ranked adjacent pair {A, B} such that:
    arr[A] < v ≤ arr[B]. Normally we return the index A. However if the search algorithm reaches the ends of the search range, the search algorithm calls the resolve() method to determine what to return to the caller.
  • LE: Find the highest ranked adjacent pair {A, B} such that:
    arr[A] ≤ v < arr[B]. Normally we return the index A. However if the search algorithm reaches the ends of the search range, the search algorithm calls the resolve() method to determine what to return to the caller.
  • EQ: Find the adjacent pair {A, B} such that:
    arr[A] ≤ v < arr[B]. We return the index A or B whichever equals v, otherwise we return -1.
  • GE: Find the lowest ranked adjacent pair {A, B} such that:
    arr[A] < v ≤ arr[B]. Normally we return the index B. However if the search algorithm reaches the ends of the search range, the search algorithm calls the resolve() method to determine what to return to the caller.
  • GT: Find the lowest ranked adjacent pair {A, B} such that:
    arr[A] ≤ v < arr[B]. Normally we return the index B. However if the search algorithm reaches the ends of the search range, the search algorithm calls the resolve() method to determine what to return to the caller.
Author:
Lee Rhodes
  • Constructor Details

  • Method Details

    • find

      public static <T> int find​(T[] arr, int low, int high, T v, GenericInequalitySearch.Inequality inequality, Comparator<T> comparator)
      Binary Search for the index of the generic value in the given search range that satisfies the given inequality. If -1 is returned there are no values in the search range that satisfy the inequality.
      Type Parameters:
      T - The generic type of value to be used in the search process.
      Parameters:
      arr - the given array that must be sorted.
      low - the index of the lowest value in the search range
      high - the index of the highest value in the search range
      v - the value to search for.
      inequality - one of LT, LE, EQ, GE, GT
      comparator - for the type T
      Returns:
      the index of the value in the given search range that satisfies the inequality.