U
    [¢h+  ã                   @   sL   d Z ddlmZ ddlmZ dddg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 | ]}|ˆ kr|‘qS © r   )Ú.0Út©Ztodo_setr   ú?/tmp/pip-unpacked-wheel-joqu7d3y/sqlalchemy/util/topological.pyÚ
<listcomp>'   s      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&    
ý
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    c                 C   sÄ   t  t¡}| D ]\}}||  |¡ qt|ƒ}tƒ }|D ]†}|g}| |¡}	|r8|d }
||
 D ]R}||kr’|| |¡d … }|	 |¡ | |¡ ||	k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*    





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   s     
  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   

(