+++ /dev/null
-// Copyright Jerome Benoit. 2022-2023. All Rights Reserved.
-
-/**
- * Queue.
- *
- * @typeParam T - Type of queue items.
- */
-export class Queue<T> {
- private items!: T[]
- private offset!: number
- /** The size of the queue. */
- public size!: number
- /** The maximum size of the queue. */
- public maxSize!: number
-
- public constructor () {
- this.clear()
- }
-
- /**
- * Enqueue an item.
- *
- * @param item - Item to enqueue.
- * @returns The new size of the queue.
- */
- public enqueue (item: T): number {
- this.items.push(item)
- ++this.size
- if (this.size > this.maxSize) {
- this.maxSize = this.size
- }
- return this.size
- }
-
- /**
- * Dequeue an item.
- *
- * @returns The dequeued item or `undefined` if the queue is empty.
- */
- public dequeue (): T | undefined {
- if (this.size <= 0) {
- return undefined
- }
- const item = this.items[this.offset]
- if (++this.offset * 2 >= this.items.length) {
- this.items = this.items.slice(this.offset)
- this.offset = 0
- }
- --this.size
- return item
- }
-
- /**
- * Peeks at the first item.
- *
- * @returns The first item or `undefined` if the queue is empty.
- */
- public peek (): T | undefined {
- if (this.size <= 0) {
- return undefined
- }
- return this.items[this.offset]
- }
-
- /**
- * Clears the queue.
- */
- public clear (): void {
- this.items = []
- this.offset = 0
- this.size = 0
- this.maxSize = 0
- }
-
- /**
- * Returns an iterator for the queue.
- *
- * @returns An iterator for the queue.
- * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols
- */
- [Symbol.iterator] (): Iterator<T> {
- const items = this.items
- let i = this.offset
-
- return {
- next: () => {
- if (i >= items.length) {
- return {
- value: undefined,
- done: true
- }
- }
- const value = items[i]
- ++i
- return {
- value,
- done: false
- }
- }
- }
- }
-}
+++ /dev/null
-// const { expect } = require('expect')
-// const { Queue } = require('../lib/queue')
-
-// describe('Queue test suite', () => {
-// it('Verify enqueue() behavior', () => {
-// const queue = new Queue()
-// let rtSize = queue.enqueue(1)
-// expect(queue.size).toBe(1)
-// expect(rtSize).toBe(queue.size)
-// expect(queue.offset).toBe(0)
-// expect(queue.maxSize).toBe(1)
-// expect(queue.items).toStrictEqual([1])
-// rtSize = queue.enqueue(2)
-// expect(queue.size).toBe(2)
-// expect(rtSize).toBe(queue.size)
-// expect(queue.offset).toBe(0)
-// expect(queue.maxSize).toBe(2)
-// expect(queue.items).toStrictEqual([1, 2])
-// rtSize = queue.enqueue(3)
-// expect(queue.size).toBe(3)
-// expect(rtSize).toBe(queue.size)
-// expect(queue.offset).toBe(0)
-// expect(queue.maxSize).toBe(3)
-// expect(queue.items).toStrictEqual([1, 2, 3])
-// })
-
-// it('Verify dequeue() behavior', () => {
-// const queue = new Queue()
-// queue.enqueue(1)
-// queue.enqueue(2)
-// queue.enqueue(3)
-// let rtItem = queue.dequeue()
-// expect(queue.size).toBe(2)
-// expect(rtItem).toBe(1)
-// expect(queue.offset).toBe(1)
-// expect(queue.maxSize).toBe(3)
-// expect(queue.items).toStrictEqual([1, 2, 3])
-// rtItem = queue.dequeue()
-// expect(queue.size).toBe(1)
-// expect(rtItem).toBe(2)
-// expect(queue.offset).toBe(0)
-// expect(queue.maxSize).toBe(3)
-// expect(queue.items).toStrictEqual([3])
-// rtItem = queue.dequeue()
-// expect(queue.size).toBe(0)
-// expect(rtItem).toBe(3)
-// expect(queue.offset).toBe(0)
-// expect(queue.maxSize).toBe(3)
-// expect(queue.items).toStrictEqual([])
-// })
-
-// it('Verify clear() behavior', () => {
-// const queue = new Queue()
-// queue.enqueue(1)
-// queue.enqueue(2)
-// queue.enqueue(3)
-// expect(queue.size).toBe(3)
-// expect(queue.maxSize).toBe(3)
-// expect(queue.items).toStrictEqual([1, 2, 3])
-// expect(queue.offset).toBe(0)
-// queue.clear()
-// expect(queue.size).toBe(0)
-// expect(queue.maxSize).toBe(0)
-// expect(queue.items).toStrictEqual([])
-// expect(queue.offset).toBe(0)
-// })
-
-// it('Verify iterator behavior', () => {
-// const queue = new Queue()
-// queue.enqueue(1)
-// queue.enqueue(2)
-// queue.enqueue(3)
-// let i = 1
-// for (const item of queue) {
-// expect(item).toBe(i)
-// ++i
-// }
-// })
-// })