fix: fix priority queue dequeue() from the last prioritized bucket
authorJérôme Benoit <jerome.benoit@piment-noir.org>
Thu, 20 Jun 2024 11:17:48 +0000 (13:17 +0200)
committerJérôme Benoit <jerome.benoit@piment-noir.org>
Thu, 20 Jun 2024 11:17:48 +0000 (13:17 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@piment-noir.org>
CHANGELOG.md
src/priority-queue.ts
tests/priority-queue.test.mjs

index 3f1fd28f67039c4198da767baf3cdec6769fd172..43cdc02ac9a2e016de94bd6c8520db731529fc56 100644 (file)
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
 
 ## [Unreleased]
 
+### Fixed
+
+- Fix priority queue dequeue() from the last prioritized bucket.
+
 ## [4.0.14] - 2024-06-12
 
 ### Changed
index 18478afefbcb0e90c12f300d07305568f8e4a661..2a266d28f6b54bc7c9655de941cb9b5de88a779d 100644 (file)
@@ -128,29 +128,38 @@ export class PriorityQueue<T> {
         }
         ++currentBucket
         tail = tail.next
-        tailChanged = true
       }
+      tailChanged = tail !== this.tail
     }
     // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
     const data = tail!.dequeue()
     // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
-    if (tail!.empty() && tail!.next != null) {
-      if (!tailChanged) {
+    if (tail!.empty()) {
+      // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
+      if (!tailChanged && tail!.next != null) {
         // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
         this.tail = tail!.next
-      } else {
+        // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
+        delete tail!.next
+      } else if (tailChanged) {
         let node: PriorityQueueNode<T> | undefined = this.tail
         while (node != null) {
-          if (node.next === tail) {
+          // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
+          if (node.next === tail && tail!.next != null) {
             // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
             node.next = tail!.next
+            // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
+            delete tail!.next
+            break
+            // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
+          } else if (node.next === tail && tail!.next == null) {
+            delete node.next
+            this.head = node
             break
           }
           node = node.next
         }
       }
-      // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
-      delete tail!.next
     }
     return data
   }
index 839b6057ba96bc99610f1641b7f2c94acb9ae7ba..80f580d15f88f8f1e811e0acd84baa13f6204138 100644 (file)
@@ -137,6 +137,7 @@ describe('Priority queue test suite', () => {
       { data: 2, priority: 0 },
     ])
     expect(priorityQueue.tail.next).toStrictEqual(priorityQueue.head)
+    expect(priorityQueue.tail).not.toStrictEqual(priorityQueue.head)
     rtSize = priorityQueue.enqueue(3, -1)
     expect(priorityQueue.buckets).toBe(2)
     expect(priorityQueue.size).toBe(4)
@@ -152,6 +153,7 @@ describe('Priority queue test suite', () => {
       { data: 2, priority: 0 },
     ])
     expect(priorityQueue.tail.next).toStrictEqual(priorityQueue.head)
+    expect(priorityQueue.tail).not.toStrictEqual(priorityQueue.head)
     rtSize = priorityQueue.enqueue(1, 1)
     expect(priorityQueue.buckets).toBe(2)
     expect(priorityQueue.size).toBe(5)
@@ -165,11 +167,13 @@ describe('Priority queue test suite', () => {
       { data: 1, priority: 0 },
       { data: 2, priority: 0 },
     ])
-    expect(priorityQueue.tail.next).not.toStrictEqual(priorityQueue.head)
     expect(priorityQueue.tail.next.nodeArray).toMatchObject([
       { data: 3, priority: -1 },
       { data: 3, priority: 0 },
     ])
+    expect(priorityQueue.tail.next.next).toStrictEqual(priorityQueue.head)
+    expect(priorityQueue.tail.next).not.toStrictEqual(priorityQueue.head)
+    expect(priorityQueue.tail).not.toStrictEqual(priorityQueue.head)
     rtSize = priorityQueue.enqueue(3, -2)
     expect(priorityQueue.buckets).toBe(3)
     expect(priorityQueue.size).toBe(6)
@@ -184,11 +188,13 @@ describe('Priority queue test suite', () => {
       { data: 1, priority: 0 },
       { data: 2, priority: 0 },
     ])
-    expect(priorityQueue.tail.next).not.toStrictEqual(priorityQueue.head)
     expect(priorityQueue.tail.next.nodeArray).toMatchObject([
       { data: 3, priority: -1 },
       { data: 3, priority: 0 },
     ])
+    expect(priorityQueue.tail.next.next).toStrictEqual(priorityQueue.head)
+    expect(priorityQueue.tail.next).not.toStrictEqual(priorityQueue.head)
+    expect(priorityQueue.tail).not.toStrictEqual(priorityQueue.head)
   })
 
   it('Verify default bucket size dequeue() behavior', () => {
@@ -201,6 +207,7 @@ describe('Priority queue test suite', () => {
     expect(priorityQueue.maxSize).toBe(3)
     expect(priorityQueue.tail.empty()).toBe(false)
     expect(priorityQueue.tail.next).toBe(undefined)
+    expect(priorityQueue.tail).toStrictEqual(priorityQueue.head)
     let rtItem = priorityQueue.dequeue()
     expect(priorityQueue.buckets).toBe(0)
     expect(priorityQueue.size).toBe(2)
@@ -208,6 +215,7 @@ describe('Priority queue test suite', () => {
     expect(rtItem).toBe(2)
     expect(priorityQueue.tail.empty()).toBe(false)
     expect(priorityQueue.tail.next).toBe(undefined)
+    expect(priorityQueue.tail).toStrictEqual(priorityQueue.head)
     rtItem = priorityQueue.dequeue()
     expect(priorityQueue.buckets).toBe(0)
     expect(priorityQueue.size).toBe(1)
@@ -215,6 +223,7 @@ describe('Priority queue test suite', () => {
     expect(rtItem).toBe(1)
     expect(priorityQueue.tail.empty()).toBe(false)
     expect(priorityQueue.tail.next).toBe(undefined)
+    expect(priorityQueue.tail).toStrictEqual(priorityQueue.head)
     rtItem = priorityQueue.dequeue()
     expect(priorityQueue.buckets).toBe(0)
     expect(priorityQueue.size).toBe(0)
@@ -222,6 +231,7 @@ describe('Priority queue test suite', () => {
     expect(rtItem).toBe(3)
     expect(priorityQueue.tail.empty()).toBe(true)
     expect(priorityQueue.tail.next).toBe(undefined)
+    expect(priorityQueue.tail).toStrictEqual(priorityQueue.head)
   })
 
   it('Verify bucketSize=2 dequeue() behavior', () => {
@@ -271,7 +281,7 @@ describe('Priority queue test suite', () => {
     expect(priorityQueue.maxSize).toBe(6)
     expect(rtItem).toBe(1)
     expect(priorityQueue.tail.empty()).toBe(false)
-    expect(priorityQueue.tail.next).toBeInstanceOf(FixedPriorityQueue)
+    expect(priorityQueue.tail.next).toBe(undefined)
     rtItem = priorityQueue.dequeue()
     expect(priorityQueue.buckets).toBe(0)
     expect(priorityQueue.size).toBe(0)