UNPKG

tstruct

Version:

Data structures & basic algorithms library

55 lines (54 loc) 1.89 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.PriorityQueue = void 0; var Heap_1 = require("../Heap/Heap"); var CompareFunction_1 = require("../CompareFunction"); var PriorityQueue = (function () { function PriorityQueue(getPriority, compareFunction) { if (compareFunction === void 0) { compareFunction = CompareFunction_1.descendingCompareFunction; } var heapCompareFunction = function (e, e2) { var ePriority = getPriority(e); var e2Priority = getPriority(e2); return compareFunction(ePriority, e2Priority); }; this._heap = new Heap_1.Heap(heapCompareFunction); } PriorityQueue.prototype.rearrange = function () { this._heap.rearrange(); }; PriorityQueue.prototype.enqueue = function (item) { this._heap.add(item); }; PriorityQueue.prototype.dequeue = function () { return this._heap.extractRoot(); }; PriorityQueue.prototype.peek = function () { return this._heap.getRoot(); }; PriorityQueue.prototype.toArray = function () { var result = []; while (!this.isEmpty) { result.push(this.dequeue()); } return result; }; Object.defineProperty(PriorityQueue.prototype, "size", { get: function () { return this._heap.size; }, enumerable: false, configurable: true }); Object.defineProperty(PriorityQueue.prototype, "isEmpty", { get: function () { return this.size == 0; }, enumerable: false, configurable: true }); PriorityQueue.prototype[Symbol.iterator] = function () { return this._heap[Symbol.iterator](); }; return PriorityQueue; }()); exports.PriorityQueue = PriorityQueue;