o
    þ§Ýjî"  ã                   @  sö   d Z ddlmZ ddlZddlmZ ddlmZ ddlm	Z	 ddl
mZ dd	lmZ dd
lmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ e d¡ZG dd„ deje ƒZG dd„ dƒZG dd„ deƒZG dd„ deeef ƒZdS )z#Provide :class:`OrderedBidictBase`.é    )ÚannotationsN)Úrefé   )Ú
BidictBase)ÚUnwrites)Úbidict)Ú	iteritems)ÚKT)ÚMISSING)ÚOKT)ÚOVT)ÚVT)Ú
MapOrItemsÚATc                   @  s0   e Zd ZdZddd„Zddd„Zdddd„ZdS )ÚWeakAttrzèDescriptor to automatically manage (de)referencing the given slot as a weakref.

    See https://docs.python.org/3/howto/descriptor.html#managed-attributes
    for an intro to using descriptors like this for managed attributes.
    ÚslotÚstrÚreturnÚNonec                C  s
   || _ d S ©N©r   )Úselfr   © r   úN/var/www/html/chatgpt/venv/lib/python3.10/site-packages/bidict/_orderedbase.pyÚ__init__,   s   
zWeakAttr.__init__Úinstanceút.AnyÚvaluer   c                 C  s   t || jt|ƒƒ d S r   )Úsetattrr   Úweakref)r   r   r   r   r   r   Ú__set__/   ó   zWeakAttr.__set__NÚ_WeakAttr__ownerc                 C  s   t  tt|| jƒƒ ¡S r   )ÚtÚcastr   Úgetattrr   )r   r   r"   r   r   r   Ú__get__2   r!   zWeakAttr.__get__)r   r   r   r   )r   r   r   r   r   r   r   )r   r   r"   r   r   r   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r    r&   r   r   r   r   r   %   s
    

r   c                   @  sN   e Zd ZU dZedd�Zded< dZded< ddd„Zddd„Z	ddd„Z
dS )ÚNodea%  A node in a circular doubly-linked list
    used to encode the order of items in an ordered bidict.

    A weak reference to the previous node is stored
    to avoid creating strong reference cycles.
    Referencing/dereferencing the weakref is handled automatically by :class:`WeakAttr`.
    Ú	_prv_weakr   úWeakAttr[Node]Úprv)Ú__weakref__r,   ÚnxtzNode | WeakAttr[Node]r0   r   r   c                 C  s   || _ || _d S r   ©r.   r0   )r   r.   r0   r   r   r   r   D   s   
zNode.__init__c                 C  s   | j | j_ | j| j _dS )zŠRemove self from in between prv and nxt.
        Self's references to prv and nxt are retained so it can be relinked (see below).
        N)r0   r.   ©r   r   r   r   ÚunlinkH   s   
zNode.unlinkc                 C  s   |  | j _| j_ dS )z=Restore self between prv and nxt after unlinking (see above).Nr1   r2   r   r   r   ÚrelinkO   s   zNode.relinkN)r.   r+   r0   r+   r   r   ©r   r   )r'   r(   r)   r*   r   r.   Ú__annotations__Ú	__slots__r   r3   r4   r   r   r   r   r+   6   s   
 

r+   c                      sT   e Zd ZU dZedd�Zded< dZd‡ fd	d
„Zddœddd„Z	ddd„Z
‡  ZS )ÚSentinelNodezÄSpecial node in a circular doubly-linked list
    that links the first node with the last node.
    When its next and previous references point back to itself
    it represents an empty list.
    Ú	_nxt_weakr   r-   r0   )r9   r   r   c                   s   t ƒ  | | ¡ d S r   )Úsuperr   r2   ©Ú	__class__r   r   r   ^   s   zSentinelNode.__init__F©Úreverser>   Úboolút.Iterator[Node]c                c  s@   � |rdnd}t | |ƒ}|| ur|V  t ||ƒ}|| usdS dS )z/Iterator yielding nodes in the requested order.r.   r0   N)r%   )r   r>   ÚattrÚnoder   r   r   Ú	iternodesa   s   €

þzSentinelNode.iternodesr+   c                 C  s    | j }t|| ƒ}| |_| _ |S )z&Create and return a new terminal node.)r.   r+   r0   )r   Zold_lastZnew_lastr   r   r   Únew_last_nodei   s   
zSentinelNode.new_last_noder5   )r>   r?   r   r@   )r   r+   )r'   r(   r)   r*   r   r0   r6   r7   r   rC   rD   Ú__classcell__r   r   r;   r   r8   T   s   
 r8   c                      sº   e Zd ZU dZded< ded< d6d7‡ fdd„Zejr(ed8dd„ƒZ	ed8dd„ƒZ
d8‡ fdd„Zd9dd„Zd:dd„Zd;‡ fd d!„Zd<‡ fd*d+„Zd=d-d.„Zd=d/d0„Zd1d2œd>d4d5„Z‡  ZS )?ÚOrderedBidictBasezABase class implementing an ordered :class:`BidirectionalMapping`.zbidict[t.Any, Node]Ú_node_by_korvr?   Ú_bykeyr   ÚargúMapOrItems[KT, VT]Úkwr   r   r   c                  s.   t ƒ | _tƒ | _d| _tƒ j|fi |¤Ž dS )a€  Make a new ordered bidirectional mapping.
        The signature behaves like that of :class:`dict`.
        Items passed in are added in the order they are passed,
        respecting the :attr:`~bidict.BidictBase.on_dup`
        class attribute in the process.

        The order in which items are inserted is remembered,
        similar to :class:`collections.OrderedDict`.
        TN)r8   Ú_sntlr   rG   rH   r:   r   )r   rI   rK   r;   r   r   r   w   s   
