import { Comparer } from "@esfx/equatable" ; export function binarySearch < T > ( array , key , comparer ) { if ( array . length === 0 ) return - 1 ; let low = 0 ; let high = array . length - 1 ; while ( low <= high ) { const middle = low + ( ( high - low ) >> 1 ) ; const midKey = array [ middle ] ; switch ( Math . sign ( comparer . compare ( midKey , key ) ) ) { case - 1 : low = middle + 1 ; break ; case 0 : return middle ; case + 1 : high = middle - 1 ; break ; } } return ~ low ; }