U
    Mh>+                     @  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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__ B/var/www/html/venv/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   7   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__A   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dgZd fdd	Z fddZdd Zd	d
 Z  ZS )_WeakHashRefr   Nc                   s   t || _t || d S r   )hashr   r   r   r   r   r   r   r   Y   s    
z_WeakHashRef.__init__c                   s    t   }t|dr|  |S r   r   r   r   r   r   r   b   s    

z_WeakHashRef.__call__c                 C  s   | j S r   r    r!   r   r   r   r"   i   s    z_WeakHashRef.__hash__c                 C  s4   |  }| }|d k	r,|d k	r,t |t |kS | |kS r   )r/   r#   r   r   r   r'   l   s
    z_WeakHashRef.__eq__)Nr(   r   r   r   r   r.   V   s
   	r.   c                   @  s   e Zd Zdefd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d2d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d3d&d'Zd4d(d)Zd*d+ Zd,d- Zd.d/ Zd0d1 ZdS )5r
   Nc                 C  sL   i | _ || _t| fdd}|| _g | _t | _d| _|d k	rH| | 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appenddataKeyError)kselfrefr   r   r   r   remove}   s    z,WeakIdKeyDictionary.__init__.<locals>.removeF)	r3   ref_typer   _remover1   setr0   
_dirty_lenupdate)r   dictr8   r7   r   r   r   r   x   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   )r1   popr3   
IndexErrorr4   )r   r>   dr   r   r   r   _commit_removals   s    

z$WeakIdKeyDictionary._commit_removalsc                   s&   | j   fdd| jD | _d| _d S )Nc                   s   g | ]}| kr|qS r   r   ).0r5   r@   r   r   
<listcomp>   s      z7WeakIdKeyDictionary._scrub_removals.<locals>.<listcomp>F)r3   r1   r;   r!   r   rC   r   _scrub_removals   s    z#WeakIdKeyDictionary._scrub_removalsc                 C  s   d| _ | j| |= d S NT)r;   r3   r8   r   r   r   r   r   __delitem__   s    zWeakIdKeyDictionary.__delitem__c                 C  s   | j | | S r   )r3   r8   rG   r   r   r   __getitem__   s    zWeakIdKeyDictionary.__getitem__c                 C  s(   | j r| jr|   t| jt| j S r   )r;   r1   rE   lenr3   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 | || j< d S r   )r3   r8   r9   )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   r3   items)r   newr   rO   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)rT   rU   r   r   r3   rQ   )r   memorU   rR   r   rO   rS   r   r   r   __deepcopy__   s    
z WeakIdKeyDictionary.__deepcopy__c                 C  s   | j | ||S r   )r3   getr8   r   r   defaultr   r   r   rX      s    zWeakIdKeyDictionary.getc                 C  s0   z|  |}W n tk
r$   Y dS X || jkS )NF)r8   	TypeErrorr3   )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   r3   rQ   )r   r\   rO   r   r   r   r   rQ      s
    
zWeakIdKeyDictionary.itemsc              	   c  s8   t | & | jD ]}| }|d k	r|V  qW 5 Q R X d S r   )r   r3   )r   r\   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   r^   )r   r\   rO   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.

        )listr3   r!   r   r   r   keyrefs   s    
zWeakIdKeyDictionary.keyrefsc                 C  s0   d| _ | j \}}| }|d k	r||fS qd S rF   )r;   r3   popitem)r   r   rO   rS   r   r   r   rd      s
    zWeakIdKeyDictionary.popitemc                 G  s   d| _ | jj| |f| S rF   )r;   r3   r>   r8   )r   r   argsr   r   r   r>     s    zWeakIdKeyDictionary.popc                 C  s   | j | || j|S r   )r3   
setdefaultr8   r9   rY   r   r   r   rf     s    zWeakIdKeyDictionary.setdefaultc                 K  s^   | j }|d k	rHt|ds$ti |}| D ]\}}||| || j< q,t|rZ| | d S )NrQ   )r3   r   typerQ   r8   r9   rJ   r<   )r   r=   kwargsr@   r   rO   r   r   r   r<   
  s    
zWeakIdKeyDictionary.updatec                 C  s   |  | | S r   )r<   r   r$   r   r   r   __ior__  s    
zWeakIdKeyDictionary.__ior__c                 C  s&   t |tjr"|  }|| |S tS r   )
isinstance_collections_abcr   rT   r<   NotImplementedr   r$   cr   r   r   __or__  s
    
zWeakIdKeyDictionary.__or__c                 C  s0   t |tjr,|  }|| ||  |S tS r   )rk   rl   r   r   r<   rm   rn   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   )rB   r5   vr   r   r   
<dictcomp>,  s      z.WeakIdKeyDictionary.__eq__.<locals>.<dictcomp>)rk   r   rm   rQ   ri   r   r   r   r'   )  s    
zWeakIdKeyDictionary.__eq__)N)N)N)r)   r*   r+   r	   r   rA   rE   rH   rI   rK   rN   rP   rT   __copy__rW   rX   r]   rQ   r`   __iter__ra   rc   rd   r>   rf   r<   rj   rp   rq   r'   r   r   r   r   r
   w   s4   	

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


c                   @  s0   e Zd ZU dZded< ddddZdd	 Zd
S )r   zuWrapper 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   )rk   r   AssertionErrorweakrefr   )r   rv   r   r   r   r   7  s    zTensorWeakRef.__init__c                 C  s.   |   }|d kr|S t|ts"t|  |S r   )r   rk   r   rw   r   )r   outr   r   r   r   ;  s    zTensorWeakRef.__call__N)r)   r*   r+   __doc____annotations__r   r   r   r   r   r   r   2  s   
)
__future__r   rx   r   _weakrefsetr   collections.abcr   r   Ztorchr   abcrl   ZWeakRef__all__r	   r.   r
   r   r   r   r   r   r   <module>   s   +! 9