Skip to content

Commit

Permalink
feat: Compute affected nodes for SimpleReplacement (#600)
Browse files Browse the repository at this point in the history
  • Loading branch information
aborgna-q committed Oct 11, 2023
1 parent 903350f commit 9254ac7
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion src/hugr/rewrite/simple_replace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ pub struct SimpleReplacement {

impl SimpleReplacement {
/// Create a new [`SimpleReplacement`] specification.
#[inline]
pub fn new(
subgraph: SiblingSubgraph,
replacement: Hugr,
Expand All @@ -45,14 +46,28 @@ impl SimpleReplacement {
}

/// The replacement hugr.
#[inline]
pub fn replacement(&self) -> &Hugr {
&self.replacement
}

/// Subgraph to be replaced.
#[inline]
pub fn subgraph(&self) -> &SiblingSubgraph {
&self.subgraph
}

/// Returns a set of nodes referenced by the replacement. Modifying any
/// these nodes will invalidate the replacement.
///
/// Two `SimpleReplacement`s can be composed if their affected nodes are
/// disjoint.
#[inline]
pub fn invalidation_set(&self) -> impl Iterator<Item = Node> + '_ {
let subcirc = self.subgraph.nodes().iter().copied();
let out_neighs = self.nu_out.keys().map(|&(n, _)| n);
subcirc.chain(out_neighs)
}
}

impl Rewrite for SimpleReplacement {
Expand Down Expand Up @@ -203,7 +218,7 @@ pub(in crate::hugr::rewrite) mod test {
use itertools::Itertools;
use portgraph::Direction;
use rstest::{fixture, rstest};
use std::collections::HashMap;
use std::collections::{HashMap, HashSet};

use crate::builder::{
BuildError, Container, DFGBuilder, Dataflow, DataflowHugr, DataflowSubContainer,
Expand Down Expand Up @@ -384,6 +399,11 @@ pub(in crate::hugr::rewrite) mod test {
nu_inp,
nu_out,
};
assert_eq!(
HashSet::<_>::from_iter(r.invalidation_set()),
HashSet::<_>::from_iter([h_node_cx, h_node_h0, h_node_h1, h_outp_node]),
);

h.apply_rewrite(r).unwrap();
// Expect [DFG] to be replaced with:
// ┌───┐┌───┐
Expand Down

0 comments on commit 9254ac7

Please sign in to comment.