Skip to content

Commit 1ed6440

Browse files
aborgna-qss2165
authored andcommitted
feat(py): Helper methods to get the neighbours of a node (#2370)
1 parent 74b25aa commit 1ed6440

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed

hugr-py/src/hugr/hugr/base.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -672,6 +672,75 @@ def incoming_links(self, node: ToNode) -> Iterable[tuple[InPort, list[OutPort]]]
672672
""" # noqa: E501
673673
return self._node_links(node, self._links.bck)
674674

675+
def neighbours(
676+
self, node: ToNode, direction: Direction | None = None
677+
) -> Iterable[Node]:
678+
"""Iterator over the neighbours of a node.
679+
680+
Args:
681+
node: Node to query.
682+
direction: If given, only return neighbours in that direction.
683+
684+
Returns:
685+
Iterator of nodes connected to `node`, ordered by direction and port
686+
offset. Nodes connected via multiple links will be returned multiple times.
687+
688+
Examples:
689+
>>> df = dfg.Dfg()
690+
>>> df.hugr.add_link(df.input_node.out(0), df.output_node.inp(0))
691+
>>> df.hugr.add_link(df.input_node.out(0), df.output_node.inp(1))
692+
>>> list(df.hugr.neighbours(df.input_node))
693+
[Node(6), Node(6)]
694+
>>> list(df.hugr.neighbours(df.output_node, Direction.OUTGOING))
695+
[]
696+
"""
697+
if direction is None or direction == Direction.INCOMING:
698+
for _, linked_outputs in self.incoming_links(node):
699+
for out_port in linked_outputs:
700+
yield out_port.node
701+
if direction is None or direction == Direction.OUTGOING:
702+
for _, linked_inputs in self.outgoing_links(node):
703+
for in_port in linked_inputs:
704+
yield in_port.node
705+
706+
def input_neighbours(self, node: ToNode) -> Iterable[Node]:
707+
"""Iterator over the input neighbours of a node.
708+
709+
Args:
710+
node: Node to query.
711+
712+
Returns:
713+
Iterator of nodes connected to `node` via incoming links.
714+
Nodes connected via multiple links will be returned multiple times.
715+
716+
Examples:
717+
>>> df = dfg.Dfg()
718+
>>> df.hugr.add_link(df.input_node.out(0), df.output_node.inp(0))
719+
>>> df.hugr.add_link(df.input_node.out(0), df.output_node.inp(1))
720+
>>> list(df.hugr.input_neighbours(df.output_node))
721+
[Node(5), Node(5)]
722+
"""
723+
return self.neighbours(node, Direction.INCOMING)
724+
725+
def output_neighbours(self, node: ToNode) -> Iterable[Node]:
726+
"""Iterator over the output neighbours of a node.
727+
728+
Args:
729+
node: Node to query.
730+
731+
Returns:
732+
Iterator of nodes connected to `node` via outgoing links.
733+
Nodes connected via multiple links will be returned multiple times.
734+
735+
Examples:
736+
>>> df = dfg.Dfg()
737+
>>> df.hugr.add_link(df.input_node.out(0), df.output_node.inp(0))
738+
>>> df.hugr.add_link(df.input_node.out(0), df.output_node.inp(1))
739+
>>> list(df.hugr.output_neighbours(df.input_node))
740+
[Node(6), Node(6)]
741+
"""
742+
return self.neighbours(node, Direction.OUTGOING)
743+
675744
def num_incoming(self, node: Node) -> int:
676745
"""The number of incoming links to a `node`.
677746

0 commit comments

Comments
 (0)