MCPcopy
hub / github.com/sqlalchemy/sqlalchemy / sort_as_subsets

Function sort_as_subsets

lib/sqlalchemy/util/topological.py:30–55  ·  view source on GitHub ↗
(
    tuples: Collection[Tuple[_T, _T]], allitems: Collection[_T]
)

Source from the content-addressed store, hash-verified

28
29
30def sort_as_subsets(
31 tuples: Collection[Tuple[_T, _T]], allitems: Collection[_T]
32) -> Iterator[Sequence[_T]]:
33 edges: DefaultDict[_T, Set[_T]] = util.defaultdict(set)
34 for parent, child in tuples:
35 edges[child].add(parent)
36
37 todo = list(allitems)
38 todo_set = set(allitems)
39
40 while todo_set:
41 output = []
42 for node in todo:
43 if todo_set.isdisjoint(edges[node]):
44 output.append(node)
45
46 if not output:
47 raise CircularDependencyError(
48 "Circular dependency detected.",
49 find_cycles(tuples, allitems),
50 _gen_edges(edges),
51 )
52
53 todo_set.difference_update(output)
54 todo = [t for t in todo if t in todo_set]
55 yield output
56
57
58def sort(

Callers 1

sortFunction · 0.85

Calls 6

find_cyclesFunction · 0.85
_gen_edgesFunction · 0.85
addMethod · 0.45
appendMethod · 0.45
difference_updateMethod · 0.45

Tested by

no test coverage detected