Enum InequalitySearch

java.lang.Object
java.lang.Enum<InequalitySearch>
org.apache.datasketches.InequalitySearch
All Implemented Interfaces:
Serializable, Comparable<InequalitySearch>, Constable

public enum InequalitySearch
extends Enum<InequalitySearch>
This provides efficient, unique and unambiguous binary searching for inequality comparison criteria for ordered arrays of values that may include duplicate values. The inequality criteria include <, ≤, ==, ≥, >. All the inequality criteria use the same search algorithm. (Although == is not an inequality, it is included for convenience.)

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. For all the search criteria, if the algorithm reaches the ends of the search range, the algorithm calls the resolve() method to determine what to return to the caller. If the key value cannot be resolved, it returns a -1 to the caller.

Given an array of values arr[] and the search key value v, the algorithms for the searching criteria are as follows:

  • LT: Find the highest ranked adjacent pair {A, B} such that:
    arr[A] < v ≤ arr[B]. The normal return is the index A.
  • LE: Find the highest ranked adjacent pair {A, B} such that:
    arr[A] ≤ v < arr[B]. The normal return is the index A.
  • EQ: Find the adjacent pair {A, B} such that:
    arr[A] ≤ v ≤ arr[B]. The normal return is the index A or B whichever equals v, otherwise it returns -1.
  • GE: Find the lowest ranked adjacent pair {A, B} such that:
    arr[A] < v ≤ arr[B]. The normal return is the index B.
  • GT: Find the lowest ranked adjacent pair {A, B} such that:
    arr[A] ≤ v < arr[B]. The normal return is the index B.
Author:
Lee Rhodes
  • Nested Class Summary

    Nested classes/interfaces inherited from class java.lang.Enum

    Enum.EnumDesc<E extends Enum<E>>
  • Enum Constant Summary

    Enum Constants 
    Enum Constant Description
    EQ
    Given a sorted array of increasing values arr[] and a key value V, this criterion instructs the binary search algorithm to find the adjacent pair of values {A,B} such that A ≤ V ≤ B.
    GE
    Given a sorted array of increasing values arr[] and a key value V, this criterion instructs the binary search algorithm to find the lowest adjacent pair of values {A,B} such that A < V ≤ B.
    GT
    Given a sorted array of increasing values arr[] and a key value V, this criterion instructs the binary search algorithm to find the lowest adjacent pair of values {A,B} such that A ≤ V < B.
    LE
    Given a sorted array of increasing values arr[] and a key value V, this criterion instructs the binary search algorithm to find the highest adjacent pair of values {A,B} such that A ≤ V < B.
    LT
    Given a sorted array of increasing values arr[] and a key value V, this criterion instructs the binary search algorithm to find the highest adjacent pair of values {A,B} such that A < V ≤ B.
  • Method Summary

    Modifier and Type Method Description
    static int find​(double[] arr, int low, int high, double v, InequalitySearch crit)
    Binary Search for the index of the double value in the given search range that satisfies the given InequalitySearch criterion.
    static int find​(float[] arr, int low, int high, float v, InequalitySearch crit)
    Binary Search for the index of the float value in the given search range that satisfies the given InequalitySearch criterion.
    static int find​(long[] arr, int low, int high, long v, InequalitySearch crit)
    Binary Search for the index of the long value in the given search range that satisfies the given InequalitySearch criterion.
    static InequalitySearch valueOf​(String name)
    Returns the enum constant of this type with the specified name.
    static InequalitySearch[] values()
    Returns an array containing the constants of this enum type, in the order they are declared.

    Methods inherited from class java.lang.Object

    getClass, notify, notifyAll, wait, wait, wait
  • Enum Constant Details

    • LT

      public static final InequalitySearch LT
      Given a sorted array of increasing values arr[] and a key value V, this criterion instructs the binary search algorithm to find the highest adjacent pair of values {A,B} such that A < V ≤ B. The returned value from the binary search algorithm will be the index of A or -1, if the value V ≤ the lowest value in the selected range of the array.
    • LE

      public static final InequalitySearch LE
      Given a sorted array of increasing values arr[] and a key value V, this criterion instructs the binary search algorithm to find the highest adjacent pair of values {A,B} such that A ≤ V < B. The returned value from the binary search algorithm will be the index of A or -1, if the value V < the lowest value in the selected range of the array.
    • EQ

      public static final InequalitySearch EQ
      Given a sorted array of increasing values arr[] and a key value V, this criterion instructs the binary search algorithm to find the adjacent pair of values {A,B} such that A ≤ V ≤ B. The returned value from the binary search algorithm will be the index of A or B, if one of them is equal to V, or -1 if V is not equal to either one.
    • GE

      public static final InequalitySearch GE
      Given a sorted array of increasing values arr[] and a key value V, this criterion instructs the binary search algorithm to find the lowest adjacent pair of values {A,B} such that A < V ≤ B. The returned value from the binary search algorithm will be the index of B or -1, if the value V > the highest value in the selected range of the array.
    • GT

      public static final InequalitySearch GT
      Given a sorted array of increasing values arr[] and a key value V, this criterion instructs the binary search algorithm to find the lowest adjacent pair of values {A,B} such that A ≤ V < B. The returned value from the binary search algorithm will be the index of B or -1, if the value V ≥ the highest value in the selected range of the array.
  • Method Details

    • values

      public static InequalitySearch[] values()
      Returns an array containing the constants of this enum type, in the order they are declared.
      Returns:
      an array containing the constants of this enum type, in the order they are declared
    • valueOf

      public static InequalitySearch valueOf​(String name)
      Returns the enum constant of this type with the specified name. The string must match exactly an identifier used to declare an enum constant in this type. (Extraneous whitespace characters are not permitted.)
      Parameters:
      name - the name of the enum constant to be returned.
      Returns:
      the enum constant with the specified name
      Throws:
      IllegalArgumentException - if this enum type has no constant with the specified name
      NullPointerException - if the argument is null
    • find

      public static int find​(double[] arr, int low, int high, double v, InequalitySearch crit)
      Binary Search for the index of the double value in the given search range that satisfies the given InequalitySearch criterion. If -1 is returned there are no values in the search range that satisfy the criterion.
      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.
      crit - one of LT, LE, EQ, GT, GE
      Returns:
      the index of the value in the given search range that satisfies the criterion
    • find

      public static int find​(float[] arr, int low, int high, float v, InequalitySearch crit)
      Binary Search for the index of the float value in the given search range that satisfies the given InequalitySearch criterion. If -1 is returned there are no values in the search range that satisfy the criterion.
      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.
      crit - one of LT, LE, EQ, GT, GE
      Returns:
      the index of the value in the given search range that satisfies the criterion
    • find

      public static int find​(long[] arr, int low, int high, long v, InequalitySearch crit)
      Binary Search for the index of the long value in the given search range that satisfies the given InequalitySearch criterion. If -1 is returned there are no values in the search range that satisfy the criterion.
      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.
      crit - one of LT, LE, EQ, GT, GE
      Returns:
      the index of the value in the given search range that satisfies the criterion