zOrderedBidictBase.__init__úOrderedBidictBase[VT, KT]c                 C  ó   d S r   r   r2   r   r   r   Úinverseˆ   ó   zOrderedBidictBase.inversec                 C  rN   r   r   r2   r   r   r   Úinv‹   rP   zOrderedBidictBase.invc                   s8   t  tttf tƒ  ¡ ¡}| j|_| j|_| j	 |_	|S r   )
r#   r$   rF   r   r	   r:   Ú_make_inverserL   rG   rH   )r   rQ   r;   r   r   rR   Ž   s
   
zOrderedBidictBase._make_inverserB   r+   Úkeyr	   Úvalc                 C  s    | j r|n|}| j ||¡ d S r   )rH   rG   Zforceput)r   rB   rS   rT   Zkorvr   r   r   Ú_assoc_node•   s   zOrderedBidictBase._assoc_nodec                 C  s   | j j|= | ¡  d S r   )rG   rO   r3   )r   rB   r   r   r   Ú_dissoc_node™   s   
zOrderedBidictBase._dissoc_nodeÚotherc                   sl   t ƒ  |¡ | j}| jj}| ¡  |j}| j | j_| j_	| jj
}t|ƒD ]\}}||ƒ |r0|n|ƒ q%dS )z"See :meth:`BidictBase._init_from`.N)r:   Ú
_init_fromrH   rG   rO   ÚclearÚ__setitem__rL   r0   r.   rD   r   )r   rW   ÚbykeyÚkorv_by_nodeZkorv_by_node_setÚnew_nodeÚkÚvr;   r   r   rX   �   s   ÿzOrderedBidictBase._init_fromÚnewkeyÚnewvalÚoldkeyúOKT[KT]ÚoldvalúOVT[VT]ÚunwritesúUnwrites | Nonec                   s‚  t ƒ  |||||¡ | j| j}}| j| j}}	|tu r:|tu r:| j ¡ }
||
||ƒ |dur8| 	||
f¡ dS dS |tury|tury|	rM|| }|| }
n|| }|| }
||ƒ ||
||ƒ |durw| 
||
||f||||f|jff¡ dS dS |turœ||	r‚|n| }||||ƒ |durš| 	||||f¡ dS dS |tus¢J ‚||	r§|n| }||||ƒ |dur¿| 	||||f¡ dS dS )z*See :meth:`bidict.BidictBase._spec_write`.N)r:   Ú_writerU   rV   rG   rH   r
   rL   rD   ÚappendÚextendr4   )r   r`   ra   rb   rd   rf   ZassocZdissocZnode_by_korvr[   ZnewnodeZoldnoderB   r;   r   r   rh   ©   sH   
ÿ



ýÿÿÿzOrderedBidictBase._writeút.Iterator[KT]c                 C  ó   | j dd�S )z4Iterator over the contained keys in insertion order.Fr=   ©Ú_iterr2   r   r   r   Ú__iter__Ö   ó   zOrderedBidictBase.__iter__c                 C  rl   )z<Iterator over the contained keys in reverse insertion order.Tr=   rm   r2   r   r   r   Ú__reversed__Ú   rp   zOrderedBidictBase.__reversed__Fr=   r>   c                c  s\   � | j j|d�}| jj}| jr|D ]}|| V  qd S | j}|D ]}|| }|| V  q d S )Nr=   )rL   rC   rG   rO   rH   Z_invm)r   r>   Únodesr\   rB   Z
key_by_valrT   r   r   r   rn   Þ   s   €ÿþzOrderedBidictBase._iter)r   )rI   rJ   rK   r   r   r   )r   rM   )rB   r+   rS   r	   rT   r   r   r   )rB   r+   r   r   )rW   rJ   r   r   )r`   r	   ra   r   rb   rc   rd   re   rf   rg   r   r   )r   rk   )r>   r?   r   rk   )r'   r(   r)   r*   r6   r   r#   ÚTYPE_CHECKINGÚpropertyrO   rQ   rR   rU   rV   rX   rh   ro   rq   rn   rE   r   r   r;   r   rF   q   s$   
 


-
rF   )r*   Ú
__future__r   Útypingr#   r   r   Z_baser   r   Z_bidictr   rn   r   Z_typingr	   r
   r   r   r   r   ÚTypeVarr   ÚGenericr   r+   r8   rF   r   r   r   r   Ú<module>   s&   
