| | |
| | | * @template V |
| | | */ |
| | | class StackedCacheMap { |
| | | /** |
| | | * Initializes the mutable fallback map and the stack of immutable cache |
| | | * layers. |
| | | */ |
| | | constructor() { |
| | | /** @type {Map<K, V>} */ |
| | | this.map = new Map(); |
| | |
| | | } |
| | | |
| | | /** |
| | | * If `immutable` is true, the map can be referenced by the StackedCacheMap |
| | | * and should not be changed afterwards. If the map is mutable, all items |
| | | * are copied into a fallback Map. |
| | | * Adds another cache layer. Immutable maps are retained by reference and |
| | | * reordered so larger layers are checked first, while mutable maps are |
| | | * copied into the fallback map. |
| | | * @param {ReadonlyMap<K, V>} map map to add |
| | | * @param {boolean=} immutable if 'map' is immutable and StackedCacheMap can keep referencing it |
| | | */ |
| | |
| | | } |
| | | |
| | | /** |
| | | * Stores or overrides a value in the mutable fallback map. |
| | | * @param {K} item the key of the element to add |
| | | * @param {V} value the value of the element to add |
| | | * @returns {void} |
| | |
| | | } |
| | | |
| | | /** |
| | | * Rejects deletions because this data structure is optimized for append-only |
| | | * cache layers. |
| | | * @param {K} item the item to delete |
| | | * @returns {void} |
| | | */ |
| | |
| | | } |
| | | |
| | | /** |
| | | * Rejects `has` checks because they would force the same layered lookup work |
| | | * as `get` without returning the cached value. |
| | | * @param {K} item the item to test |
| | | * @returns {boolean} true if the item exists in this set |
| | | */ |
| | |
| | | } |
| | | |
| | | /** |
| | | * Looks up a key by scanning immutable cache layers first and then the |
| | | * mutable fallback map. |
| | | * @param {K} item the key of the element to return |
| | | * @returns {V | undefined} the value of the element |
| | | */ |
| | |
| | | return this.map.get(item); |
| | | } |
| | | |
| | | /** |
| | | * Removes every cache layer and clears the mutable fallback map. |
| | | */ |
| | | clear() { |
| | | this.stack.length = 0; |
| | | this.map.clear(); |
| | | } |
| | | |
| | | /** |
| | | * Returns the total number of entries across the fallback map and all stacked |
| | | * cache layers. |
| | | * @returns {number} size of the map |
| | | */ |
| | | get size() { |
| | |
| | | } |
| | | |
| | | /** |
| | | * Iterates over the fallback map first and then each stacked cache layer. |
| | | * @returns {Iterator<[K, V]>} iterator |
| | | */ |
| | | [Symbol.iterator]() { |