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AAM,OAAO,KAAM,IAAI,EAAO,EAAI,EAAO,SACrG,IAAc,IAAV,EAAmB,KAAQ,GAAK,EAAS,EAAI,EAAK,KAAQ,GAAK,EAAO,EAAO,EAClF,EAEG,EAAe,CAAC,EAAY,EAAM,KACtC,MAAM,EAAI,OAAO,GAAK,GACtB,IAAI,EAAK,EACT,IAAK,IAAI,EAAI,EAAI,EAAI,EAAG,GAAK,EAAK,CAChC,IAAO,EACP,IAAI,EAAO,EAAI,EACX,EAAO,IAAK,EAAO,GACvB,GAAM,OAAO,EAAI,QAAU,GAAM,GAAQ,CAC7C,CACE,OAAO,CAAE,S","file":"/npm/@thi.ng/cellular@1.0.47/index.js/+esm","sourceRoot":"","sourcesContent":["/**\n * Bundled by jsDelivr using Rollup v2.79.2 and Terser v5.39.0.\n * Original file: /npm/@thi.ng/cellular@1.0.47/index.js\n *\n * Do NOT use SRI with dynamically generated files! More information: https://www.jsdelivr.com/using-sri-with-dynamic-files\n */\n","import { rotateTyped } from \"@thi.ng/arrays/rotate\";\nimport { isBigInt } from \"@thi.ng/checks/is-bigint\";\nimport { assert } from \"@thi.ng/errors/assert\";\nimport { SYSTEM } from \"@thi.ng/random/system\";\nimport { repeat } from \"@thi.ng/transducers\";\nimport { map } from \"@thi.ng/transducers/map\";\nimport { mapcat } from \"@thi.ng/transducers/mapcat\";\nimport { max } from \"@thi.ng/transducers/max\";\nimport { pluck } from \"@thi.ng/transducers/pluck\";\nimport { repeatedly } from \"@thi.ng/transducers/repeatedly\";\nimport { transduce } from \"@thi.ng/transducers/transduce\";\nconst $0 = BigInt(0);\nconst $1 = BigInt(1);\nconst $32 = BigInt(32);\nconst WOLFRAM3 = [\n  [-1, 0],\n  [0, 0],\n  [1, 0]\n];\nconst WOLFRAM5 = [[-2, 0], ...WOLFRAM3, [2, 0]];\nconst WOLFRAM7 = [[-3, 0], ...WOLFRAM5, [3, 0]];\nclass MultiCA1D {\n  constructor(configs, width, wrap = true) {\n    this.width = width;\n    this.wrap = wrap;\n    this.configs = configs.map(__compileSpec);\n    this.rows = transduce(\n      mapcat((c) => map((k) => k[1], c.kernel)),\n      max(),\n      configs\n    ) + 1;\n    this.numStates = transduce(pluck(\"states\"), max(), configs);\n    assert(\n      this.numStates >= 2 && this.numStates <= 256,\n      \"num states must be in [2..256] range\"\n    );\n    this.resize(width);\n  }\n  configs;\n  rows;\n  numStates;\n  mask;\n  gens;\n  prob;\n  get current() {\n    return this.gens[1];\n  }\n  get previous() {\n    return this.gens[2 % this.gens.length];\n  }\n  clear() {\n    this.gens.forEach((g) => g.fill(0));\n    this.mask.fill(0);\n    this.prob.fill(1);\n  }\n  clearTarget(target) {\n    this._getTarget(target)[0].fill(target === \"prob\" ? 1 : 0);\n  }\n  resize(width) {\n    this.width = width;\n    this.mask = new Uint8Array(width);\n    this.gens = [...repeatedly(() => new Uint8Array(width), this.rows + 1)];\n    this.prob = new Float32Array(width).fill(1);\n  }\n  /**\n   * Sets a parametric pattern in the current generation or mask array.\n   *\n   * @param target - target buffer ID to apply pattern\n   * @param width - number of consecutive cells per segment\n   * @param stride -  number of cells between each pattern segment\n   * @param val - start cell value per segment\n   * @param inc - cell value increment\n   * @param offset - start cell offset\n   */\n  setPattern(target, width, stride, val = 1, inc = 0, offset = 0) {\n    const [dest, num] = this._getTarget(target);\n    for (let x = offset, w = this.width; x < w; x += stride) {\n      for (let k = 0, v = val; k < width; k++, v += inc) {\n        dest[x + k] = v % num;\n      }\n    }\n    return this;\n  }\n  /**\n   * Sets cells in current generation array to a random state using given\n   * `probability` and optional PRNG\n   * ([`IRandom`](https://docs.thi.ng/umbrella/random/interfaces/IRandom.html)\n   * instance).\n   *\n   * @param target\n   * @param prob\n   * @param rnd\n   */\n  setNoise(target, prob = 0.5, rnd = SYSTEM) {\n    const [dest, num] = this._getTarget(target);\n    const fn = target === \"prob\" ? () => rnd.float() : () => rnd.int() % num;\n    for (let x = 0, width = this.width; x < width; x++) {\n      if (rnd.probability(prob)) dest[x] = fn();\n    }\n    return this;\n  }\n  /**\n   * Computes a single new generation using current cell states and mask only\n   * (no consideration for cell update probabilities, use\n   * {@link MultiCA1D.updateProbabilistic} for that instead). Als see\n   * {@link MultiCA1D.updateImage} for batch updates.\n   */\n  update() {\n    const { width, gens, configs, mask } = this;\n    const [next, curr] = gens;\n    for (let x = 0; x < width; x++) {\n      next[x] = this.computeCell(configs[mask[x]], x, curr[x]);\n    }\n    gens.unshift(gens.pop());\n  }\n  /**\n   * Same as {@link MultiCA1D.update}, but also considering cell update\n   * probabilities stored in the {@link MultiCA1D.prob} array.\n   *\n   * @param rnd\n   */\n  updateProbabilistic(rnd = SYSTEM) {\n    const { width, prob, gens, configs, mask } = this;\n    const [next, curr] = gens;\n    for (let x = 0; x < width; x++) {\n      next[x] = rnd.probability(prob[x]) ? this.computeCell(configs[mask[x]], x, curr[x]) : curr[x];\n    }\n    gens.unshift(gens.pop());\n  }\n  /**\n   * Computes (but doesn't apply) the new state for a single cell.\n   *\n   * @param config - CA configuration\n   * @param x - cell index\n   * @param val - current cell value\n   */\n  computeCell({ rule, kernel, weights, fn }, x, val) {\n    const { width, gens, wrap } = this;\n    let sum = $0;\n    for (let i = 0, n = kernel.length; i < n; i++) {\n      const k = kernel[i];\n      let xx = x + k[0];\n      if (wrap) {\n        if (xx < 0) xx += width;\n        else if (xx >= width) xx -= width;\n      } else if (xx < 0 || xx >= width) continue;\n      const y = k[1];\n      if (y >= 0 && gens[1 + y][xx] !== 0) sum += weights[i];\n    }\n    return rule & $1 << sum ? fn(val) : 0;\n  }\n  /**\n   * Batch version of {@link MultiCA1D.update} to compute an entire image of\n   * given `height` (and assumed to be the same width as this CA instance has\n   * been configured to). Fills given `pixels` array with consecutive\n   * generations.\n   *\n   * @remarks\n   * Via the provided options object, per-generation & per-cell perturbance\n   * settings can be provided for cell states, mask and cell update\n   * probabilities. The latter are only considered if the\n   * {@link UpdateImageOpts1D.probabilistic} option is enabled. This can be\n   * helpful to sporadically introduce noise into the sim, break constant\n   * patterns and/or produce more varied/complex outputs.\n   *\n   * See {@link UpdateImageOpts1D} for further options.\n   *\n   * @param pixels\n   * @param height\n   * @param opts\n   */\n  updateImage(pixels, height, opts = {}) {\n    assert(\n      pixels.length >= this.width * height,\n      \"target pixel buffer too small\"\n    );\n    const {\n      probabilistic = false,\n      rnd = SYSTEM,\n      cells,\n      mask,\n      prob,\n      onupdate\n    } = opts;\n    const $ = (id, conf) => {\n      conf && conf.perturb && rnd.probability(conf.perturb) && this.setNoise(id, conf.density || 0.05, rnd);\n    };\n    for (let y = 0; y < height; y++) {\n      $(\"cells\", cells);\n      $(\"mask\", mask);\n      $(\"prob\", prob);\n      probabilistic ? this.updateProbabilistic(rnd) : this.update();\n      onupdate?.(this, y);\n      pixels.set(this.current, y * this.width);\n    }\n  }\n  rotate(target, dir) {\n    if (target === \"all\") {\n      rotateTyped(this.current, dir);\n      rotateTyped(this.mask, dir);\n      rotateTyped(this.prob, dir);\n    } else {\n      rotateTyped(this._getTarget(target)[0], dir);\n    }\n  }\n  _getTarget(target) {\n    return target === \"cells\" ? [this.current, this.numStates] : target === \"mask\" ? [this.mask, this.configs.length] : [this.prob, 1];\n  }\n}\nconst __compileSpec = ({\n  rule,\n  kernel,\n  positional,\n  states,\n  reset\n}) => {\n  const max2 = states - 1;\n  return {\n    kernel,\n    states,\n    rule: isBigInt(rule) ? rule : BigInt(rule),\n    weights: positional !== false ? kernel.map((_, i) => BigInt(2) ** BigInt(i)) : [...repeat($1, kernel.length)],\n    fn: reset !== false ? (y) => ++y >= states ? 0 : y : (y) => ++y >= max2 ? max2 : y\n  };\n};\nconst randomRule1D = (kernelSize, rnd = SYSTEM) => {\n  const n = BigInt(2 ** kernelSize);\n  let id = $0;\n  for (let i = $0; i < n; i += $32) {\n    id <<= $32;\n    let mask = n - i;\n    if (mask > $32) mask = $32;\n    id |= BigInt(rnd.int()) & ($1 << mask) - $1;\n  }\n  return id;\n};\nexport {\n  MultiCA1D,\n  WOLFRAM3,\n  WOLFRAM5,\n  WOLFRAM7,\n  randomRule1D\n};\n"]}