提交 7fbce4d5 authored 作者: Matthew Rocklin's avatar Matthew Rocklin

add new io toposort

This version is simpler and more declarative. May be slower Fails tests because tests have order hard-coded
上级 007540a5
...@@ -1009,3 +1009,28 @@ def view_roots(r): ...@@ -1009,3 +1009,28 @@ def view_roots(r):
return [r] return [r]
else: else:
return [r] return [r]
def list_of_nodes(inputs, outputs):
""" Return the apply nodes of the graph between inputs and outputs """
return stack_search(
deque([o.owner for o in outputs]),
lambda o: [inp.owner for inp in o.inputs
if inp.owner
and not any(i in inp.owner.outputs for i in inputs)])
def dependence(a, b):
""" A cmp function for nodes in a graph - does a depend on b?
Returns positive number if a depends on b
Returns negative number if b depends on a
Returns 0 otherwise
"""
if b in {var.owner for var in ancestors(a.inputs)}:
return 1
if a in {var.owner for var in ancestors(b.inputs)}:
return -1
return 0
def new_io_toposort(inputs, outputs, cmp=dependence):
""" Same as io_toposort """
return sorted(list_of_nodes(inputs, outputs), cmp=cmp)
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论