a
    d#                     @   sz   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
 g dZG dd de jZG d	d
 d
eZeZdS )    N)ref)_IterationGuard)MutableMappingMapping)Dict)	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__ Y/var/www/html/stable-diffusion-webui/venv/lib/python3.9/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   1   s    

zWeakIdRef.__call__c                 C   s   | j S r   )r
   r   r   r   r   __hash__8   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__)N)	__name__
__module____qualname__	__slots__r   r   r   r   __classcell__r   r   r   r   r   %   s
   	r   c                   @   s   e Zd ZU eeef ed< d3d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d5d'd(Zd6d)d*Zd+d, Zd-d. Zd/d0 Zd1d2 Z dS )7r   dataNc                 C   sF   i | _ t| fdd}|| _g | _t | _d| _|d urB| | 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appendr%   KeyError)kselfrefr   r   r   r   removeU   s    z,WeakIdKeyDictionary.__init__.<locals>.removeF)r%   r   _remover'   setr&   
_dirty_lenupdate)r   dictr,   r   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   )r'   popr%   
IndexErrorr)   )r   r2   dr   r   r   r   _commit_removalsg   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*   r4   r   r   
<listcomp>{       z7WeakIdKeyDictionary._scrub_removals.<locals>.<listcomp>F)r%   r'   r/   r   r   r7   r   _scrub_removalsy   s    z#WeakIdKeyDictionary._scrub_removalsc                 C   s   d| _ | jt|= d S NT)r/   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   )r/   r'   r:   lenr%   r   r   r   r   __len__   s    zWeakIdKeyDictionary.__len__c                 C   s   d| j jt| f S )Nz<%s at %#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   s\   t  }t| : | j D ]\}}| }|d ur|||< qW d    n1 sN0    Y  |S r   )r   r   r%   items)r   newr   rB   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)rG   rH   r   r   r%   rD   )r   memorH   rE   r   rB   rF   r   r   r   __deepcopy__   s    
.z WeakIdKeyDictionary.__deepcopy__c                 C   s   | j t||S r   )r%   getr   r   r   defaultr   r   r   rK      s    zWeakIdKeyDictionary.getc                 C   s,   zt |}W n ty    Y dS 0 || jv S )NF)r   	TypeErrorr%   )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   r%   rD   )r   rO   rB   r   r   r   r   rD      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   r%   )r   rO   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   rQ   )r   rO   rB   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 ur||fS qd S r;   )r/   r%   popitem)r   r   rB   rF   r   r   r   rW      s
    zWeakIdKeyDictionary.popitemc                 G   s   d| _ | jjt|g|R  S r;   )r/   r%   r2   r   )r   r   argsr   r   r   r2      s    zWeakIdKeyDictionary.popc                 C   s   | j t|| j|S r   )r%   
setdefaultr   r-   rL   r   r   r   rY      s    zWeakIdKeyDictionary.setdefaultc                 K   s\   | j }|d urFt|ds$ti |}| D ]\}}||t|| j< q,t|rX| | d S )NrD   )r%   r   typerD   r   r-   r?   r0   )r   r1   kwargsr4   r   rB   r   r   r   r0      s    
zWeakIdKeyDictionary.updatec                 C   s   |  | | S r   )r0   r   r   r   r   r   __ior__   s    
zWeakIdKeyDictionary.__ior__c                 C   s&   t |tjr"|  }|| |S tS r   )
isinstance_collections_abcr   rG   r0   NotImplementedr   r   cr   r   r   __or__   s
    
zWeakIdKeyDictionary.__or__c                 C   s0   t |tjr,|  }|| ||  |S tS r   )r^   r_   r   r   r0   r`   ra   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   )r6   r*   vr   r   r   
<dictcomp>  r9   z.WeakIdKeyDictionary.__eq__.<locals>.<dictcomp>)r^   r   r`   rD   r\   r   r   r   r     s    
zWeakIdKeyDictionary.__eq__)N)N)N)N)!r    r!   r"   r   r   object__annotations__r   r5   r:   r=   r>   r@   rA   rC   rG   __copy__rJ   rK   rP   rD   rS   __iter__rT   rV   rW   r2   rY   r0   r]   rc   rd   r   r   r   r   r   r   O   s6   

	





r   )weakrefr   _weakrefsetr   collections.abcr   r   typingr   abcr_   __all__r   r   r	   r   r   r   r   <module>   s   * 9