U
    yhD                     @   s   d dl mZmZ d dlmZ ddlZddddd	d
ddgZG dd deZdd Z	dd Z
dd Zdd	 Zdd
 ZefddZdd ZdS )   )	_toposortgroupby)
isvariadic    NAmbiguityWarning
supercedes
consistent	ambiguousambiguitiessuper_signatureedgeorderingc                   @   s   e Zd ZdS )r   N)__name__
__module____qualname__ r   r   m/var/www/html/venv/lib/python3.8/site-packages/torch/fx/experimental/unification/multipledispatch/conflict.pyr   	   s   c                 C   sB  t | t |k r.|  o,t |dko,t|d S t | t |krNttt| |S d}d}|t | k r |t |k r | | }|| }t|st|st||sdS |d7 }|d7 }qVt|r|t | d kst|t |d kot||S t|rV|t |d kstt||sdS |d7 }qV|t |d ko<|t | kS dS )z3 A is consistent and strictly more specific than B r   r   FN)lenr   allmap
issubclassAssertionErrorabp1Zp2Zcur_aZcur_br   r   r   r      s.    


c                 C   s  | s| pt |d S |s,|  p*t | d S t| t|krTtdd t| |D S d}d}|t| k r|t|k r| | }|| }t||st||sdS t |st |s|d7 }|d7 }q\t |r|d7 }q\t |r\|d7 }q\t |r|t|kpt |o|t| kS dS )z= It is possible for an argument list to satisfy both A and B r   c                 s   s&   | ]\}}t ||pt ||V  qd S N)r   ).0ZaaZbbr   r   r   	<genexpr>6   s   zconsistent.<locals>.<genexpr>Fr   N)r   r   r   zipr   r   r   r   r   r   +   s2    


c                 C   s    t | |ot| |pt||  S )z> A is consistent with B but neither is strictly more specific )r   r   r   r   r   r   r   r	   M   s    c                    s    t tt   fdd D S )z5 All signature pairs such that A is ambiguous with B c                    sP   h | ]H D ]>t  t k rt rt fd dD s fqqS )c                 3   s"   | ]}t | ot |V  qd S r   r   )r   cr!   r   r   r   X   s   z(ambiguities.<locals>.<setcomp>.<genexpr>)hashr	   anyr   
signaturesr!   r   	<setcomp>U   s      
zambiguities.<locals>.<setcomp>)listr   tupler'   r   r'   r   r
   R   s    c                    s<   t d  t fddD s&tfddt D S )z* A signature that would break ambiguities r   c                 3   s   | ]}t | kV  qd S r   )r   )r   s)nr   r   r   _   s     z"super_signature.<locals>.<genexpr>c                    s*   g | ]" t  fd dD tdd qS )c                 3   s   | ]}t |  V  qd S r   )typemro)r   sigir   r   r   a   s     z-super_signature.<locals>.<listcomp>.<genexpr>)keyr   )maxr   r&   r'   r1   r   
<listcomp>a   s   z#super_signature.<locals>.<listcomp>)r   r   r   ranger'   r   )r-   r(   r   r   \   s
    
c                 C   s&   t | |o$t ||  p$|| ||kS )zV A should be checked before B
    Tie broken by tie_breaker, defaults to ``hash``
    r"   )r   r   Ztie_breakerr   r   r   r   e   s    c                    sd   t tt   fdd D }ttd|} D ]}||kr4g ||< q4dd | D }t|S )z A sane ordering of signatures to check, first to last
    Topological sort of edges as given by ``edge`` and ``supercedes``
    c                    s(   g | ] } D ]}t ||r||fqqS r   )r   r   r   r   r'   r   r   r5   s   s
       
 zordering.<locals>.<listcomp>r   c                 S   s    i | ]\}}|d d |D qS )c                 S   s   g | ]\}}|qS r   r   r7   r   r   r   r5   x   s     z'ordering.<locals>.<dictcomp>.<listcomp>r   )r   kvr   r   r   
<dictcomp>x   s      zordering.<locals>.<dictcomp>)r*   r   r+   r   operator
itemgetteritemsr   )r(   edgesr,   r   r'   r   r   n   s    
)utilsr   r   Zvariadicr   r;   __all__Warningr   r   r   r	   r
   r   r$   r   r   r   r   r   r   <module>   s    "
		