import * as _ from 'lodash-es';
export { PriorityQueue };
class PriorityQueue {
constructor() {
this._arr = [];
this._keyIndices = {};
}
size() {
return this._arr.length;
}
keys() {
return this._arr.map(function (x) {
return x.key;
});
}
has(key) {
return _.has(this._keyIndices, key);
}
priority(key) {
var index = this._keyIndices[key];
if (index !== undefined) {
return this._arr[index].priority;
}
}
min() {
if (this.size() === 0) {
throw new Error('Queue underflow');
}
return this._arr[0].key;
}
add(key, priority) {
var keyIndices = this._keyIndices;
key = String(key);
if (!_.has(keyIndices, key)) {
var arr = this._arr;
var index = arr.length;
keyIndices[key] = index;
arr.push({ key: key, priority: priority });
this._decrease(index);
return true;
}
return false;
}
removeMin() {
this._swap(0, this._arr.length - 1);
var min = this._arr.pop();
delete this._keyIndices[min.key];
this._heapify(0);
return min.key;
}
decrease(key, priority) {
var index = this._keyIndices[key];
if (priority > this._arr[index].priority) {
throw new Error(
'New priority is greater than current priority. ' +
'Key: ' +
key +
' Old: ' +
this._arr[index].priority +
' New: ' +
priority
);
}
this._arr[index].priority = priority;
this._decrease(index);
}
_heapify(i) {
var arr = this._arr;
var l = 2 * i;
var r = l + 1;
var largest = i;
if (l < arr.length) {
largest = arr[l].priority < arr[largest].priority ? l : largest;
if (r < arr.length) {
largest = arr[r].priority < arr[largest].priority ? r : largest;
}
if (largest !== i) {
this._swap(i, largest);
this._heapify(largest);
}
}
}
_decrease(index) {
var arr = this._arr;
var priority = arr[index].priority;
var parent;
while (index !== 0) {
parent = index >> 1;
if (arr[parent].priority < priority) {
break;
}
this._swap(index, parent);
index = parent;
}
}
_swap(i, j) {
var arr = this._arr;
var keyIndices = this._keyIndices;
var origArrI = arr[i];
var origArrJ = arr[j];
arr[i] = origArrJ;
arr[j] = origArrI;
keyIndices[origArrJ.key] = i;
keyIndices[origArrI.key] = j;
}
}