o znh @sLdZddlmZddlmZgdZdddZddd Zd d Zd d Z dS)zTopological sorting algorithms.)util)CircularDependencyError)sortsort_as_subsets find_cyclesFc cstt}|D] \}}|||q|rtjnt}||}|rN|}|D]} ||| r4|| q&|sBtdt||t|| ||V|s!dSdS)NzCircular dependency detected.) r defaultdictsetadd OrderedSet isdisjointrr _gen_edgesdifference_update) tuplesallitemsdeterministic_orderedgesparentchildSettodooutputnoderF/usr/local/lib/python3.10/dist-packages/sqlalchemy/util/topological.pyrs*     rccs(t|||D] }|D]}|Vq qdS)zsort the given list of items by dependency. 'tuples' is a list of tuples representing a partial ordering. 'deterministic_order' keeps items within a dependency tier in list order. N)r)rrrset_srrrr,s rc Cstt}|D] \}}|||qt|}t}|D]D}|g}||} |r`|d} || D])}||vrI|||d} | | || || vrY||| |nq0| }|s(q|S)N) rrrr differenceindexr updateappendremovepop) rrrrr nodes_to_testrrstackrtopcycrrrr8s0         rcstfddDS)Ncs"g|] }|D]}||fqqSrr).0leftrightrrr as"z_gen_edges..)rr*rr*rr `sr N)F) __doc__rexcr__all__rrrr rrrrs     (