[node] begin the great typescript rewrite
This commit is contained in:
parent
3b61a9694d
commit
d668eac4a6
31 changed files with 879 additions and 666 deletions
36
nodejs/lib/priority_queue.ts
Normal file
36
nodejs/lib/priority_queue.ts
Normal file
|
@ -0,0 +1,36 @@
|
|||
class QElement {
|
||||
element: unknown;
|
||||
priority: number;
|
||||
constructor(element: unknown, priority: number) {
|
||||
this.element = element;
|
||||
this.priority = priority;
|
||||
}
|
||||
}
|
||||
|
||||
export class PriorityQueue {
|
||||
items: Array<QElement>;
|
||||
|
||||
constructor(elt?: unknown) {
|
||||
this.items = [];
|
||||
if (elt !== undefined) this.enqueue(elt, 0);
|
||||
}
|
||||
|
||||
enqueue(element: unknown, priority: number): void {
|
||||
let qElement = new QElement(element, priority);
|
||||
|
||||
for (let i = 0; i < this.items.length; ++i) {
|
||||
if (this.items[i].priority > qElement.priority) {
|
||||
this.items.splice(i, 0, qElement);
|
||||
return;
|
||||
}
|
||||
}
|
||||
this.items.push(qElement);
|
||||
}
|
||||
dequeue(): unknown {
|
||||
// we would use pop to get the highest priority, shift() gives us the lowest priority
|
||||
return this.items.shift()?.element;
|
||||
}
|
||||
isEmpty(): boolean {
|
||||
return this.items.length === 0;
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue