@runtimeverificationinc/tsk
Version:
TypeScript/JavaScript library for K Framework functionality
80 lines (79 loc) • 2.63 kB
TypeScript
/**
* TypeScript implementation of Python's collections.Counter
* A Counter is a dict subclass for counting hashable objects.
*/
export declare class Counter<T = string> extends Map<T, number> {
constructor(iterable?: Iterable<T> | Record<string, number> | Map<T, number>);
/**
* Add an item to the counter, incrementing its count by 1
*/
increment(key: T, count?: number): void;
/**
* Subtract an item from the counter, decrementing its count by 1
*/
decrement(key: T, count?: number): void;
/**
* Get the count for a key, returning defaultValue if not found
*/
get(key: T, defaultValue?: number): number;
/**
* Update the counter with counts from another counter or iterable
*/
update(other: Iterable<T> | Counter<T> | Record<string, number> | Map<T, number>): void;
/**
* Subtract counts from another counter or iterable
*/
subtract(other: Iterable<T> | Counter<T> | Record<string, number> | Map<T, number>): void;
/**
* Return the n most common elements and their counts from the most common to the least
*/
mostCommon(n?: number): Array<[T, number]>;
/**
* Return the n least common elements and their counts from the least common to the most
*/
leastCommon(n?: number): Array<[T, number]>;
/**
* Return all elements with positive counts
*/
elements(): T[];
/**
* Return the total of all counts
*/
total(): number;
/**
* Addition: add counts from two counters
*/
add(other: Counter<T>): Counter<T>;
/**
* Subtraction: subtract counts (keeping only positive counts)
*/
sub(other: Counter<T>): Counter<T>;
/**
* Intersection: min(c[x], d[x])
*/
intersection(other: Counter<T>): Counter<T>;
/**
* Union: max(c[x], d[x])
*/
union(other: Counter<T>): Counter<T>;
/**
* Create a new Counter with only positive counts
*/
positive(): Counter<T>;
/**
* Convert to a plain object
*/
toObject(): Record<string, number>;
/**
* Convert to a string representation
*/
toString(): string;
/**
* Create a Counter from keys with a default count
*/
static fromKeys<T>(keys: Iterable<T>, count?: number): Counter<T>;
}
export declare function add<T>(a: Counter<T>, b: Counter<T>): Counter<T>;
export declare function subtract<T>(a: Counter<T>, b: Counter<T>): Counter<T>;
export declare function intersection<T>(a: Counter<T>, b: Counter<T>): Counter<T>;
export declare function union<T>(a: Counter<T>, b: Counter<T>): Counter<T>;