Comparing version 0.1.0 to 0.1.2
@@ -1,1 +0,1 @@ | ||
module.exports = require('./lib/heap.js'); | ||
module.exports = require('./lib/heap'); |
@@ -7,2 +7,12 @@ (function() { | ||
/* | ||
Default comparison function to be used | ||
*/ | ||
defaultCmp = function(x, y) { | ||
if (x < y) return -1; | ||
if (x > y) return 1; | ||
return 0; | ||
}; | ||
/* | ||
Insert item x in list a, and keep it sorted assuming a is sorted. | ||
@@ -16,10 +26,11 @@ | ||
insort = function(a, x, lo, hi) { | ||
insort = function(a, x, lo, hi, cmp) { | ||
var mid; | ||
if (lo == null) lo = 0; | ||
if (cmp == null) cmp = defaultCmp; | ||
if (lo < 0) throw new Error('lo must be non-negative'); | ||
if (hi == null) hi = a.length; | ||
while (lo < hi) { | ||
while (cmp(lo, hi) < 0) { | ||
mid = floor((lo + hi) / 2); | ||
if (x < a[mid]) { | ||
if (cmp(x, a[mid]) < 0) { | ||
hi = mid; | ||
@@ -33,12 +44,2 @@ } else { | ||
/* | ||
Default comparison function to be used | ||
*/ | ||
defaultCmp = function(x, y) { | ||
if (x < y) return -1; | ||
if (x > y) return 1; | ||
return 0; | ||
}; | ||
/* | ||
@@ -149,12 +150,13 @@ Push item onto heap, maintaining the heap invariant. | ||
nsmallest = function(n, array, cmp) { | ||
var elem, i, los, result, _i, _len, _ref, _results; | ||
var elem, i, los, result, _i, _len, _ref, _ref2, _results; | ||
if (cmp == null) cmp = defaultCmp; | ||
if (n * 10 <= array.length) { | ||
result = array.slice(0, n + 1 || 9e9).sort(); | ||
result = array.slice(0, n).sort(cmp); | ||
if (!result.length) return result; | ||
los = result[result.length - 1]; | ||
for (_i = 0, _len = array.length; _i < _len; _i++) { | ||
elem = array[_i]; | ||
_ref = array.slice(n); | ||
for (_i = 0, _len = _ref.length; _i < _len; _i++) { | ||
elem = _ref[_i]; | ||
if (cmp(elem, los) < 0) { | ||
insort(result, elem); | ||
insort(result, elem, 0, null, cmp); | ||
result.pop(); | ||
@@ -168,3 +170,3 @@ los = result[result.length - 1]; | ||
_results = []; | ||
for (i = 0, _ref = min(n, array.length); 0 <= _ref ? i < _ref : i > _ref; 0 <= _ref ? i++ : i--) { | ||
for (i = 0, _ref2 = min(n, array.length); 0 <= _ref2 ? i < _ref2 : i > _ref2; 0 <= _ref2 ? i++ : i--) { | ||
_results.push(heappop(array, cmp)); | ||
@@ -171,0 +173,0 @@ } |
{ | ||
"name" : "heap" | ||
, "version" : "0.1.0" | ||
, "version" : "0.1.2" | ||
, "description" : "binary heap (priority queue) algorithms (ported from Python's heapq module)" | ||
@@ -20,5 +20,5 @@ , "homepage" : "https://github.com/qiao/heap.js" | ||
, "licenses" : [{ | ||
"type" : "MIT" | ||
, "url" : "http://opensource.org/licenses/mit-license.php" | ||
"type" : "PSF" | ||
, "url" : "http://docs.python.org/license.html" | ||
}] | ||
} |
@@ -34,3 +34,3 @@ Heap.js | ||
1. push and pop | ||
push and pop | ||
@@ -45,3 +45,3 @@ ```js | ||
2. heap with custom comparison function | ||
custom comparison function | ||
@@ -58,3 +58,3 @@ ```js | ||
3. find 3 largest/smallest items in an array | ||
find 3 largest/smallest items in an array | ||
@@ -148,11 +148,11 @@ ```js | ||
**push(array, item)** | ||
**push(array, item, [cmp])** | ||
Push item onto array, maintaining the heap invariant. | ||
**pop(array)** | ||
**pop(array, [cmp])** | ||
Pop the smallest item off the array, maintaining the heap invariant. | ||
**replace(array, item)** | ||
**replace(array, item, [cmp])** | ||
@@ -165,15 +165,15 @@ Pop and return the current smallest value, and add the new item. | ||
**pushpop(array, item)** | ||
**pushpop(array, item, [cmp])** | ||
Fast version of a heappush followed by a heappop. | ||
**heapify(array)** | ||
**heapify(array, [cmp])** | ||
Build the heap. | ||
**nlargest(n, array)** | ||
**nlargest(n, array, [cmp])** | ||
Find the n largest elements in a dataset. | ||
**nsmallest(n, array)** | ||
**nsmallest(n, array, [cmp])** | ||
@@ -180,0 +180,0 @@ Find the n smallest elements in a dataset. |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
No License Found
License(Experimental) License information could not be found.
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
19510
224
1