o
    i%                     @  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g d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__ _/var/www/html/eduruby.in/lip-sync/lip-sync-env/lib/python3.10/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 ur|d ur||u S | |u S r   r   )r   otherabr   r   r   __eq__@   s
   zWeakIdRef.__eq__r   )	__name__
__module____qualname__	__slots__r   r   r   r!   __classcell__r   r   r   r   r	   *   s    	r	   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d4d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d4d(d)Zd4d*d+Zd,d- Zd.d/ Zd0d1 Zd2d3 ZdS )5r
   zdict[WeakIdRef, object]dataNc                 C  sJ   i | _ t| fdd}|| _g | _t | _d| _|d ur#| | d S d S )Nc                 S  sL   | }|d ur$|j r|j|  d S z|j| = W d S  ty#   Y d S w 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  sP   | j j}| j}	 z| }W n
 ty   Y d S w z||= W n	 ty&   Y nw q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 | ]}| v 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__      zWeakIdKeyDictionary.__setitem__c                 C  s^   t  }t|  | j D ]\}}| }|d ur|||< qW d    |S 1 s(w   Y  |S r   )r
   r   r'   items)r   newr   rE   or   r   r   copy   s   

zWeakIdKeyDictionary.copyc                 C  sr   ddl m} |  }t| ! | j D ]\}}| }|d ur&|||||< qW d    |S 1 s2w   Y  |S )Nr   )deepcopy)rK   rL   r   r   r'   rH   )r   memorL   rI   r   rE   rJ   r   r   r   __deepcopy__   s   

z WeakIdKeyDictionary.__deepcopy__c                 C  s   | j t||S r   )r'   getr	   r   r   defaultr   r   r   rO      s   zWeakIdKeyDictionary.getc                 C  s,   zt |}W n
 ty   Y dS w || jv S )NF)r	   	TypeErrorr'   )r   r   wrr   r   r   __contains__   s   
z WeakIdKeyDictionary.__contains__c                 c  s\    t |  | j D ]\}}| }|d ur||fV  qW d    d S 1 s'w   Y  d S r   r   r'   rH   )r   rS   rE   r   r   r   r   rH      s   

"zWeakIdKeyDictionary.itemsc                 c  sP    t |  | jD ]}| }|d ur|V  q	W d    d S 1 s!w   Y  d S r   )r   r'   )r   rS   objr   r   r   keys   s   

"zWeakIdKeyDictionary.keysc                 c  sT    t |  | j D ]\}}| d ur|V  qW d    d S 1 s#w   Y  d S r   rU   )r   rS   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  s.   d| _ 	 | j \}}| }|d ur||fS qr<   )r1   r'   popitem)r   r   rE   rJ   r   r   r   r[      s   zWeakIdKeyDictionary.popitemc                 G  s   d| _ | jjt|g|R  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/   rP   r   r   r   r]      rG   zWeakIdKeyDictionary.setdefaultc                 K  s`   | j }|d ur#t|dsti |}| D ]\}}||t|| j< qt|r.| | d S d S )NrH   )r'   r   typerH   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   rK   r2   NotImplementedr   r   cr   r   r   __or__   s
   
zWeakIdKeyDictionary.__or__c                 C  s0   t |tjr|  }|| ||  |S tS r   )rb   rc   r   r   r2   rd   re   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>)rb   r   rd   rH   r`   r   r   r   r!     s   
$zWeakIdKeyDictionary.__eq__r   )r"   r#   r$   __annotations__r   r7   r;   r>   r?   rA   rD   rF   rK   __copy__rN   rO   rT   rH   rW   __iter__rX   rZ   r[   r4   r]   r2   ra   rg   rh   r!   r   r   r   r   r
   T   s8   
 
	





r
   c                   @  s,   e Zd ZU dZde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   tensorr   c                 C  s   t |tsJ t|| _d S r   )rb   r   weakrefr   )r   rn   r   r   r   r     s   zTensorWeakRef.__init__c                 C  s.   |   }|d u r
|S t|tsJ |  |S r   )r   rb   r   r   )r   outr   r   r   r     s   zTensorWeakRef.__call__N)rn   r   )r"   r#   r$   __doc__rk   r   r   r   r   r   r   r     s
   
 
r   )
__future__r   ro   r   _weakrefsetr   collections.abcr   r   Ztorchr   abcrc   ZWeakRef__all__r	   r
   r   r   r   r   r   r   <module>   s    * 9