U
    •Ê¹j®%  ã                   @  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/bwave/python/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   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