o
    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                      @   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                      t || _t || d S N)idr   super__init__selfkeycallback	__class__ V/home/www/facesmatcher.com/frenv_anti/lib/python3.10/site-packages/torch/utils/weak.pyr   5      
zWeakIdRef.__init__c                       t   }t|dr|  |S N_fix_weakrefr   __call__hasattrr   r   rr   r   r   r    >      

zWeakIdRef.__call__c                 C     | j S r   r   r   r   r   r   __hash__E      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__r   	__name__
__module____qualname__	__slots__r   r    r(   r.   __classcell__r   r   r   r   r	   2       	r	   c                      r   )_WeakHashRefr   Nc                   r   r   )hashr   r   r   r   r   r   r   r   a   r   z_WeakHashRef.__init__c                   r   r   r   r"   r   r   r   r    j   r$   z_WeakHashRef.__call__c                 C  r%   r   r&   r'   r   r   r   r(   q   r)   z_WeakHashRef.__hash__c                 C  s4   |  }| }|d ur|d urt |t |kS | |u S r   )r7   r*   r   r   r   r.   t   s
   z_WeakHashRef.__eq__r   r/   r   r   r   r   r6   ^   r5   r6   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d2d&d'Zd2d(d)Zd*d+ Zd,d- Zd.d/ Zd0d1 ZdS )3r
   Nc                 C  sP   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appenddataKeyError)kselfrefr   r   r   r   remove   s   z,WeakIdKeyDictionary.__init__.<locals>.removeF)	r;   ref_typer   _remover9   setr8   
_dirty_lenupdate)r   dictr@   r?   r   r   r   r      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   )r9   popr;   
IndexErrorr<   )r   rF   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   ).0r=   rH   r   r   
<listcomp>   s    z7WeakIdKeyDictionary._scrub_removals.<locals>.<listcomp>F)r;   r9   rC   r'   r   rK   r   _scrub_removals   s   
z#WeakIdKeyDictionary._scrub_removalsc                 C  s   d| _ | j| |= d S NT)rC   r;   r@   r   r   r   r   r   __delitem__   s   zWeakIdKeyDictionary.__delitem__c                 C  s   | j | | S r   )r;   r@   rO   r   r   r   __getitem__   s   zWeakIdKeyDictionary.__getitem__c                 C  s(   | j r
| jr
|   t| jt| j S r   )rC   r9   rM   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   r0   r   r'   r   r   r   __repr__   s   zWeakIdKeyDictionary.__repr__c                 C  s   || j | || j< d S r   )r;   r@   rA   )r   r   valuer   r   r   __setitem__   s   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   rW   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)r\   r]   r   r   r;   rY   )r   memor]   rZ   r   rW   r[   r   r   r   __deepcopy__   s   

z WeakIdKeyDictionary.__deepcopy__c                 C  s   | j | ||S r   )r;   getr@   r   r   defaultr   r   r   r`      s   zWeakIdKeyDictionary.getc                 C  s.   z|  |}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;   rY   )r   rd   rW   r   r   r   r   rY      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   rd   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   rf   )r   rd   rW   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rN   )rC   r;   popitem)r   r   rW   r[   r   r   r   rl     s   zWeakIdKeyDictionary.popitemc                 G  s    d| _ | jj| |g|R  S rN   )rC   r;   rF   r@   )r   r   argsr   r   r   rF     s   zWeakIdKeyDictionary.popc                 C  s   | j | || j|S r   )r;   
setdefaultr@   rA   ra   r   r   r   rn     s   zWeakIdKeyDictionary.setdefaultc                 K  sb   | j }|d ur$t|dsti |}| D ]\}}||| || j< qt|r/| | d S d S )NrY   )r;   r!   typerY   r@   rA   rR   rD   )r   rE   kwargsrH   r   rW   r   r   r   rD     s   
zWeakIdKeyDictionary.updatec                 C  s   |  | | S r   )rD   r   r+   r   r   r   __ior__!  s   
zWeakIdKeyDictionary.__ior__c                 C  s&   t |tjr|  }|| |S tS r   )
isinstance_collections_abcr   r\   rD   NotImplementedr   r+   cr   r   r   __or__%  s
   
zWeakIdKeyDictionary.__or__c                 C  s0   t |tjr|  }|| ||  |S tS r   )rs   rt   r   r   rD   ru   rv   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   )rJ   r=   vr   r   r   
<dictcomp>9  s    z.WeakIdKeyDictionary.__eq__.<locals>.<dictcomp>)rs   r   ru   rY   rq   r   r   r   r.   6  s
   
zWeakIdKeyDictionary.__eq__r   )r0   r1   r2   r	   r   rI   rM   rP   rQ   rS   rV   rX   r\   __copy__r_   r`   re   rY   rh   __iter__ri   rk   rl   rF   rn   rD   rr   rx   ry   r.   r   r   r   r   r
      s6    	




r
   c                   @  s,   e Zd ZU dZde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   tensorr   c                 C  s   t |tsJ t|| _d S r   )rs   r   weakrefr   )r   r~   r   r   r   r   G  s   zTensorWeakRef.__init__c                 C  s.   |   }|d u r
|S t|tsJ |  |S r   )r   rs   r   r   )r   outr   r   r   r    K  s   zTensorWeakRef.__call__N)r~   r   )r0   r1   r2   __doc____annotations__r   r    r   r   r   r   r   B  s
   
 
r   )
__future__r   collections.abcabcrt   r   _weakrefsetr   r   r   r   Ztorchr   ZWeakRef__all__r	   r6   r
   r   r   r   r   r   r   <module>   s   !," @