UNPKG

@naturalcycles/js-lib

Version:

Standard library for universal (browser + Node.js) javascript

51 lines (50 loc) 1.44 kB
/** * Implements a "round-robin" Stack ("first-in last-out" aka FILO) with a limited size. * Like an array of a fixed size. When it runs out of space - it starts writing on top of itself * from index 0. * * */ export declare class Stack<T> { readonly size: number; constructor(size: number); /** * Index of a slot to get written TO next. * Currently this slot contains OLDEST item (if any). */ private nextIndex; readonly items: T[]; push(item: T): this; /** * Fill (overwrite) the whole Stack (all its items) with the passed `item`. */ fill(item: T): this; /** * Returns last items in the right order. * Unlike raw `items` property that returns "items buffer" as-is (not ordered properly). */ get itemsOrdered(): T[]; } /** * Fixed-size FILO stack of Numbers. * Has convenience stat methods, e.g percentile, avg, etc. */ export declare class NumberStack extends Stack<number> { avg(): number; /** * Returns null if Stack is empty. */ avgOrNull(): number | null; median(): number; medianOrNull(): number | null; /** * `pc` is a number from 0 to 100 inclusive. */ percentile(pc: number): number; /** * `pc` is a number from 0 to 100 inclusive. * Returns null if Stack is empty. */ percentileOrNull(pc: number): number | null; percentiles(pcs: number[]): Record<number, number>; }