* Removes all elements that match a criteria in the callback * @public * @param {function} cb * @returns {array}
(cb)
| 85 | * @returns {array} |
| 86 | */ |
| 87 | remove(cb) { |
| 88 | if (typeof cb !== 'function') { |
| 89 | throw new Error('PriorityQueue remove expects a callback'); |
| 90 | } |
| 91 | |
| 92 | const removed = []; |
| 93 | const dequeued = []; |
| 94 | while (!this.isEmpty()) { |
| 95 | const popped = this.pop(); |
| 96 | if (cb(popped)) { |
| 97 | removed.push(popped); |
| 98 | } else { |
| 99 | dequeued.push(popped); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | dequeued.forEach((val) => this.push(val)); |
| 104 | return removed; |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Checks if the queue contains an element that matches a criteria |