o
    Jh	                     @   s   d dl mZ d dl mZ d dl mZ d dl mZ d dl mZ d dl mZ d dlmZ d dl	m
Z
 d d	l	mZ G d
d deZedZdee deeee f dee fddZdd ZdS )    )Set)Dict)Mapping)TypeVar)Iterable)
Collection)defaultdict)heappop)heappushc                   @   s   e Zd ZdZdS )
CycleErrorz
    Raised when cycles exist in the input graph.

    The second element in the args attribute of instances will contain the
    sequence of nodes in which the cycle lies.
    N)__name__
__module____qualname____doc__ r   r   V/var/www/html/pro-man-master/venv/lib/python3.10/site-packages/yoyo/topologicalsort.pyr      s    r   Titemsdependency_graphreturnc           	      #   s>   t t| }dd |D d}t tt}t }|r|t|t| kr,t|| t|\}}fdd||g D }|sud}| ||v rR|	| |V  |
|g D ] t fdd| D sst|   f q[n||v r~|d7 }nd}|| |D ]	 |  | q|s|rt|| d S d S )	Nc                 S   s   i | ]\}}||qS r   r   ).0ixitemr   r   r   
<dictcomp>   s    z$topological_sort.<locals>.<dictcomp>r   c                    s    h | ]}|vr| v r|qS r   r   )r   d)orderingoutputr   r   	<setcomp>+   s    z#topological_sort.<locals>.<setcomp>c                 3   s    | ]} |v V  qd S Nr   )r   other)br   r   	<genexpr>5   s    z#topological_sort.<locals>.<genexpr>   )list	enumeratesetr   lenraise_cycle_errorr	   getaddremovepopanyvaluesr
   )	r   r   pqueueseen_since_last_change
blocked_onblocked_nblockersr   )r    r   r   r   topological_sort   sF   




r5   c                    sp   t  fdd|D d }|rtd|dd |D }|j|   |r4td|tt| jdtd)	Nc                 3   s    | ]	}| vr|V  qd S r   r   )r   r   r   r   r   r!   D   s    z$raise_cycle_error.<locals>.<genexpr>z.Dependency graph contains a non-existent node c                 S   s   h | ]\}}|qS r   r   )r   r2   r3   r   r   r   r   G   s    z$raise_cycle_error.<locals>.<setcomp>z%Dependency graph loop detected among )keyz6raise_cycle_error called but no unresovled nodes exist)	next
ValueErrorupdater-   r   r#   sortedr(   AssertionError)r   r.   r0   bad
unresolvedr   r6   r   r'   C   s   r'   N)typingr   r   r   r   r   r   collectionsr   heapqr	   r
   r9   r   r   r5   r'   r   r   r   r   <module>   s&    	
+