o
    (ôj#+  ã                   @  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__© úc/var/www/html/academy/tools/voices/clone/cosy-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    6   ó   

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__@   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   X   r   z_WeakHashRef.__init__c                   r   r   r   r"   r   r   r   r    a   r$   z_WeakHashRef.__call__c                 C  r%   r   r&   r'   r   r   r   r(   h   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.   k   s
   z_WeakHashRef.__eq__r   r/   r   r   r   r   r6   U   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   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   )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
   v   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   6  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   1  s
   
 
r   )Ú
__future__r   r€   r   Ú_weakrefsetr   Úcollections.abcr   r   Útorchr   Úabcru   ZWeakRefÚ__all__r	   r6   r
   r   r   r   r   r   r   Ú<module>   s    +! 9