/** 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.
*
- * @param k - Prioritized bucket size.
+ * @param k - Prioritized bucket size. @defaultValue Infinity
*/
public constructor (k = Infinity) {
if (k !== Infinity && !Number.isSafeInteger(k)) {
*/
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) {
--bucket
}
}
- if (this.size > 0) {
- --this.size
- }
+ this.decrementSize()
return this.nodeArray.shift()?.data
}
/**
* Returns an iterator for the priority queue.
*
- * @returns An iterator for the deque.
+ * @returns An iterator for the priority queue.
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols
*/
[Symbol.iterator] (): Iterator<T> {
}
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
+ }
}