Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add inline documentation #11

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 78 additions & 6 deletions search-bounds.d.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,83 @@
declare module 'binary-search-bounds' {

/**
* Custom comparison function
*
* @param {*} a
* @param {*} b
* @returns {number} A number
*
* - negative if `a` precedes `b`
* - positive if `b` precedes `a`
* - 0 if there if `a` may be both before or after `b`
*
* A consistent ordering function must be anticommutative,
* in the sense that `c(a,b) < 0` if and only if `c(b,a) > 0`.
*
*/
interface CompareFunc<T>{
(a: T, b: T): number
}

interface BSearch {
gt<T>(array:T[], y:T, compare?:((a:T, b:T) => number | null | undefined), lo?:number, hi?:number);
ge<T>(array:T[], y:T, compare?:((a:T, b:T) => number | null | undefined), lo?:number, hi?:number);
lt<T>(array:T[], y:T, compare?:((a:T, b:T) => number | null | undefined), lo?:number, hi?:number);
le<T>(array:T[], y:T, compare?:((a:T, b:T) => number | null | undefined), lo?:number, hi?:number);
eq<T>(array:T[], y:T, compare?:((a:T, b:T) => number | null | undefined), lo?:number, hi?:number);
/**
* @param {Array<*>} array
* @param {*} y
* @param {[function]} compare
* @param {number} [lo= 0] Lower bound of the search interval
* @param {number} [hi= array.length-1] upper bound for the search interval
* @returns {number} the last index `lo <= i <= hi`, such that the array such that
* `array[i]` precedes `y`, or `lo - 1` if no such element exists in the specified
* slice.
*/
gt<T>(array:T[], y:T, compare?:CompareFunc<T>, lo?:number, hi?:number): number;

/**
* @param {Array<*>} array
* @param {*} y
* @param {[function]} compare
* @param {number} [lo= 0] Lower bound of the search interval
* @param {number} [hi= array.length-1] upper bound for the search interval
* @returns {number} the first index `lo <= i <= hi`, such that
* `y` does not precede `array[i]`, or `hi + 1` if no such element exists in the
* specified slice.
*/
ge<T>(array:T[], y:T, compare?:CompareFunc<T>, lo?:number, hi?:number): number;

/**
* @param {Array<*>} array
* @param {*} y
* @param {[function]} compare
* @param {number} [lo= 0] Lower bound of the search interval
* @param {number} [hi= array.length-1] upper bound for the search interval
* @returns {number} the last index `lo <= i <= hi`, such that
* `array[i]` precedes `y`, or `lo - 1` if no such element exists in the
* specified slice.
*/
lt<T>(array:T[], y:T, compare?:CompareFunc<T>, lo?:number, hi?:number): number;
/**
* @param {Array<*>} array
* @param {*} y
* @param {[function]} compare
* @param {number} [lo= 0] Lower bound of the search interval
* @param {number} [hi= array.length-1] upper bound for the search interval
* @returns {number} the last index `lo <= i <= hi`, such that
* `y` does not precede `array[i]`, or `lo - 1` if no such element exists in the
* specified slice.
*/
le<T>(array:T[], y:T, compare?:CompareFunc<T>, lo?:number, hi?:number): number;
/**
* @param {Array<*>} array
* @param {*} y
* @param {[function]} compare
* @param {number} [lo= 0] Lower bound of the search interval
* @param {number} [hi= array.length-1] upper bound for the search interval
* @returns {number} the last index `lo <= i <= hi`, such that
* `y` does not precede `array[i]`, and `array[i]` does not precede `y`,
* or `-1` if no such element exists in the specified slice.
*/
eq<T>(array:T[], y:T, compare?:CompareFunc<T>, lo?:number, hi?:number): number;
}
const bsearch:BSearch;
export = bsearch;
}
}