es-stl - v3.0.2
    Preparing search index...

    Max-priority heap.

    Larger numeric priorities are popped before smaller priorities. Values with equal priorities are not guaranteed to preserve insertion order.

    const heap = new Heap<string>();
    heap.push('low', 1);
    heap.push('high', 10);

    heap.pop();
    // => 'high'

    Type Parameters

    • T

      Value type stored in the heap.

    Index

    Constructors

    Properties

    Accessors

    Methods

    Constructors

    • Type Parameters

      • T

        Value type stored in the heap.

      Returns Heap<T>

    Properties

    _priorities: number[] = []
    _size: number = 0
    _values: T[] = []

    Accessors

    • get size(): number

      Number of values currently stored in the heap.

      Returns number

      The heap size.

      const heap = new Heap<string>();
      heap.push('job', 1);

      heap.size;
      // => 1

    Methods

    • Removes all values from the heap.

      Returns void

      const heap = new Heap<string>();
      heap.push('task', 1);
      heap.clear();

      heap.isEmpty();
      // => true
    • Trims backing arrays to the current heap size.

      Returns void

      This can be useful after many pop operations when the heap will remain in memory and you want its internal arrays to release unused slots.

      const heap = new Heap<number>();
      heap.push(1, 1);
      heap.push(2, 2);
      heap.pop();

      heap.fit();
    • Checks whether the heap contains no values.

      Returns boolean

      true when the heap is empty.

      new Heap().isEmpty();
      // => true
    • Removes and returns the highest-priority value.

      Returns T | undefined

      Highest-priority value, or undefined when the heap is empty.

      const heap = new Heap<string>();
      heap.push('a', 1);
      heap.push('b', 2);

      heap.pop();
      // => 'b'

      heap.pop();
      // => 'a'
    • Adds a value with its numeric priority.

      Parameters

      • value: T

        Value to insert.

      • priority: number

        Numeric priority for ordering.

      Returns void

      Higher priority values are returned before lower priority values.

      const heap = new Heap<string>();
      heap.push('background', 1);
      heap.push('urgent', 100);

      heap.top();
      // => 'urgent'
    • Reads the highest-priority value without removing it.

      Returns T | undefined

      Highest-priority value, or undefined when the heap is empty.

      const heap = new Heap<string>();
      heap.push('ready', 5);

      heap.top();
      // => 'ready'

      heap.size;
      // => 1