heap: minor refactor.
This commit is contained in:
parent
e49e877f17
commit
b73b2fba85
@ -409,11 +409,11 @@ Miner.prototype.build = function build(attempt) {
|
||||
if (item.depCount > 0)
|
||||
continue;
|
||||
|
||||
queue.push(item);
|
||||
queue.insert(item);
|
||||
}
|
||||
|
||||
while (queue.size() > 0) {
|
||||
item = queue.pop();
|
||||
item = queue.shift();
|
||||
tx = item.tx;
|
||||
hash = item.hash;
|
||||
weight = attempt.weight;
|
||||
@ -441,7 +441,7 @@ Miner.prototype.build = function build(attempt) {
|
||||
queue.set(cmpRate);
|
||||
queue.init();
|
||||
priority = false;
|
||||
queue.push(item);
|
||||
queue.insert(item);
|
||||
continue;
|
||||
}
|
||||
} else {
|
||||
@ -464,7 +464,7 @@ Miner.prototype.build = function build(attempt) {
|
||||
for (j = 0; j < deps.length; j++) {
|
||||
item = deps[j];
|
||||
if (--item.depCount === 0)
|
||||
queue.push(item);
|
||||
queue.insert(item);
|
||||
}
|
||||
}
|
||||
|
||||
@ -621,11 +621,11 @@ MinerOptions.fromOptions = function fromOptions(options) {
|
||||
*/
|
||||
|
||||
function cmpPriority(a, b) {
|
||||
return a.priority - b.priority;
|
||||
return b.priority - a.priority;
|
||||
}
|
||||
|
||||
function cmpRate(a, b) {
|
||||
return a.descRate - b.descRate;
|
||||
return b.descRate - a.descRate;
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
/*!
|
||||
* 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
|
||||
*/
|
||||
|
||||
@ -9,19 +9,21 @@
|
||||
var assert = require('assert');
|
||||
|
||||
/**
|
||||
* Priority Queue
|
||||
* Binary Heap
|
||||
* @alias module:utils.Heap
|
||||
* @constructor
|
||||
* @param {Function?} cmp
|
||||
* @param {Function?} compare
|
||||
*/
|
||||
|
||||
function Heap(cmp) {
|
||||
function Heap(compare) {
|
||||
if (!(this instanceof Heap))
|
||||
return new Heap(cmp);
|
||||
return new Heap(compare);
|
||||
|
||||
this.cmp = null;
|
||||
this.compare = comparator;
|
||||
this.items = [];
|
||||
this.set(cmp);
|
||||
|
||||
if (compare)
|
||||
this.set(compare);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -50,13 +52,13 @@ Heap.prototype.size = function size() {
|
||||
|
||||
/**
|
||||
* Set comparator.
|
||||
* @param {Function} cmp
|
||||
* @param {Function} compare
|
||||
*/
|
||||
|
||||
Heap.prototype.set = function set(cmp) {
|
||||
assert(cmp == null || typeof cmp === 'function',
|
||||
Heap.prototype.set = function set(compare) {
|
||||
assert(typeof compare === '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}
|
||||
*/
|
||||
|
||||
Heap.prototype.push = function push(item) {
|
||||
Heap.prototype.insert = function insert(item) {
|
||||
this.items.push(item);
|
||||
this.up(this.items.length - 1);
|
||||
return this.items.length;
|
||||
@ -77,7 +79,7 @@ Heap.prototype.push = function push(item) {
|
||||
* @returns {Object}
|
||||
*/
|
||||
|
||||
Heap.prototype.pop = function pop(item) {
|
||||
Heap.prototype.shift = function shift() {
|
||||
var n;
|
||||
|
||||
if (this.items.length === 0)
|
||||
@ -105,6 +107,9 @@ Heap.prototype.remove = function remove(i) {
|
||||
|
||||
n = this.items.length - 1;
|
||||
|
||||
if (i < 0 || i > n)
|
||||
return;
|
||||
|
||||
if (n !== i) {
|
||||
this.swap(i, n);
|
||||
this.down(i, n);
|
||||
@ -137,7 +142,7 @@ Heap.prototype.swap = function swap(a, b) {
|
||||
*/
|
||||
|
||||
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() {
|
||||
var heap = new Heap(this.cmp);
|
||||
var heap = new Heap();
|
||||
var result = [];
|
||||
|
||||
heap.compare = this.compare;
|
||||
heap.items = this.items.slice();
|
||||
|
||||
while (heap.size() > 0)
|
||||
result.push(heap.pop());
|
||||
result.push(heap.shift());
|
||||
|
||||
return result.reverse();
|
||||
return result;
|
||||
};
|
||||
|
||||
/**
|
||||
* Instantiate heap from array and comparator.
|
||||
* @param {Function} cmp
|
||||
* @param {Function} compare
|
||||
* @param {Object[]} items
|
||||
* @returns {Heap}
|
||||
*/
|
||||
|
||||
Heap.fromArray = function fromArray(cmp, items) {
|
||||
var heap = new Heap(cmp);
|
||||
Heap.fromArray = function fromArray(compare, items) {
|
||||
var heap = new Heap();
|
||||
heap.set(compare);
|
||||
heap.items = items;
|
||||
heap.init();
|
||||
return heap;
|
||||
};
|
||||
|
||||
/*
|
||||
* Helpers
|
||||
*/
|
||||
|
||||
function comparator(a, b) {
|
||||
throw new Error('No heap comparator set.');
|
||||
}
|
||||
|
||||
/*
|
||||
* Expose
|
||||
*/
|
||||
|
||||
@ -12,6 +12,7 @@ exports.Bloom = require('./bloom');
|
||||
exports.RollingFilter = exports.Bloom.Rolling;
|
||||
exports.co = require('./co');
|
||||
exports.encoding = require('./encoding');
|
||||
exports.Heap = require('./heap');
|
||||
exports.IP = require('./ip');
|
||||
exports.lazy = require('./lazy');
|
||||
exports.Lock = require('./lock');
|
||||
|
||||
Loading…
Reference in New Issue
Block a user