/** * Performs binary search of the lower bound in the given array. * The elements in the array are transformed before the comparison. * It is assumed that the array is sorted. * The method is guaranteed to return the minimal index of the element that is * greater or equal to the given key. * @param a array that holds the values * @param key element to look for * @param keySelector function that transforms array contents to the type * of the key * @param comparator comparator that compares keys * @param <T> the type of elements in array * @param <K> the type of lookup key * @return minimal index of the element that is * greater or equal to the given key. Returns -1 when all elements exceed * the given key or the array is empty. Returns {@code a.length} when all * elements are less than the given key. */ public static <T, K> int lowerBound(T[] a, K key, Function1<T, K> keySelector, Comparator<K> comparator) { return lowerBound(a, key, 0, a.length - 1, keySelector, comparator); }
/** * Performs binary search of the upper bound in the given array. * The elements in the array are transformed before the comparison. * It is assumed that the array is sorted. * The method is guaranteed to return the maximal index of the element that is * less or equal to the given key. * @param a array that holds the values * @param key element to look for * @param keySelector function that transforms array contents to the type * of the key * @param comparator comparator that compares keys * @param <T> the type of elements in array * @param <K> the type of lookup key * @return maximal index of the element that is * less or equal to the given key. Returns -1 when all elements are less * than the given key or the array is empty. Returns {@code a.length} when * all elements exceed the given key. */ public static <T, K> int upperBound(T[] a, K key, Function1<T, K> keySelector, Comparator<K> comparator) { return upperBound(a, key, 0, a.length - 1, keySelector, comparator); }
private void search(int key, int lower, int upper, Integer... array) { Assert.assertEquals( "lower bound of " + key + " in " + Arrays.toString(array), lower, BinarySearch.lowerBound(array, key, Ordering.natural())); Assert.assertEquals( "upper bound of " + key + " in " + Arrays.toString(array), upper, BinarySearch.upperBound(array, key, Ordering.natural())); }
private void search(int key, int lower, int upper, Integer... array) { Assert.assertEquals( "lower bound of " + key + " in " + Arrays.toString(array), lower, BinarySearch.lowerBound(array, key, Ordering.natural())); Assert.assertEquals( "upper bound of " + key + " in " + Arrays.toString(array), upper, BinarySearch.upperBound(array, key, Ordering.natural())); }
/** * Performs binary search of the upper bound in the given array. * The elements in the array are transformed before the comparison. * It is assumed that the array is sorted. * The method is guaranteed to return the maximal index of the element that is * less or equal to the given key. * @param a array that holds the values * @param key element to look for * @param keySelector function that transforms array contents to the type * of the key * @param comparator comparator that compares keys * @param <T> the type of elements in array * @param <K> the type of lookup key * @return maximal index of the element that is * less or equal to the given key. Returns -1 when all elements are less * than the given key or the array is empty. Returns {@code a.length} when * all elements exceed the given key. */ public static <T, K> int upperBound(T[] a, K key, Function1<T, K> keySelector, Comparator<K> comparator) { return upperBound(a, key, 0, a.length - 1, keySelector, comparator); }
/** * Performs binary search of the lower bound in the given array. * The elements in the array are transformed before the comparison. * It is assumed that the array is sorted. * The method is guaranteed to return the minimal index of the element that is * greater or equal to the given key. * @param a array that holds the values * @param key element to look for * @param keySelector function that transforms array contents to the type * of the key * @param comparator comparator that compares keys * @param <T> the type of elements in array * @param <K> the type of lookup key * @return minimal index of the element that is * greater or equal to the given key. Returns -1 when all elements exceed * the given key or the array is empty. Returns {@code a.length} when all * elements are less than the given key. */ public static <T, K> int lowerBound(T[] a, K key, Function1<T, K> keySelector, Comparator<K> comparator) { return lowerBound(a, key, 0, a.length - 1, keySelector, comparator); }
/** * Performs binary search of the upper bound in the given array. * It is assumed that the array is sorted. * The method is guaranteed to return the maximal index of the element that is * less or equal to the given key. * @param a array that holds the values * @param key element to look for * @param comparator comparator that compares keys * @param <T> the type of elements in array * @return maximal index of the element that is * less or equal to the given key. Returns -1 when all elements are less * than the given key or the array is empty. Returns {@code a.length} when * all elements exceed the given key. */ public static <T> int upperBound(T[] a, T key, Comparator<T> comparator) { return upperBound(a, key, 0, a.length - 1, Functions.identitySelector(), comparator); }
/** * Performs binary search of the lower bound in the given array. * It is assumed that the array is sorted. * The method is guaranteed to return the minimal index of the element that is * greater or equal to the given key. * @param a array that holds the values * @param key element to look for * @param comparator comparator that compares keys * @param <T> the type of elements in array * @return minimal index of the element that is * greater or equal to the given key. Returns -1 when all elements exceed * the given key or the array is empty. Returns {@code a.length} when all * elements are less than the given key. */ public static <T> int lowerBound(T[] a, T key, Comparator<T> comparator) { return lowerBound(a, key, 0, a.length - 1, Functions.identitySelector(), comparator); }
/** * Performs binary search of the upper bound in the given array. * It is assumed that the array is sorted. * The method is guaranteed to return the maximal index of the element that is * less or equal to the given key. * @param a array that holds the values * @param key element to look for * @param comparator comparator that compares keys * @param <T> the type of elements in array * @return maximal index of the element that is * less or equal to the given key. Returns -1 when all elements are less * than the given key or the array is empty. Returns {@code a.length} when * all elements exceed the given key. */ public static <T> int upperBound(T[] a, T key, Comparator<T> comparator) { return upperBound(a, key, 0, a.length - 1, Functions.identitySelector(), comparator); }
/** * Performs binary search of the lower bound in the given array. * It is assumed that the array is sorted. * The method is guaranteed to return the minimal index of the element that is * greater or equal to the given key. * @param a array that holds the values * @param key element to look for * @param comparator comparator that compares keys * @param <T> the type of elements in array * @return minimal index of the element that is * greater or equal to the given key. Returns -1 when all elements exceed * the given key or the array is empty. Returns {@code a.length} when all * elements are less than the given key. */ public static <T> int lowerBound(T[] a, T key, Comparator<T> comparator) { return lowerBound(a, key, 0, a.length - 1, Functions.identitySelector(), comparator); }
/** * Performs binary search of the upper bound in the given array. * It is assumed that the array is sorted. * The method is guaranteed to return the maximal index of the element that is * less or equal to the given key. * @param a array that holds the values * @param key element to look for * @param imin the minimal index (inclusive) to look for * @param imax the maximum index (inclusive) to look for * @param comparator comparator that compares keys * @param <T> the type of elements in array * @return maximal index of the element that is * less or equal to the given key. Returns -1 when all elements are less * than the given key or the array is empty. Returns {@code a.length} when * all elements exceed the given key. */ public static <T> int upperBound(T[] a, T key, int imin, int imax, Comparator<T> comparator) { return upperBound(a, key, imin, imax, Functions.identitySelector(), comparator); }
/** * Performs binary search of the lower bound in the given section of array. * It is assumed that the array is sorted. * The method is guaranteed to return the minimal index of the element that is * greater or equal to the given key. * @param a array that holds the values * @param key element to look for * @param imin the minimal index (inclusive) to look for * @param imax the maximum index (inclusive) to look for * @param comparator comparator that compares keys * @param <T> the type of elements in array * @return minimal index of the element that is * greater or equal to the given key. Returns -1 when all elements exceed * the given key or the array is empty. Returns {@code a.length} when all * elements are less than the given key. */ public static <T> int lowerBound(T[] a, T key, int imin, int imax, Comparator<T> comparator) { return lowerBound(a, key, imin, imax, Functions.identitySelector(), comparator); }
/** * Performs binary search of the upper bound in the given array. * It is assumed that the array is sorted. * The method is guaranteed to return the maximal index of the element that is * less or equal to the given key. * @param a array that holds the values * @param key element to look for * @param imin the minimal index (inclusive) to look for * @param imax the maximum index (inclusive) to look for * @param comparator comparator that compares keys * @param <T> the type of elements in array * @return maximal index of the element that is * less or equal to the given key. Returns -1 when all elements are less * than the given key or the array is empty. Returns {@code a.length} when * all elements exceed the given key. */ public static <T> int upperBound(T[] a, T key, int imin, int imax, Comparator<T> comparator) { return upperBound(a, key, imin, imax, Functions.identitySelector(), comparator); }
/** * Performs binary search of the lower bound in the given section of array. * It is assumed that the array is sorted. * The method is guaranteed to return the minimal index of the element that is * greater or equal to the given key. * @param a array that holds the values * @param key element to look for * @param imin the minimal index (inclusive) to look for * @param imax the maximum index (inclusive) to look for * @param comparator comparator that compares keys * @param <T> the type of elements in array * @return minimal index of the element that is * greater or equal to the given key. Returns -1 when all elements exceed * the given key or the array is empty. Returns {@code a.length} when all * elements are less than the given key. */ public static <T> int lowerBound(T[] a, T key, int imin, int imax, Comparator<T> comparator) { return lowerBound(a, key, imin, imax, Functions.identitySelector(), comparator); }