lib/connection/result/result.js
function findFirstOverlappingChunk(chunks, windowStartIndex, windowEndIndex)
{
  var helper = function(chunks,
                        chunkIndexLeft,
                        chunkIndexRight,
                        windowStartIndex,
                        windowEndIndex)
  {
    var result;
    var chunkIndexMiddle;
    var middleChunk;
    var middleChunkEndIndex;

    // initialize the return value to -1
    result = -1;

    // compute the index of the middle chunk and get the middle chunk
    chunkIndexMiddle = Math.floor((chunkIndexLeft + chunkIndexRight) / 2);
    middleChunk = chunks[chunkIndexMiddle];

    // if we have two or fewer chunks
    if ((chunkIndexMiddle === chunkIndexLeft) ||
        (chunkIndexMiddle === chunkIndexRight))
    {
      // if we have just one chunk, and it overlaps with the specified window,
      // we've found the chunk we were looking for
      if (chunkIndexLeft === chunkIndexRight)
      {
        if (middleChunk.overlapsWithWindow(windowStartIndex, windowEndIndex))
        {
          result = chunkIndexLeft;
        }
      }
      else // we just have two chunks left to check
      {
        // if the first chunk overlaps with the specified window, that's the
        // chunk we were looking for
        if (chunks[chunkIndexLeft].overlapsWithWindow(
                windowStartIndex, windowEndIndex))
        {
          result = chunkIndexLeft;
        }

        // otherwise, if the second chunk overlaps with the specified window,
        // that's the chunk we were looking for
        else if (chunks[chunkIndexRight].overlapsWithWindow(
                windowStartIndex, windowEndIndex))
        {
          result = chunkIndexRight;
        }
      }

      return result;
    }

    // if the middle chunk does not overlap with the specified window
    if (!middleChunk.overlapsWithWindow(windowStartIndex, windowEndIndex))
    {
      middleChunkEndIndex = middleChunk.getEndIndex();

      // if the window is to the right of the middle chunk,
      // recurse on the right half
      if (windowStartIndex > middleChunkEndIndex)
      {
        return helper(
            chunks,
            chunkIndexMiddle,
            chunkIndexRight,
            windowStartIndex,
            windowEndIndex);
      }
      else
      {
        // recurse on the left half
        return helper(
            chunks,
            chunkIndexLeft,
            chunkIndexMiddle,
            windowStartIndex,
            windowEndIndex);
      }
    }
    else
    {
      // if the middle chunk overlaps but the chunk before it does not, the
      // middle chunk is the one we were looking
      if ((chunkIndexMiddle === 0) ||
          !chunks[chunkIndexMiddle - 1].overlapsWithWindow(
              windowStartIndex, windowEndIndex))
      {
        return chunkIndexMiddle;
      }
      else
      {
        // recurse on the left half
        return helper(
            chunks,
            chunkIndexLeft,
            chunkIndexMiddle,
            windowStartIndex,
            windowEndIndex);
      }
    }
  };

  return helper(chunks, 0, chunks.length - 1, windowStartIndex, windowEndIndex);
}
function findFirstOverlappingChunk(chunks, windowStartIndex, windowEndIndex)
{
  var helper = function(chunks,
                        chunkIndexLeft,
                        chunkIndexRight,
                        windowStartIndex,
                        windowEndIndex)
  {
    var result;
    var chunkIndexMiddle;
    var middleChunk;
    var middleChunkEndIndex;

    // initialize the return value to -1
    result = -1;

    // compute the index of the middle chunk and get the middle chunk
    chunkIndexMiddle = Math.floor((chunkIndexLeft + chunkIndexRight) / 2);
    middleChunk = chunks[chunkIndexMiddle];

    // if we have two or fewer chunks
    if ((chunkIndexMiddle === chunkIndexLeft) ||
        (chunkIndexMiddle === chunkIndexRight))
    {
      // if we have just one chunk, and it overlaps with the specified window,
      // we've found the chunk we were looking for
      if (chunkIndexLeft === chunkIndexRight)
      {
        if (middleChunk.overlapsWithWindow(windowStartIndex, windowEndIndex))
        {
          result = chunkIndexLeft;
        }
      }
      else // we just have two chunks left to check
      {
        // if the first chunk overlaps with the specified window, that's the
        // chunk we were looking for
        if (chunks[chunkIndexLeft].overlapsWithWindow(
                windowStartIndex, windowEndIndex))
        {
          result = chunkIndexLeft;
        }

        // otherwise, if the second chunk overlaps with the specified window,
        // that's the chunk we were looking for
        else if (chunks[chunkIndexRight].overlapsWithWindow(
                windowStartIndex, windowEndIndex))
        {
          result = chunkIndexRight;
        }
      }

      return result;
    }

    // if the middle chunk does not overlap with the specified window
    if (!middleChunk.overlapsWithWindow(windowStartIndex, windowEndIndex))
    {
      middleChunkEndIndex = middleChunk.getEndIndex();

      // if the window is to the right of the middle chunk,
      // recurse on the right half
      if (windowStartIndex > middleChunkEndIndex)
      {
        return helper(
            chunks,
            chunkIndexMiddle,
            chunkIndexRight,
            windowStartIndex,
            windowEndIndex);
      }
      else
      {
        // recurse on the left half
        return helper(
            chunks,
            chunkIndexLeft,
            chunkIndexMiddle,
            windowStartIndex,
            windowEndIndex);
      }
    }
    else
    {
      // if the middle chunk overlaps but the chunk before it does not, the
      // middle chunk is the one we were looking
      if ((chunkIndexMiddle === 0) ||
          !chunks[chunkIndexMiddle - 1].overlapsWithWindow(
              windowStartIndex, windowEndIndex))
      {
        return chunkIndexMiddle;
      }
      else
      {
        // recurse on the left half
        return helper(
            chunks,
            chunkIndexLeft,
            chunkIndexMiddle,
            windowStartIndex,
            windowEndIndex);
      }
    }
  };

  return helper(chunks, 0, chunks.length - 1, windowStartIndex, windowEndIndex);
}
function findFirstOverlappingChunk ( chunks , windowStartIndex , windowEndIndex ) { var helper = function ( chunks , chunkIndexLeft , chunkIndexRight , windowStartIndex , windowEndIndex ) { var result ; var chunkIndexMiddle ; var middleChunk ; var middleChunkEndIndex ; // initialize the return value to -1 result = - 1 ; // compute the index of the middle chunk and get the middle chunk chunkIndexMiddle = Math . floor ( ( chunkIndexLeft + chunkIndexRight ) / 2 ) ; middleChunk = chunks [ chunkIndexMiddle ] ; // if we have two or fewer chunks if ( ( chunkIndexMiddle === chunkIndexLeft ) || ( chunkIndexMiddle === chunkIndexRight ) ) { // if we have just one chunk, and it overlaps with the specified window, // we've found the chunk we were looking for if ( chunkIndexLeft === chunkIndexRight ) { if ( middleChunk . overlapsWithWindow ( windowStartIndex , windowEndIndex ) ) { result = chunkIndexLeft ; } } else // we just have two chunks left to check { // if the first chunk overlaps with the specified window, that's the // chunk we were looking for if ( chunks [ chunkIndexLeft ] . overlapsWithWindow ( windowStartIndex , windowEndIndex ) ) { result = chunkIndexLeft ; } // otherwise, if the second chunk overlaps with the specified window, // that's the chunk we were looking for else if ( chunks [ chunkIndexRight ] . overlapsWithWindow ( windowStartIndex , windowEndIndex ) ) { result = chunkIndexRight ; } } return result ; } // if the middle chunk does not overlap with the specified window if ( ! middleChunk . overlapsWithWindow ( windowStartIndex , windowEndIndex ) ) { middleChunkEndIndex = middleChunk . getEndIndex ( ) ; // if the window is to the right of the middle chunk, // recurse on the right half if ( windowStartIndex > middleChunkEndIndex ) { return helper ( chunks , chunkIndexMiddle , chunkIndexRight , windowStartIndex , windowEndIndex ) ; } else { // recurse on the left half return helper ( chunks , chunkIndexLeft , chunkIndexMiddle , windowStartIndex , windowEndIndex ) ; } } else { // if the middle chunk overlaps but the chunk before it does not, the // middle chunk is the one we were looking if ( ( chunkIndexMiddle === 0 ) || ! chunks [ chunkIndexMiddle - 1 ] . overlapsWithWindow ( windowStartIndex , windowEndIndex ) ) { return chunkIndexMiddle ; } else { // recurse on the left half return helper ( chunks , chunkIndexLeft , chunkIndexMiddle , windowStartIndex , windowEndIndex ) ; } } } ; return helper ( chunks , 0 , chunks . length - 1 , windowStartIndex , windowEndIndex ) ; }
Given a sorted array of chunks, returns the index of the first chunk in the
array that overlaps with a specified window.

@param chunks
@param windowStartIndex
@param windowEndIndex

@returns {number}
Given a sorted array of chunks returns the index of the first chunk in the array that overlaps with a specified window .