def _topo_reorder(entries, get_parents=(lambda commit: commit.parents)):
 todo = collections.deque()
 pending = {}
 num_children = defaultdict(int)
 for entry in entries:
  todo.append(entry)
  for p in get_parents(entry.commit):
   num_children[p] += 1
 while todo:
  entry = todo.popleft()
  commit = entry.commit
  commit_id = commit.id
  if num_children[commit_id]:
   pending[commit_id] = entry
   continue
  for parent_id in get_parents(commit):
   num_children[parent_id] -= 1
   if (not num_children[parent_id]):
    parent_entry = pending.pop(parent_id, None)
    if parent_entry:
     todo.appendleft(parent_entry)
  (yield entry)

def _topo_reorder ( entries , get_parents = ( lambda commit : commit . parents ) ) : todo = collections . deque ( ) pending = { } num_children = defaultdict ( int ) for entry in entries : todo . append ( entry ) for p in get_parents ( entry . commit ) : num_children [ p ] += 1 while todo : entry = todo . popleft ( ) commit = entry . commit commit_id = commit . id if num_children [ commit_id ] : pending [ commit_id ] = entry continue for parent_id in get_parents ( commit ) : num_children [ parent_id ] -= 1 if ( not num_children [ parent_id ] ) : parent_entry = pending . pop ( parent_id , None ) if parent_entry : todo . appendleft ( parent_entry ) ( yield entry )
reorder an iterable of entries topologically .