@thi.ng/heaps
Version:
Various heap implementations for arbitrary values and with customizable ordering
80 lines • 2.6 kB
TypeScript
import type { Comparator, IClear, ICopy, IEmpty, IInto, ILength, IStack, Maybe, Predicate, Predicate2 } from "@thi.ng/api";
import type { HeapOpts } from "./api.js";
/**
* Functional syntax sugar for {@link Heap} constructor.
*
* @param values
* @param opts
*/
export declare const defHeap: <T>(values?: Iterable<T> | null, opts?: Partial<HeapOpts<T>>) => Heap<T>;
/**
* Generic binary heap / priority queue with customizable ordering via
* user-supplied comparator. By default, implements min-heap ordering
* and uses @thi.ng/compare.
*
* @example
* ```ts tangle:../export/heap.ts
* import { Heap } from "@thi.ng/heaps";
*
* const h = new Heap([20, 5, 10]);
* h.push(15);
*
* console.log(h.pop()); // 5
* console.log(h.pop()); // 10
* console.log(h.pop()); // 15
* console.log(h.pop()); // 20
* console.log(h.pop()); // undefined
* ```
*/
export declare class Heap<T> implements Iterable<T>, IClear, ICopy<Heap<T>>, IEmpty<Heap<T>>, IInto<T, Heap<T>>, ILength, IStack<T, T, Heap<T>> {
static parentIndex(idx: number): number;
static childIndex(idx: number): number;
values: T[];
compare: Comparator<T>;
equiv: Predicate2<T>;
constructor(values?: Iterable<T> | null, opts?: Partial<HeapOpts<T>>);
[Symbol.iterator](): Generator<T, void, unknown>;
get length(): number;
copy(): Heap<T>;
clear(): void;
empty(): Heap<T>;
peek(): Maybe<T>;
push(val: T): this;
pop(): Maybe<T>;
pushPop(val: T, vals?: T[]): Maybe<T>;
into(vals: Iterable<T>): this;
/**
* Calls {@link Heap.pushPop} for each given value in `vals` and
* returns last result (i.e. the smallest value in heap after
* processing all `vals`).
*
* @param vals - values to insert
*/
pushPopAll(vals: Iterable<T>): Maybe<T>;
replaceHead(val: T): T;
remove(val: T): boolean;
find(val: T): T | undefined;
findWith(pred: Predicate<T>): T | undefined;
has(val: T): boolean;
heapify(vals?: T[]): void;
/**
* Returns the largest `n` values (or less) in heap, based on
* comparator ordering.
*
* @param n - number of values
*/
max(n?: number): T[];
/**
* Returns the smallest `n` values (or less) in heap, based on
* comparator ordering.
*
* @param n - number of values
*/
min(n?: number): T[];
parent(n: number): T | undefined;
children(n: number): T[] | undefined;
leaves(): T[];
protected percolateUp(i: number, vals?: T[]): void;
protected percolateDown(i: number, vals?: T[]): void;
}
//# sourceMappingURL=heap.d.ts.map