/** * HashSet - Immutable Set Operations * * An immutable, unordered collection of unique values built on JavaScript's native Set. * All operations return new Sets, leaving the original unchanged. * * ## When to Use HashSet * * Choose HashSet over other collections when: * - You need to ensure elements are unique * - You frequently need to check if an element exists in the collection * - You need to perform set operations like union, intersection, and difference * - The order of elements doesn't matter to your use case * - You want immutable, side-effect-free operations * * Choose other collections when: * - You need to maintain insertion order (use arrays) * - You need key-value associations (use objects or Map) * - You need to frequently access elements by index (use arrays) * - You need maximum performance with many modifications (consider direct Set mutation) * * ## Common Use Cases * * - Tracking unique items (e.g., users who have completed an action) * - Efficiently testing for membership in a collection * - Performing set theory operations (union, intersection, difference) * - Eliminating duplicates from a collection * - Functional programming patterns where immutability is important * * ## Performance Characteristics * * - Lookup: O(1) average - Check if a value exists * - Insertion: O(1) average - Add a value (returns new Set) * - Removal: O(1) average - Remove a value (returns new Set) * - Iteration: O(n) - Iterate over all values * - Set operations: O(n) - Union, intersection, difference * * Note: Because this is an immutable implementation, each modification creates a new Set. * If you need to perform many sequential modifications, consider using native Set directly * and converting to/from HashSet operations as needed. * * @example * ```typescript * import * as HashSet from './HashSet'; * * // Create a Set * const tags = HashSet.make('javascript', 'typescript', 'react'); * * // Add an element (immutable - returns new Set) * const moreTags = HashSet.add(tags, 'node'); * * // Check membership * const hasReact = HashSet.contains(moreTags, 'react'); // true * * // Set operations * const frontendTags = HashSet.make('react', 'vue', 'angular'); * const common = HashSet.intersection(moreTags, frontendTags); // Set { 'react' } * * // Get unique values from array * const unique = HashSet.fromArray([1, 2, 2, 3, 3, 3]); // Set { 1, 2, 3 } * ``` * * @module HashSet */ import { Eq } from '@ephox/dispute'; import { Optional } from './Optional'; type SetMorphism = (x: T) => U; type SetGuardPredicate = (x: T) => x is U; type SetPredicate = SetMorphism; /** * Creates an empty Set. * * @returns An empty Set */ export declare const empty: () => Set; /** * Creates a Set from multiple values. * Duplicate values will be automatically removed. * * @param values - The values to include in the set * @returns A new Set containing all unique values */ export declare const make: (...values: T[]) => Set; /** * Creates a Set containing a single element. * * @param x - The element to wrap in a Set * @returns A new Set containing only the provided element */ export declare const pure: (x: T) => Set; /** * Creates a Set from an array-like object. * Duplicate elements in the array will be automatically removed in the resulting Set. * * @param array - The array-like object to convert * @returns A new Set containing all unique elements from the array */ export declare const fromArray: (array: ArrayLike) => Set; /** * Creates a Set from an iterable. * * @param iterable - The iterable to convert * @returns A new Set containing all unique elements from the iterable */ export declare const fromIterable: (iterable: Iterable) => Set; /** * Creates a Set from the values of a plain JavaScript object. * Only the values are used; keys are discarded. Duplicate values are automatically removed. * * @param obj - The object whose values to extract * @returns A new Set containing all unique values from the object */ export declare const fromValues: (obj: T) => Set; /** * Creates a new Set with the specified value added. * Does not modify the original set. * * @param set - The original set * @param value - The value to add * @returns A new Set containing all elements from the original set plus the new value */ export declare const add: (set: Set, value: T) => Set; /** * Creates a new Set with the specified value removed. * Does not modify the original set. * * @param set - The original set * @param value - The value to remove * @returns A new Set containing all elements from the original set except the removed value */ export declare const remove: (set: Set, value: T) => Set; /** * Toggles a value's presence in the set. * If the value exists, it is removed; if it doesn't exist, it is added. * Does not modify the original set. * * @param set - The original set * @param value - The value to toggle * @returns A new Set with the value toggled */ export declare const toggle: (set: Set, value: T) => Set; /** * Checks if a value exists in the set. * * @param set - The set to search * @param value - The value to look for * @returns true if the value is in the set, false otherwise */ export declare const contains: (set: ReadonlySet, value: T) => boolean; /** * Returns the number of elements in the set. * * @param set - The set to measure * @returns The number of elements in the set */ export declare const size: (set: Set) => number; /** * Tests whether the set is empty. * * @param set - The set to test * @returns true if the set has no elements, false otherwise */ export declare const isEmpty: (set: Set) => boolean; /** * Returns an iterator of all values in the set. * * @param set - The set to iterate * @returns An iterator of all values */ export declare const values: (set: Set) => IterableIterator; /** * Converts a Set to an Array. * * @param set - The set to convert * @returns An array containing all elements from the set */ export declare const toArray: (set: Set) => T[]; /** * Checks if at least one element in the set satisfies the predicate. * Returns false for empty sets (no elements exist that satisfy the predicate). * * @param set - The set to test * @param pred - The predicate function to test each element * @returns true if any element satisfies the predicate, false otherwise (including empty sets) */ export declare const exists: (set: ReadonlySet, pred: SetPredicate) => boolean; /** * Tests whether all elements in the set satisfy the predicate. * Returns true for empty sets (vacuous truth - all zero elements satisfy the predicate). * * @param set - The set to test * @param pred - The predicate function to test each element * @returns true if all elements satisfy the predicate, false otherwise (true for empty sets) */ export declare const forall: (set: ReadonlySet, pred: SetPredicate) => boolean; /** * Tests whether two Sets are equal based on a custom equality function. * Sets are equal if they have the same size and all elements in s1 have * a corresponding equal element in s2. * * @param s1 - The first set * @param s2 - The second set * @param eq - The equality function (defaults to Eq.eqAny) * @returns true if the sets are equal, false otherwise */ export declare const equal: (s1: ReadonlySet, s2: ReadonlySet, eq?: Eq.Eq) => boolean; /** * Returns a new Set containing all elements from both sets. * Duplicates are automatically removed due to Set semantics. * * @param s1 - The first set * @param s2 - The second set * @returns A new Set containing all elements from both sets */ export declare const union: (s1: ReadonlySet, s2: ReadonlySet) => Set; /** * Returns a new Set containing only elements that exist in both sets. * * @param s1 - The first set * @param s2 - The second set * @returns A new Set containing elements present in both sets */ export declare const intersection: (s1: ReadonlySet, s2: ReadonlySet) => Set; /** * Returns a new Set containing elements in s1 that are not in s2. * Also known as set difference or relative complement. * * @param s1 - The first set * @param s2 - The set to subtract * @returns A new Set containing elements in s1 but not in s2 */ export declare const difference: (s1: ReadonlySet, s2: ReadonlySet) => Set; /** * Returns a new Set containing elements that exist in either set but not in both. * Also known as symmetric difference or disjunctive union. * * @param s1 - The first set * @param s2 - The second set * @returns A new Set containing elements in either set but not in both * @example * ```typescript * const a = HashSet.make(1, 2, 3); * const b = HashSet.make(2, 3, 4); * const result = HashSet.symmetricDifference(a, b); * // Set { 1, 4 } (elements in either but not both) * ``` */ export declare const symmetricDifference: (s1: ReadonlySet, s2: ReadonlySet) => Set; /** * Tests whether s1 is a subset of s2 (all elements of s1 are in s2). * * @param s1 - The potential subset * @param s2 - The potential superset * @returns true if s1 is a subset of s2, false otherwise */ export declare const isSubsetOf: (s1: ReadonlySet, s2: ReadonlySet) => boolean; /** * Tests whether s1 is a superset of s2 (all elements of s2 are in s1). * * @param s1 - The potential superset * @param s2 - The potential subset * @returns true if s1 is a superset of s2, false otherwise */ export declare const isSupersetOf: (s1: ReadonlySet, s2: ReadonlySet) => boolean; /** * Tests whether two sets have no elements in common. * * @param s1 - The first set * @param s2 - The second set * @returns true if the sets have no common elements, false otherwise */ export declare const isDisjointFrom: (s1: ReadonlySet, s2: ReadonlySet) => boolean; /** * Transforms each element of the set using the provided function and returns a new set. * Note: If the transformation function produces duplicate values, only one will be kept * in the resulting set (as per Set behavior). * * @param set - The set to transform * @param f - The transformation function * @returns A new Set containing the transformed elements */ export declare const map: (set: ReadonlySet, f: SetMorphism) => Set; /** * Creates a new Set containing only the elements that satisfy the predicate. * Supports type guard predicates for narrowing the type. * * @param set - The set to filter * @param pred - The predicate function to test each element * @returns A new Set containing only elements that satisfy the predicate */ export declare const filter: { (set: ReadonlySet, pred: SetGuardPredicate): Set; (set: ReadonlySet, pred: SetPredicate): Set; }; /** * Splits a set into two sets based on a predicate function. * Elements that satisfy the predicate go into the 'pass' set, * elements that don't go into the 'fail' set. * * @param set - The set to partition * @param pred - The predicate function to test each element * @returns An object with two Sets: 'pass' and 'fail' * @example * ```typescript * const numbers = HashSet.make(1, 2, 3, 4, 5, 6); * const { pass, fail } = HashSet.partition(numbers, (x) => x % 2 === 0); * // pass: Set { 2, 4, 6 }, fail: Set { 1, 3, 5 } * ``` */ export declare const partition: (set: ReadonlySet, pred: SetPredicate) => { pass: Set; fail: Set; }; /** * Maps a function over the set and flattens the result. * Similar to flatMap in other functional libraries. * * @param set - The set to transform * @param f - A function that transforms each element into a Set * @returns A new Set containing all elements from all resulting Sets */ export declare const bind: (set: ReadonlySet, f: SetMorphism>) => Set; /** * Executes a function for each element in the set. * This function is for side effects only and does not return a value. * * @param set - The set to iterate over * @param f - The function to execute for each element */ export declare const each: (set: ReadonlySet, f: SetMorphism) => void; /** * Reduces a set to a single value by applying a function to each element and an accumulator. * Processes elements in insertion order. * * @param set - The set to reduce * @param f - The reducer function that takes the accumulator and current element * @param acc - The initial accumulator value * @returns The final accumulated value */ export declare const foldl: (set: ReadonlySet, f: (acc: U, x: T) => U, acc: U) => U; /** * Finds the first element in the set that satisfies the predicate. * Supports type guard predicates for narrowing the type. * * @param set - The set to search * @param pred - The predicate function to test each element * @returns Optional.some(element) if found, Optional.none() otherwise */ export declare const find: { (set: ReadonlySet, pred: SetGuardPredicate): Optional; (set: ReadonlySet, pred: SetPredicate): Optional; }; /** * Maps a function over the set and returns the first Some result. * Useful for searching and transforming in a single operation. * * @param set - The set to search and transform * @param f - A function that returns Optional for each element * @returns The first Some result, or Optional.none() if no element produces a Some */ export declare const findMap: (set: ReadonlySet, f: (a: A) => Optional) => Optional; export {}; //# sourceMappingURL=HashSet.d.ts.map