diff --git a/src/hugr.rs b/src/hugr.rs index ec44afa46..82ec75501 100644 --- a/src/hugr.rs +++ b/src/hugr.rs @@ -303,12 +303,6 @@ impl Hugr { } } - /// Add a node to the graph, with the default conversion from OpType to NodeType - pub(crate) fn add_op(&mut self, op: impl Into) -> Node { - // TODO: Default to `NodeType::open_extensions` once we can infer extensions - self.add_node(NodeType::pure(op)) - } - /// Add a node to the graph. pub(crate) fn add_node(&mut self, nodetype: NodeType) -> Node { let node = self diff --git a/src/hugr/hugrmut.rs b/src/hugr/hugrmut.rs index 7f91752b5..3137e4561 100644 --- a/src/hugr/hugrmut.rs +++ b/src/hugr/hugrmut.rs @@ -75,20 +75,6 @@ pub trait HugrMut: HugrMutInternals { self.hugr_mut().add_node_before(sibling, nodetype) } - /// Add a node to the graph as the next sibling of another node. - /// - /// The sibling node's parent becomes the new node's parent. - /// - /// # Errors - /// - /// - If the sibling node does not have a parent. - /// - If the attachment would introduce a cycle. - #[inline] - fn add_op_after(&mut self, sibling: Node, op: impl Into) -> Result { - self.valid_non_root(sibling)?; - self.hugr_mut().add_op_after(sibling, op) - } - /// Remove a node from the graph. /// /// # Panics @@ -227,14 +213,6 @@ impl + AsMut> HugrMut for T { Ok(node) } - fn add_op_after(&mut self, sibling: Node, op: impl Into) -> Result { - let node = self.as_mut().add_op(op); - self.as_mut() - .hierarchy - .insert_after(node.index, sibling.index)?; - Ok(node) - } - fn remove_node(&mut self, node: Node) -> Result<(), HugrError> { if node == self.root() { // TODO: Add a HugrMutError ? @@ -461,18 +439,6 @@ pub(crate) mod sealed { self.hugr_mut().set_parent(node, parent) } - /// Move a node in the hierarchy to be the subsequent sibling of another - /// node. - /// - /// The sibling node's parent becomes the new node's parent. - /// - /// The node becomes the parent's last child. - fn move_after_sibling(&mut self, node: Node, after: Node) -> Result<(), HugrError> { - self.valid_non_root(node)?; - self.valid_non_root(after)?; - self.hugr_mut().move_after_sibling(node, after) - } - /// Move a node in the hierarchy to be the prior sibling of another node. /// /// The sibling node's parent becomes the new node's parent. @@ -544,14 +510,6 @@ pub(crate) mod sealed { Ok(()) } - fn move_after_sibling(&mut self, node: Node, after: Node) -> Result<(), HugrError> { - self.hugr_mut().hierarchy.detach(node.index); - self.hugr_mut() - .hierarchy - .insert_after(node.index, after.index)?; - Ok(()) - } - fn move_before_sibling(&mut self, node: Node, before: Node) -> Result<(), HugrError> { self.hugr_mut().hierarchy.detach(node.index); self.hugr_mut() diff --git a/src/hugr/rewrite/simple_replace.rs b/src/hugr/rewrite/simple_replace.rs index 53e3f68a7..6c280989a 100644 --- a/src/hugr/rewrite/simple_replace.rs +++ b/src/hugr/rewrite/simple_replace.rs @@ -99,12 +99,11 @@ impl Rewrite for SimpleReplacement { .collect::>(); // slice of nodes omitting Input and Output: let replacement_inner_nodes = &replacement_nodes[2..]; - let self_output_node = h.children(parent).nth(1).unwrap(); let replacement_output_node = *replacement_nodes.get(1).unwrap(); for &node in replacement_inner_nodes { // Add the nodes. let op: &OpType = self.replacement.get_optype(node); - let new_node = h.add_op_after(self_output_node, op.clone()).unwrap(); + let new_node = h.add_op_with_parent(parent, op.clone()).unwrap(); index_map.insert(node, new_node); // Move the metadata diff --git a/src/hugr/serialize.rs b/src/hugr/serialize.rs index 2fdc2fb36..57ca0716d 100644 --- a/src/hugr/serialize.rs +++ b/src/hugr/serialize.rs @@ -273,7 +273,7 @@ pub mod test { use super::*; use crate::extension::{EMPTY_REG, PRELUDE_REGISTRY}; - use crate::hugr::hugrmut::sealed::HugrMutInternals; + use crate::{ builder::{ test::closed_dfg_root_hugr, Container, DFGBuilder, Dataflow, DataflowHugr, @@ -465,10 +465,11 @@ pub mod test { hugr.connect(old_in, 0, out, 0).unwrap(); // Now add a new input - let new_in = hugr.add_op(Input::new([QB].to_vec())); + let new_in = hugr + .add_op_before(old_in, Input::new([QB].to_vec())) + .unwrap(); hugr.disconnect(old_in, Port::new_outgoing(0)).unwrap(); hugr.connect(new_in, 0, out, 0).unwrap(); - hugr.move_before_sibling(new_in, old_in).unwrap(); hugr.remove_node(old_in).unwrap(); hugr.update_validate(&PRELUDE_REGISTRY).unwrap(); diff --git a/src/hugr/validate.rs b/src/hugr/validate.rs index 57b9020c0..0ec386088 100644 --- a/src/hugr/validate.rs +++ b/src/hugr/validate.rs @@ -845,7 +845,7 @@ mod test { assert_eq!(b.validate(&EMPTY_REG), Ok(())); // Add another hierarchy root - let other = b.add_op(ops::Module); + let other = b.add_node(NodeType::pure(ops::Module)); assert_matches!( b.validate(&EMPTY_REG), Err(ValidationError::NoParent { node }) => assert_eq!(node, other) @@ -1039,8 +1039,8 @@ mod test { // Add an internal exit node let exit2 = b - .add_op_after( - exit, + .add_op_with_parent( + cfg, ops::BasicBlock::Exit { cfg_outputs: type_row![BOOL_T], },