import { ToString } from "@ixfx/core"; import { SimpleEventEmitter } from "@ixfx/events"; //#region src/set/types.d.ts type ValueSetEventMap = { readonly add: { readonly value: V; readonly updated: boolean; }; readonly clear: boolean; readonly delete: V; }; //#endregion //#region src/set/ISetMutable.d.ts /** * A Set which stores unique items determined by their value, rather * than object reference (unlike the default JS Set). Create with {@link Sets.mutable}. Mutable. * * By default the `JSON.stringify()` representation is considered the 'key' for an object. * Pass in a function to `Sets.mutable` to define your own way of creating keys for values. The principle should * be that objects that you consider identical should have the same string key value. * * ISetMutable fires `add`, `clear` and `delete` events. * * @example Overview of functions * ```js * const s = Sets.mutable(); * s.add(item); // Add one or more items. Items with same key are overriden. * s.has(item); // Returns true if item value is present * s.clear(); // Remove everything * s.delete(item); // Delete item by value * s.toArray(); // Returns values as an array * s.values(); // Returns an iterator over values * s.size; // Number of items stored in set * ``` * * @example Example usage * ```js * // Data to add * const people = [ * {name: `Barry`, city: `London`} * {name: `Sally`, city: `Bristol`} * ]; * * // Create a set, defining how keys will be generated * const set = Sets.mutable(person => { * // Key person objects by name and city. * // ie. Generated keys will be: `Barry-London`, `Sally-Bristol` * return `${person.name}-${person.city}` * }); * * // Add list * set.add(...people); * * // Demo: * set.has({name:`Barry`, city:`Manchester`})); // False, key is different (Barry-Manchester) * set.has({name:`Barry`, city:`London`})); // True, we have Barry-London as a key * set.has(people[1]); // True, key of object is found (Sally-Bristol) * ``` * * @example * Events * ```js * set.addEventListener(`add`, ev => { * console.log(`New item added: ${ev.value}`); * }); * ``` * * @typeParam V - Type of data stored */ interface ISetMutable extends SimpleEventEmitter> { /** * Add `values` to set. * Corresponding keys will be generated according to the * function provided to `setMutable`, or `JSON.stringify` by default. * @param values Value(s) to add * @returns _true_ if something new was added */ add(...values: readonly V[]): boolean; /** * Iterate over values * ```js * for (let value of set.values()) { * // use value... * } * ``` */ values(): IterableIterator; /** * Clears set */ clear(): void; /** * Deletes specified value, if present. * @param value * @returns True if value was found */ delete(value: V): boolean; /** * Returns _true_ if _value_ is contained in Set * @param value */ has(value: V): boolean; /** * Returns an array of values */ toArray(): readonly V[]; /** * Returns the number of items stored in the set */ get size(): number; } //#endregion //#region src/set/set-mutable.d.ts /** * Creates a {@link ISetMutable}. * @param keyString Function that produces a key based on a value. If unspecified, uses `JSON.stringify` * @returns */ declare const mutable: (keyString?: ToString) => ISetMutable; /** * Mutable string set */ declare class SetStringMutable extends SimpleEventEmitter> implements ISetMutable { store: Map; keyString: ToString; /** * Constructor * @param keyString Function which returns a string version of added items. If unspecified `JSON.stringify` */ constructor(keyString: ToString | undefined); /** * Number of items stored in set */ get size(): number; /** * Adds one or more items to set. `add` event is fired for each item * @param values items to add */ add(...values: V[]): boolean; /** * Returns values from set as an iterable * @returns */ values(): MapIterator; /** * Clear items from set */ clear(): void; /** * Delete value from set. * @param v Value to delete * @returns _True_ if item was found and removed */ delete(v: V): boolean; /** * Returns _true_ if item exists in set * @param v * @returns */ has(v: V): boolean; /** * Returns array copy of set * @returns Array copy of set */ toArray(): V[]; } //#endregion //#region src/set/ISet.d.ts interface ISet { has(v: V): boolean; get size(): number; values(): IterableIterator; /** * Returns an array of values */ toArray(): readonly V[]; } //#endregion //#region src/set/ISetImmutable.d.ts /** * A Set which stores unique items determined by their value, rather * than object reference (unlike the default JS Set). Create with {@link Sets.mutable}. Immutable. * * By default the `JSON.stringify()` representation is considered the 'key' for an object. * Pass in a function to `setMutable` to define your own way of creating keys for values. The principle should * be that objects that you consider identical should have the same string key value. * * The {@link Sets.ISetMutable} alternative also has events for monitoring changes. * * @example Overview of functions * ```js * const s = Sets.mutable(); * s.add(item); // Add one or more items. Items with same key are overriden. * s.has(item); // Returns true if item value is present * s.clear(); // Remove everything * s.delete(item); // Delete item by value * s.toArray(); // Returns values as an array * s.values(); // Returns an iterator over values * s.size; // Returns number of items in set * ``` * * @example Example usage * ```js * // Data to add * const people = [ * {name: `Barry`, city: `London`} * {name: `Sally`, city: `Bristol`} * ]; * * // Create a set, defining how keys will be generated * let s = Sets.mutable(person => { * // Key person objects by name and city. * // ie. Generated keys will be: `Barry-London`, `Sally-Bristol` * return `${person.name}-${person.city}` * }); * * // Add list - since it's immutable, a changed copy is returned * s = s.add(...people); * * // Accessing: has/get * s.has({name:`Barry`, city:`Manchester`})); // False, key is different (Barry-Manchester) * s.has({name:`Barry`, city:`London`})); // True, we have Barry-London as a key * s.has(people[1]); // True, key of object is found (Sally-Bristol) * * // Deleting (returns changed copy) * s = s.delete({name:`Barry`, city:`London`}); * ``` * * @typeParam V - Type of data stored */ interface ISetImmutable extends ISet { add(...values: readonly V[]): ISetImmutable; delete(v: V): ISetImmutable; } //#endregion //#region src/set/SetImmutable.d.ts declare class SetStringImmutable implements ISetImmutable { private store; private keyString; constructor(keyString?: ToString, map?: Map); get size(): number; add(...values: readonly V[]): ISetImmutable; delete(v: V): ISetImmutable; has(v: V): boolean; toArray(): V[]; values(): Generator; } /** * Immutable set that uses a `keyString` function to determine uniqueness * * @param keyString Function that produces a key based on a value. If unspecified, uses `JSON.stringify`. * @returns */ declare const immutable: (keyString?: ToString) => ISetImmutable; //#endregion //#region src/set/massive-set.d.ts /** * MassiveSet supports semantics similar to Set, but without the * limitation on how much data is stored. * * It only supports strings, and stores data in a hierarchy. * * ```js * const set = new MassiveSet(); // maxDepth=1 default * set.add(`test`); * set.add(`bloorp`); * ``` * * In the above example, it will create a subtree for the first letter * of each key, putting the value underneath it. So we'd get a sub * MassiveSet for every key starting with 't' and every one starting with 'b'. * * If `maxDepth` was 2, we'd get the same two top-level nodes, but then * another sub-node based on the _second_ character of the value. * * It's not a very smart data-structure since it does no self-balancing * or tuning. */ declare class MassiveSet { #private; children: Map; values: Array; constructor(maxDepth?: number, depth?: number); /** * Returns the number of values stored in just this level of the set * @returns */ sizeLocal(): number; /** * Returns the number of branches at this node * Use {@link sizeChildrenDeep} to count all branches recursively * @returns */ sizeChildren(): number; sizeChildrenDeep(): number; /** * Returns the total number of values stored in the set */ size(): number; add(value: string): void; remove(value: string): boolean; debugDump(): void; /** * Returns _true_ if `value` stored on this node * @param value * @returns */ hasLocal(value: string): boolean; has(value: string): boolean; } declare namespace index_d_exports { export { ISetImmutable, ISetMutable, MassiveSet, SetStringImmutable, SetStringMutable, ValueSetEventMap, immutable, mutable }; } //#endregion export { ISetImmutable as a, ISetMutable as c, immutable as i, ValueSetEventMap as l, MassiveSet as n, SetStringMutable as o, SetStringImmutable as r, mutable as s, index_d_exports as t };