Skip to content

Commit

Permalink
Cleanup some code (#236)
Browse files Browse the repository at this point in the history
* Add missing inlines in region

* Drop unused `replacement.rs`

* Merge `remove_op` and `remove_node`

* s/remove_op/remove_node/
  • Loading branch information
aborgna-q authored Jul 3, 2023
1 parent db5232f commit 8c6025e
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 14 deletions.
9 changes: 1 addition & 8 deletions src/hugr/hugrmut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ pub(crate) trait HugrMut {
/// # Panics
///
/// Panics if the node is the root node.
fn remove_op(&mut self, node: Node) -> Result<(), HugrError>;

/// Remove a node from the graph
fn remove_node(&mut self, node: Node) -> Result<(), HugrError>;

/// Connect two nodes at the given ports.
Expand Down Expand Up @@ -151,15 +148,11 @@ where
node.into()
}

fn remove_op(&mut self, node: Node) -> Result<(), HugrError> {
fn remove_node(&mut self, node: Node) -> Result<(), HugrError> {
if node.index == self.as_ref().root {
// TODO: Add a HugrMutError ?
panic!("cannot remove root node");
}
self.as_mut().remove_node(node)
}

fn remove_node(&mut self, node: Node) -> Result<(), HugrError> {
self.as_mut().hierarchy.remove(node.index);
self.as_mut().graph.remove_node(node.index);
self.as_mut().op_types.remove(node.index);
Expand Down
24 changes: 24 additions & 0 deletions src/hugr/region.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,42 +75,50 @@ impl<'g> HugrView for FlatRegionView<'g> {
> where
Self: 'a;

#[inline]
fn root(&self) -> Node {
self.root
}

#[inline]
fn get_parent(&self, node: Node) -> Option<Node> {
self.hierarchy
.parent(node.index)
.map(Into::into)
.filter(|&n| n == self.root)
}

#[inline]
fn get_optype(&self, node: Node) -> &OpType {
self.op_types.get(node.index)
}

#[inline]
fn node_count(&self) -> usize {
self.hierarchy.child_count(self.root.index) + 1
}

#[inline]
fn edge_count(&self) -> usize {
// Faster implementation than filtering all the nodes in the internal graph.
self.nodes()
.map(|n| self.output_neighbours(n).count())
.sum()
}

#[inline]
fn nodes(&self) -> Self::Nodes<'_> {
// Faster implementation than filtering all the nodes in the internal graph.
let children = self.hierarchy.children(self.root.index).map_into();
iter::once(self.root).chain(children)
}

#[inline]
fn node_ports(&self, node: Node, dir: Direction) -> Self::NodePorts<'_> {
self.graph.port_offsets(node.index, dir).map_into()
}

#[inline]
fn all_node_ports(&self, node: Node) -> Self::NodePorts<'_> {
self.graph.all_port_offsets(node.index).map_into()
}
Expand All @@ -128,21 +136,25 @@ impl<'g> HugrView for FlatRegionView<'g> {
})
}

#[inline]
fn num_ports(&self, node: Node, dir: Direction) -> usize {
self.graph.num_ports(node.index, dir)
}

#[inline]
fn children(&self, node: Node) -> Self::Children<'_> {
match node == self.root {
true => self.hierarchy.children(node.index).map_into(),
false => portgraph::hierarchy::Children::default().map_into(),
}
}

#[inline]
fn neighbours(&self, node: Node, dir: Direction) -> Self::Neighbours<'_> {
self.graph.neighbours(node.index, dir).map_into()
}

#[inline]
fn all_neighbours(&self, node: Node) -> Self::Neighbours<'_> {
self.graph.all_neighbours(node.index).map_into()
}
Expand Down Expand Up @@ -214,37 +226,45 @@ impl<'g> HugrView for RegionView<'g> {
> where
Self: 'a;

#[inline]
fn root(&self) -> Node {
self.root
}

#[inline]
fn get_parent(&self, node: Node) -> Option<Node> {
self.hierarchy
.parent(node.index)
.filter(|&parent| self.graph.contains_node(parent))
.map(Into::into)
}

#[inline]
fn get_optype(&self, node: Node) -> &OpType {
self.op_types.get(node.index)
}

#[inline]
fn node_count(&self) -> usize {
self.graph.node_count()
}

#[inline]
fn edge_count(&self) -> usize {
self.graph.link_count()
}

#[inline]
fn nodes(&self) -> Self::Nodes<'_> {
self.graph.nodes_iter().map_into()
}

#[inline]
fn node_ports(&self, node: Node, dir: Direction) -> Self::NodePorts<'_> {
self.graph.port_offsets(node.index, dir).map_into()
}

#[inline]
fn all_node_ports(&self, node: Node) -> Self::NodePorts<'_> {
self.graph.all_port_offsets(node.index).map_into()
}
Expand All @@ -262,21 +282,25 @@ impl<'g> HugrView for RegionView<'g> {
})
}

#[inline]
fn num_ports(&self, node: Node, dir: Direction) -> usize {
self.graph.num_ports(node.index, dir)
}

#[inline]
fn children(&self, node: Node) -> Self::Children<'_> {
match self.graph.contains_node(node.index) {
true => self.hierarchy.children(node.index).map_into(),
false => portgraph::hierarchy::Children::default().map_into(),
}
}

#[inline]
fn neighbours(&self, node: Node, dir: Direction) -> Self::Neighbours<'_> {
self.graph.neighbours(node.index, dir).map_into()
}

#[inline]
fn all_neighbours(&self, node: Node) -> Self::Neighbours<'_> {
self.graph.all_neighbours(node.index).map_into()
}
Expand Down
2 changes: 1 addition & 1 deletion src/hugr/validate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1047,7 +1047,7 @@ mod test {
Err(ValidationError::InvalidChildren { parent, source: ChildrenValidationError::InternalExitChildren { child, .. }, .. })
=> {assert_eq!(parent, cfg); assert_eq!(child, exit2.index)}
);
b.remove_op(exit2).unwrap();
b.remove_node(exit2).unwrap();

// Change the types in the BasicBlock node to work on qubits instead of bits
b.replace_op(
Expand Down
5 changes: 0 additions & 5 deletions src/replacement.rs

This file was deleted.

0 comments on commit 8c6025e

Please sign in to comment.