o
    hX+                     @  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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                      @   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__ d/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/torch/utils/weak.pyr   .      
zWeakIdRef.__init__c                       t   }t|dr|  |S N_fix_weakrefr   __call__hasattrr   r   rr   r   r   r    7      

zWeakIdRef.__call__c                 C     | j S r   r   r   r   r   r   __hash__>      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__A   s
   zWeakIdRef.__eq__r   	__name__
__module____qualname__	__slots__r   r    r(   r.   __classcell__r   r   r   r   r	   +       	r	   c                      r   )_WeakHashRefr   Nc                   r   r   )hashr   r   r   r   r   r   r   r   Y   r   z_WeakHashRef.__init__c                   r   r   r   r"   r   r   r   r    b   r$   z_WeakHashRef.__call__c                 C  r%   r   r&   r'   r   r   r   r(   i   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.   l   s
   z_WeakHashRef.__eq__r   r/   r   r   r   r   r6   V   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   x   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__      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;   rZ   )r   memor^   r[   r   rW   r\   r   r   r   __deepcopy__   s   

z WeakIdKeyDictionary.__deepcopy__c                 C  s   | j | ||S r   )r;   getr@   r   r   defaultr   r   r   ra      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;   rZ   )r   re   rW   r   r   r   r   rZ      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   re   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   rg   )r   re   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   rm      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   rb   r   r   r   ro     rY   zWeakIdKeyDictionary.setdefaultc                 K  sb   | j }|d ur$t|dsti |}| D ]\}}||| || j< qt|r/| | d S d S )NrZ   )r;   r!   typerZ   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   )rt   ru   r   r   rD   rv   rw   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>,  s    z.WeakIdKeyDictionary.__eq__.<locals>.<dictcomp>)rt   r   rv   rZ   rr   r   r   r   r.   )  s   
$zWeakIdKeyDictionary.__eq__r   )r0   r1   r2   r	   r   rI   rM   rP   rQ   rS   rV   rX   r]   __copy__r`   ra   rf   rZ   ri   __iter__rj   rl   rm   rF   ro   rD   rs   ry   rz   r.   r   r   r   r   r
   w   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   )rt   r   weakrefr   )r   r   r   r   r   r   7  s   zTensorWeakRef.__init__c                 C  s.   |   }|d u r
|S t|tsJ |  |S r   )r   rt   r   r   )r   outr   r   r   r    ;  s   zTensorWeakRef.__call__N)r   r   )r0   r1   r2   __doc____annotations__r   r    r   r   r   r   r   2  s
   
 
r   )
__future__r   r   r   _weakrefsetr   collections.abcr   r   torchr   abcru   WeakRef__all__r	   r6   r
   r   r   r   r   r   r   <module>   s   +! 9