UNPKG

@jcoreio/precise-bs

Version:

binary search and related utility functions

214 lines (213 loc) 13 kB
export type Comparator<T> = (a: T, b: T) => number; export declare const DEFAULT_COMPARATOR: Comparator<any>; type AnyArray<T = any> = { [Index in number]: T; } & { length: number; }; export declare function binarySearch<Arr extends AnyArray>(array: Arr, find: Arr[number]): number; export declare function binarySearch<Arr extends AnyArray, Find>(array: Arr, find: Find, comparator: Comparator<Arr[number] | Find>, low?: number, high?: number): number; /** * Finds the index of the greatest element less than a given search value in a sorted array * using a binary search. If the given search value is less than or equal to all elements in the * array, returns -1. * * @param {array} the array to search in. * @param {find} the value to search for. * @param {comparator} two-argument function that returns &lt; 0 if * the first argument is less than the second; &gt; 0 if the first * argument is greater; and 0 otherwise. (defaults to <code>a - b</code>) * @param {low} the lower bound of the range in <code>array</code> to * search in (defaults to 0) * @param {high} the upper bound of the range in <code>array</code> to * search in (defaults to <code>array.length - 1</code>) */ export declare function lowerIndex<Arr extends AnyArray>(array: Arr, find: Arr[number]): number; export declare function lowerIndex<Arr extends AnyArray, Find>(array: Arr, find: Find, comparator: Comparator<Arr[number] | Find>, low?: number, high?: number): number; /** * Finds the entry with the greatest element less than a given search value in a sorted array * using a binary search. If the search value is less than or equal to all elements in the * array, returns [-1, undefined]. * * @param {array} the array to search in. * @param {find} the value to search for. * @param {comparator} two-argument function that returns &lt; 0 if * the first argument is less than the second; &gt; 0 if the first * argument is greater; and 0 otherwise. (defaults to <code>a - b</code>) * @param {low} the lower bound of the range in <code>array</code> to * search in (defaults to 0) * @param {high} the upper bound of the range in <code>array</code> to * search in (defaults to <code>array.length - 1</code>) */ export declare function lowerEntry<Arr extends AnyArray>(array: Arr, find: Arr[number]): [number, Arr[number] | undefined]; export declare function lowerEntry<Arr extends AnyArray, Find>(array: Arr, find: Find, comparator: Comparator<Arr[number] | Find>, low?: number, high?: number): [number, Arr[number] | undefined]; /** * Finds the greatest element less than a given value in a sorted array * using a binary search. If the given value is less than or equal to all elements in the * array, returns undefined. * * @param {array} the array to search in. * @param {find} the value to search for. * @param {comparator} two-argument function that returns &lt; 0 if * the first argument is less than the second; &gt; 0 if the first * argument is greater; and 0 otherwise. (defaults to <code>a - b</code>) * @param {low} the lower bound of the range in <code>array</code> to * search in (defaults to 0) * @param {high} the upper bound of the range in <code>array</code> to * search in (defaults to <code>array.length - 1</code>) */ export declare function lowerValue<Arr extends AnyArray>(array: Arr, find: Arr[number]): Arr[number] | undefined; export declare function lowerValue<Arr extends AnyArray, Elem>(array: Arr, find: Elem, comparator: Comparator<Arr[number] | Elem>, low?: number, high?: number): Arr[number] | undefined; /** * Finds the index of the greatest element less than or equal to a given search value in a sorted array * using a binary search. If the given value is less than all elements in the * array, returns -1. * * @param {array} the array to search in. * @param {find} the value to search for. * @param {comparator} two-argument function that returns &lt; 0 if * the first argument is less than the second; &gt; 0 if the first * argument is greater; and 0 otherwise. (defaults to <code>a - b</code>) * @param {low} the lower bound of the range in <code>array</code> to * search in (defaults to 0) * @param {high} the upper bound of the range in <code>array</code> to * search in (defaults to <code>array.length - 1</code>) */ export declare function floorIndex<Arr extends AnyArray>(array: Arr, find: Arr[number]): number; export declare function floorIndex<Arr extends AnyArray, Elem>(array: Arr, find: Elem, comparator: Comparator<Arr[number] | Elem>, low?: number, high?: number): number; /** * Finds the entry with the greatest element less than or equal to a given search value in a sorted array * using a binary search. If the given value is less than all elements in the * array, returns [-1, undefined]. * * @param {array} the array to search in. * @param {find} the value to search for. * @param {comparator} two-argument function that returns &lt; 0 if * the first argument is less than the second; &gt; 0 if the first * argument is greater; and 0 otherwise. (defaults to <code>a - b</code>) * @param {low} the lower bound of the range in <code>array</code> to * search in (defaults to 0) * @param {high} the upper bound of the range in <code>array</code> to * search in (defaults to <code>array.length - 1</code>) */ export declare function floorEntry<Arr extends AnyArray>(array: Arr, find: Arr[number]): [number, Arr[number] | undefined]; export declare function floorEntry<Arr extends AnyArray, Elem>(array: Arr, find: Elem, comparator: Comparator<Arr[number] | Elem>, low?: number, high?: number): [number, Arr[number] | undefined]; /** * Finds the greatest element less than or equal to a given search value in a sorted array * using a binary search. If the given value is less than all elements in the * array, returns undefined. * * @param {array} the array to search in. * @param {find} the value to search for. * @param {comparator} two-argument function that returns &lt; 0 if * the first argument is less than the second; &gt; 0 if the first * argument is greater; and 0 otherwise. (defaults to <code>a - b</code>) * @param {low} the lower bound of the range in <code>array</code> to * search in (defaults to 0) * @param {high} the upper bound of the range in <code>array</code> to * search in (defaults to <code>array.length - 1</code>) */ export declare function floorValue<Arr extends AnyArray>(array: Arr, find: Arr[number]): Arr[number] | undefined; export declare function floorValue<Arr extends AnyArray, Elem>(array: Arr, find: Elem, comparator: Comparator<Arr[number] | Elem>, low?: number, high?: number): Arr[number] | undefined; /** * Finds the index of the least element greater than or equal to a given search value in a sorted array * using a binary search. If the given value is greater than all elements in the * array, returns the length of the array. * * @param {array} the array to search in. * @param {find} the value to search for. * @param {comparator} two-argument function that returns &lt; 0 if * the first argument is less than the second; &gt; 0 if the first * argument is greater; and 0 otherwise. (defaults to <code>a - b</code>) * @param {low} the lower bound of the range in <code>array</code> to * search in (defaults to 0) * @param {high} the upper bound of the range in <code>array</code> to * search in (defaults to <code>array.length - 1</code>) */ export declare const ceilingIndex: typeof binarySearch; /** * Finds the entry with the least element greater than or equal to a given search value in a sorted array * using a binary search. If the given value is greater than all elements in the * array, returns [array.length, undefined]. * * @param {array} the array to search in. * @param {find} the value to search for. * @param {comparator} two-argument function that returns &lt; 0 if * the first argument is less than the second; &gt; 0 if the first * argument is greater; and 0 otherwise. (defaults to <code>a - b</code>) * @param {low} the lower bound of the range in <code>array</code> to * search in (defaults to 0) * @param {high} the upper bound of the range in <code>array</code> to * search in (defaults to <code>array.length - 1</code>) */ export declare function ceilingEntry<Arr extends AnyArray>(array: Arr, find: Arr[number]): [number, Arr[number] | undefined]; export declare function ceilingEntry<Arr extends AnyArray, Elem>(array: Arr, find: Elem, comparator: Comparator<Arr[number] | Elem>, low?: number, high?: number): [number, Arr[number] | undefined]; /** * Finds the least element greater than or equal to a given search value in a sorted array * using a binary search. If the given value is greater than all elements in the * array, returns undefined. * * @param {array} the array to search in. * @param {find} the value to search for. * @param {comparator} two-argument function that returns &lt; 0 if * the first argument is less than the second; &gt; 0 if the first * argument is greater; and 0 otherwise. (defaults to <code>a - b</code>) * @param {low} the lower bound of the range in <code>array</code> to * search in (defaults to 0) * @param {high} the upper bound of the range in <code>array</code> to * search in (defaults to <code>array.length - 1</code>) */ export declare function ceilingValue<Arr extends AnyArray>(array: Arr, find: Arr[number]): Arr[number] | undefined; export declare function ceilingValue<Arr extends AnyArray, Elem>(array: Arr, find: Elem, comparator: Comparator<Arr[number] | Elem>, low?: number, high?: number): Arr[number] | undefined; /** * Finds the index of the least element greater than a given value in a sorted array * using a binary search. If the given value is greater than or equal to all elements in the * array, returns the length of the array. * * @param {array} the array to search in. * @param {find} the value to search for. * @param {comparator} two-argument function that returns &lt; 0 if * the first argument is less than the second; &gt; 0 if the first * argument is greater; and 0 otherwise. (defaults to <code>a - b</code>) * @param {low} the lower bound of the range in <code>array</code> to * search in (defaults to 0) * @param {high} the upper bound of the range in <code>array</code> to * search in (defaults to <code>array.length - 1</code>) */ export declare function higherIndex<Arr extends AnyArray>(array: Arr, find: Arr[number]): number; export declare function higherIndex<Arr extends AnyArray, Elem>(array: Arr, find: Elem, comparator: Comparator<Arr[number] | Elem>, low?: number, high?: number): number; /** * Finds the entry with the least element greater than a given value in a sorted array * using a binary search. If the given value is greater than or equal to all elements in the * array, returns [array.length, undefined]. * * @param {array} the array to search in. * @param {find} the value to search for. * @param {comparator} two-argument function that returns &lt; 0 if * the first argument is less than the second; &gt; 0 if the first * argument is greater; and 0 otherwise. (defaults to <code>a - b</code>) * @param {low} the lower bound of the range in <code>array</code> to * search in (defaults to 0) * @param {high} the upper bound of the range in <code>array</code> to * search in (defaults to <code>array.length - 1</code>) */ export declare function higherEntry<Arr extends AnyArray>(array: Arr, find: Arr[number]): [number, Arr[number] | undefined]; export declare function higherEntry<Arr extends AnyArray, Elem>(array: Arr, find: Elem, comparator: Comparator<Arr[number] | Elem>, low?: number, high?: number): [number, Arr[number] | undefined]; /** * Finds the least element greater than a given value in a sorted array * using a binary search. If the given value is greater than or equal to all elements in the * array, returns undefined. * * @param {array} the array to search in. * @param {find} the value to search for. * @param {comparator} two-argument function that returns &lt; 0 if * the first argument is less than the second; &gt; 0 if the first * argument is greater; and 0 otherwise. (defaults to <code>a - b</code>) * @param {low} the lower bound of the range in <code>array</code> to * search in (defaults to 0) * @param {high} the upper bound of the range in <code>array</code> to * search in (defaults to <code>array.length - 1</code>) */ export declare function higherValue<Arr extends AnyArray>(array: Arr, find: Arr[number]): Arr[number] | undefined; export declare function higherValue<Arr extends AnyArray, Elem>(array: Arr, find: Elem, comparator: Comparator<Arr[number] | Elem>, low?: number, high?: number): Arr[number] | undefined; export {};