[utils] add class PriorityQueue
This commit is contained in:
parent
592d993c4e
commit
dd9851674c
@ -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
52
utils/priority-queue.mjs
Normal 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;
|
||||
}
|
||||
};
|
Loading…
x
Reference in New Issue
Block a user