All files / src/interfaces set.ts

0% Statements 0/0
0% Branches 0/0
0% Functions 0/0
0% Lines 0/0

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91                                                                                                                                                                                     
/**
 * The set interfaces: collections that hold each member once.
 *
 * @packageDocumentation
 */
 
import type { Collection, ReadonlyCollection } from './collection'
 
/**
 * The read-only view of a set: a collection with no two equal members, readable the way native
 * `Set` is (`size`, `has`, `keys`, `values`, `entries`, `forEach`), so native set methods and
 * `new Set(set)` accept it.
 *
 * Two sets are equal when they have the same size and each has every member of the other,
 * whatever their insertion order.
 *
 * @example
 * ```ts
 * function overlap<E>(a: ReadonlySetCollection<E>, b: ReadonlySetCollection<E>): E[] {
 *   return a.toArray().filter((element) => b.has(element))
 * }
 * ```
 */
export interface ReadonlySetCollection<E> extends ReadonlyCollection<E> {
  /**
   * Returns an iterator over the members, as `values()` does (native `Set` has both).
   *
   * Time: O(1) to create; each step amortised O(1) unless the implementation documents otherwise.
   *
   * @returns A new iterator.
   */
  keys(): IterableIterator<E>
  /**
   * Returns an iterator over the members.
   *
   * Time: O(1) to create; each step amortised O(1) unless the implementation documents otherwise.
   *
   * @returns A new iterator.
   */
  values(): IterableIterator<E>
  /**
   * Returns an iterator over `[member, member]` pairs, as native `Set` does.
   *
   * Time: O(1) to create; each step amortised O(1) unless the implementation documents otherwise.
   *
   * @returns A new iterator.
   */
  entries(): IterableIterator<[E, E]>
  /**
   * Calls `action(member, member, set)` for each member, with `this` set to `thisArg`, as native
   * `Set` does.
   *
   * Time: O(n) plus the calls.
   *
   * @param action - Called with each member twice and the set.
   * @param thisArg - The `this` value for `action`.
   * @throws TypeError when `action` is not a function.
   */
  forEach(
    action: (element: E, same: E, set: ReadonlySetCollection<E>) => void,
    thisArg?: unknown,
  ): void
}
 
/**
 * A set: a collection with no two equal members. `add` keeps the member already present when an
 * equal one is added.
 *
 * @example
 * ```ts
 * function addEach<E>(set: SetCollection<E>, elements: E[]): number {
 *   const before = set.size
 *   set.addAll(elements)
 *   return set.size - before
 * }
 * ```
 */
export interface SetCollection<E> extends Collection<E>, ReadonlySetCollection<E> {
  /**
   * Calls `action(member, member, set)` for each member, with `this` set to `thisArg`, as native
   * `Set` does.
   *
   * Time: O(n) plus the calls.
   *
   * @param action - Called with each member twice and the set.
   * @param thisArg - The `this` value for `action`.
   * @throws TypeError when `action` is not a function.
   */
  forEach(action: (element: E, same: E, set: SetCollection<E>) => void, thisArg?: unknown): void
}