diff --git a/.github/labeler.yml b/.github/labeler.yml index 68b6a004..4b0b93a9 100644 --- a/.github/labeler.yml +++ b/.github/labeler.yml @@ -1,8 +1,3 @@ -mod:algorithm: - - changed-files: - - any-glob-to-any-file: - - 'src/algorithms/**' - mod:animation: - changed-files: - any-glob-to-any-file: diff --git a/package-lock.json b/package-lock.json index 10c8be5e..038591d5 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,7 +9,7 @@ "version": "0.3.0", "license": "SEE LICENSE in LICENSE.txt", "dependencies": { - "vifaa": "^0.2.0" + "vifaa": "^0.3.0" }, "devDependencies": { "@rollup/plugin-terser": "^1.0.0", @@ -4603,9 +4603,9 @@ } }, "node_modules/vifaa": { - "version": "0.2.0", - "resolved": "https://registry.npmjs.org/vifaa/-/vifaa-0.2.0.tgz", - "integrity": "sha512-1GsQfsNZiSuqo7xOkRHjTDFjLZ90BEDrjPPEH1dw/J03u43HaUAxdb1tfjMYSkzg2acyuzl+pxWAMrsBlNP28g==", + "version": "0.3.0", + "resolved": "https://registry.npmjs.org/vifaa/-/vifaa-0.3.0.tgz", + "integrity": "sha512-Q9x0RWc7LjP1TUTrap99AlbPA/TUy0cxFrfT4MVxLCyB8PJWKENQH4m6ErpYYoaz/oHDvMQVwp4cYVhss4vJ5w==", "license": "MIT" }, "node_modules/vite": { diff --git a/package.json b/package.json index bf8fffb6..a48473b6 100644 --- a/package.json +++ b/package.json @@ -72,6 +72,6 @@ "dist/*" ], "dependencies": { - "vifaa": "^0.2.0" + "vifaa": "^0.3.0" } } diff --git a/src/algorithms/index.js b/src/algorithms/index.js deleted file mode 100644 index b0d0db3b..00000000 --- a/src/algorithms/index.js +++ /dev/null @@ -1,2 +0,0 @@ -export * from './sort/index.js' -export * from './packnumber.js' diff --git a/src/algorithms/packnumber.js b/src/algorithms/packnumber.js deleted file mode 100644 index 6abd209c..00000000 --- a/src/algorithms/packnumber.js +++ /dev/null @@ -1,42 +0,0 @@ -/** - * Packs two numbers into a single number. - * - * #### Note - * This operation is should be analogous to packing two 32 bit - * numbers into a 64 bit number in the high and low bits.Due - * to language limitations, the high bits of this implementation - * can only be up to 2 ^ 20, otherwise the output will be garbled - * nonsense. - * - * @param {number} low - * @param {number} high - * @returns - */ -export function packInto64Int(low, high) { - - // We cant use bit manipulation as javascript has a 32 bit limit on - // manipulating "integers" - return (high * 2 ** 32) + low -} - -/** - * Unpacks two numbers from a single number. - * - * #### Note - * This operation is should be analogous to unpacking two 32 bit - * numbers from 64 bit number in the high and low bits.Due - * to language limitations, the number passed into this function - * can only be up to 2 ^ 53, otherwise the output will be garbled - * nonsense. - * - * @param {number} value - */ -export function unpackFrom64Int(value) { - - // We cant use bit manipulation as javascript has a 32 bit limit on - // manipulating "integers" - const low = value % 2 ** 32 - const high = Math.floor(value / 2 ** 32) - - return [low, high] -} diff --git a/src/algorithms/sort/index.js b/src/algorithms/sort/index.js deleted file mode 100644 index d71c5b00..00000000 --- a/src/algorithms/sort/index.js +++ /dev/null @@ -1 +0,0 @@ -export * from './quick.js' diff --git a/src/algorithms/sort/quick.js b/src/algorithms/sort/quick.js deleted file mode 100644 index 6fbb801b..00000000 --- a/src/algorithms/sort/quick.js +++ /dev/null @@ -1,57 +0,0 @@ -/** - * @template T - * @param {T[]} arr - The array to sort. - * @param {CompareFunc} [compareFunc] - Makes comparisons on the elements on the array.Default is to return true if the lhs is less than rhs. - * @param {SwapFunc} [swapFunc] - Function to swap the elements in the array. - * @param {number} [min=0] - * @param {number} [max=arr.length] - */ -export function quickSort(arr, compareFunc = defaultCompare, swapFunc = defaultSwap, min = 0, max = arr.length - 1) { - if (min < max) { - let i = min - 1 - - for (let j = min; j < max; j++) { - if (compareFunc(j, max)) { - i += 1 - swapFunc(arr, i, j) - } - } - - swapFunc(arr, i + 1, max) - quickSort(arr, compareFunc, swapFunc, min, i) - quickSort(arr, compareFunc, swapFunc, i + 2, max) - } -} - -/** - * @type {CompareFunc} - */ -function defaultCompare(i, j) { - return i < j -} - -/** - * @type {SwapFunc} - */ -function defaultSwap(arr, i, j) { - const temp = arr[i] - - arr[i] = arr[j] - arr[j] = temp -} - -/** - * @callback CompareFunc - * @param {number} i - * @param {number} j - * @returns {boolean} - */ - -/** - * @template T - * @callback SwapFunc - * @param {T[]} arr - * @param {number} i - * @param {number} j - * @returns {void} - */ diff --git a/src/algorithms/tests/packNumbers.test.js b/src/algorithms/tests/packNumbers.test.js deleted file mode 100644 index 10c4ad20..00000000 --- a/src/algorithms/tests/packNumbers.test.js +++ /dev/null @@ -1,46 +0,0 @@ -import { test, describe } from "node:test"; -import { deepStrictEqual, strictEqual } from "node:assert"; -import { packInto64Int, unpackFrom64Int } from "../packnumber.js"; - -describe("Testing pack and unpacking numbers", () => { - test('Testing packing number.', () => { - const num1 = packInto64Int(0, 0) - const num2 = packInto64Int(1, 0) - const num3 = packInto64Int(3, 0) - const num4 = packInto64Int(0, 1) - const num5 = packInto64Int(1, 1) - const num6 = packInto64Int(4, 1) - const num7 = packInto64Int(0, 3) - const num8 = packInto64Int(2, 3) - - strictEqual(num1, 0) - strictEqual(num2, 1) - strictEqual(num3, 3) - strictEqual(num4, 4294967296) - strictEqual(num5, 4294967297) - strictEqual(num6, 4294967300) - strictEqual(num7, 12884901888) - strictEqual(num8, 12884901890) - }) - - test('Testing unpacking number.', () => { - const num1 = unpackFrom64Int(0) - const num2 = unpackFrom64Int(1) - const num3 = unpackFrom64Int(3) - const num4 = unpackFrom64Int(4294967296) - const num5 = unpackFrom64Int(4294967297) - const num6 = unpackFrom64Int(4294967300) - const num7 = unpackFrom64Int(12884901888) - const num8 = unpackFrom64Int(12884901890) - - deepStrictEqual(num1, [0, 0]) - deepStrictEqual(num2, [1, 0]) - deepStrictEqual(num3, [3, 0]) - deepStrictEqual(num4, [0, 1]) - deepStrictEqual(num5, [1, 1]) - deepStrictEqual(num6, [4, 1]) - deepStrictEqual(num7, [0, 3]) - deepStrictEqual(num8, [2, 3]) - - }) -}) \ No newline at end of file diff --git a/src/asset/core/handle.js b/src/asset/core/handle.js index 0144eb32..2f99f4ab 100644 --- a/src/asset/core/handle.js +++ b/src/asset/core/handle.js @@ -2,7 +2,7 @@ /** @import {Assets} from '../resources/assets.js' */ /** @import {Constructor} from '../../type/index.js'*/ -import { packInto64Int } from '../../algorithms/index.js' +import { packInto64Int } from '../../datastructures/index.js' import { typeid } from '../../type/index.js' import { AssetServer } from '../resources/assetserver.js' import { AssetChannel } from './channel.js' diff --git a/src/asset/resources/assets.js b/src/asset/resources/assets.js index 3da732f4..76cb31f5 100644 --- a/src/asset/resources/assets.js +++ b/src/asset/resources/assets.js @@ -1,8 +1,7 @@ /** @import {AssetId} from '../types/index.js' */ /** @import {Constructor} from '../../type/index.js' */ -import { unpackFrom64Int } from '../../algorithms/index.js' -import { DenseList } from '../../datastructures/index.js' +import { unpackFrom64Int, DenseList, IndexAllocator } from '../../datastructures/index.js' import { AssetAdded, AssetDropped, AssetEvent, AssetModified } from '../events/assets.js' import { AssetChannel, AssetChannelMessageType } from '../core/channel.js' import { Handle } from '../core/handle.js' @@ -21,7 +20,7 @@ export class Assets { * @private * @type {DenseList>} */ - assets = new DenseList() + assets = new DenseList(new IndexAllocator()) /** * @private diff --git a/src/audio/resources/audiograph.js b/src/audio/resources/audiograph.js index fb314226..142799d5 100644 --- a/src/audio/resources/audiograph.js +++ b/src/audio/resources/audiograph.js @@ -1,5 +1,5 @@ /** @import {NodeId} from '../../datastructures/index.js' */ -import { GraphList } from '../../datastructures/index.js' +import { Graph } from '../../datastructures/index.js' export class AudioGraph { @@ -10,9 +10,9 @@ export class AudioGraph { context /** - * @type {GraphList} + * @type {Graph} */ - graph = new GraphList() + graph = new Graph(true) /** * @private @@ -105,11 +105,11 @@ export class AudioGraph { } if (value) { - for (const neighbour of this.graph.getNeighbours(id)) { + this.graph.forEachNeighbour(id, (neighbour) => { const node = this.graph.getNodeWeight(neighbour) if (node) value.connect(node) - } + }) } node.weight = value diff --git a/src/audio/systems/types.js b/src/audio/systems/types.js index 5480e34e..56ce27f2 100644 --- a/src/audio/systems/types.js +++ b/src/audio/systems/types.js @@ -2,7 +2,7 @@ import { World } from '../../ecs/index.js' import { EnumInfo, Field, OpaqueInfo, StructInfo } from '../../reflect/core/index.js' import { TypeRegistry } from '../../reflect/resources/index.js' import { setTypeId, typeid, typeidGeneric } from '../../type/index.js' -import { GraphList } from '../../datastructures/index.js' +import { Graph } from '../../datastructures/index.js' import { Handle, HandleSnapshot } from '../../asset/index.js' import { Audio } from '../assets/index.js' import { AudioOscillator, AudioOscillatorType, AudioPlayer, AudioPlayerSnapshot } from '../components/index.js' @@ -48,7 +48,7 @@ export function registerAudioTypes(world) { registry.get(AudioOscillator)?.setMethod(AudioOscillator.copy) registry.get(AudioOscillator)?.setMethod(AudioOscillator.clone) registry.register(AudioGraph, new StructInfo({ - graph: new Field(typeid(GraphList)) + graph: new Field(typeid(Graph)) })) registry.register(AudioCommands, new StructInfo({})) } diff --git a/src/datastructures/bitset.js b/src/datastructures/bitset.js deleted file mode 100644 index effc26cf..00000000 --- a/src/datastructures/bitset.js +++ /dev/null @@ -1,233 +0,0 @@ -import { assert } from '../logger/index.js' - -const indexerror = 'Tried to index into `Bitset()` futher than its size.' -const oplengtherror = '`Bitset`s should be of equal size to apply the operation.' - -// Must match with the bit length of element in the typed array. -const WORD_LENGTH = 32 -const WORD_LOG = Math.log2(WORD_LENGTH) - -export class Bitset { - - /** - * @private - * @type {Uint32Array} - */ - data - - /** - * @private - * @type {number} - */ - size - - /** - * @param {number} [size] - */ - constructor(size = 0) { - this.data = new Uint32Array(Math.ceil(size / WORD_LENGTH)) - this.size = size - } - - /** - * @param {number} index - * @returns {boolean} - */ - get(index) { - return Bitset.get(this, index) - } - - length() { - return this.size - } - - /** - * @param {number} index - */ - set(index) { - Bitset.set(this, index) - } - - /** - * @param {number} index - */ - reset(index) { - Bitset.reset(this, index) - } - - /** - * @param {Bitset} other - * @returns {this} - */ - and(other) { - Bitset.and(this, other, this) - - return this - } - - /** - * @param {Bitset} other - * @returns {this} - */ - or(other) { - Bitset.or(this, other, this) - - return this - } - - /** - * @param {Bitset} other - * @returns {this} - */ - xor(other) { - Bitset.xor(this, other, this) - - return this - } - - /** - * @returns {this} - */ - not() { - Bitset.not(this, this) - - return this - } - clear() { - Bitset.clear(this) - } - - /** - * @param {number} size - */ - resize(size) { - Bitset.resize(this, size) - } - - /** - * @param {Bitset} bitset - * @param {number} index - * @returns {boolean} - */ - static get(bitset, index) { - assert(index < bitset.size, indexerror) - - const indexer = index >>> WORD_LOG - const mask = 1 << index - - return (bitset.data[indexer] & mask) !== 0 - } - - /** - * @param {Bitset} bitset - * @param {number} index - */ - static set(bitset, index) { - assert(index < bitset.size, indexerror) - - const indexer = index >>> WORD_LOG - const mask = 1 << index - - bitset.data[indexer] |= mask - } - - /** - * @param {Bitset} bitset - * @param {number} index - */ - static reset(bitset, index) { - assert(index < bitset.size, indexerror) - - const indexer = index >>> WORD_LOG - const mask = 1 << index - - bitset.data[indexer] &= ~mask - } - - /** - * @param {Bitset} bitset - */ - static clear(bitset) { - for (let i = 0; i < bitset.data.length; i++) { - bitset.data[i] = 0 - } - } - - /** - * @param {Bitset} bitset - * @param {number} size - */ - static resize(bitset, size) { - const length = Math.ceil(size / WORD_LENGTH) - - if (length < bitset.data.length) return - - const data = new Uint32Array(length) - - data.set(bitset.data) - - bitset.data = data - bitset.size = size - } - - /** - * @param {Bitset} bitset1 - * @param {Bitset} bitset2 - * @param {Bitset} out - * @returns {Bitset} - */ - static and(bitset1, bitset2, out = new Bitset(bitset1.size)) { - assert(bitset1.size === bitset2.size, `${oplengtherror}\`Bitset.and()\``) - - for (let i = 0; i < bitset1.size; i++) { - out.data[i] = bitset1.data[i] & bitset2.data[i] - } - - return out - } - - /** - * @param {Bitset} bitset1 - * @param {Bitset} bitset2 - * @param {Bitset} out - * @returns {Bitset} - */ - static or(bitset1, bitset2, out = new Bitset(bitset1.size)) { - assert(bitset1.size === bitset2.size, `${oplengtherror}\`Bitset.or()\``) - - for (let i = 0; i < bitset1.size; i++) { - out.data[i] = bitset1.data[i] | bitset2.data[i] - } - - return out - } - - /** - * @param {Bitset} bitset1 - * @param {Bitset} bitset2 - * @param {Bitset} out - * @returns {Bitset} - */ - static xor(bitset1, bitset2, out = new Bitset(bitset1.size)) { - assert(bitset1.size === bitset2.size, `${oplengtherror}\`Bitset.xor()\``) - - for (let i = 0; i < bitset1.size; i++) { - out.data[i] = bitset1.data[i] ^ bitset2.data[i] - } - - return out - } - - /** - * @param {Bitset} bitset - * @param {Bitset} out - * @returns {Bitset} - */ - static not(bitset, out = new Bitset(bitset.size)) { - for (let i = 0; i < bitset.size; i++) { - out.data[i] = ~bitset.data[i] - } - - return out - } -} diff --git a/src/datastructures/core/index.js b/src/datastructures/core/index.js new file mode 100644 index 00000000..4a860d1a --- /dev/null +++ b/src/datastructures/core/index.js @@ -0,0 +1 @@ +export * from 'vifaa' diff --git a/src/datastructures/denselist.js b/src/datastructures/denselist.js deleted file mode 100644 index d5dcec17..00000000 --- a/src/datastructures/denselist.js +++ /dev/null @@ -1,71 +0,0 @@ -import { assert } from '../logger/index.js' -import { IndexAllocator } from './indexallocator.js' - -/** - * @template T - * @template {number} [I = number] - */ -export class DenseList { - - /** - * @private - * @type {T[]} - */ - list = [] - - /** - * @private - * @type {IndexAllocator} - */ - allocator = new IndexAllocator() - - /** - * @param {T} object - * @returns {I} - */ - push(object) { - const index = this.allocator.reserve() - - this.list[index] = object - - return index - } - - /** - * @param {I} index - */ - recycle(index) { - this.allocator.recycle(index) - } - - /** - * @param {I} index - * @returns {T | undefined} - */ - get(index) { - return this.list[index] - } - - /** - * @param {I} index - * @param {T} object - */ - set(index, object) { - assert(index <= this.allocator.count(), 'The index provided has never been allocated') - this.list[index] = object - } - - /** - * @returns {I} - */ - reserve() { - return this.allocator.reserve() - } - - /** - * @returns {ReadonlyArray} - */ - values() { - return this.list - } -} diff --git a/src/datastructures/graph.js b/src/datastructures/graph.js deleted file mode 100644 index bfbc0627..00000000 --- a/src/datastructures/graph.js +++ /dev/null @@ -1,148 +0,0 @@ -import { swapRemove } from '../utils/index.js' - -/** - * @template T - */ -class Node { - - /** - * @type { Node[] } - */ - paths = [] - - /** - * @type { T } - */ - value - - /** - * @param {T} obj - */ - constructor(obj) { - this.value = obj - } -} - -/** - * @template T - */ -export class Graph { - - /** - * @private - * @type { Node[] } - */ - nodes = [] - - /** - * @param { T } obj - * @returns {number} The index of the node. - */ - add(obj) { - return this.nodes.push(new Node(obj)) - 1 - } - - /** - * @param {number} index - * @returns {T} - */ - get(index) { - return this.nodes[index].value - } - - /** - * @param {number} index - */ - getNode(index) { - return this.nodes[index] - } - - /** - * TODO - This index thing actually fucks up things,maybe use mappings - * Or nodes should actually be the indices to an array of values, - * connections an array of arrays - also indexed by nodes. - * @param { number } index - The index of the node to remove. - */ - remove(index) { - const temp = this.nodes.pop() - const node = this.nodes[index] - - if (!temp) return - - for (let i = 0; i < node.paths.length; i++) { - - // @ts-ignore - this.disconnect(index, node.paths[i]) - } - - if (index !== this.nodes.length) this.nodes[index] = temp - } - size() { - return this.nodes.length - } - - /** - * @param {Node} node1 - * @param {Node} node2 - */ - existsNode(node1, node2) { - for (let i = 0; i < node1.paths.length; i++) if (node1.paths[i] === node2) return true - - return false - } - - /** - * @param { number } start - Index of the first node. - * @param { number } end - Index of the second node. - */ - exists(start, end) { - const node1 = this.nodes[start] - const node2 = this.nodes[end] - - return this.existsNode(node1, node2) - } - - /** - * @param {number} start - * @param {number} end - */ - connect(start, end) { - const node1 = this.nodes[start] - const node2 = this.nodes[end] - - if (this.existsNode(node1, node2) || start === end) return - - node1.paths.push(node2) - } - - /** - * @param { number } start - Index of the first node. - * @param { number } end - Index of the second node. - */ - biconnect(start, end) { - this.connect(start, end) - this.connect(end, start) - } - - /** - * @param { number } start - Index of the first node. - * @param { number } end - Index of the second node. - */ - disconnect(start, end) { - const node1 = this.nodes[start] - const node2 = this.nodes[end] - - if (!this.existsNode(node1, node2)) return - - swapRemove(node1.paths, node1.paths.indexOf(node2)) - } - - /** - * @param { number } start - Index of the first node. - * @param { number } end - Index of the second node. - */ - bidisconnect(start, end) { - this.disconnect(start, end) - this.disconnect(end, start) - } -} diff --git a/src/datastructures/graphlist.js b/src/datastructures/graphlist.js deleted file mode 100644 index 481b8ca7..00000000 --- a/src/datastructures/graphlist.js +++ /dev/null @@ -1,279 +0,0 @@ -/** - * @template T - */ -export class GraphNode { - - /** - * @type {[EdgeId | undefined, EdgeId | undefined]} - */ - next = [undefined, undefined] - - /** - * @type {T} - */ - weight - - /** - * @param {T} weight - */ - constructor(weight) { - this.weight = weight - } -} - -/** - * @template T - */ -export class GraphEdge { - - /** - * @type {number} - */ - to - - /** - * @type {number} - */ - from - - /** - * @type {[EdgeId | undefined, EdgeId | undefined]} - */ - next = [undefined, undefined] - - /** - * @type {T} - */ - weight - - /** - * @param {number} from - * @param {number} to - * @param {T} weight - */ - constructor(from, to, weight) { - this.from = from - this.to = to - this.next = [undefined, undefined] - this.weight = weight - } -} - -/** - * @template [T = unknown] - * @template [U = unknown] - */ -export class GraphList { - - /** - * @private - * @type {GraphNode[]} - */ - nodes = [] - - /** - * @private - * @type {GraphEdge[]} - */ - edges = [] - - /** - * @readonly - * @type {boolean} - */ - directed - - /** - * @param {boolean} [directed] - */ - constructor(directed) { - this.directed = directed - } - - /** - * @param {T} weight - * @returns {NodeId} - */ - addNode(weight) { - const node = new GraphNode(weight) - const id = this.nodes.length - - this.nodes.push(node) - - return id - } - - /** - * @param {NodeId} from - * @param {NodeId} to - * @param {U} weight - * @returns {EdgeId} - */ - addEdge(from, to, weight) { - const id = this.edges.length - const edge = new GraphEdge(from, to, weight) - - this.edges.push(edge) - const nodeA = this.nodes[from] - const nodeB = this.nodes[to] - - edge.next[0] = nodeA.next[0] - edge.next[1] = nodeB.next[1] - nodeA.next[0] = id - nodeB.next[1] = id - - return id - } - - /** - * @param {NodeId} id - * @returns {GraphNode | undefined} - */ - getNode(id) { - return this.nodes[id] - } - - /** - * @param {EdgeId} id - * @returns {GraphEdge | undefined} - */ - getEdge(id) { - return this.edges[id] - } - - /** - * @param {NodeId} id - */ - getNodeWeight(id) { - const node = this.getNode(id) - - if (!node) return undefined - - return node.weight - } - - /** - * @param {EdgeId} id - */ - getEdgeWeight(id) { - const edge = this.getEdge(id) - - if (!edge) return undefined - - return edge.weight - } - - /** - * @param {NodeId} id - */ - getNeighbours(id) { - return new GraphNeighbourIterator(this, id) - } - - /** - * @param {NodeId} id - */ - getNodeEdges(id) { - return new GraphNodeEdgesIterator(this, id) - } - getEdges() { - return this.edges - } - getNodes() { - return this.nodes - } - - getNodeCount() { - return this.nodes.length - } - - getEdgeCount() { - return this.edges.length - } -} - -/** - * @template T, U - */ -export class GraphNodeEdgesIterator { - - /** - * @private - * @type {GraphList} - */ - graph - - /** - * @private - * @type {NodeId} - */ - nodeid - - /** - * @param {GraphList} graph - * @param {number} nodeid - */ - constructor(graph, nodeid) { - this.graph = graph - this.nodeid = nodeid - } - - * [Symbol.iterator]() { - const node = this.graph.getNode(this.nodeid) - - if (node) { - let edge = this.graph.getEdge(node.next[0]) - - while (edge) { - yield edge - edge = this.graph.getEdge(edge.next[0]) - } - } - } -} - -/** - * @template T, U - */ -export class GraphNeighbourIterator { - - /** - * @private - */ - graph - - /** - * @private - */ - nodeid - - /** - * @param {GraphList} graph - * @param {number} nodeid - */ - constructor(graph, nodeid) { - this.graph = graph - this.nodeid = nodeid - } - - * [Symbol.iterator]() { - const node = this.graph.getNode(this.nodeid) - - if (node) { - let edge = this.graph.getEdge(node.next[0]) - - while (edge) { - yield edge.to - edge = this.graph.getEdge(edge.next[0]) - } - } - } -} - -/** - * @typedef {number} EdgeId - */ - -/** - * @typedef {number} NodeId - */ diff --git a/src/datastructures/index.js b/src/datastructures/index.js index 612a679b..2632019a 100644 --- a/src/datastructures/index.js +++ b/src/datastructures/index.js @@ -1,8 +1 @@ -export * from './pool.js' -export * from './bitset.js' -export * from './view.js' -export * from './graph.js' -export * from './graphlist.js' -export * from './indexallocator.js' -export * from './denselist.js' -export * from './range.js' +export * from './core/index.js' diff --git a/src/datastructures/indexallocator.js b/src/datastructures/indexallocator.js deleted file mode 100644 index da083646..00000000 --- a/src/datastructures/indexallocator.js +++ /dev/null @@ -1,45 +0,0 @@ -/** - * @template {number} [T = number] - */ -export class IndexAllocator { - - /** - * @private - * @type {number} - */ - nextid = 0 - - /** - * @private - * @type {T[]} - */ - recycled = [] - - /** - * @param {T} index - */ - recycle(index) { - this.recycled.push(index) - } - - /** - * @returns {T} - */ - reserve() { - const recycled = this.recycled.pop() - - if (recycled !== undefined) return recycled - - const index = this.nextid - - this.nextid += 1 - - // SAFETY: T is not a concrete type but extends - // number - return /** @type {T}*/(index) - } - - count() { - return (this.nextid - 1) - } -} diff --git a/src/datastructures/pool.js b/src/datastructures/pool.js deleted file mode 100644 index 8269d7b4..00000000 --- a/src/datastructures/pool.js +++ /dev/null @@ -1,88 +0,0 @@ -/** - * @template T - * An extendable object pool for optimizing performance. - */ - -export class Pool { - - /** - * List of objects. - * - * @type {T[]} - */ - pool = [] - - /** - * @readonly - * @type {PoolCreateFunc} - */ - create - - /** - * @param {PoolCreateFunc} create - * @param {number} number - Number of objects to create at the initialization. - */ - constructor(create, number = 100) { - this.create = create - - for (let i = 0; i < number; i++) { - this.pool.push(this.create()) - } - } - - /** - * The number of objects available in the pool. - * - * @type {number} - */ - get size() { - return this.pool.length - } - set size(x) { - const d = this.pool.length - x - - if (d < 0) { - for (let i = d; i < 0; i++) { - this.pool.push(this.create()) - } - - return - } - if (d > 0) { - for (let i = d; i > 0; i--) { - this.pool.pop() - } - - return - } - } - - /** - * Gives an object ownership. - * - * @returns {T} - */ - give() { - const p = this.pool.pop() - - if (p) return p - - return this.create() - } - - /** - * Takes an object's ownership. - * Do not use the taken object and remove all references of it outside otherwise you will get some wierd behaviour. - * - * @param {T} obj - */ - take(obj) { - this.pool.push(obj) - } -} - -/** - * @template T - * @callback PoolCreateFunc - * @returns {T} - */ diff --git a/src/datastructures/range.js b/src/datastructures/range.js deleted file mode 100644 index c76c4892..00000000 --- a/src/datastructures/range.js +++ /dev/null @@ -1,32 +0,0 @@ -import { lerp } from '../math/index.js' - -export class Range { - - /** - * @type {number} - */ - start - - /** - * @type {number} - */ - end - constructor(start = 0, end = 1) { - this.start = start - this.end = end - } - - /** - * @returns {boolean} - */ - valid() { - return this.start <= this.end - } - - /** - * @param {number} t - */ - lerp(t) { - return lerp(this.start, this.end, t) - } -} diff --git a/src/datastructures/view.js b/src/datastructures/view.js deleted file mode 100644 index fa4a10bd..00000000 --- a/src/datastructures/view.js +++ /dev/null @@ -1,37 +0,0 @@ -import { assert } from '../logger/index.js' - -/** - * @template T - */ -export class ArrayView { - - /** - * @readonly - * @type {number} - */ - length = 0 - - /** - * @readonly - * @type {number} - */ - offset = 0 - - /** - * @type {Array} - */ - array - - /** - * @param {Array} arr - * @param {number} offset - * @param {number} length - */ - constructor(arr, offset, length) { - this.array = arr - this.offset = offset - this.length = length - - assert(offset + length < arr.length, 'The `View()`\'s range is beyond the range of the provided array.') - } -} diff --git a/src/ecs/entities/entities.js b/src/ecs/entities/entities.js index cb0f9f9f..e606955e 100644 --- a/src/ecs/entities/entities.js +++ b/src/ecs/entities/entities.js @@ -1,6 +1,10 @@ /** @import { ArchetypeId, TableRow } from '../typedef/index.js' */ -import { DenseList } from '../../datastructures/index.js' +import { DenseList, IndexAllocator } from '../../datastructures/index.js' import { EntityLocation } from './location.js' /** @augments {DenseList} */ -export class Entities extends DenseList {} +export class Entities extends DenseList { + constructor() { + super(new IndexAllocator()) + } +} diff --git a/src/ecs/entities/entity.js b/src/ecs/entities/entity.js index 35dac8a3..5c1a28f5 100644 --- a/src/ecs/entities/entity.js +++ b/src/ecs/entities/entity.js @@ -1,4 +1,4 @@ -import { packInto64Int, unpackFrom64Int } from '../../algorithms/packnumber.js' +import { packInto64Int, unpackFrom64Int } from 'vifaa' export class Entity { diff --git a/src/index.js b/src/index.js index bed32751..83a58066 100644 --- a/src/index.js +++ b/src/index.js @@ -1,4 +1,3 @@ -export * from './algorithms/index.js' export * from './ecs/index.js' export * from './app/index.js' export * from './animation/index.js'