aureooms/js-bisect

View on GitHub
src/bisect_left.js

Summary

Maintainability
A
0 mins
Test Coverage
import { ValueError } from '@aureooms/js-error' ;

export default function bisect_left ( a , x , lo = 0 , hi = a.length ) {

    if ( lo < 0 ) throw new ValueError( "lo must be non-negative" ) ;

    while ( lo < hi ) {

        const mid = ( lo + hi ) / 2 | 0 ;

        if ( x > a[mid] ) lo = mid + 1 ;

        else hi = mid ;

    }

    return lo ;

}