A queue that pops element based on their given priority.

import { PriorityQueue } from "scl"

The queue will return elements with a lower priority first. If you want the reverse, simply invert the function that is used to compare two elements.

The following table summarises the time complexity of the most commonly used properties.

Property name Worst-case
add() O(log(n))
deleteAt() O(log(n))
peek() O(1)
pop() O(log(n))
size O(1)

Type Parameters

  • T

Implements

Constructors

  • Construct a new prioriry queue.

    const q = new PriorityQueue<number>({
    capacity: 1024,
    compare: (a, b) => a < b,
    elements: [1, 2, 3]
    })

    Type Parameters

    • T

    Parameters

    • opts: BinaryMinHeap<T> | Iterable<T, any, any> | HeapOptions<T> = {}

    Returns PriorityQueue<T>

Accessors

  • get size(): number
  • Count the amount of elements in the collection.

    ⚠️ In most cases, this should be an O(1) operation. However, there are cases where this can be an O(n) operation. Therefore, it is recommended to always cache the result in a local variable.

    Returns number

Methods

  • Returns an object which is able to sift through the values in this collection.

    The order by which the elements are traversed depends on the kind of collection. For unordered collections, the iteration order is unspecified and may even differ between two iterations on the same collection.

    Returns Generator<T, void, unknown>

  • Add an element to the collection. If the element already exists, update its value.

    The location where the element is placed depends on the collection type, and in the generic case there is no guarantee on the location where it is inserted.

    This method returns a pair with the first element indicating whether the element was added, while the second element refers to the actual location of the element.

    Parameters

    • element: T

    Returns [boolean, Cursor<T>]

  • Remove all elements from this collection, effectively setting the collection to the empty collection.

    Returns void

  • Remove an element from the collection. If multiple elements are matched, the collection picks one of them.

    Parameters

    • element: T

    Returns boolean

    true if the element was found, false otherwise.

  • Remove an element from the collection. If multiple elements are matched, the collection removes all of them.

    Parameters

    • element: T

    Returns number

    The amount of elements that was removed.

  • Remove the element pointed to by the iterator result from this collection.

    Parameters

    • cursor: VectorCursor<T>

    Returns void

  • Parameters

    • index: number

    Returns void

  • Checks if the collection holds the given element.

    Parameters

    • element: T

      The element to check membership of.

    Returns boolean

    True if the collections holds the given element, false otherwise.

  • Get the next element in the queue without removing it from the collection.

    Returns T

    Queuelike.pop()

  • Get the next element in the order defined by the queue and remove it from the collection.

    Returns T

    Queuelike.peek()

MMNEPVFCICPMFPCPTTAAATR