All files / src/interfaces sequenced-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 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106                                                                                                                                                                                                                   
/**
 * The sequenced-set interfaces: sets with a defined order and a first and last member.
 *
 * @packageDocumentation
 */
 
import type { ReadonlySequencedCollection, SequencedCollection } from './list'
import type { ReadonlySetCollection, SetCollection } from './set'
 
/**
 * The read-only view of a set with a defined order and a first and last member, readable from
 * either end.
 *
 * @example
 * ```ts
 * function newest<E>(history: ReadonlySequencedSet<E>): E | undefined {
 *   return history.isEmpty() ? undefined : history.getLast()
 * }
 * ```
 */
export interface ReadonlySequencedSet<E>
  extends ReadonlySetCollection<E>,
    ReadonlySequencedCollection<E> {
  /**
   * Returns a live view of this set in the opposite order: changes to either show in the other.
   *
   * Time: O(1).
   *
   * @returns The reversed view.
   */
  reversed(): ReadonlySequencedSet<E>
  /**
   * Calls `action(member, member, set)` for each member in order, with `this` set to `thisArg`.
   *
   * 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 with a defined order that can be changed at both ends. `addFirst` and `addLast` insert an
 * absent member at that end or move the present member equal to the argument there (keeping the
 * stored member), or throw `UnsupportedOperationError` where the set orders its members itself.
 * Whether iteration is live or fail-fast is stated by each implementation.
 *
 * @example
 * ```ts
 * function touch<E>(recent: SequencedSet<E>, item: E, limit: number): void {
 *   recent.addLast(item)
 *   while (recent.size > limit) recent.removeFirst()
 * }
 * ```
 */
export interface SequencedSet<E>
  extends SetCollection<E>,
    SequencedCollection<E>,
    ReadonlySequencedSet<E> {
  /**
   * Inserts `element` at the front, or moves the member equal to it to the front, and returns the
   * set.
   *
   * Time: as the implementation documents.
   *
   * @param element - The element to insert or move.
   * @returns The set.
   * @throws UnsupportedOperationError where the set orders its members itself.
   */
  addFirst(element: E): this
  /**
   * Inserts `element` at the end, or moves the member equal to it to the end, and returns the set.
   *
   * Time: as the implementation documents.
   *
   * @param element - The element to insert or move.
   * @returns The set.
   * @throws UnsupportedOperationError where the set orders its members itself.
   */
  addLast(element: E): this
  /**
   * Returns a live view of this set in the opposite order; changes through the view reach this
   * set at the mirrored end.
   *
   * Time: O(1).
   *
   * @returns The reversed view.
   */
  reversed(): SequencedSet<E>
  /**
   * Calls `action(member, member, set)` for each member in order, with `this` set to `thisArg`.
   *
   * 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
}