pub struct DependencyGraph { /* private fields */ }Expand description
The dependency graph of one worksheet’s formulas.
A cell is a formula cell iff it appears in the forward map; constant
cells appear only on the reverse side (as things formulas read). The
graph is single-sheet: coordinates are (row, col) within the modeled
worksheet (see crate::refs for the sheet-qualifier policy).
Implementations§
Source§impl DependencyGraph
impl DependencyGraph
Sourcepub fn extract(formula: &str) -> Result<Self, GraphError>
pub fn extract(formula: &str) -> Result<Self, GraphError>
Extracts the references of formula into a standalone graph anchored
at the origin cell (0, 0) — i.e. exactly
Self::new() + add_formula((0, 0), formula).
Useful for one-shot parsing (precedents((0, 0)) returns the sorted,
de-duplicated references) and as the typed entry point when the
anchor cell is irrelevant. Note the modeling consequence: a formula
at the origin that reads A1 produces a self-edge and will be
reported by has_circular — correct for the
anchor, so prefer add_formula with the real
address when one exists.
§Errors
GraphError — see extract_refs.
Sourcepub fn add_formula(
&mut self,
cell: (u32, u32),
formula: &str,
) -> Result<(), GraphError>
pub fn add_formula( &mut self, cell: (u32, u32), formula: &str, ) -> Result<(), GraphError>
Registers cell as holding formula, replacing any formula
previously registered there. The graph is mutated only when the
formula parses — a rejected formula leaves it untouched.
§Errors
GraphError — see extract_refs.
Sourcepub fn remove_formula(&mut self, cell: (u32, u32))
pub fn remove_formula(&mut self, cell: (u32, u32))
Unregisters the formula at cell (no-op when there is none). The
cell’s own outgoing edges go; incoming edges of other formulas
that read it stay — removing a formula does not blank its value for
readers, it just stops the cell from being recomputed.
Sourcepub fn precedents(&self, cell: (u32, u32)) -> Vec<(u32, u32)>
pub fn precedents(&self, cell: (u32, u32)) -> Vec<(u32, u32)>
Cells read by the formula at cell, sorted ascending. Empty when
cell holds no formula.
Sourcepub fn dependents(&self, cell: (u32, u32)) -> Vec<(u32, u32)>
pub fn dependents(&self, cell: (u32, u32)) -> Vec<(u32, u32)>
Formula cells whose formulas read cell, sorted ascending.
Sourcepub fn contains_formula(&self, cell: (u32, u32)) -> bool
pub fn contains_formula(&self, cell: (u32, u32)) -> bool
true when cell holds a registered formula.
Sourcepub fn formula_count(&self) -> usize
pub fn formula_count(&self) -> usize
Number of registered formulas.
Sourcepub fn formula_cells(&self) -> Vec<(u32, u32)>
pub fn formula_cells(&self) -> Vec<(u32, u32)>
All registered formula cells, sorted ascending.
Sourcepub fn topological_order(&self) -> Vec<(u32, u32)>
pub fn topological_order(&self) -> Vec<(u32, u32)>
The evaluation order: every formula cell, ordered so that each
formula comes after every formula it (transitively) reads. Ties
break by ascending (row, col), so the result is a total, stable
order. Formula cells inside a circular reference have no valid
position — and neither does anything that reads them — so both are
omitted; pair with has_circular to detect
that case.
Sourcepub fn dirty_subgraph(&self, edited: &[(u32, u32)]) -> Vec<(u32, u32)>
pub fn dirty_subgraph(&self, edited: &[(u32, u32)]) -> Vec<(u32, u32)>
The minimal recompute set for an edit of edited, in evaluation
order — ready to drive an incremental recalc directly.
Formulas among the edited cells are included (an edited formula must
re-evaluate); constants only propagate. Cycle members that the
propagation reaches are included but cannot be ordered, so they are
appended after the orderable prefix (see has_circular).
Sourcepub fn has_circular(&self) -> Vec<Vec<(u32, u32)>>
pub fn has_circular(&self) -> Vec<Vec<(u32, u32)>>
The circular references, as explicit cycles. Each cycle is sorted
ascending and the cycles themselves are sorted by first member, so
the output is deterministic. Self-references (A1 = A1+1) report as
single-member cycles. Empty when the graph is acyclic.