react / wstein / node_modules / react / node_modules / envify / node_modules / jstransform / node_modules / source-map / lib / source-map / binary-search.js
80559 views/* -*- Mode: js; js-indent-level: 2; -*- */1/*2* Copyright 2011 Mozilla Foundation and contributors3* Licensed under the New BSD license. See LICENSE or:4* http://opensource.org/licenses/BSD-3-Clause5*/6if (typeof define !== 'function') {7var define = require('amdefine')(module, require);8}9define(function (require, exports, module) {1011/**12* Recursive implementation of binary search.13*14* @param aLow Indices here and lower do not contain the needle.15* @param aHigh Indices here and higher do not contain the needle.16* @param aNeedle The element being searched for.17* @param aHaystack The non-empty array being searched.18* @param aCompare Function which takes two elements and returns -1, 0, or 1.19*/20function recursiveSearch(aLow, aHigh, aNeedle, aHaystack, aCompare) {21// This function terminates when one of the following is true:22//23// 1. We find the exact element we are looking for.24//25// 2. We did not find the exact element, but we can return the next26// closest element that is less than that element.27//28// 3. We did not find the exact element, and there is no next-closest29// element which is less than the one we are searching for, so we30// return null.31var mid = Math.floor((aHigh - aLow) / 2) + aLow;32var cmp = aCompare(aNeedle, aHaystack[mid], true);33if (cmp === 0) {34// Found the element we are looking for.35return aHaystack[mid];36}37else if (cmp > 0) {38// aHaystack[mid] is greater than our needle.39if (aHigh - mid > 1) {40// The element is in the upper half.41return recursiveSearch(mid, aHigh, aNeedle, aHaystack, aCompare);42}43// We did not find an exact match, return the next closest one44// (termination case 2).45return aHaystack[mid];46}47else {48// aHaystack[mid] is less than our needle.49if (mid - aLow > 1) {50// The element is in the lower half.51return recursiveSearch(aLow, mid, aNeedle, aHaystack, aCompare);52}53// The exact needle element was not found in this haystack. Determine if54// we are in termination case (2) or (3) and return the appropriate thing.55return aLow < 056? null57: aHaystack[aLow];58}59}6061/**62* This is an implementation of binary search which will always try and return63* the next lowest value checked if there is no exact hit. This is because64* mappings between original and generated line/col pairs are single points,65* and there is an implicit region between each of them, so a miss just means66* that you aren't on the very start of a region.67*68* @param aNeedle The element you are looking for.69* @param aHaystack The array that is being searched.70* @param aCompare A function which takes the needle and an element in the71* array and returns -1, 0, or 1 depending on whether the needle is less72* than, equal to, or greater than the element, respectively.73*/74exports.search = function search(aNeedle, aHaystack, aCompare) {75return aHaystack.length > 076? recursiveSearch(-1, aHaystack.length, aNeedle, aHaystack, aCompare)77: null;78};7980});818283