import { entityKey } from "./Entity.js";
export class SparseSet {
    denseEntities = [];
    denseValues = [];
    sparse = new Map();
    add(entity, value) {
        const key = entityKey(entity);
        const existing = this.sparse.get(key);
        if (existing !== undefined) {
            this.denseValues[existing] = value;
            return;
        }
        this.sparse.set(key, this.denseEntities.length);
        this.denseEntities.push(entity);
        this.denseValues.push(value);
    }
    get(entity) {
        const index = this.sparse.get(entityKey(entity));
        return index === undefined ? undefined : this.denseValues[index];
    }
    has(entity) {
        return this.sparse.has(entityKey(entity));
    }
    delete(entity) {
        const key = entityKey(entity);
        const index = this.sparse.get(key);
        if (index === undefined)
            return false;
        const lastIndex = this.denseEntities.length - 1;
        const lastEntity = this.denseEntities[lastIndex];
        const lastValue = this.denseValues[lastIndex];
        this.denseEntities[index] = lastEntity;
        this.denseValues[index] = lastValue;
        this.sparse.set(entityKey(lastEntity), index);
        this.denseEntities.pop();
        this.denseValues.pop();
        this.sparse.delete(key);
        return true;
    }
    entries() {
        return this.denseEntities.map((entity, index) => [entity, this.denseValues[index]]);
    }
    entities() {
        return this.denseEntities.map((entity) => ({ ...entity }));
    }
    get size() {
        return this.denseEntities.length;
    }
}
//# sourceMappingURL=SparseSet.js.map