Skip to content

Commit

Permalink
refactor: connect on the fly
Browse files Browse the repository at this point in the history
  • Loading branch information
ss2165 committed Nov 27, 2024
1 parent 44faf10 commit 9b3c722
Showing 1 changed file with 21 additions and 35 deletions.
56 changes: 21 additions & 35 deletions hugr-core/src/hugr/rewrite/simple_replace.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
//! Implementation of the `SimpleReplace` operation.
use std::collections::{HashMap, HashSet};
use std::collections::HashMap;

use crate::hugr::hugrmut::InsertionResult;
pub use crate::hugr::internal::HugrMutInternals;
use crate::hugr::views::SiblingSubgraph;
use crate::hugr::{HugrMut, HugrView, Rewrite};
use crate::ops::{OpTag, OpTrait, OpType};
use crate::{Hugr, IncomingPort, Node, OutgoingPort};
use crate::{Hugr, IncomingPort, Node};
use thiserror::Error;

use super::inline_dfg::InlineDFGError;
Expand Down Expand Up @@ -161,33 +161,24 @@ impl Rewrite for SimpleReplacement {
// remove the replacement root (which now has no children and no edges)
h.remove_node(new_root);

// 3.4. Update replacement nodes according to insertion mapping and load in to
// connection set.
let mut connect: HashSet<(Node, OutgoingPort, Node, IncomingPort)> =
HashSet::with_capacity(nu_inp_connects.len() + nu_out_connects.len() + nu_out.len());

connect.extend(nu_inp_connects.into_iter().map(
|(src_node, src_port, tgt_node, tgt_port)| {
(
src_node,
src_port,
*index_map.get(tgt_node).unwrap(),
*tgt_port,
)
},
));

connect.extend(nu_out_connects.into_iter().map(
|(src_node, src_port, tgt_node, tgt_port)| {
(
*index_map.get(&src_node).unwrap(),
src_port,
*tgt_node,
*tgt_port,
)
},
));
// 3.4. Update replacement nodes according to insertion mapping and connect
for (src_node, src_port, tgt_node, tgt_port) in nu_inp_connects {
h.connect(
src_node,
src_port,
*index_map.get(tgt_node).unwrap(),
*tgt_port,
)
}

for (src_node, src_port, tgt_node, tgt_port) in nu_out_connects {
h.connect(
*index_map.get(&src_node).unwrap(),
src_port,
*tgt_node,
*tgt_port,
)
}
// 3.5. For each q = self.nu_out[p1], p0 = self.nu_inp[q], add an edge from the predecessor of p0
// to p1.
//
Expand All @@ -204,19 +195,14 @@ impl Rewrite for SimpleReplacement {
//
// Otherwise, we might disconnect other wires in `rem_inp_node`
// that are needed for the following iterations.
connect.insert((
h.connect(
rem_inp_pred_node,
rem_inp_pred_port,
*rem_out_node,
*rem_out_port,
));
);
}
}
connect
.into_iter()
.for_each(|(src_node, src_port, tgt_node, tgt_port)| {
h.connect(src_node, src_port, tgt_node, tgt_port);
});

// 3.6. Remove all nodes in subgraph and edges between them.
Ok(subgraph
Expand Down

0 comments on commit 9b3c722

Please sign in to comment.