diff options
author | Julien Dessaux | 2023-05-14 01:50:19 +0200 |
---|---|---|
committer | Julien Dessaux | 2023-05-14 01:53:57 +0200 |
commit | efdf50a55a32c18c3563b883563f271531a6c38b (patch) | |
tree | 07343aa025294e93ea9207f0da2e437f167af8cd /lib/priority_queue.js | |
parent | Initial import (diff) | |
download | spacetraders-efdf50a55a32c18c3563b883563f271531a6c38b.tar.gz spacetraders-efdf50a55a32c18c3563b883563f271531a6c38b.tar.bz2 spacetraders-efdf50a55a32c18c3563b883563f271531a6c38b.zip |
Implemented a basic extraction loop
Diffstat (limited to '')
-rw-r--r-- | lib/priority_queue.js | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/lib/priority_queue.js b/lib/priority_queue.js new file mode 100644 index 0000000..da526b2 --- /dev/null +++ b/lib/priority_queue.js @@ -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; + } +} |