@andranik-arakelyan/js-utilities
Version:
Javascript utilities
63 lines (62 loc) • 2.28 kB
TypeScript
/**
* MultiSet (bag) implementation for js-utilities.
* Allows duplicate elements and keeps count of each unique value.
*
* - Adding a negative count decreases the count, and removes the item if the count is zero or negative.
* - Removing with a negative count increases the count.
* - The count for any item is always positive or the item is absent.
*/
export declare class MultiSet<T> {
private map;
/**
* Add an item to the MultiSet with the given count (default 1).
* If count is negative, decreases the count. If the final count is zero or negative, removes the item.
* @param item The item to add.
* @param count The number of times to add (can be negative).
*/
add(item: T, count?: number): void;
/**
* Remove an item from the MultiSet with the given count (default 1).
* If count is negative, increases the count (same as add with positive count).
* If the final count is zero or negative, removes the item.
* @param item The item to remove.
* @param count The number of times to remove (can be negative).
*/
remove(item: T, count?: number): void;
/**
* Get the count of a specific item in the MultiSet.
* @param item The item to count.
* @returns The number of times the item appears (0 if not present).
*/
count(item: T): number;
/**
* Check if the MultiSet contains the given item.
* @param item The item to check.
* @returns True if the item is present, false otherwise.
*/
has(item: T): boolean;
/**
* Get the total number of items in the MultiSet (including duplicates).
* @returns The total count of all items.
*/
size(): number;
/**
* Get the number of unique items in the MultiSet.
* @returns The number of unique items.
*/
uniqueSize(): number;
/**
* Remove all items from the MultiSet.
*/
clear(): void;
/**
* Convert the MultiSet to an array, repeating each item according to its count.
* @returns An array of all items in the MultiSet.
*/
toArray(): T[];
/**
* Get an iterator over [item, count] pairs in the MultiSet.
* @returns An iterator of [item, count] pairs.
*/
entries(): IterableIterator<[T, number]>;
}