Add files
This commit is contained in:
commit
bb80829159
18195 changed files with 2122994 additions and 0 deletions
51
509bba0_unpacked_with_node_modules/~/binary-search/index.js
generated
Executable file
51
509bba0_unpacked_with_node_modules/~/binary-search/index.js
generated
Executable file
|
@ -0,0 +1,51 @@
|
|||
module.exports = function(haystack, needle, comparator, low, high) {
|
||||
var mid, cmp;
|
||||
|
||||
if(low === undefined)
|
||||
low = 0;
|
||||
|
||||
else {
|
||||
low = low|0;
|
||||
if(low < 0 || low >= haystack.length)
|
||||
throw new RangeError("invalid lower bound");
|
||||
}
|
||||
|
||||
if(high === undefined)
|
||||
high = haystack.length - 1;
|
||||
|
||||
else {
|
||||
high = high|0;
|
||||
if(high < low || high >= haystack.length)
|
||||
throw new RangeError("invalid upper bound");
|
||||
}
|
||||
|
||||
while(low <= high) {
|
||||
/* Note that "(low + high) >>> 1" may overflow, and results in a typecast
|
||||
* to double (which gives the wrong results). */
|
||||
mid = low + (high - low >> 1);
|
||||
cmp = +comparator(haystack[mid], needle, mid, haystack);
|
||||
|
||||
/* Too low. */
|
||||
if(cmp < 0.0)
|
||||
low = mid + 1;
|
||||
|
||||
/* Too high. */
|
||||
else if(cmp > 0.0)
|
||||
high = mid - 1;
|
||||
|
||||
/* Key found. */
|
||||
else
|
||||
return mid;
|
||||
}
|
||||
|
||||
/* Key not found. */
|
||||
return ~low;
|
||||
}
|
||||
|
||||
|
||||
|
||||
//////////////////
|
||||
// WEBPACK FOOTER
|
||||
// ./~/binary-search/index.js
|
||||
// module id = 684
|
||||
// module chunks = 4
|
Loading…
Add table
Add a link
Reference in a new issue