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>>
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 }
}
// 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')
// })
// 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)
// 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)
// { 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)
// { 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)
// { 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', () => {