pax_global_header 0000666 0000000 0000000 00000000064 13522276632 0014522 g ustar 00root root 0000000 0000000 52 comment=1caaa1207a11702612afd00ef7972855ea1e12a1
darkskyapp-binary-search-1caaa12/ 0000775 0000000 0000000 00000000000 13522276632 0017073 5 ustar 00root root 0000000 0000000 darkskyapp-binary-search-1caaa12/.gitignore 0000664 0000000 0000000 00000000016 13522276632 0021060 0 ustar 00root root 0000000 0000000 /node_modules
darkskyapp-binary-search-1caaa12/.travis.yml 0000664 0000000 0000000 00000000114 13522276632 0021200 0 ustar 00root root 0000000 0000000 language: node_js
node_js:
- '6'
cache:
directories:
- node_modules
darkskyapp-binary-search-1caaa12/README.md 0000664 0000000 0000000 00000003202 13522276632 0020347 0 ustar 00root root 0000000 0000000 binary-search
=============
This is a really tiny, stupid, simple binary search library for Node.JS. We
wrote it because existing solutions were bloated and incorrect.
This version is a straight port of the Java version mentioned by Joshua Bloch
in his article, [Nearly All Binary Searches and Merge Sorts are Broken](http://googleresearch.blogspot.com/2006/06/extra-extra-read-all-about-it-nearly.html).
Thanks to [Conrad Irwin](https://github.com/ConradIrwin) and [Michael
Marino](https://github.com/mgmarino) for, ironically, pointing out bugs.
Example
-------
```js
var bs = require("binary-search");
bs([1, 2, 3, 4], 3, function(element, needle) { return element - needle; });
// => 2
bs([1, 2, 4, 5], 3, function(element, needle) { return element - needle; });
// => -3
```
Be advised that passing in a comparator function is *required*. Since you're
probably using one for your sort function anyway, this isn't a big deal.
The comparator takes a 1st and 2nd argument of element and needle, respectively.
The comparator also takes a 3rd and 4th argument, the current index and array,
respectively. You shouldn't normally need the index or array to compare values,
but it's there if you do.
You may also, optionally, specify an input range as the final two parameters,
in case you want to limit the search to a particular range of inputs. However,
be advised that this is generally a bad idea (but sometimes bad ideas are
necessary).
License
-------
To the extent possible by law, The Dark Sky Company, LLC has [waived all
copyright and related or neighboring rights][cc0] to this library.
[cc0]: http://creativecommons.org/publicdomain/zero/1.0/
darkskyapp-binary-search-1caaa12/binary-search.d.ts 0000664 0000000 0000000 00000001603 13522276632 0022414 0 ustar 00root root 0000000 0000000 //Typescript type definition for:
//https://github.com/darkskyapp/binary-search
declare module 'binary-search' {
function binarySearch(
haystack: ArrayLike,
needle: B,
comparator: (a: A, b: B, index?: number, haystack?: A[]) => any,
// Notes about comparator return value:
// * when ab the comparator's returned value should be:
// * positive number or a value such that `+value` is a positive number
// * examples: `1` or the string `"1"`
// * when a===b
// * any value other than the return cases for ab
// * examples: undefined, NaN, 'abc'
low?: number,
high?: number): number; //returns index of found result or number < 0 if not found
export = binarySearch;
}
darkskyapp-binary-search-1caaa12/index.js 0000664 0000000 0000000 00000001765 13522276632 0020551 0 ustar 00root root 0000000 0000000 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) {
// The naive `low + high >>> 1` could fail for array lengths > 2**31
// because `>>>` converts its operands to int32. `low + (high - low >>> 1)`
// works for array lengths <= 2**32-1 which is also Javascript's max array
// length.
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;
}
darkskyapp-binary-search-1caaa12/package.json 0000664 0000000 0000000 00000001132 13522276632 0021356 0 ustar 00root root 0000000 0000000 {
"name": "binary-search",
"version": "1.3.6",
"description": "tiny binary search function with comparators",
"license": "CC0-1.0",
"typings": "./binary-search.d.ts",
"author": {
"name": "The Dark Sky Company, LLC",
"email": "support@darkskyapp.com"
},
"contributors": [
{
"name": "Darcy Parker",
"web": "https://github.com/darcyparker"
}
],
"repository": {
"type": "git",
"url": "git://github.com/darkskyapp/binary-search.git"
},
"devDependencies": {
"chai": "^4.2.0",
"mocha": "^5.2.0"
},
"scripts": {
"test": "mocha"
}
}
darkskyapp-binary-search-1caaa12/test.js 0000664 0000000 0000000 00000002665 13522276632 0020421 0 ustar 00root root 0000000 0000000 var expect = require("chai").expect;
describe("binarysearch", function() {
var bs = require("./"),
arr = [1, 2, 2, 2, 3, 5, 9],
cmp = function(a, b) { return a - b; };
it("should bail if not passed an array", function() {
expect(function() { bs(undefined, 3, cmp); }).to.throw(TypeError);
});
it("should bail if not passed a comparator", function() {
expect(function() { bs(arr, 3, undefined); }).to.throw(TypeError);
});
it("should return the index of an item in a sorted array", function() {
expect(bs(arr, 3, cmp)).to.equal(4);
});
it("should return the index of where the item would go plus one, negated, if the item is not found", function() {
expect(bs(arr, 4, cmp)).to.equal(-6);
});
it("should return any valid index if an item exists multiple times in the array", function() {
expect(bs(arr, 2, cmp)).to.equal(3);
});
it("should work even on empty arrays", function() {
expect(bs([], 42, cmp)).to.equal(-1);
});
it("should work even on arrays of doubles", function() {
expect(bs([0.0, 0.1, 0.2, 0.3, 0.4], 0.25, cmp)).to.equal(-4);
});
it("should pass the index and array parameters to the comparator", function() {
var indexes = [],
indexCmp = function(a, b, i, array) {
expect(array).to.equal(arr);
indexes.push(i);
return cmp(a, b);
};
bs(arr, 3, indexCmp);
expect(indexes).to.deep.equal([3, 5, 4])
});
});