Skip to content

Commit

Permalink
fix refactoring bug; 1.0.6
Browse files Browse the repository at this point in the history
  • Loading branch information
Andras committed Jan 7, 2015
1 parent c0d61ba commit 955b441
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 21 deletions.
35 changes: 18 additions & 17 deletions lib/qheap.js
Expand Up @@ -68,25 +68,26 @@ Heap.prototype.remove = function Heap_remove( ) {
// for a sequence of repeated insert/removes than the classic combined
// bubble-down with 3-way min() test (caching effect? The least-values
// path is more likely to be reused, ie remain cache resident.)
var comparfn = this._comparFunc;
var idx, child;
for (idx = 1, child = 2; child <= len; child = child << 1) {
if (child < len && comparfn(list[child+1], list[child]) < 0) child += 1;
list[idx] = list[child];
idx = child;
}
// idx points to the just vacated leaf node, bubble up item from there
while (idx > 1) {
var parentval = list[idx >> 1];
if (comparfn(item, parentval) < 0) {
list[idx] = parentval;
idx = idx >> 1;
if (len > 0) {
var comparfn = this._comparFunc;
var idx, child;
for (idx = 1, child = 2; child <= len; child = child << 1) {
if (child < len && comparfn(list[child+1], list[child]) < 0) child += 1;
list[idx] = list[child];
idx = child;
}
else break;
// idx points to the just vacated leaf node, bubble up item from there
while (idx > 1) {
var parentval = list[idx >> 1];
if (comparfn(item, parentval) < 0) {
list[idx] = parentval;
idx = idx >> 1;
}
else break;
}
list[idx] = item;
if (this._freeSpace) this._freeSpace(list, len);
}
list[idx] = item;

if (this._freeSpace) this._freeSpace(list, len);

return ret;
};
Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "qheap",
"version": "1.0.5",
"version": "1.0.6",
"description": "binary heap priority queue",
"license": "Apache-2.0",
"repository": {
Expand Down
27 changes: 24 additions & 3 deletions test/test-qheap.js
Expand Up @@ -32,8 +32,29 @@ module.exports = {
t.done();
},

'empty heap should return null': function(t) {
assert.equal(this.cut.remove(), null);
'empty heap should return undefined': function(t) {
assert(this.cut.remove() === undefined);
t.done();
},

'peek should return the first item or undefined': function(t) {
this.cut.insert(2);
t.equal(2, this.cut.peek());
this.cut.insert(1);
t.equal(1, this.cut.peek());
this.cut.remove();
t.equal(2, this.cut.peek());
this.cut.remove();
assert(this.cut.peek() === undefined);
t.done();
},

'should update length on insert and remove': function(t) {
t.equal(0, this.cut.length);
this.cut.insert(1);
t.equal(1, this.cut.length);
this.cut.remove();
t.equal(0, this.cut.length);
t.done();
},

Expand All @@ -44,7 +65,7 @@ module.exports = {
this.cut.insert(3);
assert.equal(this.cut.remove(), 2);
assert.equal(this.cut.remove(), 3);
assert.equal(this.cut.remove(), null);
assert(this.cut.remove() === undefined);
t.done();
},

Expand Down

0 comments on commit 955b441

Please sign in to comment.