All files / src/internal/skeletons abstract-set.ts

100% Statements 34/34
100% Branches 12/12
100% Functions 15/15
100% Lines 26/26

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 107                        107x 107x     1202x                           1081028x                         10727x       165057x       1585067x       1802x       1590x       1320x             3420x 3420x 10934x                 136973x 136731x 136170x 136170x 135513x 135339x   6x   784274x 783129x 134753x             12603x   29x 12574x        
// The skeleton every set extends: live iteration, `{}` brackets, the native `Set` reading methods,
// and equality and hashing by members, whatever the insertion order.
 
import type { SetCollection } from '../../interfaces/set'
import { isBranded, SET_COLLECTION_BRAND } from '../brand'
import { createGuard, rememberEqual } from '../cycle'
import { type HashFunction, hashCodeSymbol } from '../equality'
import { apply } from '../intrinsics'
import { MappedLiveIterator } from '../iteration'
import { AbstractCollection } from './abstract-collection'
 
// One guard per operation, so a member's own code can compare or hash a set mid-operation.
const equalsGuard = /*#__PURE__*/ createGuard()
const hashGuard = /*#__PURE__*/ createGuard()
 
function pair<E>(element: E): [E, E] {
  return [element, element]
}
 
/**
 * Base class of every set.
 *
 * Subclasses implement `size`, `[Symbol.iterator]`, `add`, `cursor`, `[Symbol.toStringTag]`,
 * `owner`, `hasMember` (membership naming the calling method in errors) and `memberHashes` (the
 * order-independent sum of the members' hashes).
 *
 * @internal
 */
export abstract class AbstractSet<E> extends AbstractCollection<E> implements SetCollection<E> {
  /** `live` by default, as native `Set`; sorted sets, which have no native twin, are `fail-fast`. */
  protected readonly iterationMode: 'fail-fast' | 'live' = 'live'
 
  /** The public set handed to `forEach` callbacks: the object the caller holds. */
  abstract get owner(): SetCollection<E>
 
  /** Returns whether a member equals `element`; errors name `${className}.${method}`. */
  protected abstract hasMember(element: E, method: string): boolean
 
  /** Returns the 32-bit sum of the members' hashes, as this set's equality defines them. */
  protected abstract memberHashes(hash: HashFunction): number
 
  /** Marks every set for `isSetCollection`, across loaded copies. */
  get [SET_COLLECTION_BRAND](): true {
    return true
  }
 
  protected override brackets(): readonly [string, string] {
    return ['{', '}']
  }
 
  override has(element: E): boolean {
    return this.hasMember(element, 'has')
  }
 
  keys(): IterableIterator<E> {
    return this[Symbol.iterator]()
  }
 
  values(): IterableIterator<E> {
    return this[Symbol.iterator]()
  }
 
  entries(): IterableIterator<[E, E]> {
    return new MappedLiveIterator<E, [E, E]>(this[Symbol.iterator](), pair)
  }
 
  override forEach(
    action: (element: E, same: E, set: SetCollection<E>) => void,
    thisArg?: unknown,
  ): void {
    this.requireFunction(action, 'forEach', 'action')
    const owner = this.owner
    for (const element of this) apply(action, thisArg, [element, element, owner])
  }
 
  /**
   * Equal when `other` is a set of the same size, every member of this set is in `other` and
   * every member of `other` is in this set, so the result is the same both ways round even when
   * the two sets compare members differently.
   */
  override equals(other: unknown): boolean {
    if (other === this || other === this.owner) return true
    if (!isBranded(other, SET_COLLECTION_BRAND)) return false
    const that = other as SetCollection<unknown>
    if (that.size !== this.size) return false
    return rememberEqual(this.owner, that, () =>
      equalsGuard(
        this,
        () => false,
        () => {
          for (const element of this) if (!that.has(element)) return false
          for (const element of that) if (!this.hasMember(element as E, 'equals')) return false
          return true
        },
      ),
    )
  }
 
  override [hashCodeSymbol](hash: HashFunction): number {
    return hashGuard(
      this,
      () => 0,
      () => this.memberHashes(hash),
    )
  }
}