* @class PriorityQueue
| 9 | * @class PriorityQueue |
| 10 | */ |
| 11 | class PriorityQueue { |
| 12 | /** |
| 13 | * Creates a priority queue |
| 14 | * @params {function} compare |
| 15 | * @params {array} [values] |
| 16 | */ |
| 17 | constructor(compare, values) { |
| 18 | if (typeof compare !== 'function') { |
| 19 | throw new Error('PriorityQueue constructor expects a compare function'); |
| 20 | } |
| 21 | this._heap = new Heap(compare, values); |
| 22 | } |
| 23 | |
| 24 | /** |
| 25 | * Returns an element with highest priority in the queue |
| 26 | * @public |
| 27 | * @returns {number|string|object} |
| 28 | */ |
| 29 | front() { |
| 30 | return this._heap.root(); |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * Returns an element with lowest priority in the queue |
| 35 | * @public |
| 36 | * @returns {number|string|object} |
| 37 | */ |
| 38 | back() { |
| 39 | return this._heap.leaf(); |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Adds a value to the queue |
| 44 | * @public |
| 45 | * @param {number|string|object} value |
| 46 | * @returns {PriorityQueue} |
| 47 | */ |
| 48 | enqueue(value) { |
| 49 | this._heap.insert(value); |
| 50 | return this; |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Adds a value to the queue |
| 55 | * @public |
| 56 | * @param {number|string|object} value |
| 57 | * @returns {PriorityQueue} |
| 58 | */ |
| 59 | push(value) { |
| 60 | return this.enqueue(value); |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Removes and returns an element with highest priority in the queue |
| 65 | * @public |
| 66 | * @returns {number|string|object} |
| 67 | */ |
| 68 | dequeue() { |
nothing calls this directly
no outgoing calls
no test coverage detected