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 223 224 | 94x 94x 94x 4762x 94x 94x 200x 3979x 94x 983x 94x 94x 1x 1x 94x 20020x 5752x 5752x 5752x 5752x 13720x 13720x 9697x 9697x 13720x 5752x 2985x 2985x 1826x 2985x 2327x 94x 94x 94x 4768x 4768x 5548x 4768x 4768x 4768x 4768x 4768x 94x 94x 984x 984x | /** * @module Modules/XMap */ import * as MC from "@lisn/globals/minification-constants"; import * as MH from "@lisn/globals/minification-helpers"; import { MapBase } from "@lisn/globals/types"; export type DefaultValueGetter<K, V> = (key: K) => V; export type IteratorCallback<K, V> = ( value: V, key: K, map: XMap<K, V>, ) => void; /** * For minification optimization * * @ignore * @internal */ export const newXMap = <K, V>(getDefaultV: DefaultValueGetter<K, V>) => new XMap(getDefaultV); /** * For minification optimization. Exposed through {@link XMap.newXMapGetter}. * * @ignore * @internal */ export const newXMapGetter = <K, V>(getDefaultV: DefaultValueGetter<K, V>) => () => newXMap(getDefaultV); /** * For minification optimization * * @ignore * @internal */ export const newXWeakMap = <K extends WeakKey, V>( getDefaultV: DefaultValueGetter<K, V>, ) => new XWeakMap(getDefaultV); /** * For minification optimization. Exposed through {@link XMap.newXWeakMapGetter}. * * @ignore * @internal */ export const newXWeakMapGetter = <K extends WeakKey, V>(getDefaultV: DefaultValueGetter<K, V>) => () => newXWeakMap(getDefaultV); export abstract class XMapBase<K, V> { /** * Returns the value at the given key in the {@link XMap} or {@link XWeakMap}. */ readonly get: (key: K) => V | undefined; /** * Like {@link get} except that if the key is not found in the map, then it * will set and return a default value by calling `getDefaultV` passed to the * constructor. */ readonly sGet: (key: K) => V; /** * Sets a value at the given key in the {@link XMap} or {@link XWeakMap}. */ readonly set: (key: K, value: V) => void; /** * Deletes a value at the given key in the {@link XMap} or {@link XWeakMap}. */ readonly delete: (key: K) => void; /** * Deletes empty keys in the {@link XMap} or {@link XWeakMap} starting at the * final nested path and checking the level above after deletion. * * A key is considered empty if it's value is undefined or it's an empty Map, * Set, Array, etc (anything with size or length property equal to 0). */ readonly prune: (sk: K, ...rest: unknown[]) => void; /** * Returns true if the {@link XMap} or {@link XWeakMap} contains the given key. */ readonly has: (key: K) => boolean; protected constructor( root: MapBase<K, V>, getDefaultV: DefaultValueGetter<K, V>, ) { this.get = (key) => root.get(key); this.set = (key, value) => root.set(key, value); this.delete = (key) => MH.deleteKey(root, key); this.has = (key) => root.has(key); this.sGet = (key) => { let result = root.get(key); if (result === undefined) { result = getDefaultV(key); root.set(key, result); } return result; }; this.prune = (sk, ...rest) => { const value = root.get(sk); if (value instanceof XMapBase && MH.lengthOf(rest)) { value.prune(rest[0], ...rest.slice(1)); } if ( value === undefined || (MH.isIterableObject(value) && !( ("size" in value && value.size) || ("length" in value && value.length) )) ) { MH.deleteKey(root, sk); } }; } } /** * {@link XMap} is like * {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map | Map}, * except that it supports automatically creating missing entries with * {@link sGet} according to a default value getter function. * * @typeParam K The type of the keys the map holds. * @typeParam V The type of the values the map holds. */ export class XMap<K, V> extends XMapBase<K, V> implements Iterable<[K, V]> { /** * Returns the number of entries in the {@link XMap}. */ readonly size!: number; /** * Deletes all entries in the {@link XMap}. */ readonly clear: () => void; /** * Returns an iterator over the {@link XMap} entries. */ readonly entries: () => MapIterator<[K, V]>; /** * Returns an iterator over the {@link XMap} keys. */ readonly keys: () => MapIterator<K>; /** * Returns an iterator over the {@link XMap} values. */ readonly values: () => MapIterator<V>; readonly [Symbol.iterator]!: () => IterableIterator<[K, V]>; /** * Returns a function that when called returns a new {@link XMap}. * * You can pass this to the constructor of an {@link XMap} or an * {@link XWeakMap}, whose values are {@link XMap}s. */ static readonly newXMapGetter = newXMapGetter; /** * @param getDefaultV This function is called each time {@link sGet} is * called with a non-existent key and must return a value * that is then set for that key and returned. */ constructor(getDefaultV: DefaultValueGetter<K, V>) { const root = MH.newMap<K, V>(); super(root, getDefaultV); MH.defineProperty(this, "size", { get: () => root.size }); this.clear = () => root.clear(); this.entries = () => root.entries(); this.keys = () => root.keys(); this.values = () => root.values(); this[MC.SYMBOL.iterator] = () => root[MC.SYMBOL.iterator](); } } /** * {@link XWeakMap} is like * {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakMap | WeakMap}, * except that it supports automatically creating missing entries with * with {@link sGet} according to a default value getter function. * * @typeParam K The type of the keys the map holds. * @typeParam V The type of the values the map holds. */ export class XWeakMap<K extends WeakKey, V> extends XMapBase<K, V> { /** * Returns a function that when called returns a new {@link XWeakMap}. * * You can pass this to the constructor of an {@link XMap} or an * {@link XWeakMap}, whose values are {@link XWeakMap}s. */ static readonly newXWeakMapGetter = newXWeakMapGetter; /** * @param getDefaultV This function is called each time {@link sGet} is * called with a non-existent key and must return a value * that is then set for that key and returned. */ constructor(getDefaultV: DefaultValueGetter<K, V>) { const root = MH.newWeakMap<K, V>(); super(root, getDefaultV); } } |