javascript-collections-framework
    Preparing search index...

    Class PriorityQueue<E>Sealed

    A queue that always hands out its least element next, stored as a binary heap: add and poll cost O(log n) and peek O(1). The order is the natural order of the elements, or the comparator option.

    Iteration, toArray, toJSON and toString visit the elements in heap order, which is not sorted; take them out with poll in a loop for sorted output. Which of several equal elements is polled first is unspecified. null and undefined are not allowed as elements, so poll() and peek() returning undefined always means the queue is empty. A comparator that throws or returns NaN leaves the queue as it was. Two queues are equal only when they are the same object. Adding or removing while iterating throws ConcurrentModificationError; use a cursor to remove while iterating.

    The class is not designed to be subclassed.

    const tasks = new PriorityQueue<{ name: string; priority: number }>({
    comparator: (a, b) => a.priority - b.priority,
    })
    tasks.add({ name: 'deploy', priority: 2 }).add({ name: 'fix', priority: 1 })
    tasks.poll() // { name: 'fix', priority: 1 }

    const numbers = new PriorityQueue([5, 1, 3])
    const sorted: number[] = []
    while (!numbers.isEmpty()) sorted.push(numbers.poll()!) // [1, 3, 5]

    Type Parameters

    • E

    Implements

    Index
    • Creates a queue in natural order holding the elements of elements; empty when elements is omitted, undefined or null.

      Time: O(m) for m elements, with at most 2m comparisons.

      Type Parameters

      • E

      Parameters

      • Optionalelements: Iterable<E, any, any> | null

        The initial elements. The comparator of a PriorityQueue given here is not used; pass it as an option, or call copy().

      Returns PriorityQueue<E>

      TypeError when elements is neither iterable nor null/undefined.

      NullElementError when an element is null or undefined.

      ClassCastError when the elements cannot be compared in natural order.

      new PriorityQueue([3, 1, 2]).peek() // 1
      
    • Creates an empty queue with the given options.

      Time: O(1).

      Type Parameters

      • E

      Parameters

      Returns PriorityQueue<E>

      TypeError when comparator is neither undefined nor a function.

      new PriorityQueue<string>({ comparator: (a, b) => b.length - a.length })
      
    • Creates a queue holding the elements of elements (empty when undefined or null), ordered by options.comparator, or by natural order when it is omitted.

      Time: O(m) for m elements, with at most 2m comparisons.

      Type Parameters

      • E

      Parameters

      • elements: Iterable<E, any, any> | null | undefined

        The initial elements.

      • Optionaloptions: PriorityQueueOptions<E>

        The options; comparator is read once.

      Returns PriorityQueue<E>

      TypeError when elements is neither iterable nor null/undefined, when options is not an object, or when comparator is neither undefined nor a function.

      NullElementError when an element is null or undefined.

      ClassCastError when, in natural order, the elements cannot be compared.

      new PriorityQueue([1, 3, 2], { comparator: (a, b) => b - a }).peek() // 3
      
    • get "[COLLECTION_BRAND]"(): true

      Marks collections so isCollection recognises them across copies of the package. Its key is not exported, so the getter is not called directly. Time: O(1).

      Returns true

    • get "[PRIORITY_QUEUE_BRAND]"(): true

      Marks instances of this class so instanceof recognises them across copies of the package. Its key is not exported, so the getter is not called directly. Time: O(1).

      Returns true

    • get "[QUEUE_BRAND]"(): true

      Marks queues so isQueue recognises them across copies of the package. Its key is not exported, so the getter is not called directly. Time: O(1).

      Returns true

    • get "[toStringTag]"(): string

      'PriorityQueue', for Object.prototype.toString. Time: O(1).

      Returns string

    • get revision(): number

      A number that changes on every change to the contents and never on a read. Its exact values are unspecified. Time: O(1).

      Returns number

    • get size(): number

      The number of elements. Time: O(1).

      Returns number

    • The value-equality protocol; delegates to equals (identity). Time: O(1).

      Parameters

      • other: unknown

      Returns boolean

    • Node's inspect hook: PriorityQueue(2) [ 1, 2 ] in console.log. Time: as toString.

      Parameters

      • depth: number | null
      • options: object
      • inspect: (value: unknown, options: object) => string

      Returns string

    • Returns an iterator over the elements in heap order, which is not sorted.

      Time: O(1) to create; O(1) per step.

      Returns IterableIterator<E>

      A new iterator.

      ConcurrentModificationError (from next()) when elements are added or removed after the iterator is created, including through a cursor.

      for (const element of new PriorityQueue([2, 1])) console.log(element)
      
    • Adds element and returns the queue, so calls chain. On an error nothing is added.

      Time: O(log n), with at most ⌊log2 n⌋ + 1 comparisons, where n is the size after the add.

      Parameters

      • element: E

        The element to add.

      Returns this

      The queue.

      NullElementError when element is null or undefined.

      ClassCastError when, in natural order, element cannot be compared with the elements.

      TypeError when the comparator returns a non-number or NaN.

      ConcurrentModificationError when the comparator adds or removes elements of this queue.

      new PriorityQueue<number>().add(2).add(1).peek() // 1
      
    • Adds every element of elements, one at a time, after reading them all and checking that none is null or undefined (if one is, nothing is added). If a comparison fails part-way, the elements added before it stay.

      Time: O(m log(n + m)) for m elements.

      Parameters

      • elements: Iterable<E>

        The elements to add.

      Returns boolean

      true when any element was added.

      TypeError when elements is not iterable, or the comparator returns a non-number or NaN.

      NullElementError when an element is null or undefined.

      ClassCastError when, in natural order, an element cannot be compared.

      ConcurrentModificationError when the comparator adds or removes elements of this queue.

      new PriorityQueue([5]).addAll([3, 4]) // true; peek() is 3
      
    • Removes every element.

      Time: O(1).

      Returns void

      queue.clear() // queue.size === 0
      
    • Returns the comparator given at construction, or undefined when the queue uses natural order.

      Time: O(1).

      Returns Comparator<E> | undefined

      The comparator, or undefined.

      const byLength = (a: string, b: string) => a.length - b.length
      new PriorityQueue({ comparator: byLength }).comparator() === byLength // true
      new PriorityQueue().comparator() // undefined
    • Returns a new PriorityQueue with the same elements and the same comparator, independent of this one (the elements themselves are not copied). The comparator is not called.

      Time: O(n).

      Returns PriorityQueue<E>

      The copy.

      const copy = queue.copy()
      copy.poll() // queue is unchanged
    • Returns a cursor over the elements in heap order; remove() removes the element next() returned, and the cursor still visits every other element exactly once.

      Time: O(1) to create; next O(1); remove O(log n), or O(n) for an element the removal of another moved behind the cursor.

      Returns Cursor<E>

      A new cursor.

      const cursor = queue.cursor()
      while (cursor.hasNext()) if (cursor.next() < 0) cursor.remove()
    • Removes one element equal to element under SameValueZero (not under the comparator: two elements that compare as equal are still different elements). On an error nothing is removed.

      Time: O(n) to find it, then at most 2⌊log2 n⌋ + 1 comparisons.

      Parameters

      • element: E

        The value to remove.

      Returns boolean

      true when an element was removed.

      TypeError when the comparator returns a non-number or NaN.

      ConcurrentModificationError when the comparator adds or removes elements of this queue.

      new PriorityQueue([1, 2, 3]).delete(2) // true
      
    • Returns whether other is this queue. Queues compare by identity. Called through a Proxy of the queue, it is true for that proxy too; the queue itself does not know its proxies, so queue.equals(proxy) is false.

      Time: O(1).

      Parameters

      • other: unknown

        Any value.

      Returns boolean

      true when other is this queue.

      new PriorityQueue([1]).equals(new PriorityQueue([1])) // false
      
    • Calls action(element) for each element in heap order, which is not sorted, with this set to thisArg.

      Time: O(n) plus the calls.

      Parameters

      • action: (element: E) => void

        Called with each element.

      • OptionalthisArg: unknown

        The this value for action.

      Returns void

      TypeError when action is not a function.

      ConcurrentModificationError when action adds or removes elements.

      new PriorityQueue(['b', 'a']).forEach((s) => console.log(s))
      
    • Returns whether some element is equal to element under SameValueZero (not under the comparator); always false for null and undefined.

      Time: O(n).

      Parameters

      • element: E

        The value to look for.

      Returns boolean

      true when present.

      new PriorityQueue([1, 2]).has(2) // true
      
    • Returns whether the queue has no elements.

      Time: O(1).

      Returns boolean

      size === 0.

      new PriorityQueue().isEmpty() // true
      
    • Returns the least element, the one poll() returns next, without removing it, or undefined when the queue is empty. The comparator is not called.

      Time: O(1).

      Returns E | undefined

      The least element, or undefined.

      new PriorityQueue([2, 1]).peek() // 1
      
    • Removes and returns the least element, or returns undefined when the queue is empty. On an error nothing is removed.

      Time: O(log n), with at most 2⌊log2 n⌋ comparisons.

      Returns E | undefined

      The removed element, or undefined.

      TypeError when the comparator returns a non-number or NaN.

      ConcurrentModificationError when the comparator adds or removes elements of this queue.

      new PriorityQueue([2, 1]).poll() // 1
      
    • Removes every element equal to one in elements (SameValueZero). Decides first, then rebuilds the heap from the remaining elements; an error while deciding or rebuilding removes nothing.

      Time: O(n + m) for m elements, plus at most 2n comparisons; O(n) extra memory.

      Parameters

      • elements: Iterable<E>

        The elements to remove.

      Returns boolean

      true when any element was removed.

      TypeError when elements is not iterable, or the comparator returns a non-number or NaN.

      ConcurrentModificationError when the comparator adds or removes elements of this queue.

      new PriorityQueue([1, 2, 1]).removeAll([1]) // true; only 2 is left
      
    • Calls predicate(element) on every element, then removes those for which it returned a truthy value and rebuilds the heap; an error from predicate or the comparator removes nothing.

      Time: O(n) plus the calls, plus at most 2n comparisons; O(n) extra memory.

      Parameters

      • predicate: (element: E) => unknown

        Called with each element.

      Returns boolean

      true when any element was removed.

      TypeError when predicate is not a function, or the comparator returns a non-number or NaN.

      ConcurrentModificationError when predicate or the comparator adds or removes elements.

      new PriorityQueue([1, -2, 3]).removeIf((n) => n < 0) // true
      
    • Removes every element not equal to one in elements (SameValueZero). Decides first, then rebuilds the heap from the remaining elements; an error while deciding or rebuilding removes nothing.

      Time: O(n + m) for m elements, plus at most 2n comparisons; O(n) extra memory.

      Parameters

      • elements: Iterable<E>

        The elements to keep.

      Returns boolean

      true when any element was removed.

      TypeError when elements is not iterable, or the comparator returns a non-number or NaN.

      ConcurrentModificationError when the comparator adds or removes elements of this queue.

      new PriorityQueue([1, 2, 3]).retainAll([2]) // true; only 2 is left
      
    • Returns a new array of the elements in heap order, which is not sorted.

      Time: O(n).

      Returns E[]

      A new array.

      new PriorityQueue([2, 1]).toArray() // [1, 2]: both elements, in an unspecified order
      
    • Returns a new array of the elements in heap order, so JSON.stringify writes the queue as an array.

      Time: O(n).

      Returns E[]

      A new array.

      JSON.stringify(new PriorityQueue([1])) // '[1]'
      
    • Returns the class name, size and elements in heap order, e.g. PriorityQueue(3) [1, 3, 2].

      Time: O(length of the output) for flat contents.

      Returns string

      The text.

      String(new PriorityQueue([1])) // 'PriorityQueue(1) [1]'
      
    • Returns whether value is a PriorityQueue, including one created by another copy of the library with the same major version.

      Time: O(1).

      Parameters

      • value: unknown

        The value on the left of instanceof.

      Returns boolean

      true when value is a PriorityQueue.

      new PriorityQueue() instanceof PriorityQueue // true
      
    • Returns a new queue of the elements of elements, ordered by options.comparator, or by natural order when it is omitted. The comparator of a PriorityQueue given as elements is not used.

      Time: O(m) for m elements, with at most 2m comparisons.

      Type Parameters

      • T

      Parameters

      • elements: Iterable<T>

        The elements.

      • Optionaloptions: PriorityQueueOptions<T>

        The options; comparator is read once.

      Returns PriorityQueue<T>

      A new queue.

      TypeError when elements is not iterable (null and undefined included), when options is given and is not an object, or when comparator is neither undefined nor a function.

      NullElementError when an element is null or undefined.

      ClassCastError when, in natural order, the elements cannot be compared.

      PriorityQueue.from(new Set(['b', 'a'])).poll() // 'a'