All files / src/internal/engines hash-support.ts

100% Statements 31/31
100% Branches 32/32
100% Functions 5/5
100% Lines 28/28

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 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146                                                  109x                           850252x 7920x 13x   7907x 7907x 7907x 7907x 10x   7897x 4687x 3517x                                                                           109533x 109533x   4361x 3580x 781x 770x   11x   109522x                                           1509843x 1509843x 1509843x 1509843x       2550314x 86x       2550228x                   6499885x    
// Helpers shared by the hash engines: the guard around user callbacks, and `-0` normalisation.
 
import { ConcurrentModificationError } from '../../errors/errors'
import { describeValue, isIterable } from '../checks'
import { type KeyOf, sameValueZeroEquivalence, valueEquivalence } from '../equality'
import { type CheckedEquivalence, checkEquivalence } from '../equivalence'
import { checkKey } from '../key'
import type { ChangeCounter } from '../revision'
 
/**
 * How a hash collection decides that two values are equal, fixed at construction: SameValueZero on
 * the values (`native`), SameValueZero on derived keys (`keyed`), or an equivalence over seeded
 * buckets (`buckets`; `builtIn` for this copy's `valueEquivalence`).
 *
 * @internal
 */
export type HashEquality<E> =
  | { readonly kind: 'native' }
  | { readonly kind: 'keyed'; readonly key: KeyOf<E> }
  | {
      readonly kind: 'buckets'
      readonly equivalence: CheckedEquivalence
      readonly builtIn: boolean
    }
 
const SAME_VALUE_ZERO = { kind: 'native' } as const
 
/**
 * Reads a hash collection's options once: `key` and `equivalence` are each read a single time and
 * checked; `undefined` and `null` mean neither.
 *
 * Time: O(1).
 *
 * @param options - The options argument as given.
 * @param where - The constructor or factory to name in errors.
 * @throws TypeError when `options` is not an object, both are given, or either is invalid.
 * @internal
 */
export function readEquality<E>(options: unknown, where: string): HashEquality<E> {
  if (options === undefined || options === null) return SAME_VALUE_ZERO
  if (typeof options !== 'object') {
    throw new TypeError(`${where}: options is not an object (${describeValue(options)})`)
  }
  const record = options as { key?: unknown; equivalence?: unknown }
  const key = record.key
  const equivalence = record.equivalence
  if (key !== undefined && equivalence !== undefined) {
    throw new TypeError(`${where}: key and equivalence cannot both be given`)
  }
  if (key !== undefined) return { kind: 'keyed', key: checkKey<E>(key, where) as KeyOf<E> }
  if (equivalence === undefined || equivalence === sameValueZeroEquivalence) return SAME_VALUE_ZERO
  return {
    kind: 'buckets',
    equivalence: checkEquivalence(equivalence, where),
    builtIn: equivalence === valueEquivalence,
  }
}
 
/**
 * The arguments of a hash set's constructor, read by the native `Set` rules: an iterable first
 * argument is the elements; `undefined` or `null` means none; any other object, when no second
 * argument is given, is the options.
 *
 * @internal
 */
export interface SetArguments<E> {
  readonly elements: Iterable<E> | undefined
  readonly equality: HashEquality<E>
}
 
/**
 * Reads a hash set's constructor arguments: elements first, then options, as `readEquality` checks
 * them.
 *
 * Time: O(1).
 *
 * @param first - The first argument as given: elements, options, `undefined` or `null`.
 * @param second - The second argument as given: options, `undefined` or `null`.
 * @param where - The constructor to name in errors.
 * @throws TypeError when `first` is neither iterable, an options object nor `null`/`undefined`, or
 *   the options are invalid.
 * @internal
 */
export function readSetArguments<E>(
  first: unknown,
  second: unknown,
  where: string,
): SetArguments<E> {
  let elements: Iterable<E> | undefined
  let given: unknown = second
  if (first === undefined || first === null) {
    // empty
  } else if (isIterable(first)) {
    elements = first as Iterable<E>
  } else if (typeof first === 'object' && (second === undefined || second === null)) {
    given = first
  } else {
    throw new TypeError(`${where}: elements is not iterable (${describeValue(first)})`)
  }
  return { elements, equality: readEquality<E>(given, where) }
}
 
/**
 * One operation on a hash collection: the API to name in errors, and the structure count when it
 * started. A `key`, `equals` or `hash` call is passed through `after`, which throws when the call
 * added or removed members or mappings, so the operation itself changes nothing.
 *
 * @internal
 */
export class Operation {
  private readonly start: number
  private readonly noun: string
  readonly changes: ChangeCounter
  readonly where: string
 
  /**
   * @param changes - The collection's change counter.
   * @param where - `<Class>.<method>`, for messages.
   * @param noun - What the collection is called in messages: `set` or `map`.
   */
  constructor(changes: ChangeCounter, where: string, noun = 'set') {
    this.changes = changes
    this.where = where
    this.noun = noun
    this.start = changes.structure
  }
 
  after<T>(result: T): T {
    if (this.changes.structure !== this.start) {
      throw new ConcurrentModificationError(
        `${this.where}: the ${this.noun} was changed by a key, equals or hash function`,
      )
    }
    return result
  }
}
 
/**
 * Turns `-0` into `0`, as native `Set` and `Map` do, before any user function sees the value.
 *
 * @internal
 */
export function normalise<E>(value: E): E {
  return ((value as unknown) === 0 ? 0 : value) as E
}