Skip to content

Commit

Permalink
make _dependencies_sans_invariants non-recursive
Browse files Browse the repository at this point in the history
  • Loading branch information
gertjanvanzwieten authored and joostvanzwieten committed Mar 26, 2024
1 parent 3b8c820 commit de07c59
Showing 1 changed file with 15 additions and 13 deletions.
28 changes: 15 additions & 13 deletions nutils/evaluable.py
Original file line number Diff line number Diff line change
Expand Up @@ -4556,26 +4556,28 @@ def _isunique(array):
return numpy.unique(array).size == array.size


_AddDependency = collections.namedtuple('_AddDependency', ['dependency'])

def _dependencies_sans_invariants(func, arg):
invariants = []
dependencies = []
_populate_dependencies_sans_invariants(func, arg, invariants, dependencies, {arg})
cache = {arg}
stack = [func]
while stack:
func_ = stack.pop()
if isinstance(func_, _AddDependency):
dependencies.append(func_.dependency)
elif func_ not in cache:
cache.add(func_)
if arg in func_.arguments:
stack.append(_AddDependency(func_))
stack.extend(func_._Evaluable__args)
else:
invariants.append(func_)
assert (dependencies or invariants or [arg])[-1] == func
return tuple(invariants), tuple(dependencies)


def _populate_dependencies_sans_invariants(func, arg, invariants, dependencies, cache):
if func in cache:
return
cache.add(func)
if arg in func.arguments:
for child in func._Evaluable__args:
_populate_dependencies_sans_invariants(child, arg, invariants, dependencies, cache)
dependencies.append(func)
else:
invariants.append(func)


class _Stats:

def __init__(self, ncalls: int = 0, time: int = 0) -> None:
Expand Down

0 comments on commit de07c59

Please sign in to comment.