All files / src/data-structures/priority-queue max-priority-queue.ts

100% Statements 12/12
91.66% Branches 11/12
100% Functions 4/4
100% Lines 12/12

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              19x     19x                   7x         363038x 363038x 363038x                             2x         21x 21x 21x     2x 2x      
/**
 * 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 {PriorityQueueOptions, SpecifyOptional} from '../../types';
 
export class MaxPriorityQueue<T = number> extends PriorityQueue<T> {
  constructor(options?: Omit<PriorityQueueOptions<number>, 'comparator'>);
  constructor(options: PriorityQueueOptions<T>);
 
  /**
   * The constructor initializes a priority queue with an optional comparator function.
   * @param [options] - The `options` parameter is an optional object that can contain various properties to configure
   * the priority queue.
   */
  constructor(options?: SpecifyOptional<PriorityQueueOptions<T>, 'comparator'>) {
    super({
      ...options,
      comparator: options?.comparator
        ? options.comparator
        : (a: T, b: T) => {
            const aKey = a as unknown as number,
              bKey = b as unknown as number;
            return bKey - aKey;
          }
    });
  }
 
  static override heapify<T extends number>(options?: Omit<PriorityQueueOptions<T>, 'comparator'>): MaxPriorityQueue<T>;
  static override heapify<T>(options: PriorityQueueOptions<T>): MaxPriorityQueue<T>;
 
  /**
   * The function `heapify` creates a max priority queue from the given options and returns it.
   * @param options - The `options` parameter is an object that contains configuration options for creating a priority
   * queue. It can have the following properties:
   * @returns a MaxPriorityQueue object.
   */
  static override heapify<T>(options: PriorityQueueOptions<T>): MaxPriorityQueue<T> {
    const maxPQ = new MaxPriorityQueue<T>({
      ...options,
      comparator: options?.comparator
        ? options.comparator
        : (a: T, b: T) => {
            const aKey = a as unknown as number,
              bKey = b as unknown as number;
            return bKey - aKey;
          }
    });
    maxPQ._fix();
    return maxPQ;
  }
}