U
    9%e%                     @  s   d dl mZ d dlZd dlmZ d dlmZ d dlmZmZ d dl	m
Z
 d dlmZ eZddd	d
gZG dd dejZG dd	 d	eZeZG dd dZdS )    )annotationsN)ref)_IterationGuard)MutableMappingMapping)TensorTensorWeakRef	WeakIdRefWeakIdKeyDictionaryWeakTensorKeyDictionaryc                      s@   e Zd ZdgZd fdd	Z fddZdd Zd	d
 Z  ZS )r	   _idNc                   s   t || _t || d S N)idr   super__init__)selfkeycallback	__class__ O/var/www/html/Darija-Ai-API/env/lib/python3.8/site-packages/torch/utils/weak.pyr   -   s    
zWeakIdRef.__init__c                   s    t   }t|dr|  |S )N_fix_weakref)r   __call__hasattrr   )r   rr   r   r   r   6   s    

zWeakIdRef.__call__c                 C  s   | j S r   )r   r   r   r   r   __hash__=   s    zWeakIdRef.__hash__c                 C  s,   |  }| }|d k	r$|d k	r$||kS | |kS r   r   )r   otherabr   r   r   __eq__@   s
    zWeakIdRef.__eq__)N)	__name__
__module____qualname__	__slots__r   r   r   r!   __classcell__r   r   r   r   r	   *   s
   	c                   @  s   e Zd ZU ded< d4ddZdd Zdd	 Zd
d Zdd Zdd Z	dd Z
dd Zdd ZeZdd Zd5ddZdd Zdd Zdd ZeZd d! Zd"d# Zd$d% Zd&d' Zd6d(d)Zd7d*d+Zd,d- Zd.d/ Zd0d1 Zd2d3 ZdS )8r
   zdict[WeakIdRef, object]dataNc                 C  sF   i | _ t| fdd}|| _g | _t | _d| _|d k	rB| | d S )Nc                 S  sH   | }|d k	rD|j r"|j|  n"z|j| = W n tk
rB   Y nX d S r   )
_iterating_pending_removalsappendr'   KeyError)kselfrefr   r   r   r   removeZ   s    z,WeakIdKeyDictionary.__init__.<locals>.removeF)r'   r   _remover)   setr(   
_dirty_lenupdate)r   dictr.   r   r   r   r   W   s    
zWeakIdKeyDictionary.__init__c                 C  sV   | j j}| j}z
| }W n tk
r.   Y d S X z
||= W q tk
rN   Y qX qd S r   )r)   popr'   
IndexErrorr+   )r   r4   dr   r   r   r   _commit_removalsl   s    

z$WeakIdKeyDictionary._commit_removalsc                   s&   | j   fdd| jD | _d| _d S )Nc                   s   g | ]}| kr|qS r   r   ).0r,   r6   r   r   
<listcomp>   s      z7WeakIdKeyDictionary._scrub_removals.<locals>.<listcomp>F)r'   r)   r1   r   r   r9   r   _scrub_removals~   s    z#WeakIdKeyDictionary._scrub_removalsc                 C  s   d| _ | jt|= d S NT)r1   r'   r	   r   r   r   r   r   __delitem__   s    zWeakIdKeyDictionary.__delitem__c                 C  s   | j t| S r   )r'   r	   r=   r   r   r   __getitem__   s    zWeakIdKeyDictionary.__getitem__c                 C  s(   | j r| jr|   t| jt| j S r   )r1   r)   r;   lenr'   r   r   r   r   __len__   s    zWeakIdKeyDictionary.__len__c                 C  s   d| j j dt| ddS )N<z at z#x>)r   r"   r   r   r   r   r   __repr__   s    zWeakIdKeyDictionary.__repr__c                 C  s   || j t|| j< d S r   )r'   r	   r/   )r   r   valuer   r   r   __setitem__   s    zWeakIdKeyDictionary.__setitem__c              	   C  sH   t  }t| 0 | j D ]\}}| }|d k	r|||< qW 5 Q R X |S r   )r
   r   r'   items)r   newr   rE   or   r   r   copy   s    
