[utils] add class PriorityQueue

This commit is contained in:
方而静 2023-11-02 09:17:34 +08:00
parent 592d993c4e
commit dd9851674c
Signed by: szTom
GPG Key ID: 072D999D60C6473C
2 changed files with 53 additions and 0 deletions

View File

@ -4,3 +4,4 @@ export { readJsonFile, writeJsonFile, parseLogin, waitEvent,
export { Queue, QueueEmptyError } from './queue.mjs';
export { AsyncLock } from './async-lock.mjs';
export { Task, TaskInteruptedError } from './task.mjs';
export { PriorityQueue } from './priority-queue.mjs';

52
utils/priority-queue.mjs Normal file
View File

@ -0,0 +1,52 @@
export class PriorityQueue {
constructor() {
this.val = [null];
}
push(value, priority) {
if (priority == null) {
priority = value;
}
this.val.push([value, priority]);
let id = this.val.length - 1;
while (id > 1 && this.val[id][1] > this.val[id >>> 1][1]) {
const kid = id >>> 1;
[this.val[id], this.val[kid]] = [this.val[kid], this.val[id]];
id = kid;
}
return this;
}
pop() {
let lv = this.val.pop();
if (this.val.length == 1) { return lv; }
let res = this.val[1];
this.val[1] = lv;
let id = 1;
while (id * 2 < this.val.length) {
if (this.val[id][1] > this.val[id * 2][1] && (id * 2 + 1 >= this.val.length || this.val[id][1] > this.val[id * 2 + 1][1])) {
break;
}
let kid = (id * 2 + 1 >= this.val.length || this.val[id * 2][1] > this.val[id * 2 + 1][1]) ? id * 2 : id * 2 + 1;
[this.val[id], this.val[kid]] = [this.val[kid], this.val[id]];
id = kid;
}
return res;
}
top() {
return this.val[1];
}
size() {
return this.val.length - 1;
}
empty() {
return this.size() == 0;
}
};