refactor: cleanup deque implementation
[poolifier.git] / src / deque.ts
index 00f60e6f7256c5204969ac827d4533cfaf177994..44431789f8808f96458fd200e24e6ac31c92ff5b 100644 (file)
@@ -1,15 +1,15 @@
 // Copyright Jerome Benoit. 2023. All Rights Reserved.
 
 /**
- * Node.
+ * Linked list node.
  *
- * @typeParam T - Type of node data.
+ * @typeParam T - Type of linked list node data.
  * @internal
  */
-export class Node<T> {
+export class LinkedListNode<T> {
   public data: T
-  public next?: Node<T>
-  public prev?: Node<T>
+  public next?: LinkedListNode<T>
+  public prev?: LinkedListNode<T>
 
   public constructor (data: T) {
     this.data = data
@@ -24,8 +24,8 @@ export class Node<T> {
  * @internal
  */
 export class Deque<T> {
-  private head?: Node<T>
-  private tail?: Node<T>
+  private head?: LinkedListNode<T>
+  private tail?: LinkedListNode<T>
   /** The size of the deque. */
   public size!: number
   /** The maximum size of the deque. */
@@ -42,7 +42,7 @@ export class Deque<T> {
    * @returns The new size of the queue.
    */
   public push (data: T): number {
-    const node = new Node(data)
+    const node = new LinkedListNode(data)
     if (this.tail == null) {
       this.head = this.tail = node
     } else {
@@ -59,7 +59,7 @@ export class Deque<T> {
    * @returns The new size of the queue.
    */
   public unshift (data: T): number {
-    const node = new Node(data)
+    const node = new LinkedListNode(data)
     if (this.head == null) {
       this.head = this.tail = node
     } else {
@@ -79,8 +79,7 @@ export class Deque<T> {
       return
     }
     const tail = this.tail
-    // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
-    this.tail = this.tail!.prev
+    this.tail = this.tail?.prev
     if (this.tail == null) {
       delete this.head
     } else {
@@ -107,7 +106,7 @@ export class Deque<T> {
       delete this.head.prev
     }
     --this.size
-    return head?.data
+    return head.data
   }
 
   /**
@@ -156,8 +155,7 @@ export class Deque<T> {
           value: node.data,
           done: false
         }
-        // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
-        node = node.next!
+        node = node.next
         return ret
       }
     }
@@ -185,8 +183,7 @@ export class Deque<T> {
               value: node.data,
               done: false
             }
-            // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
-            node = node.prev!
+            node = node.prev
             return ret
           }
         }