fix: fix fixed priority queue array length change
authorJérôme Benoit <jerome.benoit@piment-noir.org>
Sat, 25 May 2024 18:49:34 +0000 (20:49 +0200)
committerJérôme Benoit <jerome.benoit@piment-noir.org>
Sat, 25 May 2024 18:49:34 +0000 (20:49 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@piment-noir.org>
src/fixed-priority-queue.ts
tests/fixed-priority-queue.test.mjs

index 60096ce04e9c77da42639420ba4a4a9675b9d2ea..075986e26b09acbd5b30f1d3247725f6ccc52588 100644 (file)
@@ -14,6 +14,12 @@ export interface PriorityQueueNode<T> {
   priority: number
 }
 
+/**
+ * Fixed priority queue.
+ *
+ * @typeParam T - Type of fixed priority queue data.
+ * @internal
+ */
 export class FixedPriorityQueue<T> {
   private start!: number
   private readonly nodeArray: Array<PriorityQueueNode<T>>
@@ -39,18 +45,21 @@ export class FixedPriorityQueue<T> {
       throw new Error('Priority queue is full')
     }
     priority = priority ?? 0
+    const nodeArrayLength = this.nodeArray.length
     let inserted = false
-    for (let index = this.start; index < this.nodeArray.length; index++) {
+    for (let index = this.start; index < nodeArrayLength; index++) {
       if (this.nodeArray[index]?.priority > priority) {
         this.nodeArray.splice(index, 0, { data, priority })
         inserted = true
         break
       }
     }
+    this.nodeArray.length !== nodeArrayLength &&
+      (this.nodeArray.length = nodeArrayLength)
     if (!inserted) {
       let index = this.start + this.size
-      if (index >= this.nodeArray.length) {
-        index -= this.nodeArray.length
+      if (index >= nodeArrayLength) {
+        index -= nodeArrayLength
       }
       this.nodeArray[index] = { data, priority }
     }
index b70bc17f7e760072bafa4fc9c381b4703c2337ad..7f8e380f013f4a8af1970021f93223259a46419d 100644 (file)
@@ -5,7 +5,7 @@
 //   FixedPriorityQueue
 // } from '../lib/fixed-priority-queue.cjs'
 
-// describe('Fixed Priority queue test suite', () => {
+// describe('Fixed priority queue test suite', () => {
 //   it('Verify constructor() behavior', () => {
 //     expect(() => new FixedPriorityQueue('')).toThrow(
 //       new TypeError('Invalid fixed priority queue size:  is not an integer')
@@ -28,7 +28,8 @@
 //   })
 
 //   it('Verify enqueue() behavior', () => {
-//     const fixedPriorityQueue = new FixedPriorityQueue()
+//     const queueSize = 5
+//     const fixedPriorityQueue = new FixedPriorityQueue(queueSize)
 //     let rtSize = fixedPriorityQueue.enqueue(1)
 //     expect(fixedPriorityQueue.start).toBe(0)
 //     expect(fixedPriorityQueue.size).toBe(1)
@@ -37,6 +38,7 @@
 //     expect(fixedPriorityQueue.nodeArray).toMatchObject([
 //       { data: 1, priority: 0 }
 //     ])
+//     expect(fixedPriorityQueue.nodeArray.length).toBe(queueSize)
 //     rtSize = fixedPriorityQueue.enqueue(2)
 //     expect(fixedPriorityQueue.start).toBe(0)
 //     expect(fixedPriorityQueue.size).toBe(2)
@@ -46,6 +48,7 @@
 //       { data: 1, priority: 0 },
 //       { data: 2, priority: 0 }
 //     ])
+//     expect(fixedPriorityQueue.nodeArray.length).toBe(queueSize)
 //     rtSize = fixedPriorityQueue.enqueue(3)
 //     expect(fixedPriorityQueue.start).toBe(0)
 //     expect(fixedPriorityQueue.size).toBe(3)
@@ -56,6 +59,7 @@
 //       { data: 2, priority: 0 },
 //       { data: 3, priority: 0 }
 //     ])
+//     expect(fixedPriorityQueue.nodeArray.length).toBe(queueSize)
 //     rtSize = fixedPriorityQueue.enqueue(3, -1)
 //     expect(fixedPriorityQueue.start).toBe(0)
 //     expect(fixedPriorityQueue.size).toBe(4)
@@ -67,6 +71,7 @@
 //       { data: 2, priority: 0 },
 //       { data: 3, priority: 0 }
 //     ])
+//     expect(fixedPriorityQueue.nodeArray.length).toBe(queueSize)
 //     rtSize = fixedPriorityQueue.enqueue(1, 1)
 //     expect(fixedPriorityQueue.start).toBe(0)
 //     expect(fixedPriorityQueue.size).toBe(5)
 //       { data: 3, priority: 0 },
 //       { data: 1, priority: 1 }
 //     ])
+//     expect(fixedPriorityQueue.nodeArray.length).toBe(queueSize)
+//     expect(() => fixedPriorityQueue.enqueue(4)).toThrow(
+//       new Error('Priority queue is full')
+//     )
 //   })
 
 //   it('Verify dequeue() behavior', () => {
-//     const fixedPriorityQueue = new FixedPriorityQueue()
+//     const queueSize = 5
+//     const fixedPriorityQueue = new FixedPriorityQueue(queueSize)
 //     fixedPriorityQueue.enqueue(1)
 //     fixedPriorityQueue.enqueue(2, -1)
 //     fixedPriorityQueue.enqueue(3)
 //     expect(fixedPriorityQueue.start).toBe(0)
 //     expect(fixedPriorityQueue.size).toBe(3)
 //     expect(fixedPriorityQueue.maxSize).toBe(3)
+//     expect(fixedPriorityQueue.nodeArray.length).toBe(queueSize)
 //     let rtItem = fixedPriorityQueue.dequeue()
 //     expect(fixedPriorityQueue.start).toBe(1)
 //     expect(fixedPriorityQueue.size).toBe(2)
 //       { data: 1, priority: 0 },
 //       { data: 3, priority: 0 }
 //     ])
+//     expect(fixedPriorityQueue.nodeArray.length).toBe(queueSize)
 //     rtItem = fixedPriorityQueue.dequeue()
 //     expect(fixedPriorityQueue.start).toBe(2)
 //     expect(fixedPriorityQueue.size).toBe(1)
 //       { data: 1, priority: 0 },
 //       { data: 3, priority: 0 }
 //     ])
+//     expect(fixedPriorityQueue.nodeArray.length).toBe(queueSize)
 //     rtItem = fixedPriorityQueue.dequeue()
 //     expect(fixedPriorityQueue.start).toBe(3)
 //     expect(fixedPriorityQueue.size).toBe(0)
 //       { data: 1, priority: 0 },
 //       { data: 3, priority: 0 }
 //     ])
-//     expect(fixedPriorityQueue.dequeue()).toBe(undefined)
+//     expect(fixedPriorityQueue.nodeArray.length).toBe(queueSize)
+//     rtItem = fixedPriorityQueue.dequeue()
+//     expect(fixedPriorityQueue.start).toBe(3)
+//     expect(fixedPriorityQueue.size).toBe(0)
+//     expect(fixedPriorityQueue.maxSize).toBe(3)
+//     expect(rtItem).toBe(undefined)
+//     expect(fixedPriorityQueue.nodeArray).toMatchObject([
+//       { data: 2, priority: -1 },
+//       { data: 1, priority: 0 },
+//       { data: 3, priority: 0 }
+//     ])
+//     expect(fixedPriorityQueue.nodeArray.length).toBe(queueSize)
 //   })
 
 //   it('Verify iterator behavior', () => {