react / wstein / node_modules / browserify / node_modules / insert-module-globals / node_modules / combine-source-map / node_modules / inline-source-map / node_modules / source-map / lib / source-map / binary-search.js
80620 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) {10/**11* Recursive implementation of binary search.12*13* @param aLow Indices here and lower do not contain the needle.14* @param aHigh Indices here and higher do not contain the needle.15* @param aNeedle The element being searched for.16* @param aHaystack The non-empty array being searched.17* @param aCompare Function which takes two elements and returns -1, 0, or 1.18* @param aBias Either 'binarySearch.LEAST_UPPER_BOUND' or19* 'binarySearch.GREATEST_LOWER_BOUND'. Specifies whether to return the20* closest element that is smaller than or greater than the element we are21* searching for if the exact element cannot be found.22*/23function recursiveSearch(aLow, aHigh, aNeedle, aHaystack, aCompare, aBias) {24// This function terminates when one of the following is true:25//26// 1. We find the exact element we are looking for.27//28// 2. We did not find the exact element, but we can return the index of29// the next closest element.30//31// 3. We did not find the exact element, and there is no next-closest32// element than the one we are searching for, so we return -1.33var mid = Math.floor((aHigh - aLow) / 2) + aLow;34var cmp = aCompare(aNeedle, aHaystack[mid], true);35if (cmp === 0) {36// Found the element we are looking for.37return mid;38}39else if (cmp > 0) {40// Our needle is greater than aHaystack[mid].41if (aHigh - mid > 1) {42// The element is in the upper half.43return recursiveSearch(mid, aHigh, aNeedle, aHaystack, aCompare, aBias);44}45// The exact needle element was not found in this haystack. Determine if46// we are in termination case (3) or (2) and return the appropriate thing.47if (aBias == exports.LEAST_UPPER_BOUND) {48return aHigh < aHaystack.length ? aHigh : -1;49} else {50return mid;51}52}53else {54// Our needle is less than aHaystack[mid].55if (mid - aLow > 1) {56// The element is in the lower half.57return recursiveSearch(aLow, mid, aNeedle, aHaystack, aCompare, aBias);58}59// The exact needle element was not found in this haystack. Determine if60// we are in termination case (3) or (2) and return the appropriate thing.61if (aBias == exports.LEAST_UPPER_BOUND) {62return mid;63} else {64return aLow < 0 ? -1 : aLow;65}66}67}6869exports.LEAST_UPPER_BOUND = 1;70exports.GREATEST_LOWER_BOUND = 2;7172/**73* This is an implementation of binary search which will always try and return74* the index of next highest value checked if there is no exact hit. This is75* because mappings between original and generated line/col pairs are single76* points, and there is an implicit region between each of them, so a miss77* just means that you aren't on the very start of a region.78*79* @param aNeedle The element you are looking for.80* @param aHaystack The array that is being searched.81* @param aCompare A function which takes the needle and an element in the82* array and returns -1, 0, or 1 depending on whether the needle is less83* than, equal to, or greater than the element, respectively.84* @param aBias Either 'exports.LEAST_UPPER_BOUND' or85* 'exports.GREATEST_LOWER_BOUND'. Specifies whether to return the86* closest element that is smaller than or greater than the element we are87* searching for if the exact element cannot be found. Defaults to88* 'exports.LEAST_UPPER_BOUND'.89*/90exports.search = function search(aNeedle, aHaystack, aCompare, aBias) {91var aBias = aBias || exports.LEAST_UPPER_BOUND;9293if (aHaystack.length === 0) {94return -1;95}96return recursiveSearch(-1, aHaystack.length, aNeedle, aHaystack, aCompare, aBias)97};9899});100101102