
    i"                    V   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dS )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dS )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.
    slotstrreturnNonec                   || _         d S Nr   )selfr   s     >/usr/local/lib/python3.11/dist-packages/bidict/_orderedbase.py__init__zWeakAttr.__init__,   s    			    instancet.Anyvaluer   c                L    t          || j        t          |                     d S r   )setattrr   weakref)r   r   r    s      r   __set__zWeakAttr.__set__/   s"    $)WU^^44444r   N_WeakAttr__ownerc                l    t          j        t           t          || j                                        S r   )tcastr   getattrr   )r   r   r%   s      r   __get__zWeakAttr.__get__2   s)    vb6'(DI6688999r   )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   %   s_            5 5 5 5: : : : : : :r   r   c                  \    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
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__r2   nxtzNode | WeakAttr[Node]r6   r   r   c                "    || _         || _        d S r   r4   r6   )r   r4   r6   s      r   r   zNode.__init__D   s    r   c                J    | j         | j        _         | j        | j         _        dS )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     xxr   c                6    | x| j         _        | j        _         dS )z=Restore self between prv and nxt after unlinking (see above).Nr8   r:   s    r   relinkzNode.relinkO   s    &**tx|||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   s           #(444C44443I          + + + + + +r   r1   c                  b     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   r   r   c                L    t                                          | |            d S r   )superr   )r   	__class__s    r   r   zSentinelNode.__init__^   s#    t$$$$$r   FreverserH   boolt.Iterator[Node]c             #  r   K   |rdnd}t          | |          }|| ur|V  t          ||          }|| udS dS )z/Iterator yielding nodes in the requested order.r4   r6   N)r)   )r   rH   attrnodes       r   	iternodeszSentinelNode.iternodesa   s_      *uuUtT""$JJJ4&&D $r   r1   c                P    | 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+    8$''"**txr   r>   )rH   rI   r   rJ   )r   r1   )r+   r,   r-   r.   r   r6   r?   r@   r   rN   rR   __classcell__rF   s   @r   rB   rB   T   s           #(444C444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d0 fdZd1 fd#Zd2d%Zd2d&Zd'd(d3d*Z xZS )4OrderedBidictBasezABase class implementing an ordered :class:`BidirectionalMapping`.zbidict[t.Any, Node]_node_by_korvrI   _bykeyr/   argMapOrItems[KT, VT]kwr   r   r   c                   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)rB   _sntlr   rW   rX   rE   r   )r   rY   r[   rF   s      r   r   zOrderedBidictBase.__init__w   sH     "^^
#XX#######r   OrderedBidictBase[VT, KT]c                    d S r   r/   r:   s    r   inversezOrderedBidictBase.inverse   s    8;r   c                    d S r   r/   r:   s    r   invzOrderedBidictBase.inv   s    47Cr   c                    t          j        t          t          t          f         t                                                                }| j        |_        | j        |_        | j	         |_	        |S r   )
r'   r(   rV   r   r
   rE   _make_inverser]   rW   rX   )r   rb   rF   s     r   rd   zOrderedBidictBase._make_inverse   sR    f&r2v.0E0E0G0GHHJ	 ._

r   rM   r1   keyr
   valc                R    | j         r|n|}| j                            ||           d S r   )rX   rW   forceput)r   rM   re   rf   korvs        r   _assoc_nodezOrderedBidictBase._assoc_node   s1    k*sss##D$/////r   c                H    | j         j        |= |                                 d S r   )rW   r`   r;   )r   rM   s     r   _dissoc_nodezOrderedBidictBase._dissoc_node   s!    &t,r   otherc                R   t                                          |           | j        }| j        j        }|                                 |j        }| j        x| j        _        | j        _	        | j        j
        }t          |          D ]\  }} | |            |r|n|           dS )z"See :meth:`BidictBase._init_from`.N)rE   
_init_fromrX   rW   r`   clear__setitem__r]   r6   r4   rR   r	   )	r   rm   bykeykorv_by_nodekorv_by_node_setnew_nodekvrF   s	           r   ro   zOrderedBidictBase._init_from   s    5!!!)1'3*.*4
:+e$$ 	< 	<DAqXXZZe):;;;;	< 	<r   newkeynewvaloldkeyOKT[KT]oldvalOVT[VT]unwritesUnwrites | Nonec                *   t                                          |||||           | j        | j        }}| j        | j        }	}|t          u rL|t          u rC| j                                        }
 ||
||           ||	                    ||
f           dS dS |t          urp|t          urg|	r||         }||         }
n||         }||         }
 ||            ||
||           |(|
                    ||
||f||||f|j        ff           dS dS |t          ur8||	r|n|         } ||||           ||	                    ||||f           dS dS |t          usJ ||	r|n|         } ||||           ||	                    ||||f           dS dS )z*See :meth:`bidict.BidictBase._spec_write`.N)rE   _writerj   rl   rW   rX   r   r]   rR   appendextendr=   )r   rx   ry   rz   r|   r~   assocdissocnode_by_korvrr   newnodeoldnoderM   rF   s                r   r   zOrderedBidictBase._write   s2   vvvvx@@@($*;v"0$+eW7!2!2j..00GE'66***# 122222 $#7""vW'<'<  /&v.&v.&v.&v.F7OOOE'66***#GVV4GVV4^%!      $# 7""  % ;V<DE$'''#ff =>>>>> $# ((((  % ;V<DE$'''#ff =>>>>> $#r   t.Iterator[KT]c                .    |                      d          S )z4Iterator over the contained keys in insertion order.FrG   _iterr:   s    r   __iter__zOrderedBidictBase.__iter__   s    zz%z(((r   c                .    |                      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   rH   c             #     K   | j                             |          }| j        j        }| j        r|D ]}||         V  d S | j        }|D ]}||         }||         V  d S )NrG   )r]   rN   rW   r`   rX   _invm)r   rH   nodesrs   rM   
key_by_valrf   s          r   r   zOrderedBidictBase._iter   s      
$$W$55)1; 	& ) )"4((((() ) J & &"4( o%%%%& &r   )r/   )rY   rZ   r[   r   r   r   )r   r^   )rM   r1   re   r
   rf   r   r   r   )rM   r1   r   r   )rm   rZ   r   r   )rx   r
   ry   r   rz   r{   r|   r}   r~   r   r   r   )r   r   )rH   rI   r   r   )r+   r,   r-   r.   r?   r   r'   TYPE_CHECKINGpropertyr`   rb   rd   rj   rl   ro   r   r   r   r   rS   rT   s   @r   rV   rV   q   se        KK&&&&LLL$ $ $ $ $ $ $ 	 8	;;; 
;	777 
7     0 0 0 0   
< 
< 
< 
< 
< 
<+? +? +? +? +? +?Z) ) ) )( ( ( ( (- 
& 
& 
& 
& 
& 
& 
& 
& 
& 
&r   rV   )r.   
__future__r   typingr'   r#   r   _baser   r   _bidictr   r   r	   _typingr
   r   r   r   r   r   TypeVarr   Genericr   r1   rB   rV   r/   r   r   <module>r      s   * ) " " " " " "     " " " " " "                                                             QYt__: : : : :qy} : : :"+ + + + + + + +<    4   :w& w& w& w& w&
2r6* w& w& w& w& w&r   