{
  "version": 3,
  "sources": ["../../src/lib/ArraySet.ts"],
  "sourcesContent": ["/**\n * The number of items an ArraySet holds in array mode before switching to a Set.\n * Exported only for tests.\n * @internal\n */\nexport const ARRAY_SIZE_THRESHOLD = 8\n\n/**\n * An ArraySet operates as an array until it reaches a certain size, after which a Set is used\n * instead. In either case, the same methods are used to get, set, remove, and visit the items.\n *\n * `set` and `array` are never both non-null. `set` being null means array mode, but the array\n * itself is only allocated on the first `add` (most signals never get a child, and an empty\n * ArraySet is created for every atom and effect, and two for every computed), so array-mode code\n * must handle `array === null`; `arraySize` is 0 in that state. Once promoted to a set, an\n * ArraySet never goes back.\n * @internal\n */\nexport class ArraySet<T> {\n\tprivate set: Set<T> | null = null\n\n\t// Slots [0, arraySize) hold the items; slots beyond are undefined. `add`/`has` scan the whole\n\t// array with indexOf, which is why `clear` and `remove` must blank vacated slots.\n\tprivate array: (T | undefined)[] | null = null\n\n\tprivate arraySize = 0\n\n\t/**\n\t * Get whether this ArraySet has any elements.\n\t */\n\t// eslint-disable-next-line tldraw/no-setter-getter\n\tget isEmpty() {\n\t\tif (this.set) {\n\t\t\treturn this.set.size === 0\n\t\t}\n\n\t\treturn this.arraySize === 0\n\t}\n\n\t/**\n\t * Add an element to the ArraySet if it is not already present.\n\t *\n\t * @returns `true` if the element was added, `false` if it was already present\n\t */\n\tadd(elem: T) {\n\t\tif (this.set) {\n\t\t\tif (this.set.has(elem)) {\n\t\t\t\treturn false\n\t\t\t}\n\n\t\t\tthis.set.add(elem)\n\t\t\treturn true\n\t\t}\n\n\t\tif (!this.array) {\n\t\t\tthis.array = Array(ARRAY_SIZE_THRESHOLD)\n\t\t} else if (this.array.indexOf(elem) !== -1) {\n\t\t\treturn false\n\t\t}\n\n\t\tif (this.arraySize < ARRAY_SIZE_THRESHOLD) {\n\t\t\tthis.array[this.arraySize] = elem\n\t\t\tthis.arraySize++\n\n\t\t\treturn true\n\t\t}\n\n\t\t// The array is full: promote to a set.\n\t\tthis.set = new Set(this.array as T[])\n\t\tthis.set.add(elem)\n\t\tthis.array = null\n\t\tthis.arraySize = 0\n\n\t\treturn true\n\t}\n\n\t/**\n\t * Remove an element from the ArraySet if it is present.\n\t *\n\t * @returns `true` if the element was removed, `false` if it was not present\n\t */\n\tremove(elem: T) {\n\t\tif (this.set) {\n\t\t\treturn this.set.delete(elem)\n\t\t}\n\n\t\tif (!this.array) {\n\t\t\treturn false\n\t\t}\n\n\t\tconst idx = this.array.indexOf(elem)\n\n\t\tif (idx === -1) {\n\t\t\treturn false\n\t\t}\n\n\t\tthis.arraySize--\n\n\t\t// Move the last item into the vacated slot so the items stay dense.\n\t\tthis.array[idx] = this.array[this.arraySize]\n\t\tthis.array[this.arraySize] = undefined\n\n\t\treturn true\n\t}\n\n\t/**\n\t * Execute a callback function for each element in the ArraySet.\n\t */\n\tvisit(visitor: (item: T) => void) {\n\t\tif (this.set) {\n\t\t\tthis.set.forEach(visitor)\n\n\t\t\treturn\n\t\t}\n\n\t\tif (!this.array) {\n\t\t\treturn\n\t\t}\n\n\t\tfor (let i = 0; i < this.arraySize; i++) {\n\t\t\tvisitor(this.array[i]!)\n\t\t}\n\t}\n\n\t/**\n\t * Make the ArraySet iterable, allowing it to be used in for...of loops and with spread syntax.\n\t */\n\t*[Symbol.iterator]() {\n\t\tif (this.set) {\n\t\t\tyield* this.set\n\t\t} else if (this.array) {\n\t\t\tfor (let i = 0; i < this.arraySize; i++) {\n\t\t\t\tyield this.array[i]!\n\t\t\t}\n\t\t}\n\t}\n\n\t/**\n\t * Check whether an element is present in the ArraySet.\n\t */\n\thas(elem: T) {\n\t\tif (this.set) {\n\t\t\treturn this.set.has(elem)\n\t\t}\n\n\t\treturn this.array ? this.array.indexOf(elem) !== -1 : false\n\t}\n\n\t/**\n\t * Remove all elements from the ArraySet.\n\t */\n\tclear() {\n\t\tif (this.set) {\n\t\t\tthis.set.clear()\n\t\t} else if (this.array) {\n\t\t\t// Blank the used slots in place rather than allocating a new array: this runs on every\n\t\t\t// computed derive and effect run.\n\t\t\tthis.array.fill(undefined, 0, this.arraySize)\n\t\t\tthis.arraySize = 0\n\t\t}\n\t}\n\n\t/**\n\t * Get the number of elements in the ArraySet.\n\t */\n\tsize() {\n\t\tif (this.set) {\n\t\t\treturn this.set.size\n\t\t}\n\n\t\treturn this.arraySize\n\t}\n}\n"],
  "mappings": "AAKO,MAAM,uBAAuB;AAa7B,MAAM,SAAY;AAAA,EAChB,MAAqB;AAAA;AAAA;AAAA,EAIrB,QAAkC;AAAA,EAElC,YAAY;AAAA;AAAA;AAAA;AAAA;AAAA,EAMpB,IAAI,UAAU;AACb,QAAI,KAAK,KAAK;AACb,aAAO,KAAK,IAAI,SAAS;AAAA,IAC1B;AAEA,WAAO,KAAK,cAAc;AAAA,EAC3B;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOA,IAAI,MAAS;AACZ,QAAI,KAAK,KAAK;AACb,UAAI,KAAK,IAAI,IAAI,IAAI,GAAG;AACvB,eAAO;AAAA,MACR;AAEA,WAAK,IAAI,IAAI,IAAI;AACjB,aAAO;AAAA,IACR;AAEA,QAAI,CAAC,KAAK,OAAO;AAChB,WAAK,QAAQ,MAAM,oBAAoB;AAAA,IACxC,WAAW,KAAK,MAAM,QAAQ,IAAI,MAAM,IAAI;AAC3C,aAAO;AAAA,IACR;AAEA,QAAI,KAAK,YAAY,sBAAsB;AAC1C,WAAK,MAAM,KAAK,SAAS,IAAI;AAC7B,WAAK;AAEL,aAAO;AAAA,IACR;AAGA,SAAK,MAAM,IAAI,IAAI,KAAK,KAAY;AACpC,SAAK,IAAI,IAAI,IAAI;AACjB,SAAK,QAAQ;AACb,SAAK,YAAY;AAEjB,WAAO;AAAA,EACR;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOA,OAAO,MAAS;AACf,QAAI,KAAK,KAAK;AACb,aAAO,KAAK,IAAI,OAAO,IAAI;AAAA,IAC5B;AAEA,QAAI,CAAC,KAAK,OAAO;AAChB,aAAO;AAAA,IACR;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