export class PriorityQueue<T> {
private nodeArray!: Array<PriorityQueueNode<T>>
/** Prioritized bucket size. */
- private readonly k: number
+ private readonly bucketSize: number
/** The size of the priority queue. */
public size!: number
/** The maximum size of the priority queue. */
* The number of filled prioritized buckets.
*/
public get buckets (): number {
- return this.k === Infinity ? 1 : Math.trunc(this.nodeArray.length / this.k)
+ return this.bucketSize === Infinity
+ ? 1
+ : Math.trunc(this.nodeArray.length / this.bucketSize)
}
/**
* Constructs a priority queue.
*
- * @param k - Prioritized bucket size. @defaultValue Infinity
+ * @param bucketSize - Prioritized bucket size. @defaultValue Infinity
*/
- public constructor (k = Infinity) {
- if (k !== Infinity && !Number.isSafeInteger(k)) {
- throw new TypeError('k must be an integer')
+ public constructor (bucketSize = Infinity) {
+ if (bucketSize !== Infinity && !Number.isSafeInteger(bucketSize)) {
+ throw new TypeError('bucketSize must be an integer')
}
- if (k < 1) {
- throw new RangeError('k must be greater than or equal to 1')
+ if (bucketSize < 1) {
+ throw new RangeError('bucketSize must be greater than or equal to 1')
}
- this.k = k
+ this.bucketSize = bucketSize
this.clear()
}
*/
public enqueue (data: T, priority?: number): number {
priority = priority ?? 0
- const startIndex = this.k === Infinity ? 0 : this.buckets * this.k
+ const startIndex =
+ this.bucketSize === Infinity ? 0 : this.buckets * this.bucketSize
let inserted = false
for (let index = startIndex; index < this.nodeArray.length; index++) {
if (this.nodeArray[index].priority > priority) {
* @returns The dequeued data or `undefined` if the priority queue is empty.
*/
public dequeue (bucket = 0): T | undefined {
- if (this.k !== Infinity && bucket > 0) {
+ if (this.bucketSize !== Infinity && bucket > 0) {
while (bucket > 0) {
- const index = bucket * this.k
+ const index = bucket * this.bucketSize
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
if (this.nodeArray[index] != null) {
--this.size
expect(workerNode.tasksQueue).toBeInstanceOf(PriorityQueue)
expect(workerNode.tasksQueue.size).toBe(0)
expect(workerNode.tasksQueue.maxSize).toBe(0)
- expect(workerNode.tasksQueue.k).toBe(numberOfWorkers * 2)
+ expect(workerNode.tasksQueue.bucketSize).toBe(numberOfWorkers * 2)
}
await pool.destroy()
pool = new DynamicThreadPool(
expect(workerNode.tasksQueue).toBeInstanceOf(PriorityQueue)
expect(workerNode.tasksQueue.size).toBe(0)
expect(workerNode.tasksQueue.maxSize).toBe(0)
- expect(workerNode.tasksQueue.k).toBe(numberOfWorkers * 2)
+ expect(workerNode.tasksQueue.bucketSize).toBe(numberOfWorkers * 2)
}
await pool.destroy()
})
expect(threadWorkerNode.tasksQueueBackPressureSize).toBe(12)
expect(threadWorkerNode.tasksQueue).toBeInstanceOf(PriorityQueue)
expect(threadWorkerNode.tasksQueue.size).toBe(0)
- expect(threadWorkerNode.tasksQueue.k).toBe(6)
+ expect(threadWorkerNode.tasksQueue.bucketSize).toBe(6)
expect(threadWorkerNode.tasksQueueSize()).toBe(
threadWorkerNode.tasksQueue.size
)
expect(clusterWorkerNode.tasksQueueBackPressureSize).toBe(12)
expect(clusterWorkerNode.tasksQueue).toBeInstanceOf(PriorityQueue)
expect(clusterWorkerNode.tasksQueue.size).toBe(0)
- expect(clusterWorkerNode.tasksQueue.k).toBe(6)
+ expect(clusterWorkerNode.tasksQueue.bucketSize).toBe(6)
expect(clusterWorkerNode.tasksQueueSize()).toBe(
clusterWorkerNode.tasksQueue.size
)
describe('Priority queue test suite', () => {
it('Verify constructor() behavior', () => {
expect(() => new PriorityQueue('')).toThrow(
- new TypeError('k must be an integer')
+ new TypeError('bucketSize must be an integer')
)
expect(() => new PriorityQueue(-1)).toThrow(
- new RangeError('k must be greater than or equal to 1')
+ new RangeError('bucketSize must be greater than or equal to 1')
)
expect(() => new PriorityQueue(0)).toThrow(
- new RangeError('k must be greater than or equal to 1')
+ new RangeError('bucketSize must be greater than or equal to 1')
)
let priorityQueue = new PriorityQueue()
- expect(priorityQueue.k).toBe(Infinity)
+ expect(priorityQueue.bucketSize).toBe(Infinity)
expect(priorityQueue.buckets).toBe(1)
expect(priorityQueue.size).toBe(0)
expect(priorityQueue.maxSize).toBe(0)
expect(priorityQueue.nodeArray).toStrictEqual([])
priorityQueue = new PriorityQueue(2)
- expect(priorityQueue.k).toBe(2)
+ expect(priorityQueue.bucketSize).toBe(2)
expect(priorityQueue.buckets).toBe(0)
expect(priorityQueue.size).toBe(0)
expect(priorityQueue.maxSize).toBe(0)
expect(priorityQueue.nodeArray).toStrictEqual([])
})
- it('Verify default k enqueue() behavior', () => {
+ it('Verify default bucketSize enqueue() behavior', () => {
const priorityQueue = new PriorityQueue()
let rtSize = priorityQueue.enqueue(1)
expect(priorityQueue.buckets).toBe(1)
])
})
- it('Verify k=2 enqueue() behavior', () => {
+ it('Verify bucketSize=2 enqueue() behavior', () => {
const priorityQueue = new PriorityQueue(2)
let rtSize = priorityQueue.enqueue(1)
expect(priorityQueue.buckets).toBe(0)
])
})
- it('Verify default k dequeue() behavior', () => {
+ it('Verify default bucketSize dequeue() behavior', () => {
const priorityQueue = new PriorityQueue()
priorityQueue.enqueue(1)
priorityQueue.enqueue(2, -1)
expect(priorityQueue.nodeArray).toStrictEqual([])
})
- it('Verify k=2 dequeue() behavior', () => {
+ it('Verify bucketSize=2 dequeue() behavior', () => {
const priorityQueue = new PriorityQueue(2)
priorityQueue.enqueue(1)
priorityQueue.enqueue(2)