From: Jérôme Benoit Date: Sat, 25 May 2024 18:49:34 +0000 (+0200) Subject: fix: fix fixed priority queue array length change X-Git-Tag: v4.0.13~16 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=eadb37e247acfa716cad2a1e211c947513c251bf;p=poolifier.git fix: fix fixed priority queue array length change Signed-off-by: Jérôme Benoit --- diff --git a/src/fixed-priority-queue.ts b/src/fixed-priority-queue.ts index 60096ce0..075986e2 100644 --- a/src/fixed-priority-queue.ts +++ b/src/fixed-priority-queue.ts @@ -14,6 +14,12 @@ export interface PriorityQueueNode { priority: number } +/** + * Fixed priority queue. + * + * @typeParam T - Type of fixed priority queue data. + * @internal + */ export class FixedPriorityQueue { private start!: number private readonly nodeArray: Array> @@ -39,18 +45,21 @@ export class FixedPriorityQueue { 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 } } diff --git a/tests/fixed-priority-queue.test.mjs b/tests/fixed-priority-queue.test.mjs index b70bc17f..7f8e380f 100644 --- a/tests/fixed-priority-queue.test.mjs +++ b/tests/fixed-priority-queue.test.mjs @@ -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) @@ -79,16 +84,22 @@ // { 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) @@ -99,6 +110,7 @@ // { 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) @@ -109,6 +121,7 @@ // { 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) @@ -119,7 +132,18 @@ // { 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', () => {