Simplified migrations.graph.Node.iterative_dfs(), ancestors(), and descendants().
This commit is contained in:
parent
09b3e46635
commit
a8bb493556
|
@ -1,5 +1,4 @@
|
||||||
import warnings
|
import warnings
|
||||||
from collections import deque
|
|
||||||
from functools import total_ordering
|
from functools import total_ordering
|
||||||
|
|
||||||
from django.db.migrations.state import ProjectState
|
from django.db.migrations.state import ProjectState
|
||||||
|
@ -56,9 +55,10 @@ class Node:
|
||||||
# Use self.key instead of self to speed up the frequent hashing
|
# Use self.key instead of self to speed up the frequent hashing
|
||||||
# when constructing an OrderedSet.
|
# when constructing an OrderedSet.
|
||||||
if '_ancestors' not in self.__dict__:
|
if '_ancestors' not in self.__dict__:
|
||||||
ancestors = deque([self.key])
|
ancestors = []
|
||||||
for parent in sorted(self.parents):
|
for parent in sorted(self.parents, reverse=True):
|
||||||
ancestors.extendleft(reversed(parent.ancestors()))
|
ancestors += parent.ancestors()
|
||||||
|
ancestors.append(self.key)
|
||||||
self.__dict__['_ancestors'] = list(OrderedSet(ancestors))
|
self.__dict__['_ancestors'] = list(OrderedSet(ancestors))
|
||||||
return self.__dict__['_ancestors']
|
return self.__dict__['_ancestors']
|
||||||
|
|
||||||
|
@ -68,9 +68,10 @@ class Node:
|
||||||
# Use self.key instead of self to speed up the frequent hashing
|
# Use self.key instead of self to speed up the frequent hashing
|
||||||
# when constructing an OrderedSet.
|
# when constructing an OrderedSet.
|
||||||
if '_descendants' not in self.__dict__:
|
if '_descendants' not in self.__dict__:
|
||||||
descendants = deque([self.key])
|
descendants = []
|
||||||
for child in sorted(self.children):
|
for child in sorted(self.children, reverse=True):
|
||||||
descendants.extendleft(reversed(child.descendants()))
|
descendants += child.descendants()
|
||||||
|
descendants.append(self.key)
|
||||||
self.__dict__['_descendants'] = list(OrderedSet(descendants))
|
self.__dict__['_descendants'] = list(OrderedSet(descendants))
|
||||||
return self.__dict__['_descendants']
|
return self.__dict__['_descendants']
|
||||||
|
|
||||||
|
@ -288,24 +289,13 @@ class MigrationGraph:
|
||||||
|
|
||||||
def iterative_dfs(self, start, forwards=True):
|
def iterative_dfs(self, start, forwards=True):
|
||||||
"""Iterative depth-first search for finding dependencies."""
|
"""Iterative depth-first search for finding dependencies."""
|
||||||
visited = deque()
|
visited = []
|
||||||
visited.append(start)
|
stack = [start]
|
||||||
if forwards:
|
|
||||||
stack = deque(sorted(start.parents))
|
|
||||||
else:
|
|
||||||
stack = deque(sorted(start.children))
|
|
||||||
while stack:
|
while stack:
|
||||||
node = stack.popleft()
|
node = stack.pop()
|
||||||
visited.appendleft(node)
|
visited.append(node)
|
||||||
if forwards:
|
stack += sorted(node.parents if forwards else node.children)
|
||||||
children = sorted(node.parents, reverse=True)
|
return list(OrderedSet(reversed(visited)))
|
||||||
else:
|
|
||||||
children = sorted(node.children, reverse=True)
|
|
||||||
# reverse sorting is needed because prepending using deque.extendleft
|
|
||||||
# also effectively reverses values
|
|
||||||
stack.extendleft(children)
|
|
||||||
|
|
||||||
return list(OrderedSet(visited))
|
|
||||||
|
|
||||||
def root_nodes(self, app=None):
|
def root_nodes(self, app=None):
|
||||||
"""
|
"""
|
||||||
|
|
Loading…
Reference in New Issue