41 lines
1.8 KiB
JavaScript
41 lines
1.8 KiB
JavaScript
|
|
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
||
|
|
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
||
|
|
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
||
|
|
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
||
|
|
};
|
||
|
|
var _PriorityQueue_queue;
|
||
|
|
import lowerBound from './lower-bound.js';
|
||
|
|
class PriorityQueue {
|
||
|
|
constructor() {
|
||
|
|
_PriorityQueue_queue.set(this, []);
|
||
|
|
}
|
||
|
|
enqueue(run, options) {
|
||
|
|
options = {
|
||
|
|
priority: 0,
|
||
|
|
...options,
|
||
|
|
};
|
||
|
|
const element = {
|
||
|
|
priority: options.priority,
|
||
|
|
run,
|
||
|
|
};
|
||
|
|
if (this.size && __classPrivateFieldGet(this, _PriorityQueue_queue, "f")[this.size - 1].priority >= options.priority) {
|
||
|
|
__classPrivateFieldGet(this, _PriorityQueue_queue, "f").push(element);
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
const index = lowerBound(__classPrivateFieldGet(this, _PriorityQueue_queue, "f"), element, (a, b) => b.priority - a.priority);
|
||
|
|
__classPrivateFieldGet(this, _PriorityQueue_queue, "f").splice(index, 0, element);
|
||
|
|
}
|
||
|
|
dequeue() {
|
||
|
|
const item = __classPrivateFieldGet(this, _PriorityQueue_queue, "f").shift();
|
||
|
|
return item === null || item === void 0 ? void 0 : item.run;
|
||
|
|
}
|
||
|
|
filter(options) {
|
||
|
|
return __classPrivateFieldGet(this, _PriorityQueue_queue, "f").filter((element) => element.priority === options.priority).map((element) => element.run);
|
||
|
|
}
|
||
|
|
get size() {
|
||
|
|
return __classPrivateFieldGet(this, _PriorityQueue_queue, "f").length;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
_PriorityQueue_queue = new WeakMap();
|
||
|
|
export default PriorityQueue;
|