lib/third_party/tracing/base/sorted_array_utils.js
function findIndexInSortedIntervals(ary, mapLoFn, mapWidthFn, loVal) {
    var first = findLowIndexInSortedArray(ary, mapLoFn, loVal);
    if (first == 0) {
      if (loVal >= mapLoFn(ary[0]) &&
          loVal < mapLoFn(ary[0]) + mapWidthFn(ary[0], 0)) {
        return 0;
      } else {
        return -1;
      }
    } else if (first < ary.length) {
      if (loVal >= mapLoFn(ary[first]) &&
          loVal < mapLoFn(ary[first]) + mapWidthFn(ary[first], first)) {
        return first;
      } else if (loVal >= mapLoFn(ary[first - 1]) &&
                 loVal < mapLoFn(ary[first - 1]) +
                 mapWidthFn(ary[first - 1], first - 1)) {
        return first - 1;
      } else {
        return ary.length;
      }
    } else if (first == ary.length) {
      if (loVal >= mapLoFn(ary[first - 1]) &&
          loVal < mapLoFn(ary[first - 1]) +
          mapWidthFn(ary[first - 1], first - 1)) {
        return first - 1;
      } else {
        return ary.length;
      }
    } else {
      return ary.length;
    }
  }
function findIndexInSortedIntervals(ary, mapLoFn, mapWidthFn, loVal) {
    var first = findLowIndexInSortedArray(ary, mapLoFn, loVal);
    if (first == 0) {
      if (loVal >= mapLoFn(ary[0]) &&
          loVal < mapLoFn(ary[0]) + mapWidthFn(ary[0], 0)) {
        return 0;
      } else {
        return -1;
      }
    } else if (first < ary.length) {
      if (loVal >= mapLoFn(ary[first]) &&
          loVal < mapLoFn(ary[first]) + mapWidthFn(ary[first], first)) {
        return first;
      } else if (loVal >= mapLoFn(ary[first - 1]) &&
                 loVal < mapLoFn(ary[first - 1]) +
                 mapWidthFn(ary[first - 1], first - 1)) {
        return first - 1;
      } else {
        return ary.length;
      }
    } else if (first == ary.length) {
      if (loVal >= mapLoFn(ary[first - 1]) &&
          loVal < mapLoFn(ary[first - 1]) +
          mapWidthFn(ary[first - 1], first - 1)) {
        return first - 1;
      } else {
        return ary.length;
      }
    } else {
      return ary.length;
    }
  }
function findIndexInSortedIntervals ( ary , mapLoFn , mapWidthFn , loVal ) { var first = findLowIndexInSortedArray ( ary , mapLoFn , loVal ) ; if ( first == 0 ) { if ( loVal >= mapLoFn ( ary [ 0 ] ) && loVal < mapLoFn ( ary [ 0 ] ) + mapWidthFn ( ary [ 0 ] , 0 ) ) { return 0 ; } else { return - 1 ; } } else if ( first < ary . length ) { if ( loVal >= mapLoFn ( ary [ first ] ) && loVal < mapLoFn ( ary [ first ] ) + mapWidthFn ( ary [ first ] , first ) ) { return first ; } else if ( loVal >= mapLoFn ( ary [ first - 1 ] ) && loVal < mapLoFn ( ary [ first - 1 ] ) + mapWidthFn ( ary [ first - 1 ] , first - 1 ) ) { return first - 1 ; } else { return ary . length ; } } else if ( first == ary . length ) { if ( loVal >= mapLoFn ( ary [ first - 1 ] ) && loVal < mapLoFn ( ary [ first - 1 ] ) + mapWidthFn ( ary [ first - 1 ] , first - 1 ) ) { return first - 1 ; } else { return ary . length ; } } else { return ary . length ; } }
Finds an index in an array of intervals that either intersects
the provided loVal, or if no intersection is found, -1 or ary.length.

The array of intervals is defined implicitly via two mapping functions
over the provided ary. mapLoFn determines the lower value of the interval,
mapWidthFn the width. Intersection is lower-inclusive, e.g. [lo,lo+w).

The array of intervals formed by this mapping must be non-overlapping and
sorted in ascending order by loVal.

@param {Array} ary An array of objects that can be converted into sorted
nonoverlapping ranges [x,y) using the mapLoFn and mapWidth.
@param {function():*} mapLoFn Callback that produces the low value for the
interval represented by an  element in the array.
@param {function():*} mapWidthFn Callback that produces the width for the
interval represented by an  element in the array.
@param {number} loVal The low value for the search.
@return {Number} An index in the array that intersects or is first-above
loVal, -1 if none found and loVal is below than all the intervals,
ary.length if loVal is greater than all the intervals.
Finds an index in an array of intervals that either intersects the provided loVal or if no intersection is found - 1 or ary . length .