U
    8‹Ñh>+  ã                   @  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__© ú>/tmp/user/2202/pip-unpacked-wheel-bf9o3bgk/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