build: bump volta pnpm version
[poolifier.git] / src / deque.ts
index 2eae8034845789b4991c3d1435c24f7050726c42..52441a3ec25f4f7251575104a0884f9ffeb22d00 100644 (file)
@@ -1,19 +1,15 @@
-// Copyright Jerome Benoit. 2023. All Rights Reserved.
+// Copyright Jerome Benoit. 2023-2024. All Rights Reserved.
 
 /**
- * Node.
+ * Linked list node interface.
  *
- * @typeParam T - Type of node data.
+ * @typeParam T - Type of linked list node data.
  * @internal
  */
-export class Node<T> {
-  public data: T
-  public next?: Node<T>
-  public prev?: Node<T>
-
-  public constructor (data: T) {
-    this.data = data
-  }
+export interface ILinkedListNode<T> {
+  data: T
+  next?: ILinkedListNode<T>
+  prev?: ILinkedListNode<T>
 }
 
 /**
@@ -24,8 +20,8 @@ export class Node<T> {
  * @internal
  */
 export class Deque<T> {
-  private head?: Node<T>
-  private tail?: Node<T>
+  private head?: ILinkedListNode<T>
+  private tail?: ILinkedListNode<T>
   /** The size of the deque. */
   public size!: number
   /** The maximum size of the deque. */
@@ -39,10 +35,10 @@ export class Deque<T> {
    * Appends data to the deque.
    *
    * @param data - Data to append.
-   * @returns The new size of the queue.
+   * @returns The new size of the deque.
    */
   public push (data: T): number {
-    const node = new Node(data)
+    const node: ILinkedListNode<T> = { data }
     if (this.tail == null) {
       this.head = this.tail = node
     } else {
@@ -56,10 +52,10 @@ export class Deque<T> {
    * Prepends data to the deque.
    *
    * @param data - Data to prepend.
-   * @returns The new size of the queue.
+   * @returns The new size of the deque.
    */
   public unshift (data: T): number {
-    const node = new Node(data)
+    const node: ILinkedListNode<T> = { data }
     if (this.head == null) {
       this.head = this.tail = node
     } else {
@@ -76,14 +72,14 @@ export class Deque<T> {
    */
   public pop (): T | undefined {
     if (this.head == null) {
-      return undefined
+      return
     }
     const tail = this.tail
-    this.tail = (this.tail as Node<T>).prev
+    this.tail = this.tail?.prev
     if (this.tail == null) {
-      this.head = undefined
+      delete this.head
     } else {
-      this.tail.next = undefined
+      delete this.tail.next
     }
     --this.size
     return tail?.data
@@ -96,17 +92,17 @@ export class Deque<T> {
    */
   public shift (): T | undefined {
     if (this.head == null) {
-      return undefined
+      return
     }
     const head = this.head
     this.head = this.head.next
     if (this.head == null) {
-      this.tail = undefined
+      delete this.tail
     } else {
-      this.head.prev = undefined
+      delete this.head.prev
     }
     --this.size
-    return head?.data
+    return head.data
   }
 
   /**
@@ -129,8 +125,8 @@ export class Deque<T> {
    * Clears the deque.
    */
   public clear (): void {
-    this.head = undefined
-    this.tail = undefined
+    delete this.head
+    delete this.tail
     this.size = 0
     this.maxSize = 0
   }
@@ -155,7 +151,7 @@ export class Deque<T> {
           value: node.data,
           done: false
         }
-        node = node.next as Node<T>
+        node = node.next
         return ret
       }
     }
@@ -183,7 +179,7 @@ export class Deque<T> {
               value: node.data,
               done: false
             }
-            node = node.prev as Node<T>
+            node = node.prev
             return ret
           }
         }