
    Yh                     ,    d dl mZ  G d d          ZdS )    )dequec                       e Zd ZdZd Zd Zd Zd Zd Ze	d             Z
e	d             Zd	 Zd
 Zdedee         fdZdedee         fdZdedefdZdedee         fdZdefdZdS )DiGraphzReally simple unweighted directed graph data structure to track dependencies.

    The API is pretty much the same as networkx so if you add something just
    copy their API.
    c                 L    i | _         i | _        i | _        i | _        d| _        d S )Nr   )_node_succ_pred_node_order_insertion_idxselfs    h/var/www/tools.fuzzalab.pt/emblema-extractor/venv/lib/python3.11/site-packages/torch/package/_digraph.py__init__zDiGraph.__init__   s1    
 

     c                     || j         vr?|| j         |<   i | j        |<   i | j        |<   | j        | j        |<   | xj        dz  c_        dS | j         |                             |           dS )zAdd a node to the graph.

        Args:
            n: the node. Can we any object that is a valid dict key.
            **kwargs: any attributes you want to attach to the node.
           N)r   r   r	   r   r
   update)r   nkwargss      r   add_nodezDiGraph.add_node   s|     DJ"DJqMDJqMDJqM"&"5DQ1$JqM  (((((r   c                     |                      |           |                      |           d| j        |         |<   d| j        |         |<   dS )zAdd an edge to graph between nodes ``u`` and ``v``

        ``u`` and ``v`` will be created if they do not already exist.
        TN)r   r   r	   )r   uvs      r   add_edgezDiGraph.add_edge*   sP     	aa  
1a
1ar   c                     	 t          | j        |                   S # t          $ r}t          d| d          |d}~ww xY w)z.Returns an iterator over successor nodes of n.	The node  is not in the digraph.N)iterr   KeyError
ValueErrorr   r   es      r   
successorszDiGraph.successors7   [    	L
1&&& 	L 	L 	LCCCCDD!K	L    
?:?c                     	 t          | j        |                   S # t          $ r}t          d| d          |d}~ww xY w)z1Returns an iterator over predecessors nodes of n.r   r   N)r   r	   r   r    r!   s      r   predecessorszDiGraph.predecessors>   r$   r%   c              #   ^   K   | j                                         D ]\  }}|D ]}||fV  	dS )z6Returns an iterator over all edges (u, v) in the graphN)r   items)r   r   r#   succs       r   edgeszDiGraph.edgesE   sV       "Z--// 	 	MAz"  g	 	r   c                     | j         S )z6Returns a dictionary of all nodes to their attributes.)r   r   s    r   nodeszDiGraph.nodesL   s     zr   c                 *    t          | j                  S )zIterate over the nodes.)r   r   r   s    r   __iter__zDiGraph.__iter__Q   s    DJr   c                 8    	 || j         v S # t          $ r Y dS w xY w)z>Returns True if ``n`` is a node in the graph, False otherwise.F)r   	TypeError)r   r   s     r   __contains__zDiGraph.__contains__U   s5    	
?" 	 	 	55	s    
srcreturnc                 B   t          |          }t          |          }t          |          dk    rm|                                }|                     |          D ]0}||vr*|                    |           |                    |           1t          |          dk    m|S )z2Returns a set of nodes that are reachable from srcr   )setr   lenpopleftr#   addappendr   r3   resultworking_setcurr   s         r   forward_transitive_closurez"DiGraph.forward_transitive_closure\   s     SCjj+""%%''C__S)) * *F??JJqMMM&&q))) +"" r   c                 B   t          |          }t          |          }t          |          dk    rm|                                }|                     |          D ]0}||vr*|                    |           |                    |           1t          |          dk    m|S )zGReturns a set of nodes that are reachable from src in reverse directionr   )r6   r   r7   r8   r'   r9   r:   r;   s         r   backward_transitive_closurez#DiGraph.backward_transitive_closurei   s     SCjj+""%%''C&&s++ * *F??JJqMMM&&q))) +"" r   dstc                    t                      }|                     |          }||vr|S t          |          }t          |          dk    rn|                                }|                     |          D ]1}||v r+|                    ||           |                    |           2t          |          dk    n|                                S )zAReturns a subgraph rooted at src that shows all the paths to dst.r   )	r   r?   r   r7   r8   r'   r   r:   to_dot)r   r3   rB   result_graphforward_reachable_from_srcr=   r>   r   s           r   	all_pathszDiGraph.all_pathsv   s     yy%)%D%DS%I%I"000
 Cjj+""%%''C&&s++ * *222 ))!S111&&q))) +"" ""$$$r   c                    g }|rk|                     |           | j        |                                         }d\  }}|D ]-}| j                            |d          }| n|||k     r|}|}.|kt          t          |                    S )z_Returns a list of nodes that show the first path that resulted in dst being added to the graph.) NN)r:   r	   keysr
   getlistreversed)r   rB   path
candidatesmin_idx	candidateidxs          r   
first_pathzDiGraph.first_path   s     
	$KKC--//J#LC' $ $	&**9d;;;E?cGmm!G#C  
	$ HTNN###r   c                 V    d                     d | j        D                       }d| dS )zvReturns the dot representation of the graph.

        Returns:
            A dot representation of the graph.
        
c              3   .   K   | ]\  }}d | d| dV  dS )"z" -> "z";N ).0fts      r   	<genexpr>z!DiGraph.to_dot.<locals>.<genexpr>   s:      DDA,a,,q,,,DDDDDDr   z,digraph G {
rankdir = LR;
node [shape=box];
z
}
)joinr+   )r   r+   s     r   rD   zDiGraph.to_dot   sD     		DDDDDDD 	   	r   N)__name__
__module____qualname____doc__r   r   r   r#   r'   propertyr+   r-   r/   r2   strr6   r?   rA   rG   rL   rS   rD   rX   r   r   r   r      sr             ) ) )      L L LL L L   X   X       c c#h    s s3x    %S %s % % % %0$c $d3i $ $ $ $$      r   r   N)collectionsr   r   rX   r   r   <module>re      sS         h h h h h h h h h hr   