Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80559 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
/**
13
* Recursive implementation of binary search.
14
*
15
* @param aLow Indices here and lower do not contain the needle.
16
* @param aHigh Indices here and higher do not contain the needle.
17
* @param aNeedle The element being searched for.
18
* @param aHaystack The non-empty array being searched.
19
* @param aCompare Function which takes two elements and returns -1, 0, or 1.
20
*/
21
function recursiveSearch(aLow, aHigh, aNeedle, aHaystack, aCompare) {
22
// This function terminates when one of the following is true:
23
//
24
// 1. We find the exact element we are looking for.
25
//
26
// 2. We did not find the exact element, but we can return the next
27
// closest element that is less than that element.
28
//
29
// 3. We did not find the exact element, and there is no next-closest
30
// element which is less than the one we are searching for, so we
31
// return null.
32
var mid = Math.floor((aHigh - aLow) / 2) + aLow;
33
var cmp = aCompare(aNeedle, aHaystack[mid], true);
34
if (cmp === 0) {
35
// Found the element we are looking for.
36
return aHaystack[mid];
37
}
38
else if (cmp > 0) {
39
// aHaystack[mid] is greater than our needle.
40
if (aHigh - mid > 1) {
41
// The element is in the upper half.
42
return recursiveSearch(mid, aHigh, aNeedle, aHaystack, aCompare);
43
}
44
// We did not find an exact match, return the next closest one
45
// (termination case 2).
46
return aHaystack[mid];
47
}
48
else {
49
// aHaystack[mid] is less than our needle.
50
if (mid - aLow > 1) {
51
// The element is in the lower half.
52
return recursiveSearch(aLow, mid, aNeedle, aHaystack, aCompare);
53
}
54
// The exact needle element was not found in this haystack. Determine if
55
// we are in termination case (2) or (3) and return the appropriate thing.
56
return aLow < 0
57
? null
58
: aHaystack[aLow];
59
}
60
}
61
62
/**
63
* This is an implementation of binary search which will always try and return
64
* the next lowest value checked if there is no exact hit. This is because
65
* mappings between original and generated line/col pairs are single points,
66
* and there is an implicit region between each of them, so a miss just means
67
* that you aren't on the very start of a region.
68
*
69
* @param aNeedle The element you are looking for.
70
* @param aHaystack The array that is being searched.
71
* @param aCompare A function which takes the needle and an element in the
72
* array and returns -1, 0, or 1 depending on whether the needle is less
73
* than, equal to, or greater than the element, respectively.
74
*/
75
exports.search = function search(aNeedle, aHaystack, aCompare) {
76
return aHaystack.length > 0
77
? recursiveSearch(-1, aHaystack.length, aNeedle, aHaystack, aCompare)
78
: null;
79
};
80
81
});
82
83