
    iw                        d Z ddlmZ ddlmZmZ ddlmZ  ed          Z	ddZ
ddZ G d deee	                            ZdS )z&Helpers for manipulations with graphs.    )annotations)IteratorSet)TypeVarTverticesAbstractSet[T]edgesdict[T, list[T]]returnIterator[set[T]]c              #     K   t                      g i g dfd| D ]}|vr |          E d{V  dS )a  Compute Strongly Connected Components of a directed graph.

    Args:
      vertices: the labels for the vertices
      edges: for each vertex, gives the target vertices of its outgoing edges

    Returns:
      An iterator yielding strongly connected components, each
      represented as a set of vertices.  Each input vertex will occur
      exactly once; vertices not part of a SCC are returned as
      singleton sets.

    From https://code.activestate.com/recipes/578507/.
    vr   r   r   c              3    K   t                    | <                       |                                |                     |          D ]T}|vr |          E d {V  |vr8|         d         k     r&                                 |         d         k     &Ud         |          k    rW                                 t          |          d                    }|          d =                     |           |V  d S d S )N)lenappendpopsetupdate)	r   wscc
boundariesdfsr
   
identifiedindexstacks	      O/root/projects/qq-shell/venv/lib64/python3.11/site-packages/mypy/graph_utils.pyr   z*strongly_connected_components.<locals>.dfs!   s8     u::aQ%(###q 	% 	%A~~3q66!!!!!!!!*$$AhB//NN$$$ AhB// b>U1X%%NNeE!HJJ'((CeAhjj!c"""IIIII &%    N)r   r   r   r   )r   )r   r
   r   r   r   r   r   r   s    ` @@@@@r   strongly_connected_componentsr       s      " JEEJ          &   E>>s1vv r   sccslist[set[T]])dict[AbstractSet[T], set[AbstractSet[T]]]c                    i | D ]}t          |          }|D ]}||<   i }| D ]N}t                      }|D ])}|                    fd||         D                        *||t          |          <   O|S )zLUse original edges to organize SCCs in a graph by dependencies between them.c              3  (   K   | ]}|         V  d S N ).0xsccsmaps     r   	<genexpr>zprepare_sccs.<locals>.<genexpr>F   s'      55q
555555r   )	frozensetr   r   )r!   r
   r   
scc_frozenr   datadepsr*   s          @r   prepare_sccsr0   9   s     G $ $s^^
 	$ 	$A#GAJJ	$68D $ $$'EE 	6 	6AKK5555E!H5555555#Ys^^Kr   c                  *    e Zd ZdZddZddZdd
ZdS )topsorta  Topological sort using Kahn's algorithm.

    Uses in-degree counters and a reverse adjacency list, so the total work
    is O(V + E).

    Implemented as a class rather than a generator for better mypyc
    compilation.

    Args:
      data: A map from vertices to all vertices that it has an edge
            connecting it to. NOTE: dependency sets in this data
            structure are modified in place to remove self-dependencies.
            Orphans are handled internally and are not added to `data`.

    Returns:
      An iterator yielding sets of vertices that have an equivalent
      ordering.

    Example:
      Suppose the input has the following structure:

        {A: {B, C}, B: {D}, C: {D}}

      The algorithm treats orphan dependencies as if normalized to:

        {A: {B, C}, B: {D}, C: {D}, D: {}}

      It will yield the following values:

        {D}
        {B, C}
        {A}
    r.   dict[T, set[T]]r   Nonec                   i }i }t                      }|                                D ]\  }}|                    |           t          |          }|||<   |dk    r|                    |           ||vrg ||<   |D ]F}||v r||                             |           "|g||<   ||vrd||<   |                    |           G|| _        || _        || _        t          |          t          |          z
  | _	        d S )Nr   )
r   itemsdiscardr   addr   	in_degreerevready	remaining)	selfr.   r9   r:   r;   itemr/   degdeps	            r   __init__ztopsort.__init__n   s    #%	 "**,, 	' 	'JD$LLd))C!IdOaxx		$3D	 ' '#::HOOD)))) $vCH$)*	#		#' #
Y#e**4r   r   c                    | S r&   r'   )r=   s    r   __iter__ztopsort.__iter__   s    r   set[T]c                   | j         }|s@| j        dk    s.J dd | j                                        D                         t          | j        }| j        }t                      }|D ]8}||         D ]-}||         dz
  }|||<   |dk    r|                    |           .9| xj        t          |          z  c_        || _         |S )Nr   z#A cyclic dependency exists amongst c                $    g | ]\  }}|d k    |S )r   r'   )r(   kr?   s      r   
<listcomp>z$topsort.__next__.<locals>.<listcomp>   s!    FFF&!ScAggAgggr      )	r;   r<   r9   r6   StopIterationr:   r   r8   r   )r=   r;   r9   r:   	new_readyr>   	dependentnew_degs           r   __next__ztopsort.__next__   s    
 	 >Q&&&KFFDN$8$8$:$:FFFK K '&&  N	hEE	 	- 	-D Y - -	#I.2'.	)$a<<MM),,,	-
 	#i..(
r   N)r.   r3   r   r4   )r   r   )r   rD   )__name__
__module____qualname____doc__rA   rC   rN   r'   r   r   r2   r2   K   s[           D5 5 5 5:        r   r2   N)r   r	   r
   r   r   r   )r!   r"   r
   r   r   r#   )rR   
__future__r   collections.abcr   r   AbstractSettypingr   r   r    r0   r   r2   r'   r   r   <module>rW      s    , , " " " " " " 8 8 8 8 8 8 8 8      GCLL+ + + +\   $V V V V Vhs1v V V V V Vr   