diff --git a/crates/calc/src/recalc.rs b/crates/calc/src/recalc.rs index f0a838fe0..52de341aa 100644 --- a/crates/calc/src/recalc.rs +++ b/crates/calc/src/recalc.rs @@ -562,21 +562,27 @@ impl Calc { #[derive(Default)] struct Former { areas: HashMap, - /// The cells of `areas`, built when an array first needs more cells than its own former area - /// (so each check costs what checking the cells themselves does, however many areas there are). - cells: Option>, + /// The areas over each [`FORMER_TILE`]-cell square of a sheet, built when an array first needs + /// more cells than its own former area. A check looks only at the areas near the array, and the + /// index grows with the squares the areas cover, not with their cells: a file's `ref`s may + /// declare millions of them. + tiles: Option>>, +} + +/// The side of [`Former`]'s index squares, in cells. +const FORMER_TILE: u32 = 64; + +/// The index squares `range` overlaps, as (row, column) square numbers. +fn former_tiles(range: RangeRef) -> impl Iterator { + let cols = range.start.col / FORMER_TILE..=range.end.col / FORMER_TILE; + (range.start.row / FORMER_TILE..=range.end.row / FORMER_TILE).flat_map(move |r| cols.clone().map(move |c| (r, c))) } impl Former { - /// Frees `key`'s former area, as it is laid out now. + /// Frees `key`'s former area, as it is laid out now. The index keeps its key; checks skip + /// keys whose area is gone. fn take(&mut self, key: &Key) -> Option { - let area = self.areas.remove(key)?; - if let Some(cells) = &mut self.cells { - for cell in area.iter() { - cells.remove(&(key.0, cell)); - } - } - Some(area) + self.areas.remove(key) } /// Whether `range` on `sheet` reaches a former area that is still taken. @@ -585,8 +591,17 @@ impl Former { return false; } let areas = &self.areas; - let cells = self.cells.get_or_insert_with(|| areas.iter().flat_map(|(k, area)| area.iter().map(move |cell| (k.0, cell))).collect()); - range.iter().any(|cell| cells.contains(&(sheet, cell))) + let tiles = self.tiles.get_or_insert_with(|| { + let mut tiles: HashMap<(usize, u32, u32), Vec> = HashMap::default(); + for (key, area) in areas { + for (r, c) in former_tiles(*area) { + tiles.entry((key.0, r, c)).or_default().push(*key); + } + } + tiles + }); + former_tiles(range) + .any(|(r, c)| tiles.get(&(sheet, r, c)).is_some_and(|keys| keys.iter().any(|k| areas.get(k).is_some_and(|area| area.intersects(&range))))) } } diff --git a/crates/calc/src/tests.rs b/crates/calc/src/tests.rs index 5e84c8b4e..35f3ab0cc 100644 --- a/crates/calc/src/tests.rs +++ b/crates/calc/src/tests.rs @@ -737,6 +737,23 @@ fn a_full_recalc_keeps_a_spill_from_an_array_that_grew_into_it() { assert_eq!([t.num("C14"), t.num("F14"), t.num("G14"), t.num("I14")], [1.0, 4.0, 5.0, 7.0]); } +#[test] +fn a_full_recalc_keeps_a_former_area_where_it_crosses_into_far_cells() { + // As above, with one array at BI14:BO14 and the other at BN13: the former area runs past + // column BL, and the array grows into the part beyond it (former areas are indexed by squares + // of cells, so the overlap must be found away from the area's first cells too). + let mut t = T::new(); + t.set("J1", "1"); + t.set("BI14", "=SEQUENCE(1,7)"); + t.set("BN13", "=SEQUENCE(J1,2)"); + assert_eq!([t.num("BN13"), t.num("BO13")], [1.0, 2.0]); + t.wb.calc.mode = gridcraft_model::CalcMode::Manual; + t.set("J1", "5"); + t.calc.recalc_all(&mut t.wb); + assert_eq!(t.get("BN13"), Value::Error(CellError::Spill)); + assert_eq!([t.num("BI14"), t.num("BN14"), t.num("BO14")], [1.0, 6.0, 7.0]); +} + #[test] fn a_full_recalc_lays_out_arrays_as_an_automatic_one_when_one_shrinks_for_another() { // C1 grows into C3, which A3's array fills, and A3 grows into E3, which E1's array leaves.