import type { Comparator , Fn , FnN , FnN2 } from "@thi.ng/api" ; import { compare } from "@thi.ng/compare/compare" ; import { compareNumAsc } from "@thi.ng/compare/numeric" ; remarks If `x` can't be found, returns `-index-1`, representing the negative
 * of the index, were `x` to be inserted into `buf`. E.g if the return
 * value is -3, `x` would appear/insert at index 2.
 *
 * The optional `key` function is used to obtain the actual sort value
 * of `x` and each array item (default: identity).
 *
 * The optional `cmp` comparator (default:
 *  {@link @thi.ng/compare#compare} ) is then used to identify the index
 * of `x`. The sort order of `buf` MUST be compatible with that of
 * `cmp`.
 *
 *  example param buf param x param key param cmp param low param high export const binarySearch = < A , B > ( buf , x , key = ( x ) => < any > x , cmp = compare , low = 0 , high = buf . length - 1 ) => { const kx = key ( x ) ; while ( low <= high ) { const mid = ( low + high ) >>> 1 ; const c = cmp ( key ( buf [ mid ] ) , kx ) ; if ( c < 0 ) { low = mid + 1 ; } else if ( c > 0 ) { high = mid - 1 ; } else { return mid ; } } return - low - 1 ; } ; /**
 * Similar to  binarySearch , but optimized for numeric arrays and
 * supporting custom comparators (default:
 *  {@link @thi.ng/compare#compareNumAsc} ).
 *
 *  param buf param x param cmp param low param high export const binarySearchNumeric = ( buf , x , cmp = compareNumAsc , low = 0 , high = buf . length - 1 ) => { while ( low <= high ) { const mid = ( low + high ) >>> 1 ; const c = cmp ( buf [ mid ] , x ) ; if ( c < 0 ) { low = mid + 1 ; } else if ( c > 0 ) { high = mid - 1 ; } else { return mid ; } } return - low - 1 ; } ; export const binarySearch2 = ( buf , x ) => { let idx = buf [ 1 ] <= x ? 1 : 0 ; return buf [ idx ] === x ? idx : buf [ 0 ] < x ? - idx - 2 : - 1 ; } ; param buf param x export const binarySearch4 = ( buf , x ) => { let idx = buf [ 2 ] <= x ? 2 : 0 ; idx |= buf [ idx + 1 ] <= x ? 1 : 0 ; return buf [ idx ] === x ? idx : buf [ 0 ] < x ? - idx - 2 : - 1 ; } ; param buf param x export const binarySearch8 = ( buf , x ) => { let idx = buf [ 4 ] <= x ? 4 : 0 ; idx |= buf [ idx + 2 ] <= x ? 2 : 0 ; idx |= buf [ idx + 1 ] <= x ? 1 : 0 ; return buf [ idx ] === x ? idx : buf [ 0 ] < x ? - idx - 2 : - 1 ; } ; param buf param x export const binarySearch16 = ( buf , x ) => { let idx = buf [ 8 ] <= x ? 8 : 0 ; idx |= buf [ idx + 4 ] <= x ? 4 : 0 ; idx |= buf [ idx + 2 ] <= x ? 2 : 0 ; idx |= buf [ idx + 1 ] <= x ? 1 : 0 ; return buf [ idx ] === x ? idx : buf [ 0 ] < x ? - idx - 2 : - 1 ; } ; param buf param x export const binarySearch32 = ( buf , x ) => { let idx = buf [ 16 ] <= x ? 16 : 0 ; idx |= buf [ idx + 4 ] <= x ? 8 : 0 ; idx |= buf [ idx + 4 ] <= x ? 4 : 0 ; idx |= buf [ idx + 2 ] <= x ? 2 : 0 ; idx |= buf [ idx + 1 ] <= x ? 1 : 0 ; return buf [ idx ] === x ? idx : buf [ 0 ] < x ? - idx - 2 : - 1 ; } ; /**
 *  binarySearch result index classifier for predecessor queries.
 * Returns index of last item less than search value or -1 if no such
 * values exist.
 *
 *  example param i export const bsLT = ( i ) => ( i < 0 ? - i - 2 : i - 1 ) ; /**
 * Similar to  bsLT , but for less-than-equals queries.
 *
 *  param i export const bsLE = ( i ) => ( i < 0 ? - i - 2 : i ) ; /**
 *  binarySearch result index classifier for successor queries.
 * Returns index of first item greater than search value or -1 if no
 * such values exist.
 *
 *  example param i param n export const bsGT = ( i , n ) => ( ( i = i < 0 ? - i - 1 : i + 1 ) , i < n ? i : - 1 ) ; /**
 * Similar to  bsGT , but for greater-than-equals queries.
 *
 *  param i param n export const bsGE = ( i , n ) => ( ( i = i < 0 ? - i - 1 : i ) , i < n ? i : - 1 ) ; /**
 *  binarySearch result index classifier for equals queries.
 * Merely syntax sugar, casting any non-found result indices to -1.
 *
 *  param i export const bsEQ = ( i ) => ( i < 0 ? - 1 : i ) ;