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 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 | 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 18x 18x 2x 2x 2x 8x 8x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 5x 5x 5x 5x 5x 2x 2x 2x 2x 2x 1x 1x 2x 2x 2x 2x 2x 2x 2x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 7x 7x 2x 2x 2x 2x 2x 2x 19x 19x 2x 2x 2x 2x 2x 2x 2x 34x 34x 34x 6x 1x 6x 33x 33x 33x 33x 33x 2x 2x 2x 2x 2x 2x 3x 3x 2x 2x 2x 2x 2x 2x 16x 16x 29x 29x 29x 29x 29x 16x 2x 2x 5x 5x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 22x 22x 2x 2x 2x 12x 12x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 2x 2x 2x 2x 2x 2x 2x 1x 1x 2x 2x 2x 2x 2x 2x 11x 11x 2x 2x 2x 2x 2x 2x 40x 40x 14x 14x 39x 39x 39x 39x 39x 2x 2x 2x 2x 2x 2x 3x 3x 2x 2x 2x 2x 2x 2x 3x 3x 2x 2x 2x 2x 2x 2x 17x 17x 2x 2x 9x 9x 2x 2x 2x 2x 2x 2x 2x 2x 2x 42x 42x 42x 42x 78x 78x 4x 4x 4x 74x 74x 42x 2x 2x 2x 2x 2x 2x 2x 4x 4x 4x 4x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 4x 4x 4x 4x 4x 2x 2x 2x 2x 2x 2x 2x 20x 20x 20x 20x 20x 2x 2x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 7x 7x 7x 7x 7x 7x 7x 7x 178x 178x 7x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x | /**
* WeakMap that is iterable with `for..of` and `forEach()` while making the keys referenced
* weakly with the map, meaning the key can be garbage collected if there is no strong
* references from the map
*
* @class
* @template {WeakKey} K
* @template V
*/
export class IterableWeakMap {
/** @param {Iterable<Readonly<[K, V]>>} [iterable] - initial data of the map */
constructor (iterable = []) {
for (const [key, value] of iterable) { this.set(key, value) }
}
/** @returns {number} the number of elements in the IterableWeakMap */
get size () {
return getSize(this)
}
/**
* Removes the specified element from the IterableWeakMap.
* @param {K} key - target key
* @returns {boolean} true if the element was successfully removed, or false if it was not present.
*/
delete (key) {
return deleteKey(key, this)
}
/**
* Returns the value corresponding to the specified key in this WeakMap.
* If the key is not present, it inserts a new entry with the key and a given default value, and returns the inserted value.
* @param {K} key - key object
* @param {V} defaultValue - value to insert if not found
* @returns {V} - entry value of found entry with key or inserted value
*/
getOrInsert (key, defaultValue) {
if (this.has(key)) { return /** @type {V} */ (this.get(key)) }
this.set(key, defaultValue)
return defaultValue
}
/**
* returns the value corresponding to the specified key in this WeakMap. If the key is not present, it inserts a new entry with the key and a default value computed from a given callback, and returns the inserted value
* @param {K} key - key object
* @param {(key: K) => V} callback - A function that returns the value to insert and return if the key is not already present
* @returns {V} - entry value of found entry with key or inserted value
*/
getOrInsertComputed (key, callback) {
if (this.has(key)) { return /** @type {V} */ (this.get(key)) }
const value = callback(key)
this.set(key, value)
return value
}
/**
* Removes all elements from the map.
*/
clear () {
clear(this)
}
/**
* Executes a provided function once per each key/value pair in the IterableWeakMap, in insertion order.
* @param {(value : V, key : K, map : this) => void} callback - forEach callback
* @param { unknown } [thisArg] - value of `this` variable in `callback`, optional
*/
forEach (callback, thisArg) {
for (const [key, value] of this.entries()) { callback.call(thisArg, value, key, this) }
}
/**
* @param {K} key - key object
* @returns {V | undefined} - entry value of found entry with key, undefined if not found
*/
get (key) {
return dataOf(this).refWeakMap.get(key)?.value
}
/**
* @param {K} key - key object
* @returns {boolean} a boolean indicating whether an element with the specified key exists or not
*/
has (key) {
return dataOf(this).refWeakMap.has(key)
}
/**
* @yields {K} - map entry key
* @returns {Generator<K, void>} - generator object of map keys
*/
* keys () {
yield * iterateKeys(this)
}
/**
* @param {K} key - key target
* @param {V} value - value to set or override
* @returns {IterableWeakMap<K, V>} the same instance
*/
set (key, value) {
const { keySet, refWeakMap } = dataOf(this)
const refVal = refWeakMap.get(key)
if (refVal !== undefined) {
refVal.value = value
return this
}
const ref = new WeakRef(key)
refWeakMap.set(key, { ref, value })
keySet.add(ref)
return this
}
/**
* @yields {V} - map entry value
* @returns {Generator<V, void>} - generator object of map values
*/
* values () {
for (const [, value] of this.entries()) { yield value }
}
/**
* @yields {[K, V]} - map entry
* @returns {Generator<[K, V], void>} an iterable of key, value pairs for every entry in the IterableWeakMap, in insertion order
*/
* entries () {
const { refWeakMap } = dataOf(this)
for (const key of this.keys()) {
const entry = refWeakMap.get(key)
if (entry) {
yield [key, entry.value]
}
}
}
[Symbol.iterator] () {
return this.entries()
}
}
/**
* WeakSet that is iterable with `for..of` and `forEach()` while making the values referenced
* weakly with the set, meaning the value can be garbage collected if there is no strong
* references from the value
*
* @class
* @template {WeakKey} V
*/
export class IterableWeakSet {
/** @param {Iterable<V>} [iterable] - initial data of the set */
constructor (iterable = []) {
for (const value of iterable) { this.add(value) }
}
/** @returns {number} the number of (unique) elements in Set */
get size () {
return getSize(this)
}
/**
* Deletes a value from the iterable weak set, does nothing if not found
* @param {V} value - target value
*/
delete (value) {
deleteKey(value, this)
}
/**
* Removes all elements from the set.
*/
clear () {
clear(this)
}
/**
* Executes a provided function once for each value in this set, in insertion order.
* @param {(value : V, key : V, set : this) => void} callback - forEach callback
* @param { unknown } [thisArg] - value of `this` variable in `callback`, optional
*/
forEach (callback, thisArg) {
for (const value of this.entries()) { callback.call(thisArg, value, value, this) }
}
/**
* @param {V} value - value
* @returns {boolean} a boolean indicating whether an element with the specified value exists in the Set or not
*/
has (value) {
return dataOf(this).refWeakMap.has(value)
}
/**
* @param {V} value - value to add
* @returns {IterableWeakSet<V>} the same instance
*/
add (value) {
const { keySet, refWeakMap } = dataOf(this)
if (refWeakMap.has(value)) {
return this
}
const ref = new WeakRef(value)
refWeakMap.set(value, { ref, value: true })
keySet.add(ref)
return this
}
/**
* @yields {V} - map entry value
* @returns {Generator<V, void>} - generator object of set values
*/
* keys () {
yield * iterateKeys(this)
}
/**
* @yields {V} - map entry value
* @returns {Generator<V, void>} - generator object of set values
*/
* values () {
yield * iterateKeys(this)
}
/**
* @yields {V} - map entry value
* @returns {Generator<V, void>} - generator object of set values
*/
* entries () {
yield * iterateKeys(this)
}
[Symbol.iterator] () {
return this.entries()
}
}
/**
* @template {WeakKey} K
* @template V
* Generates an iterable weak struct key iterator
* @param {IterableWeakSet<K> | IterableWeakMap<K, V>} struct - target weak struct
* @yields {K} - weak struck key
*/
function * iterateKeys (struct) {
const { keySet } = dataOf(struct)
const array = Array.from(keySet)
for (const ref of array) {
const deref = ref.deref()
if (!deref) {
keySet.delete(ref)
continue
}
yield deref
}
}
/**
* Deletes a key from an iterable weak struct
* @param {WeakKey} key - target key
* @param {IterableWeakSet<WeakKey> | IterableWeakMap<WeakKey, unknown>} struct - target weak struct
* @returns {boolean} true if deleted, false if not found
*/
function deleteKey (key, struct) {
const { keySet, refWeakMap } = dataOf(struct)
const entry = refWeakMap.get(key)
if (!entry) { return false }
keySet.delete(entry.ref)
refWeakMap.delete(key)
return true
}
/**
* clears an iterable weak struct
* @param {IterableWeakSet<WeakKey> | IterableWeakMap<WeakKey, unknown>} struct - target weak struct
*/
function clear (struct) {
const { keySet, refWeakMap } = dataOf(struct)
const array = Array.from(keySet)
for (const ref of array) {
const deref = ref.deref()
if (deref) {
refWeakMap.delete(deref)
}
}
keySet.clear()
}
/**
* Gets actual size of iterable weak struct
* @param {IterableWeakSet<WeakKey> | IterableWeakMap<WeakKey, unknown>} struct - target weak struct
*/
function getSize (struct) {
const { keySet } = dataOf(struct)
keySet.forEach(ref => { if (!ref.deref()) { keySet.delete(ref) } })
return keySet.size
}
const dataOf = (() => {
/**
* @type {WeakMap<IterableWeakSet<any> | IterableWeakMap<WeakKey, any>, IterableWeakMapData<any, any>>}
*/
const map = new WeakMap()
/**
* @template {WeakKey} K
* @template V
* @param {IterableWeakSet<K> | IterableWeakMap<K, V>} iter - target iterable weak struct to initialize
* @returns {IterableWeakMapData<K, V>} target weak struct data
*/
function init (iter) {
const keySet = new Set()
/** @type {WeakMap<K, ValueRef<K,V>>} */
const refWeakMap = new WeakMap()
const result = {
keySet,
refWeakMap,
}
map.set(iter, result)
return result
}
/**
* @template {WeakKey} K
* @template V
* @param {IterableWeakSet<K> | IterableWeakMap<K, V>} iter - target iterable weak struct
* @returns {IterableWeakMapData<K, V>} target weak struct data
*/
function getData (iter) {
return map.get(iter) ?? init(iter)
}
return getData
})()
/**
* @template {WeakKey} K
* @template V
* @typedef {object} ValueRef
* @property {WeakRef<K>} ref - reference object applied on the map
* @property {V} value - value on the map
*/
/**
* @template {WeakKey} K
* @template V
* @typedef {object} IterableWeakMapData
* @property {WeakMap<K, ValueRef<K,V>>} refWeakMap - weakmap of weak refs and values
* @property {Set<WeakRef<K>>} keySet - iterable set of weak keys
*/
|