
    wg"                    >   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j*                  d      Z G d dej.                  e         Z G d d      Z G d de      Z G d deeef         Zy)z#Provide :class:`OrderedBidictBase`.    )annotationsN)ref   )
BidictBase)Unwrites)bidict)	iteritems)KT)MISSING)OKT)OVT)VT)
MapOrItemsATc                  *    e Zd ZdZddZddZdd	dZy)
WeakAttrzDescriptor 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.
    c                   || _         y Nslot)selfr   s     X/home/mcse/projects/flask/flask-venv/lib/python3.12/site-packages/bidict/_orderedbase.py__init__zWeakAttr.__init__,   s	    	    c                D    t        || j                  t        |             y r   )setattrr   weakref)r   instancevalues      r   __set__zWeakAttr.__set__/   s    $))WU^4r   Nc                h    t        j                  t         t        || j                                     S r   )tcastr   getattrr   )r   r   _WeakAttr__owners      r   __get__zWeakAttr.__get__2   s$    vvb6'(DII6899r   )r   strreturnNone)r   t.Anyr   r   r(   r)   r   )r   r*   r%   r*   r(   r   )__name__
__module____qualname____doc__r   r    r&    r   r   r   r   %   s    5:r   r   c                  T    e Zd ZU dZ ed      Zded<   dZded<   dd	Zdd
Z	ddZ
y)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__r2   nxtzNode | WeakAttr[Node]r6   c                     || _         || _        y r   r4   r6   )r   r4   r6   s      r   r   zNode.__init__D   s    r   c                p    | j                   | j                  _         | j                  | j                   _        y)zRemove self from in between prv and nxt.
        Self's references to prv and nxt are retained so it can be relinked (see below).
        N)r6   r4   r   s    r   unlinkzNode.unlinkH   s"     xxxxr   c                H    | x| j                   _        | j                  _         y)z=Restore self between prv and nxt after unlinking (see above).Nr8   r:   s    r   relinkzNode.relinkO   s    &**txx|r   N)r4   r1   r6   r1   r(   r)   r(   r)   )r+   r,   r-   r.   r   r4   __annotations__	__slots__r   r;   r=   r/   r   r   r1   r1   6   s2     #4C43I	 +r   r1   c                  \     e Zd ZU dZ ed      Zded<   dZd fdZdd	dd
Z	ddZ
 xZS )SentinelNodezSpecial 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   r3   r6   )rC   c                &    t         |   | |        y r   )superr   )r   	__class__s    r   r   zSentinelNode.__init__^   s    t$r   Freversec             #  d   K   |rdnd}t        | |      }|| ur| t        ||      }|| uryyw)z/Iterator yielding nodes in the requested order.r4   r6   N)r$   )r   rH   attrnodes       r   	iternodeszSentinelNode.iternodesa   s>     uUtT"$J4&D $s   +00c                R    | j                   }t        ||       }|x|_        | _         |S )z&Create and return a new terminal node.)r4   r1   r6   )r   old_lastnew_lasts      r   new_last_nodezSentinelNode.new_last_nodei   s*    88$'"**txr   r>   )rH   boolr(   zt.Iterator[Node])r(   r1   )r+   r,   r-   r.   r   r6   r?   r@   r   rL   rP   __classcell__rF   s   @r   rB   rB   T   s2     #4C4I% ,1 'r   rB   c                       e Zd ZU dZded<   ded<   dd fdZej                  redd       Z	edd       Z
d fd	Zdd
ZddZd fdZd fdZddZddZddddZ xZS )OrderedBidictBasezABase class implementing an ordered :class:`BidirectionalMapping`.zbidict[t.Any, Node]_node_by_korvrQ   _bykeyc               p    t               | _        t               | _        d| _        t        |   |fi | y)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)rB   _sntlr   rV   rW   rE   r   )r   argkwrF   s      r   r   zOrderedBidictBase.__init__w   s1     "^
#X##r   c                     y r   r/   r:   s    r   inversezOrderedBidictBase.inverse   s    8;r   c                     y r   r/   r:   s    r   invzOrderedBidictBase.inv   s    47r   c                    t        j                  t        t        t        f   t
        |                }| j                  |_        | j                  |_        | j                   |_	        |S r   )
