| | |
| | | "use strict"; |
| | | |
| | | /** |
| | | * Strong-key child map used for tuple elements that cannot be stored in a |
| | | * `WeakMap`. |
| | | * @template {EXPECTED_ANY[]} T |
| | | * @template V |
| | | * @typedef {Map<EXPECTED_ANY, WeakTupleMap<T, V>>} M |
| | | */ |
| | | |
| | | /** |
| | | * Weak-key child map used for tuple elements that are objects and can be held |
| | | * without preventing garbage collection. |
| | | * @template {EXPECTED_ANY[]} T |
| | | * @template V |
| | | * @typedef {WeakMap<EXPECTED_OBJECT, WeakTupleMap<T, V>>} W |
| | | */ |
| | | |
| | | /** |
| | | * Reports whether a tuple element can be stored in a `WeakMap`. |
| | | * @param {EXPECTED_ANY} thing thing |
| | | * @returns {boolean} true if is weak |
| | | */ |
| | | const isWeakKey = (thing) => typeof thing === "object" && thing !== null; |
| | | |
| | | /** |
| | | * Extracts the element type from a tuple-like array. |
| | | * @template {unknown[]} T |
| | | * @typedef {T extends ReadonlyArray<infer ElementType> ? ElementType : never} ArrayElement |
| | | */ |
| | | |
| | | /** |
| | | * Stores values by tuple keys while using `WeakMap` for object elements so the |
| | | * cache can release entries when those objects are collected. |
| | | * @template {EXPECTED_ANY[]} K |
| | | * @template V |
| | | */ |
| | | class WeakTupleMap { |
| | | /** |
| | | * Initializes an empty tuple trie node with optional value and child maps. |
| | | */ |
| | | constructor() { |
| | | /** @private */ |
| | | this.f = 0; |
| | |
| | | } |
| | | |
| | | /** |
| | | * Stores a value at the node identified by the provided tuple key. |
| | | * @param {[...K, V]} args tuple |
| | | * @returns {void} |
| | | */ |
| | |
| | | } |
| | | |
| | | /** |
| | | * Checks whether the exact tuple key has a stored value. |
| | | * @param {K} args tuple |
| | | * @returns {boolean} true, if the tuple is in the Set |
| | | */ |
| | |
| | | } |
| | | |
| | | /** |
| | | * Returns the value stored for the exact tuple key, if any. |
| | | * @param {K} args tuple |
| | | * @returns {V | undefined} the value |
| | | */ |
| | |
| | | } |
| | | |
| | | /** |
| | | * Returns an existing value for the tuple or computes, stores, and returns a |
| | | * new one when the tuple is missing. |
| | | * @param {[...K, (...args: K) => V]} args tuple |
| | | * @returns {V} the value |
| | | */ |
| | |
| | | } |
| | | |
| | | /** |
| | | * Removes the value stored for the tuple key without pruning the trie. |
| | | * @param {K} args tuple |
| | | * @returns {void} |
| | | */ |
| | |
| | | } |
| | | |
| | | /** |
| | | * Clears the stored value and all strong and weak child maps from this node. |
| | | * @returns {void} |
| | | */ |
| | | clear() { |
| | |
| | | this.m = undefined; |
| | | } |
| | | |
| | | /** |
| | | * Returns the value stored directly on this trie node. |
| | | * @returns {V | undefined} stored value |
| | | */ |
| | | _getValue() { |
| | | return this.v; |
| | | } |
| | | |
| | | /** |
| | | * Reports whether this trie node currently stores a value. |
| | | * @returns {boolean} true when a value is present |
| | | */ |
| | | _hasValue() { |
| | | return (this.f & 1) === 1; |
| | | } |
| | | |
| | | /** |
| | | * Stores a value directly on this trie node. |
| | | * @param {V} v value |
| | | * @private |
| | | */ |
| | |
| | | this.v = v; |
| | | } |
| | | |
| | | /** |
| | | * Removes the value stored directly on this trie node. |
| | | */ |
| | | _deleteValue() { |
| | | this.f &= 6; |
| | | this.v = undefined; |
| | | } |
| | | |
| | | /** |
| | | * Returns the child node for a tuple element without creating one. |
| | | * @param {ArrayElement<K>} thing thing |
| | | * @returns {WeakTupleMap<K, V> | undefined} thing |
| | | * @private |
| | |
| | | } |
| | | |
| | | /** |
| | | * Returns the child node for a tuple element, creating and storing it when |
| | | * necessary. |
| | | * @private |
| | | * @param {ArrayElement<K>} thing thing |
| | | * @returns {WeakTupleMap<K, V>} value |