zWeakIdKeyDictionary.copyc              	   C  s\   ddl m} |  }t| 6 | j D ]$\}}| }|d k	r(|||||< q(W 5 Q R X |S )Nr   )deepcopy)rJ   rK   r   r   r'   rG   )r   memorK   rH   r   rE   rI   r   r   r   __deepcopy__   s    
z WeakIdKeyDictionary.__deepcopy__c                 C  s   | j t||S r   )r'   getr	   r   r   defaultr   r   r   rN      s    zWeakIdKeyDictionary.getc                 C  s.   zt |}W n tk
r"   Y dS X || jkS )NF)r	   	TypeErrorr'   )r   r   wrr   r   r   __contains__   s
    z WeakIdKeyDictionary.__contains__c              	   c  sD   t | 2 | j D ] \}}| }|d k	r||fV  qW 5 Q R X d S r   r   r'   rG   )r   rR   rE   r   r   r   r   rG      s
    
zWeakIdKeyDictionary.itemsc              	   c  s8   t | & | jD ]}| }|d k	r|V  qW 5 Q R X d S r   )r   r'   )r   rR   objr   r   r   keys   s
    

zWeakIdKeyDictionary.keysc              	   c  s<   t | * | j D ]\}}| d k	r|V  qW 5 Q R X d S r   rT   )r   rR   rE   r   r   r   values   s    

zWeakIdKeyDictionary.valuesc                 C  s
   t | jS )az  Return a list of weak references to the keys.

        The references are not guaranteed to be 'live' at the time
        they are used, so the result of calling the references needs
        to be checked before being used.  This can be used to avoid
        creating references that will cause the garbage collector to
        keep the keys around longer than needed.

        )listr'   r   r   r   r   keyrefs   s    
zWeakIdKeyDictionary.keyrefsc                 C  s0   d| _ | j \}}| }|d k	r||fS qd S r<   )r1   r'   popitem)r   r   rE   rI   r   r   r   rZ      s
    zWeakIdKeyDictionary.popitemc                 G  s   d| _ | jjt|f| S r<   )r1   r'   r4   r	   )r   r   argsr   r   r   r4      s    zWeakIdKeyDictionary.popc                 C  s   | j t|| j|S r   )r'   
setdefaultr	   r/   rO   r   r   r   r\      s    zWeakIdKeyDictionary.setdefaultc                 K  s\   | j }|d k	rFt|ds$ti |}| D ]\}}||t|| j< q,t|rX| | d S )NrG   )r'   r   typerG   r	   r/   r@   r2   )r   r3   kwargsr6   r   rE   r   r   r   r2      s    
zWeakIdKeyDictionary.updatec                 C  s   |  | | S r   )r2   r   r   r   r   r   __ior__   s    
zWeakIdKeyDictionary.__ior__c                 C  s&   t |tjr"|  }|| |S tS r   )
isinstance_collections_abcr   rJ   r2   NotImplementedr   r   cr   r   r   __or__   s
    
zWeakIdKeyDictionary.__or__c                 C  s0   t |tjr,|  }|| ||  |S tS r   )ra   rb   r   r   r2   rc   rd   r   r   r   __ror__   s    

zWeakIdKeyDictionary.__ror__c                 C  s2   t |tstS dd |  D dd | D kS )Nc                 S  s   i | ]\}}t ||qS r   )r   )r8   r,   vr   r   r   
<dictcomp>	  s      z.WeakIdKeyDictionary.__eq__.<locals>.<dictcomp>)ra   r   rc   rG   r_   r   r   r   r!     s    
zWeakIdKeyDictionary.__eq__)N)N)N)N)r"   r#   r$   __annotations__r   r7   r;   r>   r?   rA   rD   rF   rJ   __copy__rM   rN   rS   rG   rV   __iter__rW   rY   rZ   r4   r\   r2   r`   rf   rg   r!   r   r   r   r   r
   T   s6   

	





c                   @  s0   e Zd ZU dZded< ddddZdd	 Zd
S )r   z
    Wrapper around a weak ref of a Tensor that handles the _fix_weakref() call required
    when unwrapping a Tensor weakref.
    zWeakRef[Tensor]r   r   )tensorc                 C  s   t |tstt|| _d S r   )ra   r   AssertionErrorweakrefr   )r   rm   r   r   r   r     s    zTensorWeakRef.__init__c                 C  s.   |   }|d kr|S t|ts"t|  |S r   )r   ra   r   rn   r   )r   outr   r   r   r     s    zTensorWeakRef.__call__N)r"   r#   r$   __doc__rj   r   r   r   r   r   r   r     s   
)
__future__r   ro   r   _weakrefsetr   collections.abcr   r   Ztorchr   abcrb   ZWeakRef__all__r	   r
   r   r   r   r   r   r   <module>   s   * 9