U
    mÇf  ã                   @   s$   G d d„ de ƒZdd„ Zdd„ ZdS )c                   @   s   e Zd ZdS )ÚCyclicDependencyErrorN)Ú__name__Ú
__module__Ú__qualname__© r   r   úA/tmp/pip-unpacked-wheel-siwebuq3/django/utils/topological_sort.pyr      s   r   c                 #   sf   |   ¡ }|rbdd„ | ¡ D ƒ‰ ˆ sDtd d dd„ | ¡ D ƒ¡¡ƒ‚ˆ V  ‡ fdd„| ¡ D ƒ}qd	S )
ag  
    Variation of Kahn's algorithm (1962) that returns sets.

    Take a dependency graph as a dictionary of node => dependencies.

    Yield sets of items in topological order, where the first set contains
    all nodes without dependencies, and each following set contains all
    nodes that may depend on the nodes only in the previously yielded sets.
    c                 S   s   h | ]\}}|s|’qS r   r   )Ú.0ÚnodeÚdepsr   r   r   Ú	<setcomp>   s      z+topological_sort_as_sets.<locals>.<setcomp>zCyclic dependency in graph: {}z, c                 s   s   | ]}t |ƒV  qd S ©N)Úrepr)r   Úxr   r   r   Ú	<genexpr>   s     z+topological_sort_as_sets.<locals>.<genexpr>c                    s"   i | ]\}}|ˆ kr||ˆ  “qS r   r   )r   r   Zdependencies©Úcurrentr   r   Ú
<dictcomp>   s   þ z,topological_sort_as_sets.<locals>.<dictcomp>N)ÚcopyÚitemsr   ÚformatÚjoin)Údependency_graphÚtodor   r   r   Útopological_sort_as_sets   s    
ÿÿ
þr   c                 C   s2   g }t |ƒD ] }| D ]}||kr| |¡ qq|S r   )r   Úappend)Znodesr   ÚresultZlayerr   r   r   r   Ústable_topological_sort$   s    r   N)Ú
ValueErrorr   r   r   r   r   r   r   Ú<module>   s   