a
    h+                     @  s   d dl mZ d dlmZ d dl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g d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)_IterationGuard)MappingMutableMapping)ref)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__ >/var/www/auris/lib/python3.9/site-packages/torch/utils/weak.pyr   5   s    
zWeakIdRef.__init__c                   s    t   }t|dr|  |S N_fix_weakrefr   __call__hasattrr   r   rr   r   r   r   >   s    

zWeakIdRef.__call__c                 C  s   | j S r   r   r   r   r   r   __hash__E   s    zWeakIdRef.__hash__c                 C  s,   |  }| }|d ur$|d ur$||u S | |u S r   r   r   otherabr   r   r   __eq__H   s
    zWeakIdRef.__eq__)N	__name__
__module____qualname__	__slots__r   r   r"   r'   __classcell__r   r   r   r   r	   2   s
   	r	   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   a   s    
z_WeakHashRef.__init__c                   s    t   }t|dr|  |S r   r   r   r   r   r   r   j   s    

z_WeakHashRef.__call__c                 C  s   | j S r   r    r!   r   r   r   r"   q   s    z_WeakHashRef.__hash__c                 C  s4   |  }| }|d ur,|d ur,t |t |kS | |u S r   )r/   r#   r   r   r   r'   t   s
    z_WeakHashRef.__eq__)Nr(   r   r   r   r   r.   ^   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 urH| | d S )Nc                 S  sF   | }|d urB|j r"|j|  n z|j| = W n ty@   Y n0 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      s    zWeakIdKeyDictionary.__init__c                 C  sR   | j j}| j}z
| }W n ty,   Y d S 0 z
||= W q tyJ   Y q0 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 | ]}| v r|qS r   r   ).0r5   r@   r   r   
<listcomp>       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   rH   r   r   r   __getitem__   s    zWeakIdKeyDictionary.__getitem__c                 C  s(   | j r| jr|   t| jt| j S r   )r;   r1   rF   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  s\   t  }t| : | j D ]\}}| }|d ur|||< qW d    n1 sN0    Y  |S r   )r
   r   r3   items)r   newr   rP   or   r   r   copy   s    
(zWeakIdKeyDictionary.copyc                 C  sp   ddl m} |  }t| @ | j D ]$\}}| }|d ur(|||||< q(W d    n1 sb0    Y  |S )Nr   )deepcopy)rU   rV   r   r   r3   rR   )r   memorV   rS   r   rP   rT   r   r   r   __deepcopy__   s    
.z WeakIdKeyDictionary.__deepcopy__c                 C  s   | j | ||S r   )r3   getr8   r   r   defaultr   r   r   rY      s    zWeakIdKeyDictionary.getc                 C  s.   z|  |}W n ty"   Y dS 0 || jv S )NF)r8   	TypeErrorr3   )r   r   wrr   r   r   __contains__   s
    z WeakIdKeyDictionary.__contains__c                 c  sX   t | < | j D ] \}}| }|d ur||fV  qW d    n1 sJ0    Y  d S r   r   r3   rR   )r   r]   rP   r   r   r   r   rR      s
    
zWeakIdKeyDictionary.itemsc                 c  sL   t | 0 | jD ]}| }|d ur|V  qW d    n1 s>0    Y  d S r   )r   r3   )r   r]   objr   r   r   keys   s
    

zWeakIdKeyDictionary.keysc                 c  sP   t | 4 | j D ]\}}| d ur|V  qW d    n1 sB0    Y  d S r   r_   )r   r]   rP   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 ur||fS qd S rG   )r;   r3   popitem)r   r   rP   rT   r   r   r   re     s
    zWeakIdKeyDictionary.popitemc                 G  s    d| _ | jj| |g|R  S rG   )r;   r3   r>   r8   )r   r   argsr   r   r   r>     s    zWeakIdKeyDictionary.popc                 C  s   | j | || j|S r   )r3   
setdefaultr8   r9   rZ   r   r   r   rg     s    zWeakIdKeyDictionary.setdefaultc                 K  s^   | j }|d urHt|ds$ti |}| D ]\}}||| || j< q,t|rZ| | d S )NrR   )r3   r   typerR   r8   r9   rK   r<   )r   r=   kwargsr@   r   rP   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   rU   r<   NotImplementedr   r$   cr   r   r   __or__%  s
    
zWeakIdKeyDictionary.__or__c                 C  s0   t |tjr,|  }|| ||  |S tS r   )rl   rm   r   r   r<   rn   ro   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>9  rE   z.WeakIdKeyDictionary.__eq__.<locals>.<dictcomp>)rl   r   rn   rR   rj   r   r   r   r'   6  s
    
zWeakIdKeyDictionary.__eq__)N)N)N)r)   r*   r+   r	   r   rA   rF   rI   rJ   rL   rO   rQ   rU   __copy__rX   rY   r^   rR   ra   __iter__rb   rd   re   r>   rg   r<   rk   rq   rr   r'   r   r   r   r   r
      s4   	




r
   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J t|| _d S r   )rl   r   weakrefr   )r   rw   r   r   r   r   G  s    zTensorWeakRef.__init__c                 C  s.   |   }|d u r|S t|ts"J |  |S r   )r   rl   r   r   )r   outr   r   r   r   K  s    zTensorWeakRef.__call__N)r)   r*   r+   __doc____annotations__r   r   r   r   r   r   r   B  s   
r   )
__future__r   collections.abcabcrm   rx   _weakrefsetr   r   r   r   Ztorchr   ZWeakRef__all__r	   r.   r
   r   r   r   r   r   r   <module>   s   !," @