react / wstein / node_modules / browserify / node_modules / insert-module-globals / node_modules / combine-source-map / 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 index of26// the next 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 -1.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 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 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 < 0 ? -1 : aLow;56}57}5859/**60* This is an implementation of binary search which will always try and return61* the index of next lowest value checked if there is no exact hit. This is62* because mappings between original and generated line/col pairs are single63* points, and there is an implicit region between each of them, so a miss64* just means that you aren't on the very start of a region.65*66* @param aNeedle The element you are looking for.67* @param aHaystack The array that is being searched.68* @param aCompare A function which takes the needle and an element in the69* array and returns -1, 0, or 1 depending on whether the needle is less70* than, equal to, or greater than the element, respectively.71*/72exports.search = function search(aNeedle, aHaystack, aCompare) {73if (aHaystack.length === 0) {74return -1;75}76return recursiveSearch(-1, aHaystack.length, aNeedle, aHaystack, aCompare)77};7879});808182