react / wstein / node_modules / browserify / node_modules / browser-pack / 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) {1011exports.GREATEST_LOWER_BOUND = 1;12exports.LEAST_UPPER_BOUND = 2;1314/**15* Recursive implementation of binary search.16*17* @param aLow Indices here and lower do not contain the needle.18* @param aHigh Indices here and higher do not contain the needle.19* @param aNeedle The element being searched for.20* @param aHaystack The non-empty array being searched.21* @param aCompare Function which takes two elements and returns -1, 0, or 1.22* @param aBias Either 'binarySearch.GREATEST_LOWER_BOUND' or23* 'binarySearch.LEAST_UPPER_BOUND'. Specifies whether to return the24* closest element that is smaller than or greater than the one we are25* searching for, respectively, if the exact element cannot be found.26*/27function recursiveSearch(aLow, aHigh, aNeedle, aHaystack, aCompare, aBias) {28// This function terminates when one of the following is true:29//30// 1. We find the exact element we are looking for.31//32// 2. We did not find the exact element, but we can return the index of33// the next-closest element.34//35// 3. We did not find the exact element, and there is no next-closest36// element than the one we are searching for, so we return -1.37var mid = Math.floor((aHigh - aLow) / 2) + aLow;38var cmp = aCompare(aNeedle, aHaystack[mid], true);39if (cmp === 0) {40// Found the element we are looking for.41return mid;42}43else if (cmp > 0) {44// Our needle is greater than aHaystack[mid].45if (aHigh - mid > 1) {46// The element is in the upper half.47return recursiveSearch(mid, aHigh, aNeedle, aHaystack, aCompare, aBias);48}4950// The exact needle element was not found in this haystack. Determine if51// we are in termination case (3) or (2) and return the appropriate thing.52if (aBias == exports.LEAST_UPPER_BOUND) {53return aHigh < aHaystack.length ? aHigh : -1;54} else {55return mid;56}57}58else {59// Our needle is less than aHaystack[mid].60if (mid - aLow > 1) {61// The element is in the lower half.62return recursiveSearch(aLow, mid, aNeedle, aHaystack, aCompare, aBias);63}6465// we are in termination case (3) or (2) and return the appropriate thing.66if (aBias == exports.LEAST_UPPER_BOUND) {67return mid;68} else {69return aLow < 0 ? -1 : aLow;70}71}72}7374/**75* This is an implementation of binary search which will always try and return76* the index of the closest element if there is no exact hit. This is because77* mappings between original and generated line/col pairs are single points,78* and there is an implicit region between each of them, so a miss just means79* that you aren't on the very start of a region.80*81* @param aNeedle The element you are looking for.82* @param aHaystack The array that is being searched.83* @param aCompare A function which takes the needle and an element in the84* array and returns -1, 0, or 1 depending on whether the needle is less85* than, equal to, or greater than the element, respectively.86* @param aBias Either 'binarySearch.GREATEST_LOWER_BOUND' or87* 'binarySearch.LEAST_UPPER_BOUND'. Specifies whether to return the88* closest element that is smaller than or greater than the one we are89* searching for, respectively, if the exact element cannot be found.90* Defaults to 'binarySearch.GREATEST_LOWER_BOUND'.91*/92exports.search = function search(aNeedle, aHaystack, aCompare, aBias) {93if (aHaystack.length === 0) {94return -1;95}9697var index = recursiveSearch(-1, aHaystack.length, aNeedle, aHaystack,98aCompare, aBias || exports.GREATEST_LOWER_BOUND);99if (index < 0) {100return -1;101}102103// We have found either the exact element, or the next-closest element than104// the one we are searching for. However, there may be more than one such105// element. Make sure we always return the smallest of these.106while (index - 1 >= 0) {107if (aCompare(aHaystack[index], aHaystack[index - 1], true) !== 0) {108break;109}110--index;111}112113return index;114};115116});117118119