All files / utils/algorithms iterable-weak-struct.js

91.15% Statements 237/260
86.2% Branches 25/29
69.56% Functions 16/23
91.15% Lines 237/260

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 2611x 1x 1x 1x 1x 1x 1x 1x 2x 2x 1x 1x 1x     1x 1x 1x 1x 1x 1x     1x 1x     1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x     1x 1x 1x 1x 1x 1x 67x 67x 1x 1x 1x 1x 1x 1x     1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x       2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x     1x 1x 1x 1x 1x 1x 1x 1x 1x 3x 3x 1x 1x 1x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x     1x 1x 1x 1x 1x 1x 55x 55x 1x 1x 1x 1x 1x 1x 1x 55x 55x 1x 1x 1x 1x 1x 1x 1x     1x 1x 1x 1x 1x 1x 12x 12x     12x 12x 12x 12x 12x 1x 1x 1x 1x 1x 1x 55x 55x 1x 1x 1x 1x 1x 1x     1x 1x 1x 1x 1x 1x 1x 1x 1x 56x 56x 56x 56x 438x 438x 4x 4x 4x 434x 434x 56x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 4x 4x 4x 4x 4x 4x 2x 2x 2x 2x 2x 2x 2x 2x 141x 141x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x  
/**
 * @class
 * @template {object} 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} amount of entries in the map */
  get size () {
    return dataOf(this).keySet.size
  }
 
  /**
   * Deletes the entry with key `key` from the iterable weak map, does nothing if not found
   * @param {K} key - target key
   */
  delete (key) {
    deleteKey(key, this)
  }
 
  [Symbol.iterator] () {
    return this.entries()
  }
 
  /**
   * @yields {[K, V]} - map entry
   * @returns {Generator<[K, V], void>} - generator object of map entries
   */
  * entries () {
    const { refWeakMap } = dataOf(this)
    for (const key of this.keys()) {
      const entry = refWeakMap.get(key)
      if (entry) {
        yield [key, entry.value]
      }
    }
  }
 
  /**
   * @param {(value : V, key : K, map : this) => void} callback - forEach callback
   * @param {*} [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} true if map contains `key`
   */
  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 }
  }
}
 
/**
 * @class
 * @template {object} 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} amount of entries in the set */
  get size () {
    return dataOf(this).keySet.size
  }
 
  /**
   * Deletes a value from the iterable weak set, does nothing if not found
   * @param {V} value - target value
   */
  delete (value) {
    deleteKey(value, this)
  }
 
  [Symbol.iterator] () {
    return this.entries()
  }
 
  /**
   * @yields {V} - map entry value
   * @returns {Generator<V, void>} - generator object of map values
   */
  * entries () {
    for (const value of this.keys()) { yield value }
  }
 
  /**
   *
   * @param {(value : V, set : this) => void} callback - forEach callback
   * @param {*} [thisArg] - value of `this` variable in `callback`, optional
   */
  forEach (callback, thisArg) {
    for (const value of this.entries()) { callback.call(thisArg, value, this) }
  }
 
  /**
   *
   * @param {V} value - value
   * @returns {boolean} true if map contains `value`
   */
  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 map values
   */
  * keys () {
    yield * iterateKeys(this)
  }
 
  /**
   * @yields {V} - map entry value
   * @returns {Generator<V, void>} - generator object of map values
   */
  * values () {
    yield * iterateKeys(this)
  }
}
 
/**
 * @template {object} 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 {object} key - target key
 * @param {IterableWeakSet<object> | IterableWeakMap<object, *>} 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
}
 
const dataOf = (() => {
  /**
   * @type {WeakMap<*, IterableWeakMapData<object,*>>} valueMap
   */
  const map = new WeakMap()
 
  /**
   * @param {IterableWeakSet<object> | IterableWeakMap<object, *>} iter - target weak struct to initialize
   * @returns {IterableWeakMapData<*, *>} target weak struct data
   */
  function init (iter) {
    const keySet = new Set()
    const refWeakMap = new WeakMap()
    const result = { keySet, refWeakMap }
    map.set(iter, result)
    return result
  }
 
  /**
   * @template {object} K
   * @template V
   * @param {IterableWeakSet<K> | IterableWeakMap<K, V>} iter - target weak struct
   * @returns {IterableWeakMapData<K, V>} target weak struct data
   */
  function getData (iter) {
    return map.get(iter) ?? init(iter)
  }
  return getData
})()
 
export default IterableWeakMap
 
/**
 * @template {object} K
 * @template V
 * @typedef {object} IterableWeakMapData
 * @property {WeakMap<K, {ref: WeakRef<K> , value: V}>} refWeakMap - weakmap of weak refs and values
 * @property {Set<WeakRef<K>>} keySet - iterable set of weak keys
 */