Skip to content

Commit

Permalink
remove ndarray support
Browse files Browse the repository at this point in the history
  • Loading branch information
mikolalysenko committed Jun 15, 2015
1 parent c20038c commit d047eee
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 25 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ Returns an index of some item in the array `== y`.

The following comments apply to the above methods:

* `array` can be either an array or an [`ndarray`](https://github.com/mikolalysenko/ndarray)
* `cmp` is a comparison function, just like what you would pass to `Array.sort()`
* `y` will always be the second argument passed to `cmp`, so you can ignore it if you are just binary searching on a predicate.
* Assumes the array is sorted as would be the case if you called `Array.sort(cmp)` on it
Expand All @@ -46,4 +45,4 @@ The following comments apply to the above methods:
* `bounds.eq` will return the first found item with the given index. It can be a little faster than the other methods if you just want to find some random match and do not care where it is.

## Credits
(c) 2013 Mikola Lysenko. MIT License
(c) 2013 Mikola Lysenko. MIT License
6 changes: 2 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,10 @@
},
"dependencies": {},
"devDependencies": {
"tape": "~2.0.0",
"tap": "~0.4.4",
"ndarray": "~1.0.8"
"tape": "^4.0.0"
},
"scripts": {
"test": "tap test/*.js"
"test": "tape test/*.js"
},
"repository": {
"type": "git",
Expand Down
22 changes: 7 additions & 15 deletions search-bounds.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
"use strict"

function compileSearch(funcName, predicate, reversed, extraArgs, useNdarray, earlyOut) {
function compileSearch(funcName, predicate, reversed, extraArgs, earlyOut) {
var code = [
"function ", funcName, "(a,l,h,", extraArgs.join(","), "){",
earlyOut ? "" : "var i=", (reversed ? "l-1" : "h+1"),
";while(l<=h){\
var m=(l+h)>>>1,x=a", useNdarray ? ".get(m)" : "[m]"]
var m=(l+h)>>>1,x=a[m]"]
if(earlyOut) {
if(predicate.indexOf("c") < 0) {
code.push(";if(x===y){return m}else if(x<=y){")
Expand All @@ -31,22 +31,14 @@ var m=(l+h)>>>1,x=a", useNdarray ? ".get(m)" : "[m]"]

function compileBoundsSearch(predicate, reversed, suffix, earlyOut) {
var result = new Function([
compileSearch("A", "x" + predicate + "y", reversed, ["y"], false, earlyOut),
compileSearch("B", "x" + predicate + "y", reversed, ["y"], true, earlyOut),
compileSearch("P", "c(x,y)" + predicate + "0", reversed, ["y", "c"], false, earlyOut),
compileSearch("Q", "c(x,y)" + predicate + "0", reversed, ["y", "c"], true, earlyOut),
compileSearch("A", "x" + predicate + "y", reversed, ["y"], earlyOut),
compileSearch("P", "c(x,y)" + predicate + "0", reversed, ["y", "c"], earlyOut),
"function dispatchBsearch", suffix, "(a,y,c,l,h){\
if(a.shape){\
if(typeof(c)==='function'){\
return Q(a,(l===undefined)?0:l|0,(h===undefined)?a.shape[0]-1:h|0,y,c)\
return P(a,(l===void 0)?0:l|0,(h===void 0)?a.length-1:h|0,y,c)\
}else{\
return B(a,(c===undefined)?0:c|0,(l===undefined)?a.shape[0]-1:l|0,y)\
}}else{\
if(typeof(c)==='function'){\
return P(a,(l===undefined)?0:l|0,(h===undefined)?a.length-1:h|0,y,c)\
}else{\
return A(a,(c===undefined)?0:c|0,(l===undefined)?a.length-1:l|0,y)\
}}}\
return A(a,(c===void 0)?0:c|0,(l===void 0)?a.length-1:l|0,y)\
}}\
return dispatchBsearch", suffix].join(""))
return result()
}
Expand Down
7 changes: 3 additions & 4 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

var tape = require("tape")
var bounds = require("../search-bounds.js")
var ndarray = require("ndarray")

tape("greaterThanEquals", function(t) {

Expand All @@ -18,7 +17,7 @@ tape("greaterThanEquals", function(t) {
t.equals(lb(arr, values[i]), j)
}
}

checkArray([0,1,1,1,2], [-1, 0, 1, 2, 0.5, 1.5, 5])

t.equals(lb([0,2,5,6], 0), 0)
Expand All @@ -32,7 +31,7 @@ tape("greaterThanEquals", function(t) {
function cmp(a,b) {
return a - b
}

t.equals(lb([0,1,1,1,2], -1, cmp), 0)
t.equals(lb([0,1,1,1,2], 0, cmp), 0)
t.equals(lb([0,1,1,1,2], 1, cmp), 1)
Expand Down Expand Up @@ -133,4 +132,4 @@ tape("equals", function(t) {
checkArray([0,1,1,1,2], [-1, 0, 1, 2, 0.5, 1.5, 5])

t.end()
})
})

0 comments on commit d047eee

Please sign in to comment.