binarysearch
Advanced tools
Comparing version 0.2.1 to 0.2.2
58
index.js
@@ -32,3 +32,3 @@ | ||
opts = opts||{}; | ||
if(!comparitor) comparitor = module.exports._defaultComparitor(); | ||
if(!comparitor) comparitor = this._defaultComparitor(); | ||
@@ -127,2 +127,6 @@ var closest = bsclosest(arr, search, comparitor, opts.end, opts.exists?false:true); | ||
module.exports.cmp = function(v1,v2){ | ||
return (v1<v2?-1:v1>v2?1:0) | ||
} | ||
module.exports._defaultComparitor = function() { | ||
@@ -157,3 +161,3 @@ var indexMode,indexModeSearch; | ||
cmp = comparitor(arr[middle],search); | ||
cmp = comparitor(arr[middle],search,middle); | ||
@@ -173,43 +177,29 @@ if (cmp === -1) { | ||
function bsclosest(arr, search, comparitor, invert, closest) { | ||
var mids = {}; | ||
var min = 0,max = arr.length-1,middle,cmp; | ||
var sanity = arr.length; | ||
var mids = {} | ||
, min = 0,max = arr.length-1,middle,cmp | ||
, sanity = arr.length; | ||
while (min < max) { | ||
middle = midCareful(min, max,mids); | ||
cmp = comparitor(arr[middle],search); | ||
cmp = comparitor(arr[middle],search,middle); | ||
if(invert){ | ||
if (cmp === 1){ | ||
max = middle - 1; | ||
} else { | ||
min = middle; | ||
} | ||
if (cmp === 1)max = middle - 1; | ||
else min = middle; | ||
} else { | ||
if (cmp === -1){ | ||
min = middle + 1; | ||
} else { | ||
max = middle; | ||
} | ||
if (cmp === -1)min = middle + 1; | ||
else max = middle; | ||
} | ||
sanity--; | ||
if(!sanity) { | ||
break; | ||
} | ||
if(!--sanity) break; | ||
} | ||
if (max == min && comparitor(arr[min],search) === 0) return min; | ||
if (max == min && comparitor(arr[min],search) === 0) { | ||
return min; | ||
} else { | ||
if(closest) { | ||
var match = comparitor(arr[min],search); | ||
if(min == arr.length-1 && match === -1) return min; | ||
if(min == 0 && match === 1) return 0; | ||
if(closest) { | ||
var match = comparitor(arr[min],search); | ||
if(min == arr.length-1 && match === -1) return min; | ||
if(min == 0 && match === 1) return 0; | ||
return closest?(invert?min+1:min-1):-1; | ||
} | ||
return -1; | ||
} | ||
return closest?(invert?min+1:min-1):-1; | ||
} | ||
return -1; | ||
} | ||
@@ -216,0 +206,0 @@ |
{ | ||
"name": "binarysearch", | ||
"description": "pure js binary search for sorted javascript arrays||array like objects. returns any || last || first || closest matched key for value, or slice between 2 values where values need not exist.", | ||
"version": "0.2.1", | ||
"version": "0.2.2", | ||
"repository": { | ||
@@ -6,0 +6,0 @@ "url": "git://github.com/soldair/node-binarysearch.git" |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
13839
12
315