B
    ‰KLc+  ã               @   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¡}x| D ]\}}||  |¡ qW t|ƒ}t|ƒ‰ xnˆ r¨g }x$|D ]}ˆ  || ¡rJ| |¡ qJW |s„tdt| |ƒt	|ƒƒ‚ˆ  
|¡ ‡ fdd„|D ƒ}|V  q<W d S )NzCircular dependency detected.c                s   g | ]}|ˆ kr|‘qS © r   )Ú.0Út)Útodo_setr   ú:lib/python3.7/site-packages/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,   x&t | |ƒD ]}x|D ]
}|V  qW qW 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_orderZset_Úsr   r   r   r   +   s    
c             C   sÒ   t  t¡}x| D ]\}}||  |¡ qW t|ƒ}tƒ }x”|D ]Œ}|g}| |¡}	xv|rÈ|d }
xd||
 D ]P}||krœ|| |¡d … }|	 |¡ | |¡ ||	krj| |¡ |	 	|¡ P qjW | 
¡ }qTW q>W |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   

(