X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fdeque.ts;h=fe82a79896a08757e3f261b66aa364b1407ab6e9;hb=efc4d37dd21d0c80e5fa2d58009e6298eefe5d48;hp=9be270ffa8d6f2e95f1d045b07459d595576d30a;hpb=069e56f4a71b48e89eeb1b4b2ccf693b75116f07;p=poolifier.git diff --git a/src/deque.ts b/src/deque.ts index 9be270ff..fe82a798 100644 --- a/src/deque.ts +++ b/src/deque.ts @@ -1,15 +1,18 @@ // Copyright Jerome Benoit. 2023. All Rights Reserved. /** + * Node. + * + * @typeParam T - Type of node data. * @internal */ export class Node { - public value: T + public data: T public next?: Node public prev?: Node - public constructor (value: T) { - this.value = value + public constructor (data: T) { + this.data = data } } @@ -17,7 +20,7 @@ export class Node { * Deque. * Implemented with a doubly linked list. * - * @typeParam T - Type of deque values. + * @typeParam T - Type of deque data. * @internal */ export class Deque { @@ -33,13 +36,13 @@ export class Deque { } /** - * Appends a value to the deque. + * Appends data to the deque. * - * @param value - Value to append. + * @param data - Data to append. * @returns The new size of the queue. */ - public push (value: T): number { - const node = new Node(value) + public push (data: T): number { + const node = new Node(data) if (this.tail == null) { this.head = this.tail = node } else { @@ -50,13 +53,13 @@ export class Deque { } /** - * Prepends a value to the deque. + * Prepends data to the deque. * - * @param value - Value to prepend. + * @param data - Data to prepend. * @returns The new size of the queue. */ - public unshift (value: T): number { - const node = new Node(value) + public unshift (data: T): number { + const node = new Node(data) if (this.head == null) { this.head = this.tail = node } else { @@ -67,67 +70,67 @@ export class Deque { } /** - * Pops a value from the deque. + * Pops data from the deque. * - * @returns The popped value or `undefined` if the deque is empty. + * @returns The popped data or `undefined` if the deque is empty. */ public pop (): T | undefined { if (this.head == null) { - return undefined + return } const tail = this.tail - this.tail = (this.tail as Node).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?.value + return tail?.data } /** - * Shifts a value from the deque. + * Shifts data from the deque. * - * @returns The shifted value or `undefined` if the deque is empty. + * @returns The shifted data or `undefined` if the deque is empty. */ 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?.value + return head.data } /** - * Peeks at the first value. - * @returns The first value or `undefined` if the deque is empty. + * Peeks at the first data. + * @returns The first data or `undefined` if the deque is empty. */ public peekFirst (): T | undefined { - return this.head?.value + return this.head?.data } /** - * Peeks at the last value. - * @returns The last value or `undefined` if the deque is empty. + * Peeks at the last data. + * @returns The last data or `undefined` if the deque is empty. */ public peekLast (): T | undefined { - return this.tail?.value + return this.tail?.data } /** * Clears the deque. */ public clear (): void { - this.head = undefined - this.tail = undefined + delete this.head + delete this.tail this.size = 0 this.maxSize = 0 } @@ -149,10 +152,10 @@ export class Deque { } } const ret = { - value: node.value, + value: node.data, done: false } - node = node.next as Node + node = node.next return ret } } @@ -177,10 +180,10 @@ export class Deque { } } const ret = { - value: node.value, + value: node.data, done: false } - node = node.prev as Node + node = node.prev return ret } }