U kf @sdZddlmZddlmZddlmZddlmZddlmZddlmZddlm Z dd lm Z dd lm Z dd lm Z d d l mZd dlmZe dedZdddgZddddddZd(dddddddZddd dd!dZd"d#d$d%d&Zd'S))zTopological sorting algorithms.) annotations)Any) Collection) DefaultDict)Iterable)Iterator)Sequence)Set)Tuple)TypeVar)util)CircularDependencyError_T)boundsortsort_as_subsets find_cycleszCollection[Tuple[_T, _T]]zCollection[_T]zIterator[Sequence[_T]])tuplesallitemsreturnc#stt}|D]\}}|||qt|}t|rg}|D]}||rB||qB|sztdt||t | |fdd|D}|Vq6dS)NzCircular dependency detected.csg|]}|kr|qSr).0tZtodo_setrK/opt/hc_python/lib64/python3.8/site-packages/sqlalchemy/util/topological.py 6sz#sort_as_subsets..) r defaultdictsetaddlist isdisjointappendrr _gen_edgesdifference_update)rredgesparentchildtodooutputnoderrrrs&    Tboolz Iterator[_T])rrdeterministic_orderrccst||D]}|EdHq 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)rrr,set_rrrr:szIterable[Tuple[_T, _T]]z Iterable[_T]zSet[_T]c Cstt}|D]\}}|||qt|}t}|D]}|g}||} |r8|d} || D]R}||kr|||d} | | || || kr`||| |qLq`| }qLq8|S)N) r rrr differenceindexr$updater"removepop) rrr%r&r'Z nodes_to_testr)r*stackr(topZcycrrrrMs*          zDefaultDict[_T, Set[_T]]zSet[Tuple[_T, _T]])r%rcsfddDS)Ncs"h|]}|D] }||fqqSrr)rleftrightr%rr xs z_gen_edges..rr8rr8rr#wsr#N)T)__doc__ __future__rtypingrrrrrrr r r r excrr__all__rrrr#rrrrs&              *