1 | export default class PriorityQueue<T> {
|
2 | private arr: Array<T> = [];
|
3 | private prios: Array<number> = [];
|
4 |
|
5 | public add(t: T, prio: number): void {
|
6 | for (let i = 0; i < this.arr.length; i++) {
|
7 | if (this.prios[i] < prio) {
|
8 | this.arr.splice(i, 0, t);
|
9 | this.prios.splice(i, 0, prio);
|
10 | return;
|
11 | }
|
12 | }
|
13 | this.arr.push(t);
|
14 | this.prios.push(prio);
|
15 | }
|
16 |
|
17 | public forEach(f: (t: T, i: number, arr: Array<T>) => void): void {
|
18 | for (let i = 0; i < this.arr.length; i++) {
|
19 | f(this.arr[i], i, this.arr);
|
20 | }
|
21 | }
|
22 |
|
23 | public delete(t: T): void {
|
24 | for (let i = 0; i < this.arr.length; i++) {
|
25 | if (this.arr[i] === t) {
|
26 | this.arr.splice(i, 1);
|
27 | this.prios.splice(i, 1);
|
28 | return;
|
29 | }
|
30 | }
|
31 | }
|
32 | }
|