Skip to content

Commit 7387ddc

Browse files
committed
[IMP] evaluation_plugin: use rangeSet
1 parent dd7a0e4 commit 7387ddc

File tree

3 files changed

+29
-38
lines changed

3 files changed

+29
-38
lines changed

packages/o-spreadsheet-engine/src/plugins/ui_core_views/cell_evaluation/evaluation_plugin.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import { ExcelWorkbookData } from "../../../types/workbook_data";
3333
import { FormulaCellWithDependencies } from "../../core/cell";
3434
import { CoreViewPlugin, CoreViewPluginConfig } from "../../core_view_plugin";
3535
import { Evaluator } from "./evaluator";
36+
import { RangeSet } from "./range_set";
3637

3738
//#region
3839

@@ -172,7 +173,7 @@ export class EvaluationPlugin extends CoreViewPlugin {
172173
private shouldRebuildDependenciesGraph = true;
173174

174175
private evaluator: Evaluator;
175-
private positionsToUpdate: CellPosition[] = [];
176+
private positionsToUpdate: RangeSet = new RangeSet();
176177

177178
constructor(config: CoreViewPluginConfig) {
178179
super(config);
@@ -199,7 +200,7 @@ export class EvaluationPlugin extends CoreViewPlugin {
199200
return;
200201
}
201202
const position = { sheetId: cmd.sheetId, row: cmd.row, col: cmd.col };
202-
this.positionsToUpdate.push(position);
203+
this.positionsToUpdate.addPosition(position);
203204

204205
if ("content" in cmd) {
205206
this.evaluator.updateDependencies(position);
@@ -210,14 +211,12 @@ export class EvaluationPlugin extends CoreViewPlugin {
210211
return;
211212
}
212213
for (const zone of cmd.target) {
213-
this.positionsToUpdate.push(...cellPositions(cmd.sheetId, zone));
214+
this.positionsToUpdate.add({ sheetId: cmd.sheetId, zone });
214215
}
215216
break;
216217
case "EVALUATE_CELLS":
217218
if (cmd.cellIds) {
218-
for (let i = 0; i < cmd.cellIds.length; i++) {
219-
this.positionsToUpdate.push(this.getters.getCellPosition(cmd.cellIds[i]));
220-
}
219+
this.positionsToUpdate.addManyPositions(cmd.cellIds.map(this.getters.getCellPosition));
221220
} else {
222221
this.evaluator.evaluateAllCells();
223222
}
@@ -230,10 +229,10 @@ export class EvaluationPlugin extends CoreViewPlugin {
230229
this.evaluator.buildDependencyGraph();
231230
this.evaluator.evaluateAllCells();
232231
this.shouldRebuildDependenciesGraph = false;
233-
} else if (this.positionsToUpdate.length) {
232+
} else if (this.positionsToUpdate.size()) {
234233
this.evaluator.evaluateCells(this.positionsToUpdate);
235234
}
236-
this.positionsToUpdate = [];
235+
this.positionsToUpdate = new RangeSet();
237236
}
238237

239238
// ---------------------------------------------------------------------------

packages/o-spreadsheet-engine/src/plugins/ui_core_views/cell_evaluation/evaluator.ts

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,14 @@ export class Evaluator {
109109
return this.evaluatedCells.keysForSheet(sheetId);
110110
}
111111

112+
getArrayFormulaSpreadingOnRange(range: BoundedRange): CellPosition[] {
113+
const arrayFormulas = this.spreadingRelations.searchFormulaPositionsSpreadingOn(
114+
range.sheetId,
115+
range.zone
116+
);
117+
return arrayFormulas.filter((position) => !this.blockedArrayFormulas.has(position));
118+
}
119+
112120
getArrayFormulaSpreadingOn(position: CellPosition): CellPosition | undefined {
113121
const isEmpty = this.getEvaluatedCell(position).type === CellValueType.empty;
114122
if (isEmpty) {
@@ -162,35 +170,22 @@ export class Evaluator {
162170
return new PositionSet(this.getters.getSheetIds());
163171
}
164172

165-
evaluateCells(positions: CellPosition[]) {
173+
evaluateCells(rangesToCompute: RangeSet) {
166174
const start = performance.now();
167-
const rangesToCompute = new RangeSet();
168-
rangesToCompute.addManyPositions(positions);
169-
const arrayFormulasPositions = this.getArrayFormulasImpactedByChangesOf(positions);
175+
rangesToCompute.addMany(this.getArrayFormulasImpactedByChangesOf(rangesToCompute));
170176
rangesToCompute.addMany(this.getCellsDependingOn(rangesToCompute));
171-
rangesToCompute.addMany(arrayFormulasPositions);
172-
rangesToCompute.addMany(this.getCellsDependingOn(arrayFormulasPositions));
173177
this.formatCache = this.getters.getCellFormatInRanges(rangesToCompute);
174178
this.evaluate(rangesToCompute);
175179
console.debug("evaluate Cells", performance.now() - start, "ms");
176180
}
177181

178-
private getArrayFormulasImpactedByChangesOf(positions: Iterable<CellPosition>): RangeSet {
182+
private getArrayFormulasImpactedByChangesOf(ranges: RangeSet): RangeSet {
179183
const impactedRanges = new RangeSet();
180-
181-
for (const position of positions) {
182-
const content = this.getters.getCell(position)?.content;
183-
const arrayFormulaPosition = this.getArrayFormulaSpreadingOn(position);
184-
if (arrayFormulaPosition !== undefined) {
185-
// take into account new collisions.
186-
impactedRanges.addPosition(arrayFormulaPosition);
187-
}
188-
if (!content) {
189-
// The previous content could have blocked some array formulas
190-
impactedRanges.addPosition(position);
191-
}
184+
for (const range of ranges) {
185+
impactedRanges.add(range);
186+
impactedRanges.addManyPositions(this.getArrayFormulaSpreadingOnRange(range));
192187
}
193-
for (const range of [...impactedRanges]) {
188+
for (const range of impactedRanges) {
194189
impactedRanges.addMany(this.getArrayFormulasBlockedBy(range.sheetId, range.zone));
195190
}
196191
return impactedRanges;

packages/o-spreadsheet-engine/src/plugins/ui_feature/format.ts

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,13 @@ export class FormatPlugin extends UIPlugin {
3333
const measurePositions: CellPosition[] = [];
3434
const measuresByPivotId: Record<string, Set<string>> = {};
3535
for (const zone of recomputeZones(zones)) {
36-
for (let col = zone.left; col <= zone.right; col++) {
37-
for (let row = zone.top; row <= zone.bottom; row++) {
38-
const position = { sheetId, col, row };
39-
const pivotCell = this.getters.getPivotCellFromPosition(position);
40-
if (this.isSpilledPivotValueFormula(position, pivotCell)) {
41-
measurePositions.push(position);
42-
const pivotId = this.getters.getPivotIdFromPosition(position) || "";
43-
measuresByPivotId[pivotId] ??= new Set();
44-
measuresByPivotId[pivotId].add(pivotCell.measure);
45-
}
36+
for (const position of this.getters.getEvaluatedCellsPositionInZone(sheetId, zone)) {
37+
const pivotCell = this.getters.getPivotCellFromPosition(position);
38+
if (this.isSpilledPivotValueFormula(position, pivotCell)) {
39+
measurePositions.push(position);
40+
const pivotId = this.getters.getPivotIdFromPosition(position) || "";
41+
measuresByPivotId[pivotId] ??= new Set();
42+
measuresByPivotId[pivotId].add(pivotCell.measure);
4643
}
4744
}
4845
}

0 commit comments

Comments
 (0)