r"   r#   rU   r   r
   rE   _make_inverserY   rV   rW   )r   r_   rF   s     r   ra   zOrderedBidictBase._make_inverse   sP    ff&r2v.0E0GHJJ	 .._

r   c                \    | j                   r|n|}| j                  j                  ||       y r   )rW   rV   forceput)r   rK   keyvalkorvs        r   _assoc_nodezOrderedBidictBase._assoc_node   s%    kkss##D$/r   c                R    | j                   j                  |= |j                          y r   )rV   r]   r;   )r   rK   s     r   _dissoc_nodezOrderedBidictBase._dissoc_node   s    &&t,r   c                n   t         |   |       | j                  }| j                  j                  }|j                          |j                  }| j                  x| j                  _        | j                  _	        | j                  j                  }t        |      D ]  \  }} | |       |r|n|        y)z"See :meth:`BidictBase._init_from`.N)rE   
_init_fromrW   rV   r]   clear__setitem__rY   r6   r4   rP   r	   )	r   otherbykeykorv_by_nodekorv_by_node_setnew_nodekvrF   s	           r   rk   zOrderedBidictBase._init_from   s    5!))11'33*.**4

::++e$ 	<DAqXZe;	<r   c                   t         |   |||||       | j                  | j                  }}| j                  | j
                  }	}|t        u rC|t        u r;| j                  j                         }
 ||
||       ||j                  ||
f       yy|t        ur\|t        urT|	r||   }||   }
n
||   }||   }
 ||        ||
||       |(|j                  ||
||f||||f|j                  ff       yy|t        ur,||	r|n|   } ||||       ||j                  ||||f       yy|t        usJ ||	r|n|   } ||||       ||j                  ||||f       yy)z*See :meth:`bidict.BidictBase._spec_write`.N)rE   _writerg   ri   rV   rW   r   rY   rP   appendextendr=   )r   newkeynewvaloldkeyoldvalunwritesassocdissocnode_by_korvro   newnodeoldnoderK   rF   s                r   rv   zOrderedBidictBase._write   s   vvvvx@(($*;*;v"00$++eW7!2jj..0G'66*# 12 $7"vW'< &v.&v.&v.&v.7O'66*#GVV4GVV4^^%!  $ 7"  %V<D$'#ff => $ (((  %V<D$'#ff => $r   c                &    | j                  d      S )z4Iterator over the contained keys in insertion order.FrG   _iterr:   s    r   __iter__zOrderedBidictBase.__iter__   s    zz%z((r   c                &    | j                  d      S )z<Iterator over the contained keys in reverse insertion order.TrG   r   r:   s    r   __reversed__zOrderedBidictBase.__reversed__   s    zz$z''r   FrG   c             #     K   | j                   j                  |      }| j                  j                  }| j                  r|D ]	  }||     y | j
                  }|D ]  }||   }||     y w)NrG   )rY   rL   rV   r]   rW   _invm)r   rH   nodesrp   rK   
key_by_valre   s          r   r   zOrderedBidictBase._iter   s}     

$$W$5))11;; )"4(() J &"4( o%&s   A.A0)r/   )rZ   MapOrItems[KT, VT]r[   r   r(   r)   )r(   zOrderedBidictBase[VT, KT])rK   r1   rd   r
   re   r   r(   r)   )rK   r1   r(   r)   )rn   r   r(   r)   )ry   r
   rz   r   r{   zOKT[KT]r|   zOVT[VT]r}   zUnwrites | Noner(   r)   )r(   t.Iterator[KT])rH   rQ   r(   r   )r+   r,   r-   r.   r?   r   r"   TYPE_CHECKINGpropertyr]   r_   ra   rg   ri   rk   rv   r   r   r   rR   rS   s   @r   rU   rU   q   sn    K&&L$ 		; 
;	7 
70
<+?Z)( (- 
& 
&r   rU   )r.   
__future__r   typingr"   r   r   _baser   r   _bidictr   r   r	   _typingr
   r   r   r   r   r   TypeVarr   Genericr   r1   rB   rU   r/   r   r   <module>r      s    * "  "           QYYt_:qyy} :"+ +<4 :w&
2r6* w&r   