U
    *Ç-eV  ã                   @   sL   d dddddgZ dd „ Zdd„ Zdd„ Zd	d
„ Zdd„ Zdd„ Zdd„ ZdS )ÚhashableÚtransitive_getÚraisesÚreverse_dictÚxfailÚfreezec                 C   s*   zt | ƒ W dS  tk
r$   Y dS X d S )NTF)ÚhashÚ	TypeError)Úx© r
   úh/var/www/html/Darija-Ai-Train/env/lib/python3.8/site-packages/torch/fx/experimental/unification/utils.pyr      s
    c                 C   s   t | ƒr| |kr||  } q | S )zr Transitive dict.get
    >>> d = {1: 2, 2: 3, 3: 4}
    >>> d.get(1)
    2
    >>> transitive_get(1, d)
    4
    )r   )ÚkeyÚdr
   r
   r   r   
   s    
c                 C   s(   z|ƒ  W dS  | k
r"   Y dS X d S )NFTr
   )ÚerrZlamdar
   r
   r   r      s
    c                    s¬   t | ƒ‰ dd„ ˆ  ¡ D ƒ‰ ‡ fdd„| D ƒ}g }|rŠ| ¡ }| |¡ |  |d¡D ]4}|ˆ | ksft‚ˆ |  |¡ ˆ | sR| |¡ qRq0t‡ fdd„| D ƒƒr¨t	dƒ‚|S )	a   Topological sort algorithm by Kahn [1] - O(nodes + vertices)
    inputs:
        edges - a dict of the form {a: {b, c}} where b and c depend on a
    outputs:
        L - an ordered list of nodes that satisfy the dependencies of edges
    >>> # xdoctest: +SKIP
    >>> _toposort({1: (2, 3), 2: (3, )})
    [1, 2, 3]
    Closely follows the wikipedia page [2]
    [1] Kahn, Arthur B. (1962), "Topological sorting of large networks",
    Communications of the ACM
    [2] http://en.wikipedia.org/wiki/Toposort#Algorithms
    c                 S   s   i | ]\}}|t |ƒ“qS r
   )Úset)Ú.0ÚkÚvalr
   r
   r   Ú
<dictcomp>0   s      z_toposort.<locals>.<dictcomp>c                    s   h | ]}|ˆ kr|’qS r
   r
   ©r   Úv©Zincoming_edgesr
   r   Ú	<setcomp>1   s      z_toposort.<locals>.<setcomp>r
   c                 3   s   | ]}ˆ   |d ¡V  qd S )N)Úgetr   r   r
   r   Ú	<genexpr><   s     z_toposort.<locals>.<genexpr>zInput has cycles)
r   ÚitemsÚpopÚappendr   ÚAssertionErrorÚremoveÚaddÚanyÚ
ValueError)ÚedgesÚSÚLÚnÚmr
   r   r   Ú	_toposort!   s    
r'   c                 C   s8   i }| D ]*}| | D ]}|  |tƒ ¡|f ||< qq|S )a˜  Reverses direction of dependence dict
    >>> d = {'a': (1, 2), 'b': (2, 3), 'c':()}
    >>> reverse_dict(d)  # doctest: +SKIP
    {1: ('a',), 2: ('a', 'b'), 3: ('b',)}
    :note: dict order are not deterministic. As we iterate on the
        input dict, it make the output of this function depend on the
        dict order. So this function output order should be considered
        as undeterministic.
    )r   Útuple)r   Úresultr   r   r
   r
   r   r   A   s
    
c                 C   s,   z| ƒ  t dƒ‚W n t k
r&   Y nX d S )NzXFailed test passed)Ú	Exception)Úfuncr
   r
   r   r   R   s
    c                 C   sT   t | tƒrttt|  ¡ ƒƒS t | tƒr4ttt| ƒƒS t | ttfƒrPttt| ƒƒS | S )z¡ Freeze container to hashable form
    >>> freeze(1)
    1
    >>> freeze([1, 2])
    (1, 2)
    >>> freeze({1: 2}) # doctest: +SKIP
    frozenset([(1, 2)])
    )	Ú
isinstanceÚdictÚ	frozensetÚmapr   r   r   r(   Úlist)r   r
   r
   r   r   Z   s    	

N)Ú__all__r   r   r   r'   r   r   r   r
   r
   r
   r   Ú<module>   s   
 