/*! * V4Fire Core * https://github.com/V4Fire/Core * * Released under the MIT license * https://github.com/V4Fire/Core/blob/master/LICENSE */ export interface InnerQueue { readonly length: number; readonly head?: CanUndef; [Symbol.iterator](): IterableIterator; push(task: T): number; unshift(task: T): number; pop(): CanUndef; shift(): CanUndef; clone?(): InnerQueue; } export interface CreateInnerQueue = InnerQueue> { (): T; } export interface QueueOptions = InnerQueue> { /** * A factory to create an internal queue to store elements */ queueFactory?: CreateInnerQueue; } /** * An abstract class for any queue data structure * @typeparam T - queue element */ export default abstract class Queue { /** * The first element in the queue */ abstract readonly head: CanUndef; /** * Number of elements in the queue */ abstract readonly length: number; /** * Adds a new element to the queue * @param el */ abstract push(el: T): unknown; /** * Removes the head element from the queue and returns it */ abstract pop(): CanUndef; /** * Alias to `push` * @see [[Queue.push]] */ unshift(el: T): ReturnType { return Object.cast(this.push(el)); } /** * Alias to `pop` * @see [[Queue.pop]] */ shift(): ReturnType { return Object.cast(this.pop()); } /** * Clears the queue */ abstract clear(): void; /** * Creates a new queue based on the current one and returns it */ abstract clone(): Queue; /** * Returns an iterator over the queue elements */ [Symbol.iterator](): IterableIterator { return this.values(); } /** * Returns an iterator over the queue elements */ values(): IterableIterator { const clonedQueue = this.clone(); return { [Symbol.iterator]() { return this; }, next(): IteratorResult { const done = clonedQueue.length <= 0, value = clonedQueue.pop(); if (done || value == null) { return {done: true, value: undefined}; } return {done, value}; } }; } }