Package org.apache.datasketches
Enum 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
-
Enum Constant Summary
Enum Constants Enum Constant Description EQGiven 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.GEGiven 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.GTGiven 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.LEGiven 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.LTGiven 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 intfind(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 intfind(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 intfind(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 InequalitySearchvalueOf(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.
-
Enum Constant Details
-
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
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
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
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
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
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
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 nameNullPointerException- if the argument is null
-
find
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 rangehigh- the index of the highest value in the search rangev- 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
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 rangehigh- the index of the highest value in the search rangev- 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
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 rangehigh- the index of the highest value in the search rangev- 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
-