repositories
/
poolifier.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
refactor: cleanup priority queue size handling
[poolifier.git]
/
src
/
priority-queue.ts
diff --git
a/src/priority-queue.ts
b/src/priority-queue.ts
index 64b5a9248a804130107b810c412bf4c978ae8cd9..e68fe37d16e732efc907562adbb9716b9793c612 100644
(file)
--- a/
src/priority-queue.ts
+++ b/
src/priority-queue.ts
@@
-26,6
+26,13
@@
export class PriorityQueue<T> {
/** The maximum size of the priority queue. */
public maxSize!: number
/** The maximum size of the priority queue. */
public maxSize!: number
+ /**
+ * The number of filled prioritized buckets.
+ */
+ public get buckets (): number {
+ return this.k === Infinity ? 1 : Math.trunc(this.nodeArray.length / this.k)
+ }
+
/**
* Constructs a priority queue.
*
/**
* Constructs a priority queue.
*
@@
-51,10
+58,7
@@
export class PriorityQueue<T> {
*/
public enqueue (data: T, priority?: number): number {
priority = priority ?? 0
*/
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
+ const startIndex = this.k === Infinity ? 0 : this.buckets * this.k
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) {
@@
-87,9
+91,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
+158,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
+ }
}
}