1
0
Fork 0

Implemented a basic extraction loop

This commit is contained in:
Julien Dessaux 2023-05-14 01:50:19 +02:00
parent f190aea975
commit efdf50a55a
Signed by: adyxax
GPG key ID: F92E51B86E07177E
12 changed files with 2022 additions and 0 deletions

39
lib/priority_queue.js Normal file
View file

@ -0,0 +1,39 @@
export class QElement {
constructor(element, priority) {
this.element = element;
this.priority = priority;
}
}
export class PriorityQueue {
constructor(elt) {
this.items = [];
if (elt !== undefined) {
this.enqueue(elt, 0);
}
}
enqueue(element, priority) {
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() {
return this.items.shift(); // we would use pop to get the highest priority, shift() gives us the lowest priority
}
front() {
return this.items[0];
}
rear() {
return this.items[this.items.length - 1];
}
isEmpty() {
return this.items.length === 0;
}
}