From: Jérôme Benoit Date: Sun, 27 Aug 2023 17:48:45 +0000 (+0200) Subject: refactor: cleanup deque variables namespace X-Git-Tag: v2.6.37~28 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=4843c5e8874c71a3ba37ae9c152a0fce133f23e7;p=poolifier.git refactor: cleanup deque variables namespace Signed-off-by: Jérôme Benoit --- diff --git a/src/deque.ts b/src/deque.ts index 9be270ff..2eae8034 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,9 +70,9 @@ 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) { @@ -83,13 +86,13 @@ export class Deque { this.tail.next = undefined } --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) { @@ -103,23 +106,23 @@ export class Deque { this.head.prev = undefined } --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 } /** @@ -149,7 +152,7 @@ export class Deque { } } const ret = { - value: node.value, + value: node.data, done: false } node = node.next as Node @@ -177,7 +180,7 @@ export class Deque { } } const ret = { - value: node.value, + value: node.data, done: false } node = node.prev as Node diff --git a/tests/deque.test.js b/tests/deque.test.js index 290eadbf..73e54590 100644 --- a/tests/deque.test.js +++ b/tests/deque.test.js @@ -8,20 +8,20 @@ describe('Deque test suite', () => { expect(deque.size).toBe(1) expect(deque.maxSize).toBe(1) expect(rtSize).toBe(deque.size) - expect(deque.head).toMatchObject({ value: 1 }) - expect(deque.tail).toMatchObject({ value: 1 }) + expect(deque.head).toMatchObject({ data: 1 }) + expect(deque.tail).toMatchObject({ data: 1 }) rtSize = deque.push(2) expect(deque.size).toBe(2) expect(deque.maxSize).toBe(2) expect(rtSize).toBe(deque.size) - expect(deque.head).toMatchObject({ value: 1 }) - expect(deque.tail).toMatchObject({ value: 2 }) + expect(deque.head).toMatchObject({ data: 1 }) + expect(deque.tail).toMatchObject({ data: 2 }) rtSize = deque.push(3) expect(deque.size).toBe(3) expect(deque.maxSize).toBe(3) expect(rtSize).toBe(deque.size) - expect(deque.head).toMatchObject({ value: 1 }) - expect(deque.tail).toMatchObject({ value: 3 }) + expect(deque.head).toMatchObject({ data: 1 }) + expect(deque.tail).toMatchObject({ data: 3 }) }) it('Verify unshift() behavior', () => { @@ -30,20 +30,20 @@ describe('Deque test suite', () => { expect(deque.size).toBe(1) expect(deque.maxSize).toBe(1) expect(rtSize).toBe(deque.size) - expect(deque.head).toMatchObject({ value: 1 }) - expect(deque.tail).toMatchObject({ value: 1 }) + expect(deque.head).toMatchObject({ data: 1 }) + expect(deque.tail).toMatchObject({ data: 1 }) rtSize = deque.unshift(2) expect(deque.size).toBe(2) expect(deque.maxSize).toBe(2) expect(rtSize).toBe(deque.size) - expect(deque.head).toMatchObject({ value: 2 }) - expect(deque.tail).toMatchObject({ value: 1 }) + expect(deque.head).toMatchObject({ data: 2 }) + expect(deque.tail).toMatchObject({ data: 1 }) rtSize = deque.unshift(3) expect(deque.size).toBe(3) expect(deque.maxSize).toBe(3) expect(rtSize).toBe(deque.size) - expect(deque.head).toMatchObject({ value: 3 }) - expect(deque.tail).toMatchObject({ value: 1 }) + expect(deque.head).toMatchObject({ data: 3 }) + expect(deque.tail).toMatchObject({ data: 1 }) }) it('Verify pop() behavior', () => { @@ -55,14 +55,14 @@ describe('Deque test suite', () => { expect(deque.size).toBe(2) expect(deque.maxSize).toBe(3) expect(rtItem).toBe(3) - expect(deque.head).toMatchObject({ value: 1 }) - expect(deque.tail).toMatchObject({ value: 2 }) + expect(deque.head).toMatchObject({ data: 1 }) + expect(deque.tail).toMatchObject({ data: 2 }) rtItem = deque.pop() expect(deque.size).toBe(1) expect(deque.maxSize).toBe(3) expect(rtItem).toBe(2) - expect(deque.head).toMatchObject({ value: 1 }) - expect(deque.tail).toMatchObject({ value: 1 }) + expect(deque.head).toMatchObject({ data: 1 }) + expect(deque.tail).toMatchObject({ data: 1 }) rtItem = deque.pop() expect(deque.size).toBe(0) expect(deque.maxSize).toBe(3) @@ -80,14 +80,14 @@ describe('Deque test suite', () => { expect(deque.size).toBe(2) expect(deque.maxSize).toBe(3) expect(rtItem).toBe(1) - expect(deque.head).toMatchObject({ value: 2 }) - expect(deque.tail).toMatchObject({ value: 3 }) + expect(deque.head).toMatchObject({ data: 2 }) + expect(deque.tail).toMatchObject({ data: 3 }) rtItem = deque.shift() expect(deque.size).toBe(1) expect(deque.maxSize).toBe(3) expect(rtItem).toBe(2) - expect(deque.head).toMatchObject({ value: 3 }) - expect(deque.tail).toMatchObject({ value: 3 }) + expect(deque.head).toMatchObject({ data: 3 }) + expect(deque.tail).toMatchObject({ data: 3 }) rtItem = deque.shift() expect(deque.size).toBe(0) expect(deque.maxSize).toBe(3) @@ -123,8 +123,8 @@ describe('Deque test suite', () => { deque.push(3) expect(deque.size).toBe(3) expect(deque.maxSize).toBe(3) - expect(deque.head).toMatchObject({ value: 1 }) - expect(deque.tail).toMatchObject({ value: 3 }) + expect(deque.head).toMatchObject({ data: 1 }) + expect(deque.tail).toMatchObject({ data: 3 }) deque.clear() expect(deque.size).toBe(0) expect(deque.maxSize).toBe(0)