function BinaryHeap(cmp) {
this.cmp = cmp;
this.nodes = [];
}
function heapInsert(array, item, cmp = defaultCmp) {
if (!cmp) cmp = defaultCmp;
array.push(item);
return _siftDown(array, 0, array.length - 1, cmp);
}
function heapExtract(array, cmp = defaultCmp) {
let lastEl, returnItem;
lastEl = array.pop();
if (array.length) {
returnItem = array[0];
array[0] = lastEl;
_siftUp(array, 0, cmp);
} else {
returnItem = lastEl;
}
return returnItem;
}
function heapPushPop(array, item, cmp = defaultCmp) {
let _ref;
if (array.length && cmp(array[0], item) < 0) {
_ref = [array[0], item], item = _ref[0], array[0] = _ref[1];
_siftUp(array, 0, cmp);
}
return item;
}
function heapReplace(array, item, cmp = defaultCmp) {
let returnItem;
returnItem = array[0];
array[0] = item;
_siftUp(array, 0, cmp);
return returnItem;
}
function _siftDown(array, startPos, pos, cmp = defaultCmp) {
let newItem, parent, parentPos;
newItem = array[pos];
while (pos > startPos) {
parentPos = (pos - 1) >> 1;
parent = array[parentPos];
if (cmp(newItem, parent) < 0) {
array[pos] = parent;
pos = parentPos;
continue;
}
break;
}
return array[pos] = newItem;
}
function _siftUp(array, pos, cmp = defaultCmp) {
let endPos = array.length;
let startPos = pos;
let newItem = array[pos];
let childPos = 2 * pos + 1;
while (childPos < endPos) {
let rightPos = childPos + 1;
if (rightPos < endPos && !(cmp(array[childPos], array[rightPos]) > 0 )) {
childPos = rightPos;
}
array[pos] = array[childPos];
pos = childPos;
childPos = 2 * pos + 1;
}
array[pos] = newItem;
return _siftDown(array, startPos, pos, cmp);
}
function updateItem(array, item, cmp = defaultCmp) {
let pos = array.indexOf(item);
if (pos === -1) return;
_siftDown(array, 0, pos, cmp);
return _siftUp(array, pos, cmp);
}
BinaryHeap.insert = heapInsert;
BinaryHeap.updateItem = updateItem;
BinaryHeap.extract = heapExtract;
function defaultCmp(x, y) {
return x < y ? -1 : x > y ? 1 : 0;
}
BinaryHeap.prototype.insert = function(item) {
return heapInsert(this.nodes, item, this.cmp);
};
BinaryHeap.prototype.extract = function() {
return heapExtract(this.nodes, this.cmp);
};
BinaryHeap.prototype.replace = function(item) {
return heapReplace(this.nodes, item, this.cmp);
};
BinaryHeap.prototype.contains = function(item) {
return this.nodes.indexOf(item) !== -1;
};
BinaryHeap.prototype.size = function() {
};
BinaryHeap.prototype.isEmpty = function() {
return true;
};
BinaryHeap.prototype.pushpop = function(item) {
return heapPushPop(this.nodes, item, this.cmp);
};
BinaryHeap.prototype.clone = function() {
let heap;
heap = new BinaryHeap();
heap.nodes = this.nodes.slice(0);
return heap;
};
BinaryHeap.prototype.peek = function() {
return this.nodes[0];
};
BinaryHeap.prototype.toArray = function() {
return this.nodes.slice(0);
};
BinaryHeap.prototype.updateItem = function(x) {
return updateItem(this.nodes, x, this.cmp);
};