docs: refine README.md badges
[poolifier.git] / src / priority-queue.ts
index c613cd2e9ee393589a80950bda985bb7004f35ff..2a266d28f6b54bc7c9655de941cb9b5de88a779d 100644 (file)
@@ -40,11 +40,11 @@ export class PriorityQueue<T> {
   ) {
     if (!Number.isSafeInteger(bucketSize)) {
       throw new TypeError(
-        `Invalid bucket size: '${bucketSize}' is not an integer`
+        `Invalid bucket size: '${bucketSize.toString()}' is not an integer`
       )
     }
     if (bucketSize < 0) {
-      throw new RangeError(`Invalid bucket size: ${bucketSize} < 0`)
+      throw new RangeError(`Invalid bucket size: ${bucketSize.toString()} < 0`)
     }
     this.bucketSize = bucketSize
     this.head = this.tail = new FixedPriorityQueue(
@@ -56,6 +56,7 @@ export class PriorityQueue<T> {
 
   /**
    * The priority queue size.
+   * @returns The priority queue size.
    */
   public get size (): number {
     let node: PriorityQueueNode<T> | undefined = this.tail
@@ -84,6 +85,7 @@ export class PriorityQueue<T> {
 
   /**
    * The number of filled prioritized buckets.
+   * @returns The number of filled prioritized buckets.
    */
   public get buckets (): number {
     return Math.trunc(this.size / this.bucketSize)
@@ -126,29 +128,38 @@ export class PriorityQueue<T> {
         }
         ++currentBucket
         tail = tail.next
-        tailChanged = true
       }
+      tailChanged = tail !== this.tail
     }
     // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
     const data = tail!.dequeue()
     // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
-    if (tail!.empty() && tail!.next != null) {
-      if (!tailChanged) {
+    if (tail!.empty()) {
+      // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
+      if (!tailChanged && tail!.next != null) {
         // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
         this.tail = tail!.next
-      } else {
+        // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
+        delete tail!.next
+      } else if (tailChanged) {
         let node: PriorityQueueNode<T> | undefined = this.tail
         while (node != null) {
-          if (node.next === tail) {
+          // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
+          if (node.next === tail && tail!.next != null) {
             // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
             node.next = tail!.next
+            // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
+            delete tail!.next
+            break
+            // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
+          } else if (node.next === tail && tail!.next == null) {
+            delete node.next
+            this.head = node
             break
           }
           node = node.next
         }
       }
-      // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
-      delete tail!.next
     }
     return data
   }