From: Jérôme Benoit Date: Wed, 23 Aug 2023 12:17:34 +0000 (+0200) Subject: feat: add backward iterator to deque X-Git-Tag: v2.6.32~3 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=31a7af933d0ca6b37a9a5ff8474ea940ae968f6c;p=poolifier.git feat: add backward iterator to deque Signed-off-by: Jérôme Benoit --- diff --git a/src/deque.ts b/src/deque.ts index 109304a8..49373cef 100644 --- a/src/deque.ts +++ b/src/deque.ts @@ -154,6 +154,30 @@ export class Deque { } } + backward (): Iterable { + return { + [Symbol.iterator]: (): Iterator => { + let node = this.tail + return { + next: () => { + if (node == null) { + return { + value: undefined, + done: true + } + } + const ret = { + value: node.value, + done: false + } + node = node.prev as Node + return ret + } + } + } + } + } + private incrementSize (): number { ++this.size if (this.size > this.maxSize) { diff --git a/src/pools/selection-strategies/abstract-worker-choice-strategy.ts b/src/pools/selection-strategies/abstract-worker-choice-strategy.ts index c2b4d1d3..2fba4e9f 100644 --- a/src/pools/selection-strategies/abstract-worker-choice-strategy.ts +++ b/src/pools/selection-strategies/abstract-worker-choice-strategy.ts @@ -197,15 +197,15 @@ export abstract class AbstractWorkerChoiceStrategy< /** * Check the next worker node eligibility. * - * @param chosenWorkerNodeKey - The chosen worker node key. + * @param chosenNextWorkerNodeKey - The chosen worker node key. */ protected checkNextWorkerNodeEligibility ( - chosenWorkerNodeKey: number | undefined + chosenNextWorkerNodeKey: number | undefined ): void { if (!this.isWorkerNodeEligible(this.nextWorkerNodeKey as number)) { this.nextWorkerNodeKey = undefined this.previousWorkerNodeKey = - chosenWorkerNodeKey ?? this.previousWorkerNodeKey + chosenNextWorkerNodeKey ?? this.previousWorkerNodeKey } } diff --git a/tests/deque.test.js b/tests/deque.test.js index dec96b35..8521047d 100644 --- a/tests/deque.test.js +++ b/tests/deque.test.js @@ -123,4 +123,16 @@ describe('Deque test suite', () => { ++i } }) + + it('Verify backward() iterator behavior', () => { + const deque = new Deque() + deque.push(1) + deque.push(2) + deque.push(3) + let i = deque.size + for (const value of deque.backward()) { + expect(value).toBe(i) + --i + } + }) })