repositories
/
poolifier.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
Merge dependabot/npm_and_yarn/examples/typescript/websocket-server-pool/ws-cluster...
[poolifier.git]
/
src
/
priority-queue.ts
diff --git
a/src/priority-queue.ts
b/src/priority-queue.ts
index 64b5a9248a804130107b810c412bf4c978ae8cd9..8e9c1ae2db52fb4eba012fcc27be33d48b7e2885 100644
(file)
--- a/
src/priority-queue.ts
+++ b/
src/priority-queue.ts
@@
-20,25
+20,34
@@
export interface PriorityQueueNode<T> {
export class PriorityQueue<T> {
private nodeArray!: Array<PriorityQueueNode<T>>
/** Prioritized bucket size. */
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. */
public maxSize!: number
/** The size of the priority queue. */
public size!: number
/** The maximum size of the priority queue. */
public maxSize!: number
+ /**
+ * The number of filled prioritized buckets.
+ */
+ public get buckets (): number {
+ return this.bucketSize === Infinity
+ ? 1
+ : Math.trunc(this.nodeArray.length / this.bucketSize)
+ }
+
/**
* Constructs a priority queue.
*
/**
* 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()
}
this.clear()
}
@@
-52,9
+61,7
@@
export class PriorityQueue<T> {
public enqueue (data: T, priority?: number): number {
priority = priority ?? 0
const startIndex =
public enqueue (data: T, priority?: number): number {
priority = priority ?? 0
const startIndex =
- this.k === Infinity || this.nodeArray.length / this.k < 1
- ? 0
- : Math.trunc(this.nodeArray.length / this.k) * this.k
+ 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) {
let inserted = false
for (let index = startIndex; index < this.nodeArray.length; index++) {
if (this.nodeArray[index].priority > priority) {
@@
-76,9
+83,9
@@
export class PriorityQueue<T> {
* @returns The dequeued data or `undefined` if the priority queue is empty.
*/
public dequeue (bucket = 0): T | undefined {
* @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) {
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
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
if (this.nodeArray[index] != null) {
--this.size
@@
-87,9
+94,7
@@
export class PriorityQueue<T> {
--bucket
}
}
--bucket
}
}
- if (this.size > 0) {
- --this.size
- }
+ this.decrementSize()
return this.nodeArray.shift()?.data
}
return this.nodeArray.shift()?.data
}
@@
-156,4
+161,16
@@
export class PriorityQueue<T> {
}
return this.size
}
}
return this.size
}
+
+ /**
+ * Decrements the size of the priority queue.
+ *
+ * @returns The new size of the priority queue.
+ */
+ private decrementSize (): number {
+ if (this.size > 0) {
+ --this.size
+ }
+ return this.size
+ }
}
}