a
    xˆc+  ã                   @   sJ   d Z ddlmZ ddlmZ g d¢Zdd„ Zddd	„Zd
d„ Zdd„ Z	dS )zTopological sorting algorithms.é   )Úutil)ÚCircularDependencyError)ÚsortÚsort_as_subsetsÚfind_cyclesc                 #   s¢   t  t¡}| D ]\}}||  |¡ qt|ƒ}t|ƒ‰ ˆ ržg }|D ]}ˆ  || ¡rB| |¡ qB|sztdt| |ƒt	|ƒƒ‚ˆ  
|¡ ‡ fdd„|D ƒ}|V  q6d S )NzCircular dependency detected.c                    s   g | ]}|ˆ v r|‘qS © r   )Ú.0Út©Ztodo_setr   ú^/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/sqlalchemy/util/topological.pyÚ
<listcomp>'   ó    z#sort_as_subsets.<locals>.<listcomp>)r   ÚdefaultdictÚsetÚaddÚlistÚ
isdisjointÚappendr   r   Ú
_gen_edgesÚdifference_update)ÚtuplesÚallitemsÚedgesÚparentÚchildÚtodoÚoutputÚnoder   r
   r   r      s&    
ý
r   Tc                 c   s$   t | |ƒD ]}|D ]
}|V  qq
dS )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   )r   r   Zdeterministic_orderÚset_Úsr   r   r   r   +   s    r   c                 C   sÄ   t  t¡}| D ]\}}||  |¡ qt|ƒ}tƒ }|D ]†}|g}| |¡}	|r8|d }
||
 D ]R}||v r’|| |¡d … }|	 |¡ | |¡ ||	v r`| |¡ |	 	|¡  qLq`| 
¡ }qLq8|S )Néÿÿÿÿ)r   r   r   r   Ú
differenceÚindexr   Úupdater   ÚremoveÚpop)r   r   r   r   r   Znodes_to_testr   r   Ústackr   ÚtopZcycr   r   r   r   ;   s*    





r   c                    s   t ‡ fdd„ˆ D ƒƒS )Nc                    s"   g | ]}ˆ | D ]}||f‘qqS r   r   )r   ÚleftÚright©r   r   r   r   d   r   z_gen_edges.<locals>.<listcomp>)r   r*   r   r*   r   r   c   s    r   N)T)
Ú__doc__Ú r   Úexcr   Ú__all__r   r   r   r   r   r   r   r   Ú<module>   s   
(