export function createFormulaInstanceTable() {
    const records = [];
    let recordCount = 0;
    return {
        upsert(record) {
            if (records[record.cellIndex] === undefined) {
                recordCount += 1;
            }
            records[record.cellIndex] = record;
        },
        upsertMany(entries) {
            for (let index = 0; index < entries.length; index += 1) {
                const record = entries[index];
                if (records[record.cellIndex] === undefined) {
                    recordCount += 1;
                }
                records[record.cellIndex] = record;
            }
        },
        get(cellIndex) {
            return records[cellIndex];
        },
        delete(cellIndex) {
            if (records[cellIndex] === undefined) {
                return false;
            }
            records[cellIndex] = undefined;
            recordCount -= 1;
            return true;
        },
        clear() {
            records.length = 0;
            recordCount = 0;
        },
        list() {
            if (recordCount === 0) {
                return [];
            }
            const snapshots = [];
            for (let cellIndex = 0; cellIndex < records.length; cellIndex += 1) {
                const record = records[cellIndex];
                if (record !== undefined) {
                    snapshots.push(record);
                }
            }
            return snapshots.toSorted((left, right) => left.sheetName.localeCompare(right.sheetName) || left.row - right.row || left.col - right.col || left.cellIndex - right.cellIndex);
        },
        hydrate(entries) {
            records.length = 0;
            recordCount = 0;
            for (let index = 0; index < entries.length; index += 1) {
                const record = entries[index];
                if (records[record.cellIndex] === undefined) {
                    recordCount += 1;
                }
                records[record.cellIndex] = record;
            }
        },
    };
}
//# sourceMappingURL=formula-instance-table.js.map