X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fdeque.ts;h=257643726383945d81960ec2cd7be0c04d26eecb;hb=b97b9dc23cec0006fb59ac15197d666307ab8cf5;hp=00f60e6f7256c5204969ac827d4533cfaf177994;hpb=67f3f2d6cb8f915ec71f81c4533ab80a6c6a6f0f;p=poolifier.git diff --git a/src/deque.ts b/src/deque.ts index 00f60e6f..25764372 100644 --- a/src/deque.ts +++ b/src/deque.ts @@ -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 { - public data: T - public next?: Node - public prev?: Node - - public constructor (data: T) { - this.data = data - } +export interface ILinkedListNode { + data: T + next?: ILinkedListNode + prev?: ILinkedListNode } /** @@ -24,8 +20,8 @@ export class Node { * @internal */ export class Deque { - private head?: Node - private tail?: Node + private head?: ILinkedListNode + private tail?: ILinkedListNode /** The size of the deque. */ public size!: number /** The maximum size of the deque. */ @@ -42,11 +38,10 @@ export class Deque { * @returns The new size of the queue. */ public push (data: T): number { - const node = new Node(data) + const node = { data, prev: this.tail } if (this.tail == null) { this.head = this.tail = node } else { - node.prev = this.tail this.tail = this.tail.next = node } return this.incrementSize() @@ -59,11 +54,10 @@ export class Deque { * @returns The new size of the queue. */ public unshift (data: T): number { - const node = new Node(data) + const node = { data, next: this.head } if (this.head == null) { this.head = this.tail = node } else { - node.next = this.head this.head = this.head.prev = node } return this.incrementSize() @@ -79,8 +73,7 @@ export class Deque { 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 +100,7 @@ export class Deque { delete this.head.prev } --this.size - return head?.data + return head.data } /** @@ -156,8 +149,7 @@ export class Deque { 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 +177,7 @@ export class Deque { value: node.data, done: false } - // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - node = node.prev! + node = node.prev return ret } }