All files / src/internal/engines linked-hash-map-engine.ts

100% Statements 25/25
100% Branches 18/18
100% Functions 6/6
100% Lines 22/22

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                                                                                              1032322x       1032322x 632316x 629975x 2353x 627622x 627181x 1032322x                                 1032322x 1032322x 1032322x       1309539x       317135x       73220x       73108x               402347x 402347x 2341x     400006x 400006x 200022x   199984x      
// The engine behind `LinkedHashMap`: a linked hash table walked from the front. The public class
// holds one engine and delegates to it, so nothing here reaches the published types.
 
import type { SequencedMap } from '../../interfaces/sequenced-map'
import { ChangeCounter } from '../revision'
import { AbstractLinkedMap, type LinkedMapState } from '../skeletons/abstract-linked-map'
import { ReversedLinkedHashMap } from '../views/reversed-linked-hash-map'
import { AccessSlots } from './access-slots'
import { EvictionQueue } from './eviction-queue'
import type { HashEquality } from './hash-support'
import { LinkedHashTable } from './linked-hash-table'
import { MISSING } from './map-stores'
 
/**
 * The options of a linked hash map, read and checked once at construction.
 *
 * @internal
 */
export interface LinkedMapSettings<K, V> {
  readonly equality: HashEquality<K>
  readonly accessOrder: boolean
  readonly maxSize: number | undefined
  readonly onEvict: ((value: V, key: K, map: never) => void) | undefined
}
 
/**
 * A map in insertion or access order, changeable at both ends and optionally bounded in size, whose
 * keys are compared with SameValueZero, by derived key, or by an equivalence; iteration is live.
 * With SameValueZero keys and no access order it starts in the native layout.
 *
 * @internal
 */
export class LinkedHashMapEngine<K, V> extends AbstractLinkedMap<K, V> {
  private readonly facade: SequencedMap<K, V>
  private readonly settings: LinkedMapSettings<K, V>
 
  /**
   * @param facade - The public map, for identity, callbacks and `onEvict`.
   * @param settings - The options.
   * @param contents - The table to use, or the native `Map` of a map in the native layout; a new
   *   empty one by default (the slots of an access-ordered map with SameValueZero keys).
   */
  constructor(
    facade: SequencedMap<K, V>,
    settings: LinkedMapSettings<K, V>,
    contents?: LinkedHashTable<K, V> | Map<K, V>,
  ) {
    const { equality, accessOrder, maxSize, onEvict } = settings
    let flat: Map<K, V> | undefined
    let table: LinkedHashTable<K, V> | undefined
    let slots: AccessSlots<K, V> | undefined
    if (contents instanceof LinkedHashTable) table = contents
    else if (contents !== undefined) flat = contents
    else if (equality.kind !== 'native')
      table = new LinkedHashTable<K, V>(equality, undefined, accessOrder)
    else if (accessOrder) slots = new AccessSlots<K, V>()
    else flat = new Map<K, V>()
    const state: LinkedMapState<K, V> = {
      flat,
      table,
      slots,
      front: undefined,
      frontKey: MISSING,
      frontValue: undefined,
      changes: new ChangeCounter(),
      equality,
      accessOrder,
      maxSize,
      onEvict,
      queue:
        maxSize !== undefined && onEvict !== undefined
          ? new EvictionQueue<K, V>(onEvict, facade)
          : undefined,
    }
    super(state, false)
    this.facade = facade
    this.settings = settings
  }
 
  get [Symbol.toStringTag](): string {
    return 'LinkedHashMap'
  }
 
  get owner(): SequencedMap<K, V> {
    return this.facade
  }
 
  opposite(): AbstractLinkedMap<K, V> {
    return new ReversedLinkedHashMap(this)
  }
 
  reversed(): SequencedMap<K, V> {
    return this.opposite().owner
  }
 
  /**
   * A new engine for `facade` with the same options, order and stored keys and hashes; in the
   * native layout whenever the options allow it, whatever this map's layout.
   */
  copy(facade: SequencedMap<K, V>): LinkedHashMapEngine<K, V> {
    const settings = this.settings
    if (settings.equality.kind === 'native' && !settings.accessOrder) {
      return new LinkedHashMapEngine(facade, settings, new Map(this.entries()))
    }
    // A map in the slotted layout is copied into a new table, leaving its own layout as it is.
    const slots = this.state.slots
    if (slots !== undefined) {
      return new LinkedHashMapEngine(facade, settings, slots.toTable(settings.equality))
    }
    return new LinkedHashMapEngine(facade, settings, this.table.copy(settings.equality))
  }
}