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

100% Statements 80/80
100% Branches 21/21
100% Functions 29/29
100% Lines 64/64

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 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222                                105x 105x                 8669x                                           1283059x                                           3103544x         2510069x       867x       46130x 46130x       38158x 38158x       49523x       17337x 17337x 28739x   15822x       403574x       41197x       1404874x             167956x       21315x 21315x 21315x 21315x 22068x                     151343x 151320x 151186x 151186x 151069x 148187x   12x   148175x 148175x 148175x 148175x 1113554x   147627x 147627x 1112792x 1112792x   147626x               8136x   22x 8114x                   8232x 8232x 8232x 8719x 8719x 8719x   8148x         252x 252x 252x 419x   228x       163931x       1000793x 1000793x 1000793x 4408436x   1000793x               40x      
// The skeleton every map extends: the native `Map` reading methods, live iterators, and equality,
// hashing and printing by mappings, whatever the insertion order. A map is not a collection; the
// public class carries the map brand.
 
import type { MapCollection, MapEntry } from '../../interfaces/map'
import { isBranded, MAP_COLLECTION_BRAND } from '../brand'
import { requireFunction } from '../checks'
import { createGuard, rememberEqual } from '../cycle'
import { MISSING } from '../engines/map-stores'
import { equals, type HashFunction, hashCodeSymbol, sameValueZero } from '../equality'
import { apply, imul } from '../intrinsics'
import { MappedLiveIterator } from '../iteration'
import { INSPECT, type InspectOptions, inspectMap, mapToString } from '../output'
import { ChangeCounter } from '../revision'
 
// One guard per operation, so a value's own code can compare or hash a map mid-operation.
const equalsGuard = /*#__PURE__*/ createGuard()
const hashGuard = /*#__PURE__*/ createGuard()
 
/**
 * Combines the hash of a key and the hash of its value into the hash of one mapping. Equal
 * mappings (and equal entries) get equal hashes.
 *
 * @internal
 */
export function mappingHash(keyHash: number, valueHash: number): number {
  return (imul(keyHash, 31) + valueHash) | 0
}
 
/**
 * Base class of every map.
 *
 * Subclasses implement `size`, `[Symbol.toStringTag]`, `owner`, `handles` (an iterator over one
 * handle per mapping), `keyAt`, `valueAt`, `find` (the handle of a key, or `MISSING`) and
 * `keyHash` (a stored key's hash under the map's key equality), and record every change through
 * `changes`.
 *
 * @internal
 */
export abstract class AbstractMap<K, V> {
  /**
   * The change counter: `structural()` when mappings are added or removed, `replaced()` when a
   * value is written. Views share it.
   */
  readonly changes: ChangeCounter
 
  /** @param changes - The change counter; a new one by default, the map's own for a map view. */
  constructor(changes: ChangeCounter = new ChangeCounter()) {
    this.changes = changes
  }
 
  abstract get size(): number
  abstract get [Symbol.toStringTag](): string
  /** The public map handed to callbacks: the object the caller holds. */
  abstract get owner(): MapCollection<K, V>
  /**
   * An iterator over one handle per mapping, in the map's order: live for a map that iterates
   * live, fail-fast otherwise, with `where` naming the method in its errors.
   */
  abstract handles(where: string): Iterator<unknown>
  abstract keyAt(handle: unknown): K
  abstract valueAt(handle: unknown): V
  /** Returns the handle of the mapping whose key equals `key`, or `MISSING`; errors name `where`. */
  abstract find(key: K, where: string): unknown
  /** Returns the hash of a stored key under this map's key equality. */
  abstract keyHash(handle: unknown, hash: HashFunction): number
  /** Returns a new live entry for a stored mapping. */
  abstract entryAt(handle: unknown): MapEntry<K, V>
 
  get revision(): number {
    return this.changes.revision
  }
 
  /** `<Class>.<method>`, for messages. */
  protected at(method: string): string {
    return `${this[Symbol.toStringTag]}.${method}`
  }
 
  isEmpty(): boolean {
    return this.size === 0
  }
 
  get(key: K): V | undefined {
    const handle = this.find(key, this.at('get'))
    return handle === MISSING ? undefined : this.valueAt(handle)
  }
 
  getOrDefault<D>(key: K, fallback: D): V | D {
    const handle = this.find(key, this.at('getOrDefault'))
    return handle === MISSING ? fallback : this.valueAt(handle)
  }
 
