heap: minor refactor.

This commit is contained in:
Christopher Jeffrey 2017-02-26 21:45:16 -08:00
parent e49e877f17
commit b73b2fba85
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
3 changed files with 42 additions and 26 deletions

View File

@ -409,11 +409,11 @@ Miner.prototype.build = function build(attempt) {
if (item.depCount > 0) if (item.depCount > 0)
continue; continue;
queue.push(item); queue.insert(item);
} }
while (queue.size() > 0) { while (queue.size() > 0) {
item = queue.pop(); item = queue.shift();
tx = item.tx; tx = item.tx;
hash = item.hash; hash = item.hash;
weight = attempt.weight; weight = attempt.weight;
@ -441,7 +441,7 @@ Miner.prototype.build = function build(attempt) {
queue.set(cmpRate); queue.set(cmpRate);
queue.init(); queue.init();
priority = false; priority = false;
queue.push(item); queue.insert(item);
continue; continue;
} }
} else { } else {
@ -464,7 +464,7 @@ Miner.prototype.build = function build(attempt) {
for (j = 0; j < deps.length; j++) { for (j = 0; j < deps.length; j++) {
item = deps[j]; item = deps[j];
if (--item.depCount === 0) if (--item.depCount === 0)
queue.push(item); queue.insert(item);
} }
} }
@ -621,11 +621,11 @@ MinerOptions.fromOptions = function fromOptions(options) {
*/ */
function cmpPriority(a, b) { function cmpPriority(a, b) {
return a.priority - b.priority; return b.priority - a.priority;
} }
function cmpRate(a, b) { function cmpRate(a, b) {
return a.descRate - b.descRate; return b.descRate - a.descRate;
} }
/* /*

View File

@ -1,6 +1,6 @@
/*! /*!
* heap.js - heap object for bcoin * heap.js - heap object for bcoin
* Copyright (c) 2014-2017, Christopher Jeffrey (MIT License). * Copyright (c) 2017, Christopher Jeffrey (MIT License).
* https://github.com/bcoin-org/bcoin * https://github.com/bcoin-org/bcoin
*/ */
@ -9,19 +9,21 @@
var assert = require('assert'); var assert = require('assert');
/** /**
* Priority Queue * Binary Heap
* @alias module:utils.Heap * @alias module:utils.Heap
* @constructor * @constructor
* @param {Function?} cmp * @param {Function?} compare
*/ */
function Heap(cmp) { function Heap(compare) {
if (!(this instanceof Heap)) if (!(this instanceof Heap))
return new Heap(cmp); return new Heap(compare);
this.cmp = null; this.compare = comparator;
this.items = []; this.items = [];
this.set(cmp);
if (compare)
this.set(compare);
} }
/** /**
@ -50,13 +52,13 @@ Heap.prototype.size = function size() {
/** /**
* Set comparator. * Set comparator.
* @param {Function} cmp * @param {Function} compare
*/ */
Heap.prototype.set = function set(cmp) { Heap.prototype.set = function set(compare) {
assert(cmp == null || typeof cmp === 'function', assert(typeof compare === 'function',
'Comparator must be a function.'); 'Comparator must be a function.');
this.cmp = cmp || null; this.compare = compare;
}; };
/** /**
@ -65,7 +67,7 @@ Heap.prototype.set = function set(cmp) {
* @returns {Number} * @returns {Number}
*/ */
Heap.prototype.push = function push(item) { Heap.prototype.insert = function insert(item) {
this.items.push(item); this.items.push(item);
this.up(this.items.length - 1); this.up(this.items.length - 1);
return this.items.length; return this.items.length;
@ -77,7 +79,7 @@ Heap.prototype.push = function push(item) {
* @returns {Object} * @returns {Object}
*/ */
Heap.prototype.pop = function pop(item) { Heap.prototype.shift = function shift() {
var n; var n;
if (this.items.length === 0) if (this.items.length === 0)
@ -105,6 +107,9 @@ Heap.prototype.remove = function remove(i) {
n = this.items.length - 1; n = this.items.length - 1;
if (i < 0 || i > n)
return;
if (n !== i) { if (n !== i) {
this.swap(i, n); this.swap(i, n);
this.down(i, n); this.down(i, n);
@ -137,7 +142,7 @@ Heap.prototype.swap = function swap(a, b) {
*/ */
Heap.prototype.less = function less(i, j) { Heap.prototype.less = function less(i, j) {
return this.cmp(this.items[i], this.items[j]) >= 0; return this.compare(this.items[i], this.items[j]) < 0;
}; };
/** /**
@ -203,31 +208,41 @@ Heap.prototype.up = function up(i) {
*/ */
Heap.prototype.toArray = function toArray() { Heap.prototype.toArray = function toArray() {
var heap = new Heap(this.cmp); var heap = new Heap();
var result = []; var result = [];
heap.compare = this.compare;
heap.items = this.items.slice(); heap.items = this.items.slice();
while (heap.size() > 0) while (heap.size() > 0)
result.push(heap.pop()); result.push(heap.shift());
return result.reverse(); return result;
}; };
/** /**
* Instantiate heap from array and comparator. * Instantiate heap from array and comparator.
* @param {Function} cmp * @param {Function} compare
* @param {Object[]} items * @param {Object[]} items
* @returns {Heap} * @returns {Heap}
*/ */
Heap.fromArray = function fromArray(cmp, items) { Heap.fromArray = function fromArray(compare, items) {
var heap = new Heap(cmp); var heap = new Heap();
heap.set(compare);
heap.items = items; heap.items = items;
heap.init(); heap.init();
return heap; return heap;
}; };
/*
* Helpers
*/
function comparator(a, b) {
throw new Error('No heap comparator set.');
}
/* /*
* Expose * Expose
*/ */

View File

@ -12,6 +12,7 @@ exports.Bloom = require('./bloom');
exports.RollingFilter = exports.Bloom.Rolling; exports.RollingFilter = exports.Bloom.Rolling;
exports.co = require('./co'); exports.co = require('./co');
exports.encoding = require('./encoding'); exports.encoding = require('./encoding');
exports.Heap = require('./heap');
exports.IP = require('./ip'); exports.IP = require('./ip');
exports.lazy = require('./lazy'); exports.lazy = require('./lazy');
exports.Lock = require('./lock'); exports.Lock = require('./lock');