UNPKG

async-await-queue

Version:

async/await simple priority queues

85 lines (84 loc) 2.49 kB
/** * Lighter version of * Heap.ts from https://github.com/ignlg/heap-js/blob/master/src/Heap.ts * heap-js by @ignlg */ export type Comparator<T> = (a: T, b: T) => number; export type IsEqual<T> = (e: T, o: T) => boolean; export declare const toInt: (n: number) => number; /** * Heap * @type {Class} */ export declare class Heap<T> { compare: Comparator<T>; heapArray: Array<T>; _limit: number; /** * Heap instance constructor. * @param {Function} compare Optional comparison function, defaults to Heap.minComparator<number> */ constructor(compare: Comparator<T>); /** * Gets children indices for given index. * @param {Number} idx Parent index * @return {Array(Number)} Array of children indices */ static getChildrenIndexOf(idx: number): Array<number>; /** * Gets parent index for given index. * @param {Number} idx Children index * @return {Number | undefined} Parent index, -1 if idx is 0 */ static getParentIndexOf(idx: number): number; /** * Adds an element to the heap. Aliases: `offer`. * Same as: push(element) * @param {any} element Element to be added * @return {Boolean} true */ push(element: T): boolean; /** * Length of the heap. * @return {Number} */ length(): number; /** * Top node. Aliases: `element`. * Same as: `top(1)[0]` * @return {any} Top node */ peek(): T | undefined; /** * Extract the top node (root). Aliases: `poll`. * @return {any} Extracted top node, undefined if empty */ pop(): T | undefined; /** * Pop the current peek value, and add the new item. * @param {any} element Element to replace peek * @return {any} Old peek */ replace(element: T): T; /** * Size of the heap * @return {Number} */ size(): number; /** * Move a node to a new index, switching places * @param {Number} j First node index * @param {Number} k Another node index */ _moveNode(j: number, k: number): void; /** * Move a node down the tree (to the leaves) to find a place where the heap is sorted. * @param {Number} i Index of the node */ _sortNodeDown(i: number): void; /** * Move a node up the tree (to the root) to find a place where the heap is sorted. * @param {Number} i Index of the node */ _sortNodeUp(i: number): void; }