  has(key: K): boolean {
    return this.find(key, this.at('has')) !== MISSING
  }
 
  containsValue(value: V): boolean {
    const handles = this.handles(this.at('containsValue'))
    for (let step = handles.next(); step.done !== true; step = handles.next()) {
      if (sameValueZero(this.valueAt(step.value), value)) return true
    }
    return false
  }
 
  keys(): IterableIterator<K> {
    return new MappedLiveIterator(this.handles(this.at('keys')), (handle) => this.keyAt(handle))
  }
 
  values(): IterableIterator<V> {
    return new MappedLiveIterator(this.handles(this.at('values')), (handle) => this.valueAt(handle))
  }
 
  entries(): IterableIterator<[K, V]> {
    return new MappedLiveIterator(this.handles(this.at('entries')), (handle): [K, V] => [
      this.keyAt(handle),
      this.valueAt(handle),
    ])
  }
 
  [Symbol.iterator](): IterableIterator<[K, V]> {
    return this.entries()
  }
 
  forEach(action: (value: V, key: K, map: MapCollection<K, V>) => void, thisArg?: unknown): void {
    requireFunction(action, this.at('forEach'), 'action')
    const owner = this.owner
    const handles = this.handles(this.at('forEach'))
    for (let step = handles.next(); step.done !== true; step = handles.next()) {
      apply(action, thisArg, [this.valueAt(step.value), this.keyAt(step.value), owner])
    }
  }
 
  /**
   * Equal when `other` is a map of the same size, every key of this map is in `other` with an
   * equal value, and every key of `other` is in this map with an equal value, so the result is the
   * same both ways round even when the two maps compare keys differently. `other` is asked through
   * its entry view's `has`, which never reorders a map.
   */
  equals(other: unknown): boolean {
    if (other === this || other === this.owner) return true
    if (!isBranded(other, MAP_COLLECTION_BRAND)) return false
    const that = other as MapCollection<K, V>
    if (that.size !== this.size) return false
    return rememberEqual(this.owner, that, () =>
      equalsGuard(
        this,
        () => false,
        () => {
          const where = this.at('equals')
          const handles = this.handles(where)
          const entries = that.entrySet()
          for (let step = handles.next(); step.done !== true; step = handles.next()) {
            if (!entries.has(this.entryAt(step.value))) return false
          }
          const pairs = that[Symbol.iterator]()
          for (let step = pairs.next(); step.done !== true; step = pairs.next()) {
            const handle = this.find(step.value[0], where)
            if (handle === MISSING || !equals(step.value[1], this.valueAt(handle))) return false
          }
          return true
        },
      ),
    )
  }
 
  /** The order-independent 32-bit sum of the mappings' hashes. */
  [hashCodeSymbol](hash: HashFunction): number {
    return hashGuard(
      this,
      () => 0,
      () => this.mappingHashes(hash),
    )
  }
 
  /**
   * The 32-bit sum of the mappings' hashes, unguarded (the entry view's hash is the map's). Each
   * value is read before its key is hashed: hashing runs user code, after which a handle may no
   * longer be read.
   */
  mappingHashes(hash: HashFunction): number {
    let sum = 0
    const handles = this.handles(this.at('[hashCodeSymbol]'))
    for (let step = handles.next(); step.done !== true; step = handles.next()) {
      const handle = step.value
      const value = this.valueAt(handle)
      sum = (sum + mappingHash(this.keyHash(handle, hash), hash(value))) | 0
    }
    return sum
  }
 
  /** The 32-bit sum of the keys' hashes: the hash of the key view. */
  keyHashes(hash: HashFunction): number {
    let sum = 0
    const handles = this.handles(this.at('[hashCodeSymbol]'))
    for (let step = handles.next(); step.done !== true; step = handles.next()) {
      sum = (sum + this.keyHash(step.value, hash)) | 0
    }
    return sum
  }
 
  toString(): string {
    return mapToString(this)
  }
 
  toJSON(): Array<[K, V]> {
    const out: Array<[K, V]> = []
    const handles = this.handles(this.at('toJSON'))
    for (let step = handles.next(); step.done !== true; step = handles.next()) {
      out.push([this.keyAt(step.value), this.valueAt(step.value)])
    }
    return out
  }
 
  [INSPECT](
    depth: number | null,
    options: InspectOptions,
    inspect: (value: unknown, options: InspectOptions) => string,
  ): string {
    return inspectMap(this, depth, options, inspect)
  }
}