lifecycle-utils
    Preparing search index...

    Class Queue<T>

    An efficient queue implementation that allows you to enqueue and dequeue items in O(1) time complexity.

    import {Queue} from "lifecycle-utils";

    const queue = new Queue([1, 2, 3]);

    queue.push(4);
    console.log(queue.shift()); // 1

    console.log(queue.first); // 2
    console.log(queue.last); // 4
    console.log(queue.length); // 3
    console.log([...queue]); // [2, 3, 4]

    Type Parameters

    • const T

    Implements

    Index

    Constructors

    Accessors

    • get first(): T | undefined

      The first (next) value in the queue, or undefined when the queue is empty.

      Time complexity: O(1).

      Returns T | undefined

    • get isEmpty(): boolean

      Whether the queue is empty.

      Time complexity: O(1).

      Returns boolean

    • get last(): T | undefined

      The last value in the queue, or undefined when the queue is empty.

      Time complexity: O(1).

      Returns T | undefined

    • get length(): number

      The number of values in the queue.

      Time complexity: O(1).

      Returns number

    Methods

    • Returns an iterator over the values in the queue from first to last.

      Time complexity: O(1) to create the iterator, and O(n) for a full iteration.

      Returns IterableIterator<T>

    • Returns the value at the given index without removing it.

      Negative indexes count backwards from the end of the queue.

      Time complexity: O(1).

      Parameters

      • index: number

      Returns T | undefined

    • Removes all values from the queue.

      Time complexity: O(1).

      Returns void

    • Deletes values from the queue starting at start and ending before end.

      When end is omitted, only the value at start is deleted. Negative indexes count backwards from the end of the queue.

      Returns the number of deleted values.

      Time complexity: O(k) when deleting from either end, where k is the number of deleted values, and O(n) when deleting from the middle.

      Parameters

      • start: number
      • Optionalend: number

      Returns number

    • Returns an iterator over index-value pairs in the queue.

      Time complexity: O(n) for a full iteration and O(1) per value.

      Returns IterableIterator<[number, T]>

    • Returns the index of the first occurrence of a value in the queue, or -1 when it is not found.

      Time complexity: O(n).

      Parameters

      • item: T
      • fromIndex: number = 0

      Returns number

    • Returns the index of the last occurrence of a value in the queue, or -1 when it is not found.

      Time complexity: O(n).

      Parameters

      • item: T
      • OptionalfromIndex: number

      Returns number

    • Adds a value to the end of the queue.

      Time complexity: O(1) amortized.

      Parameters

      • item: T

      Returns void

    • Removes and returns the first (next) value in the queue.

      Returns undefined when the queue is empty.

      Time complexity: O(1) amortized, with occasional O(n) compaction.

      Returns T | undefined

    • Returns the queue values as a new array.

      Time complexity: O(n).

      Returns T[]

    • Returns an iterator over the values in the queue from first to last.

      Time complexity: O(n) for a full iteration and O(1) per value.

      Returns IterableIterator<T>