1use crate::SheetId;
2use crate::engine::sheet_registry::SheetRegistry;
3use formualizer_common::Coord as AbsCoord;
4use formualizer_common::ExcelError;
5use formualizer_parse::parser::{CollectPolicy, ReferenceType};
6use rustc_hash::FxHashMap;
7
8#[derive(Debug, Clone, PartialEq, Eq, Hash)]
10pub enum RangeKey {
11 Rect {
12 sheet: SheetId,
13 start: AbsCoord,
14 end: AbsCoord, },
16 WholeRow {
17 sheet: SheetId,
18 row: u32,
19 },
20 WholeCol {
21 sheet: SheetId,
22 col: u32,
23 },
24 OpenRect {
26 sheet: SheetId,
27 start: Option<AbsCoord>,
28 end: Option<AbsCoord>,
29 },
30}
31
32pub type FormulaFlags = u8;
34pub const F_VOLATILE: FormulaFlags = 0b0000_0001;
35pub const F_HAS_RANGES: FormulaFlags = 0b0000_0010;
36pub const F_HAS_NAMES: FormulaFlags = 0b0000_0100;
37pub const F_LIKELY_ARRAY: FormulaFlags = 0b0000_1000;
38
39#[derive(Debug, Default, Clone)]
40pub struct DependencyPlan {
41 pub formula_targets: Vec<(SheetId, AbsCoord)>,
42 pub global_cells: Vec<(SheetId, AbsCoord)>,
43 pub per_formula_cells: Vec<Vec<u32>>, pub per_formula_ranges: Vec<Vec<RangeKey>>,
45 pub per_formula_names: Vec<Vec<String>>,
46 pub per_formula_flags: Vec<FormulaFlags>,
47 pub edges_flat: Option<Vec<u32>>, pub offsets: Option<Vec<u32>>, }
50
51pub fn build_dependency_plan<'a, I>(
54 sheet_reg: &mut SheetRegistry,
55 formulas: I,
56 policy: &CollectPolicy,
57 volatile_flags: Option<&[bool]>,
58) -> Result<DependencyPlan, ExcelError>
59where
60 I: Iterator<Item = (&'a str, u32, u32, &'a formualizer_parse::parser::ASTNode)>,
61{
62 let mut plan = DependencyPlan::default();
63
64 let mut cell_index: FxHashMap<(SheetId, AbsCoord), u32> = FxHashMap::default();
66
67 for (i, (sheet_name, row, col, ast)) in formulas.enumerate() {
68 let sheet_id = sheet_reg.id_for(sheet_name);
69 let target = (sheet_id, AbsCoord::from_excel(row, col));
70 plan.formula_targets.push(target);
71
72 let mut flags: FormulaFlags = 0;
73 if let Some(v) = volatile_flags.and_then(|v| v.get(i)).copied()
74 && v
75 {
76 flags |= F_VOLATILE;
77 }
78
79 let mut per_cells: Vec<u32> = Vec::new();
80 let mut per_ranges: Vec<RangeKey> = Vec::new();
81 let mut per_names: Vec<String> = Vec::new();
82
83 let refs = ast.collect_references(policy);
85 for r in refs {
86 match r {
87 ReferenceType::Cell { sheet, row, col } => {
88 let dep_sheet = sheet
89 .as_deref()
90 .map(|name| sheet_reg.id_for(name))
91 .unwrap_or(sheet_id);
92 let key = (dep_sheet, AbsCoord::from_excel(row, col));
93 let idx = match cell_index.get(&key) {
94 Some(&idx) => idx,
95 None => {
96 let new_idx = plan.global_cells.len() as u32;
97 plan.global_cells.push(key);
98 cell_index.insert(key, new_idx);
99 new_idx
100 }
101 };
102 per_cells.push(idx);
103 }
104 ReferenceType::Range {
105 sheet,
106 start_row,
107 start_col,
108 end_row,
109 end_col,
110 } => {
111 let dep_sheet = sheet
112 .as_deref()
113 .map(|name| sheet_reg.id_for(name))
114 .unwrap_or(sheet_id);
115 match (start_row, start_col, end_row, end_col) {
116 (Some(sr), Some(sc), Some(er), Some(ec)) => {
117 per_ranges.push(RangeKey::Rect {
118 sheet: dep_sheet,
119 start: AbsCoord::from_excel(sr, sc),
120 end: AbsCoord::from_excel(er, ec),
121 })
122 }
123 (None, Some(c), None, Some(ec)) if c == ec => {
124 per_ranges.push(RangeKey::WholeCol {
125 sheet: dep_sheet,
126 col: c,
127 })
128 }
129 (Some(r), None, Some(er), None) if r == er => {
130 per_ranges.push(RangeKey::WholeRow {
131 sheet: dep_sheet,
132 row: r,
133 })
134 }
135 _ => per_ranges.push(RangeKey::OpenRect {
136 sheet: dep_sheet,
137 start: start_row
138 .zip(start_col)
139 .map(|(r, c)| AbsCoord::from_excel(r, c)),
140 end: end_row
141 .zip(end_col)
142 .map(|(r, c)| AbsCoord::from_excel(r, c)),
143 }),
144 }
145 }
146 ReferenceType::NamedRange(name) => {
147 flags |= F_HAS_NAMES;
149 per_names.push(name);
150 }
151 ReferenceType::Table(_tbl) => {
152 }
154 }
155 }
156
157 plan.per_formula_cells.push(per_cells);
158 plan.per_formula_ranges.push(per_ranges);
159 plan.per_formula_names.push(per_names);
160 plan.per_formula_flags.push(flags);
161 }
162
163 Ok(plan)
164}