java.lang.Object
io.smallrye.common.search.BinarySearch
A utility class which provides multiple variations on binary searching.
Ranges
The binary search algorithm operates over a range of values. The search will always return in logarithmic time with respect to the search interval. This implementation supports multiple kinds of value ranges:intvia theintRange()methodlongvia thelongRange()method- Object ranges via the
objRange()method
find() methods that perform variations on the binary search.
Range predicates
Ranges are searched by predicate. The predicate is evaluated for the lowest value within the range (that is, the value closest to thefrom argument) for which it returns true.
If no value satisfies the predicate, the highest value (that is, the value given for the to argument) is returned.
The value returned by each find() method is always within the range [from, to].
In order to be well-defined, the predicate must be continuous, which is to say that
given some value n,
the predicate must return false for all < n and true for all >= n
when from ≤ n ≤ to.
If this constraint does not hold, then the results of the search will not be well-defined.
The behavior of the predicate outside of this range does not affect the well-definedness of the search operation.
Inverted ranges
It is possible to search over an inverted range, i.e. a range for whichto is numerically lower than from.
While such searches are well-defined, it should be noted that the from bound
remains inclusive while the to bound remains exclusive,
which might be surprising in some circumstances.-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classSearch operations which apply over anintrange.static final classSearch operations which apply over anlongrange.static final classBinary searches over a range of objects. -
Method Summary
Modifier and TypeMethodDescriptionstatic <E> EgetFromArray(E[] array, int index) A getter function which can satisfyObjIntFunction<E[], E>for accessing values in an array.static BinarySearch.IntRangeintRange()Returns an object which can perform binary searches over an integer range (notnull).static BinarySearch.LongRangeReturns an object which can perform binary searches over a long integer range (notnull).static BinarySearch.ObjRangeobjRange()Returns an object which can perform binary searches over an object range (notnull).
-
Method Details
-
intRange
Returns an object which can perform binary searches over an integer range (notnull). The returned object operates on a signed range by default.- Returns:
- an object which can perform binary searches over an integer range (not
null) - See Also:
-
longRange
Returns an object which can perform binary searches over a long integer range (notnull). The returned object operates on a signed range by default.- Returns:
- an object which can perform binary searches over a long integer range (not
null) - See Also:
-
objRange
Returns an object which can perform binary searches over an object range (notnull).- Returns:
- an object which can perform binary searches over an object range (not
null)
-
getFromArray
public static <E> E getFromArray(E[] array, int index) A getter function which can satisfyObjIntFunction<E[], E>for accessing values in an array.- Type Parameters:
E- the array element type- Parameters:
array- the array (must not benull)index- the array index- Returns:
- the array value at the index
-