All files / src/data-structures/heap heap.ts

65.95% Statements 31/47
55.76% Branches 29/52
65.21% Functions 15/23
67.44% Lines 29/43

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213                    19x                 32x 32x           224x                   138x               19x             6x             6x             25x                         26x                                       6x 6x   6x                                                         32x 32x   32x                       24x 24x 24x       24x                 25x     25x   58x                             6x 6x   14x                               1x 1x   4x                    
/**
 * data-structure-typed
 *
 * @author Tyler Zeng
 * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
 * @license MIT License
 */
import {PriorityQueue} from '../priority-queue';
import type {HeapOptions} from '../../types';
 
export class HeapItem<T = number> {
  /**
   * The constructor function initializes an instance of a class with a priority and a value.
   * @param {number} priority - The `priority` parameter is a number that represents the priority of the value. It is
   * optional and has a default value of `NaN`.
   * @param {T | null} [val=null] - The `val` parameter is of type `T | null`, which means it can accept a value of type
   * `T` or `null`.
   */
  constructor(priority: number = Number.MAX_SAFE_INTEGER, val: T | null = null) {
    this._val = val;
    this._priority = priority;
  }
 
  private _priority: number;
 
  get priority(): number {
    return this._priority;
  }
 
  set priority(value: number) {
    this._priority = value;
  }
 
  private _val: T | null;
 
  get val(): T | null {
    return this._val;
  }
 
  set val(value: T | null) {
    this._val = value;
  }
}
 
export abstract class Heap<T = number> {
  /**
   * The function is a constructor for a class that initializes a priority callback function based on the
   * options provided.
   * @param [options] - An optional object that contains configuration options for the Heap.
   */
  protected constructor(options?: HeapOptions<T>) {
    Iif (options) {
      const {priorityExtractor} = options;
      Iif (priorityExtractor !== undefined && typeof priorityExtractor !== 'function') {
        throw new Error('.constructor expects a valid priority function');
      }
      this._priorityExtractor = priorityExtractor || (el => +el);
    } else {
      this._priorityExtractor = el => +el;
    }
  }
 
  protected abstract _pq: PriorityQueue<HeapItem<T>>;
 
  get pq() {
    return this._pq;
  }
 
  protected _priorityExtractor: (val: T) => number;
  get priorityExtractor() {
    return this._priorityExtractor;
  }
 
  /**
   * The function returns the size of a priority queue.
   * @returns The size of the priority queue.
   */
  get size(): number {
    return this._pq.size;
  }
 
  /**
   * The function checks if a priority queue is empty.
   * @returns {boolean} A boolean value indicating whether the size of the priority queue is less than 1.
   */
  isEmpty(): boolean {
    return this._pq.size < 1;
  }
 
  peek(isItem?: undefined): T | undefined;
  peek(isItem: false): T | undefined;
  peek(isItem: true): HeapItem<T> | null;
 
  /**
   * The `peek` function returns the top item in the priority queue without removing it.
   * @returns The `peek()` method is returning either a `HeapItem<T>` object or `null`.Returns an val with the highest priority in the queue
   */
  peek(isItem?: boolean): HeapItem<T> | null | T | undefined {
    isItem = isItem ?? false;
    const peeked = this._pq.peek();
 
    return isItem ? peeked : peeked?.val;
  }
 
  peekLast(isItem?: undefined): T | undefined;
  peekLast(isItem: false): T | undefined;
  peekLast(isItem: true): HeapItem<T> | null;
 
  /**
   * The `peekLast` function returns the last item in the heap.
   * @returns The method `peekLast()` returns either a `HeapItem<T>` object or `null`.Returns an val with the lowest priority in the queue
   */
  peekLast(isItem?: boolean): HeapItem<T> | null | T | undefined {
    isItem = isItem ?? false;
    const leafItem = this._pq.leaf();
 
    return isItem ? leafItem : leafItem?.val;
  }
 
  /**
   * The `add` function adds an val to a priority queue with an optional priority value.
   * @param {T} val - The `val` parameter represents the value that you want to add to the heap. It can be of any
   * type.
   * @param {number} [priority] - The `priority` parameter is an optional number that represents the priority of the
   * val being added to the heap. If the `val` parameter is a number, then the `priority` parameter is set to
   * the value of `val`. If the `val` parameter is not a number, then the
   * @returns The `add` method returns the instance of the `Heap` class.
   * @throws {Error} if priority is not a valid number
   */
  add(priority: number, val?: T): Heap<T> {
    val = val === undefined ? (priority as unknown as T) : val;
    this._pq.add(new HeapItem<T>(priority, val));
 
    return this;
  }
 
  poll(isItem?: undefined): T | undefined;
  poll(isItem: false): T | undefined;
  poll(isItem: true): HeapItem<T> | null;
 
  /**
   * The `poll` function returns the top item from a priority queue or null if the queue is empty.Removes and returns an val with the highest priority in the queue
   * @returns either a HeapItem<T> object or null.
   */
  poll(isItem?: boolean): HeapItem<T> | null | T | undefined {
    isItem = isItem ?? false;
    const top = this._pq.poll();
    Iif (!top) {
      return null;
    }
 
    return isItem ? top : top.val;
  }
 
  /**
   * The function checks if a given node or value exists in the priority queue.
   * @param {T | HeapItem<T>} node - The parameter `node` can be of type `T` or `HeapItem<T>`.
   * @returns a boolean value.
   */
  has(node: T | HeapItem<T>): boolean {
    Iif (node instanceof HeapItem) {
      return this.pq.getNodes().includes(node);
    } else {
      return (
        this.pq.getNodes().findIndex(item => {
          return item.val === node;
        }) !== -1
      );
    }
  }
 
  toArray(isItem?: undefined): (T | undefined)[];
  toArray(isItem: false): (T | undefined)[];
  toArray(isItem: true): (HeapItem<T> | null)[];
 
  /**
   * The `toArray` function returns an array of `HeapItem<T>` objects.
   * @returns An array of HeapItem<T> objects.Returns a sorted list of vals
   */
  toArray(isItem?: boolean): (HeapItem<T> | null | T | undefined)[] {
    isItem = isItem ?? false;
    const itemArray = this._pq.toArray();
 
    return isItem ? itemArray : itemArray.map(item => item.val);
  }
 
  sort(isItem?: undefined): (T | undefined)[];
  sort(isItem: false): (T | undefined)[];
  sort(isItem: true): (HeapItem<T> | null)[];
 
  /**
   * The function sorts the elements in the priority queue and returns either the sorted items or their values depending
   * on the value of the isItem parameter.
   * @param {boolean} [isItem] - The `isItem` parameter is a boolean flag that indicates whether the sorted result should
   * be an array of `HeapItem<T>` objects or an array of the values (`T`) of those objects. If `isItem` is `true`, the
   * sorted result will be an array of `HeapItem
   * @returns an array of either `HeapItem<T>`, `null`, `T`, or `undefined` values.
   */
  sort(isItem?: boolean): (HeapItem<T> | null | T | undefined)[] {
    isItem = isItem ?? false;
    const sorted = this._pq.sort();
 
    return isItem ? sorted : sorted.map(item => item.val);
  }
 
  /**
   * The clear function clears the priority queue.
   */
  clear(): void {
    this._pq.clear();
  }
}