提交 9e3c69be authored 作者: Brandon T. Willard's avatar Brandon T. Willard 提交者: Brandon T. Willard

Clean up FunctionGraph.toposort and orderings docstrings and code

上级 73dc0c8b
...@@ -635,43 +635,35 @@ class FunctionGraph(MetaObject): ...@@ -635,43 +635,35 @@ class FunctionGraph(MetaObject):
return d return d
def toposort(self) -> List[Apply]: def toposort(self) -> List[Apply]:
"""Return a toposorted list of the nodes. r"""Return a toposorted list of the nodes.
Return an ordering of the graph's ``Apply`` nodes such that: Return an ordering of the graph's :class:`Apply` nodes such that:
* all the nodes of the inputs of a node are before that node and * all the nodes of the inputs of a node are before that node, and
* they satisfy the orderings provided by each feature that has * they satisfy the additional orderings provided by
an ``orderings`` method. :meth:`FunctionGraph.orderings`.
If a feature has an ``orderings`` method, it will be called with
this `FunctionGraph` as sole argument. It should return a dictionary of
``{node: predecessors}`` where predecessors is a list of nodes that
should be computed before the key node.
""" """
if len(self.apply_nodes) < 2: if len(self.apply_nodes) < 2:
# optimization # No sorting is necessary
# when there are 0 or 1 nodes, no sorting is necessary
# This special case happens a lot because the OpWiseCLinker
# produces 1-element graphs.
return list(self.apply_nodes) return list(self.apply_nodes)
fg = self
ords = self.orderings() return io_toposort(self.inputs, self.outputs, self.orderings())
order = io_toposort(fg.inputs, fg.outputs, ords)
return order
def orderings(self) -> Dict[Apply, List[Apply]]: def orderings(self) -> Dict[Apply, List[Apply]]:
"""Return ``dict`` ``d`` s.t. ``d[node]`` is a list of nodes that must be evaluated before ``node`` itself can be evaluated. """Return a map of node to node evaluation dependencies.
Each key node is mapped to a list of nodes that must be evaluated
before the key nodes can be evaluated.
This is used primarily by the ``destroy_handler`` feature to ensure that This is used primarily by the :class:`DestroyHandler` :class:`Feature`
the clients of any destroyed inputs have already computed their to ensure that the clients of any destroyed inputs have already
outputs. computed their outputs.
Notes Notes
----- -----
This only calls the ``orderings()`` function on all features. It does not This only calls the :meth:`Feature.orderings` method of each
:class:`Feature` attached to the :class:`FunctionGraph`. It does not
take care of computing the dependencies by itself. take care of computing the dependencies by itself.
""" """
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论