module Webglimpse { export function indexOf ( vs , x ) { var a = 0 ; var b = vs . length - 1 ; while ( a <= b ) { var pivot = ( ( a + b ) / 2 ) | 0 ; var vPivot = vs [ pivot ] ; if ( vPivot < x ) { a = pivot + 1 ; } else if ( vPivot > x ) { b = pivot - 1 ; } else { return pivot ; } } return - ( a + 1 ) ; } export function indexNearest ( vs , x ) { var i = indexOf ( vs , x ) ; if ( i >= 0 ) return i ; var iAfter = - i - 1 ; var iBefore = iAfter - 1 ; if ( iAfter >= vs . length ) return iBefore ; if ( iBefore < 0 ) return iAfter ; var diffAfter = vs [ iAfter ] - x ; var diffBefore = x - vs [ iBefore ] ; return ( diffAfter <= diffBefore ? iAfter : iBefore ) ; } export function indexAfter ( vs , x ) { var i = indexOf ( vs , x ) ; if ( i < 0 ) return ( - i - 1 ) ; var n = vs . length ; for ( var j = i + 1 ; j < n ; j ++ ) { if ( vs [ j ] > x ) return j ; } return n ; } export function indexAtOrAfter ( vs , x ) { var i = indexOf ( vs , x ) ; if ( i < 0 ) return ( - i - 1 ) ; var n = vs . length ; for ( var j = i ; j > 0 ; j -- ) { if ( vs [ j - 1 ] < x ) return j ; } return 0 ; } export function indexBefore ( vs , x ) { return indexAtOrAfter ( vs , x ) - 1 ; } export function indexAtOrBefore ( vs , x ) { return indexAfter ( vs , x ) - 1 ; } }