public export const binarySearch = < T > ( elems , lessThan , lo , f ) : number => { let hi = elems . length - 1 ; let mid = 0 ; while ( lo <= hi ) { mid = lo + ( ( hi - lo ) >> 1 ) ; const v = f ( elems [ mid ] ) ; switch ( v ) { case - 1 : lo = mid + 1 ; break ; case 1 : hi = mid - 1 ; break ; default : return mid ; } } return lessThan ? lo - 1 : lo ; } ;