* Checks if the queue contains an element that matches a criteria * @public * @param {function} cb * @returns {boolean}
(cb)
| 111 | * @returns {boolean} |
| 112 | */ |
| 113 | contains(cb) { |
| 114 | if (typeof cb !== 'function') { |
| 115 | throw new Error('PriorityQueue contains expects a callback'); |
| 116 | } |
| 117 | |
| 118 | let found = false; |
| 119 | const dequeued = []; |
| 120 | while (!this.isEmpty()) { |
| 121 | const popped = this.pop(); |
| 122 | dequeued.push(popped); |
| 123 | if (cb(popped)) { |
| 124 | found = true; |
| 125 | break; |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | dequeued.forEach((val) => this.push(val)); |
| 130 | return found; |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Returns the number of elements in the queue |