@jiminp/tooltool
    Preparing search index...

    Class Deque<T>

    A double-ended queue with amortized O(1) insertion and removal at both ends.

    Uses a sparse index map so head and tail grow independently without array shifts.

    The item-type constraint is enforced by TypeScript, not runtime checks. Because missing items also return null, use length to distinguish a stored null item from an empty deque.

    const deque = new Deque<number>();
    deque.push(1, 2);
    deque.unshift(0);
    deque.pop(); // 2

    Type Parameters

    • T extends NonNullable<unknown> | null

      Item type; must exclude undefined. null is allowed.

    Index
    • Type Parameters

      • T extends {} | null

        Item type; must exclude undefined. null is allowed.

      Returns Deque<T>

    • Retrieves the item at the given index.

      Supports negative indices (-1 = tail). Non-integers are truncated toward zero.

      Parameters

      • index: number

        Position in the deque.

      Returns T | null

      The item, or null if out of range.

    • Removes and returns the tail item.

      Returns T | null

      The removed item, or null if empty.

      Returns null (not undefined) when empty, diverging from native Array.pop().

    • Appends items to the tail.

      Parameters

      • ...items: T[]

        Items to append (in order).

      Returns number

      The new length.

    • Removes and returns the head item.

      Returns T | null

      The removed item, or null if empty.

      Returns null (not undefined) when empty, diverging from native Array.shift().

    • Inserts items at the head.

      Parameters

      • ...items: T[]

        Items to insert; first argument becomes the new head.

      Returns number

      The new length.