Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80620 views
1
/* -*- Mode: js; js-indent-level: 2; -*- */
2
/*
3
* Copyright 2011 Mozilla Foundation and contributors
4
* Licensed under the New BSD license. See LICENSE or:
5
* http://opensource.org/licenses/BSD-3-Clause
6
*/
7
if (typeof define !== 'function') {
8
var define = require('amdefine')(module, require);
9
}
10
define(function (require, exports, module) {
11
/**
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
* @param aBias Either 'binarySearch.LEAST_UPPER_BOUND' or
20
* 'binarySearch.GREATEST_LOWER_BOUND'. Specifies whether to return the
21
* closest element that is smaller than or greater than the element we are
22
* searching for if the exact element cannot be found.
23
*/
24
function recursiveSearch(aLow, aHigh, aNeedle, aHaystack, aCompare, aBias) {
25
// This function terminates when one of the following is true:
26
//
27
// 1. We find the exact element we are looking for.
28
//
29
// 2. We did not find the exact element, but we can return the index of
30
// the next closest element.
31
//
32
// 3. We did not find the exact element, and there is no next-closest
33
// element than the one we are searching for, so we return -1.
34
var mid = Math.floor((aHigh - aLow) / 2) + aLow;
35
var cmp = aCompare(aNeedle, aHaystack[mid], true);
36
if (cmp === 0) {
37
// Found the element we are looking for.
38
return mid;
39
}
40
else if (cmp > 0) {
41
// Our needle is greater than aHaystack[mid].
42
if (aHigh - mid > 1) {
43
// The element is in the upper half.
44
return recursiveSearch(mid, aHigh, aNeedle, aHaystack, aCompare, aBias);
45
}
46
// The exact needle element was not found in this haystack. Determine if
47
// we are in termination case (3) or (2) and return the appropriate thing.
48
if (aBias == exports.LEAST_UPPER_BOUND) {
49
return aHigh < aHaystack.length ? aHigh : -1;
50
} else {
51
return mid;
52
}
53
}
54
else {
55
// Our needle is less than aHaystack[mid].
56
if (mid - aLow > 1) {
57
// The element is in the lower half.
58
return recursiveSearch(aLow, mid, aNeedle, aHaystack, aCompare, aBias);
59
}
60
// The exact needle element was not found in this haystack. Determine if
61
// we are in termination case (3) or (2) and return the appropriate thing.
62
if (aBias == exports.LEAST_UPPER_BOUND) {
63
return mid;
64
} else {
65
return aLow < 0 ? -1 : aLow;
66
}
67
}
68
}
69
70
exports.LEAST_UPPER_BOUND = 1;
71
exports.GREATEST_LOWER_BOUND = 2;
72
73
/**
74
* This is an implementation of binary search which will always try and return
75
* the index of next highest value checked if there is no exact hit. This is
76
* because mappings between original and generated line/col pairs are single
77
* points, and there is an implicit region between each of them, so a miss
78
* just means that you aren't on the very start of a region.
79
*
80
* @param aNeedle The element you are looking for.
81
* @param aHaystack The array that is being searched.
82
* @param aCompare A function which takes the needle and an element in the
83
* array and returns -1, 0, or 1 depending on whether the needle is less
84
* than, equal to, or greater than the element, respectively.
85
* @param aBias Either 'exports.LEAST_UPPER_BOUND' or
86
* 'exports.GREATEST_LOWER_BOUND'. Specifies whether to return the
87
* closest element that is smaller than or greater than the element we are
88
* searching for if the exact element cannot be found. Defaults to
89
* 'exports.LEAST_UPPER_BOUND'.
90
*/
91
exports.search = function search(aNeedle, aHaystack, aCompare, aBias) {
92
var aBias = aBias || exports.LEAST_UPPER_BOUND;
93
94
if (aHaystack.length === 0) {
95
return -1;
96
}
97
return recursiveSearch(-1, aHaystack.length, aNeedle, aHaystack, aCompare, aBias)
98
};
99
100
});
101
102