Skip to content

Commit

Permalink
fix remove() bug introduced when inlining _bubbleDown, fix unit test …
Browse files Browse the repository at this point in the history
…to catch the above, document options.freeSpace; 0.9.5
  • Loading branch information
Andras committed Jan 3, 2015
1 parent e01a77d commit f971682
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 16 deletions.
20 changes: 12 additions & 8 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,15 @@ create a new empty heap.

Options:

compar - comparison function to determine the item ordering. The
function should return a value less than zero if the first
argument should be ordered before the second (compatible
with the function passed to `sort()`).
The default ordering if no compar is specified is by `<`:
`function(a,b){ return a < b ? -1 : 1 }`
compar: comparison function to determine the item ordering. The function
should return a value less than zero if the first argument should be
ordered before the second (compatible with the function passed to
`sort()`). The default ordering if no compar is specified is by `<`:
`function(a,b){ return a < b ? -1 : 1 }`

freeSpace: when the heap shrinks to 1/4 its high-water mark, reallocate
the storage space to free the unused memory, and reset the high-water
mark. Default is false, avoiding the overhead of the array slice.

### insert( item )

Expand All @@ -51,11 +54,12 @@ only the `compar` function needs to know the actual type.
### remove( )

remove and return the item at the root of the heap (the next item in the
sequence), and rebalance the tree.
sequence), and rebalance the tree. When empty, returns `undefined`.

### peek( )

return the item at the root of the heap, but do not remove it.
return the item at the root of the heap, but do not remove it. When empty,
returns `undefined`.

### length

Expand Down
10 changes: 5 additions & 5 deletions lib/qheap.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ module.exports = Heap;
function Heap( opts ) {
opts = opts || {};
this._comparFunc = opts.compar || function(a, b) { return a < b ? -1 : 1 };
this._resize = opts.resizeArray || false;
this._freeSpace = opts.freeSpace || false;
this._list = new Array(1);
this.length = 0;
}
Expand Down Expand Up @@ -58,7 +58,7 @@ Heap.prototype.get = function Heap_get( ) {
var list = this._list;
var ret = list[1];
if (len < 2) {
if (len < 1) return null;
if (len < 1) return undefined;
list[1] = undefined;
this.length = 0;
return ret;
Expand All @@ -70,7 +70,7 @@ Heap.prototype.get = function Heap_get( ) {

var idx = 1;
var comparfn = this._comparFunc;
var compar = function(i, j) { comparfn(list[i], list[j]); }
var compar = function(i, j) { return comparfn(list[i], list[j]); }
while (true) {
var l = 2*idx, r = 2*idx+1;
if (r <= len && compar(r, idx) < 0) {
Expand All @@ -93,8 +93,8 @@ Heap.prototype.get = function Heap_get( ) {

if (len > 10000 && list.length > 4 * len) {
// use slice to actually free memory; 7% slower than setting .length
if (this._resize) this._list = list.slice(0, this.length);
else list.length = this.length;
if (this._freeSpace) this._list = list.slice(0, this.length+1);
else list.length = this.length+1;
}

return ret;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "qheap",
"version": "0.9.4",
"version": "0.9.5",
"description": "very fast binary heap priority queue",
"license": "Apache-2.0",
"repository": {
Expand Down
8 changes: 6 additions & 2 deletions test/test-qheap.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,16 @@ module.exports = {

'should sort the data': function(t) {
var i, data = [580, 253, 610, 176];
for (i=0; i<100000; i++) data[i] = Math.random() * 1000 | 0;
for (var i in data) {
for (i=0; i<100000; i++) {
data[i] = Math.random() * 1000 | 0;
this.cut.put(data[i]);
}
var ok = this.cut._check();
t.ok(ok);
t.equal(this.cut.length, data.length);
var item = this.cut.get();
while (this.cut.peek() !== undefined) { var x = this.cut.get(); assert(x >= item); item = x; }
t.equal(this.cut.length, 0);
t.done();
},
};

0 comments on commit f971682

Please sign in to comment.