Skip to content

Commit

Permalink
refactor completely duplicated numpreds/successor processing
Browse files Browse the repository at this point in the history
  • Loading branch information
hexylena committed Sep 3, 2015
1 parent 1aa7ef1 commit a437083
Showing 1 changed file with 7 additions and 23 deletions.
30 changes: 7 additions & 23 deletions lib/galaxy/util/topsort.py
Expand Up @@ -129,8 +129,7 @@ def pick_a_cycle(self):
answer.reverse()
return answer


def topsort(pairlist):
def _numpreds_and_successors_from_pairlist(pairlist):
numpreds = OrderedDict() # elt -> # of predecessors
successors = OrderedDict() # elt -> list of successors
for first, second in pairlist:
Expand All @@ -152,6 +151,11 @@ def topsort(pairlist):
successors[first].append(second)
else:
successors[first] = [second]
return numpreds, successors


def topsort(pairlist):
numpreds, successors = _numpreds_and_successors_from_pairlist(pairlist)

# suck up everything without a predecessor
answer = filter(lambda x, numpreds=numpreds: numpreds[x] == 0,
Expand Down Expand Up @@ -182,27 +186,7 @@ def topsort(pairlist):


def topsort_levels(pairlist):
numpreds = OrderedDict() # elt -> # of predecessors
successors = OrderedDict() # elt -> list of successors
for first, second in pairlist:
# make sure every elt is a key in numpreds
if first not in numpreds:
numpreds[first] = 0
if second not in numpreds:
numpreds[second] = 0

# if they're the same, there's no real dependence
if first == second:
continue

# since first < second, second gains a pred ...
numpreds[second] = numpreds[second] + 1

# ... and first gains a succ
if first in successors:
successors[first].append(second)
else:
successors[first] = [second]
numpreds, successors = _numpreds_and_successors_from_pairlist(pairlist)

answer = []

Expand Down

0 comments on commit a437083

Please sign in to comment.