U a @sLdZddlmZddlmZdddgZddZdd dZd dZd d Z d S)zTopological sorting algorithms.)util)CircularDependencyErrorsortsort_as_subsets find_cyclesc#stt}|D]\}}|||qt|}t|rg}|D]}||rB||qB|sztdt||t | |fdd|D}|Vq6dS)NzCircular dependency detected.csg|]}|kr|qSr).0tZtodo_setr\C:\Users\vtejo\AppData\Local\Temp\pip-unpacked-wheel-nyjtotrf\sqlalchemy\util\topological.py 'sz#sort_as_subsets..) r defaultdictsetaddlist isdisjointappendrr _gen_edgesdifference_update)tuplesallitemsedgesparentchildtodooutputnoderr r rs&    Tccs$t||D]}|D] }|Vqq dS)a,sort the given list of items by dependency. 'tuples' is a list of tuples representing a partial ordering. deterministic_order is no longer used, the order is now always deterministic given the order of "allitems". the flag is there for backwards compatibility with Alembic. N)r)rrZdeterministic_orderset_srrr r+s c Cstt}|D]\}}|||qt|}t}|D]}|g}||} |r8|d} || D]R}||kr|||d} | | || || kr`||| |qLq`| }qLq8|S)N) rr rr differenceindexrupdaterremovepop) rrrrrZ nodes_to_testrrstackrtopZcycrrr r;s*          cstfddDS)Ncs"g|]}|D] }||fqqSrr)rleftrightrrr r ds z_gen_edges..)rr)rr)r rcsrN)T) __doc__rexcr__all__rrrrrrrr s    (