
    _?i%x              
          S r SSKJrJr  SSKJrJrJrJrJ	r	J
r
Jr  Sr\
" S5      r " S S\\   5      r\
" S\S	9rS
\S\4S jrS
\S\4S jr " S S5      r " S S\\\4   5      r " S S\\\4   5      r " S S\5      r " S S\\\4   5      r\
" S5      r " S S\\\\4   5      r " S S\\\4   \\\\\4   4   \\\4   5      r " S S\\\   5      r " S S \\\   \\   5      rg!)"z
A BTree in the style of Cormen, Leiserson, and Rivest's "Algorithms" book, with
copy-on-write node updates, cursors, and optional space optimization for mostly-in-order
insertion.
    )MutableMapping
MutableSet)AnyCallableGenericOptionalTupleTypeVarcast   KTc                   &    \ rS rSrSrS\4S jrSrg)Element   z+All items stored in the BTree are Elements.returnc                     [         e)zFThe key for this element; the returned type must implement comparison.)NotImplementedErrorselfs    M/var/www/html/livechat/backend/venv/lib/python3.13/site-packages/dns/btree.pykeyElement.key   s    !!     N)__name__
__module____qualname____firstlineno____doc__r   r   __static_attributes__r   r   r   r   r      s    5"R "r   r   ET)boundtr   c                     U S-
  $ )zSThe minimum number of keys in a non-root node for a BTree with the specified
``t``
   r   r#   s    r   _MINr'      s     q5Lr   c                     SU -  S-
  $ )zGThe maximum number of keys in node for a BTree with the specified ``t``   r%   r   r&   s    r   _MAXr*   #   s    q519r   c                       \ rS rSrSrS rSrg)_Creator(   zA _Creator class instance is used as a unique id for the BTree which created
a node.

We use a dedicated creator rather than just a BTree reference to avoid circularity
that would complicate GC.
c                     [        U 5      S $ )Nx)idr   s    r   __str___Creator.__str__0   s    T(1r   r   N)r   r   r   r   r   r1   r    r   r   r   r,   r,   (   s    r   r,   c            	       (   \ rS rSrSr/ SQrS\S\S\4S jr	S\4S	 jr
S\4S
 jrS\S\\\4   4S jrS\SS4S jrS\S\\S   \4   4S jrS\S\S-  4S jrS\SS4S jrS\S\S\S-  4S jrS\S\S4   4S jrSSS\S\4S jrSSS\S\4S jrSSS\SSSS4S jrSSS\SS4S jrS\4S  jrS\4S! jrSSS\SS4S" jrS\S\S   S#\S-  S\S-  4S$ jrS%\ \/S4   SS4S& jr!S%\ S/S4   SS4S' jr"S\S\S   4S( jr#S\SS4S) jr$S* r%S+r&g),_Node4   z>A Node in the BTree.

A Node (leaf or internal) of the BTree.
r#   creatoris_leafeltschildrenr#   r7   r8   c                 T    US:  d   eXl         X l        X0l        / U l        / U l        g )N   r6   )r   r#   r7   r8   s       r   __init___Node.__init__<   s*    Avv 	-/r   r   c                     [        U R                  5      [        U R                  5      ::  d   e[        U R                  5      [        U R                  5      :H  $ )z/Does this node have the maximal number of keys?)lenr9   r*   r#   r   s    r   
is_maximal_Node.is_maximalD   ;    499~dff---499~dff--r   c                     [        U R                  5      [        U R                  5      :  d   e[        U R                  5      [        U R                  5      :H  $ )z/Does this node have the minimal number of keys?)r@   r9   r'   r#   r   s    r   
is_minimal_Node.is_minimalI   rC   r   r   c                 f   [        U R                  5      nUS:  a'  XR                  US-
     R                  5       :  a  US4$ Sn[        U R                  5      nUS-
  nSnX$::  aJ  X$-   S-  nU R                  U   R                  5       nX:X  a  UnSn X54$ X:  a  UnUS-
  nOUS-   nX$::  a  MJ  X54$ )zGet the index of the ``Element`` matching ``key`` or the index of its
least successor.

Returns a tuple of the index and an ``equal`` boolean that is ``True`` iff.
the key was found.
r   r%   Fr)   T)r@   r9   r   )r   r   lirequalmks           r   search_in_node_Node.search_in_nodeN   s     		Nq5S99QU+//11e8O		NEf1A		!  "Ax x EE f xr   indexz_Node[KT, ET]c                     U R                   (       a   eU R                  U   nUR                  U R                  5      nU(       a  X0R                  U'   U$ U$ N)r8   r:   	maybe_cowr7   )r   rP   childcloneds       r   maybe_cow_child_Node.maybe_cow_childk   sH    <<e$.#)MM% MLr   c                     U R                  U5      u  p#U(       a  X4$ U R                  (       a  gU R                  U5      nUR                  U5      $ )zGet the node associated with key and its index, doing
copy-on-write if we have to descend.

Returns a tuple of the node and the index, or the tuple ``(None, 0)``
if the key was not found.
Nr   )rN   r8   rV   	_get_node)r   r   rI   rK   rT   s        r   rZ   _Node._get_nodeu   sJ     &&s+9\\((+E??3''r   Nc                     U R                  U5      u  p#U(       a  U R                  U   $ U R                  (       a  gU R                  U   R	                  U5      $ )z8Get the element associated with *key* or return ``None``N)rN   r9   r8   r:   get)r   r   rI   rK   s       r   r]   	_Node.get   sI    &&s+99Q<\\==#'',,r   c                    US:X  a  gU R                   US-
     n[        UR                  5      [        U R                  5      :X  a  gU R                  US-
  5      n[        UR                  5      [        U R                  5      :  aI  UR                  XS-
  5      (       d  g[        UR                  5      [        U R                  5      :  a  MH  gg)a  Try to minimize the number of Nodes in a BTree where the insertion
is done in-order or close to it, by stealing as much as we can from our
right sibling.

If we don't do this, then an in-order insertion will produce a BTree
where most of the nodes are minimal.
r   Nr%   )r:   r@   r9   r*   r#   rV   try_right_steal)r   rP   lefts      r   optimize_in_order_insertion!_Node.optimize_in_order_insertion   s     A:}}UQY'tyy>T$&&\)##EAI.$))ntDFF|+''ai88 $))ntDFF|+r   elementin_orderc                    U R                  5       (       a   e UR                  5       nU R                  U5      u  pEU(       a  U R                  U   nXR                  U'   U$ U R                  (       a  U R                  R                  XA5        g U R                  U5      nUR                  5       (       a  U R                  " UR                  5       6   M  UR                  X5      nU(       a  U R                  U5        U$ rR   )rA   r   rN   r9   r8   insertrV   adoptsplitinsert_nonfullrb   )	r   rd   re   r   rI   rK   oldrT   oelts	            r   rj   _Node.insert_nonfull   s    ??$$$$++-C**3/HAiil&		!
		  ,,,Q/##%%JJ. ++G>44Q7r   c                    U R                  5       (       d   eU R                  U R                  U R                  U R                  5      n[        U R                  [        U R                  5      S-   S 5      Ul        U R                  [        U R                  5         n[        U R                  S[        U R                  5       5      U l        U R                  (       df  [        U R                  [        U R                  5      S-   S 5      Ul        [        U R                  S[        U R                  5      S-    5      U l        XU4$ )zASplit a maximal node into two minimal ones and a central element.r%   N)	rA   	__class__r#   r7   r8   listr9   r'   r:   )r   rightmiddles      r   ri   _Node.split   s        tvvt||T\\B$))DL1$4$678
4<(>T$&&\23	||!$--TVVq0@0B"CDEN /Adff1A!BCDMU""r   parentc                    US:w  a  UR                   US-
     nUR                  5       (       d  UR                  US-
  5      nUR                  US-
     nUR                  R	                  5       UR                  US-
  '   U R                  R                  SU5        UR                  (       dI  U R                  (       a   eUR                   R	                  5       nU R                   R                  SU5        gg)zTry to steal from this Node's left sibling for balancing purposes.

Returns ``True`` if the theft was successful, or ``False`` if not.
r   r%   TF)r:   rE   rV   r9   poprg   r8   )r   rt   rP   ra   eltrT   s         r   try_left_steal_Node.try_left_steal   s    
 A:??519-D??$$--eai8kk%!),)-EAI&		  C(||#||++ MM--/EMM((E2r   c                    US-   [        UR                  5      :  a  UR                  US-      nUR                  5       (       d  UR                  US-   5      nUR                  U   nUR                  R                  S5      UR                  U'   U R                  R                  U5        UR                  (       dI  U R                  (       a   eUR                  R                  S5      nU R                  R                  U5        gg)zTry to steal from this Node's right sibling for balancing purposes.

Returns ``True`` if the theft was successful, or ``False`` if not.
r%   r   TF)r@   r:   rE   rV   r9   rv   appendr8   )r   rt   rP   rq   rw   rT   s         r   r`   _Node.try_right_steal   s    
 19s6??++OOEAI.E##%%..uqy9kk%(%*ZZ^^A%6E"		  %}}#||++!NN..q1EMM((/r   ra   rr   rq   c                    U R                  5       (       a   eU R                  (       a   eUR                  5       nU R                  U5      u  pVU(       a   eU R                  R                  XR5        [        U R                  5      S:X  a	  X/U l        gU R                  U   U:X  d   eU R                  R                  US-   U5        g)zAdopt left, middle, and right into our Node (which must not be maximal,
and which must not be a leaf).  In the case were we are not the new root,
then the left child must already be in the Node.r   r%   N)rA   r8   r   rN   r9   rg   r@   r:   )r   ra   rr   rq   r   rI   rK   s          r   rh   _Node.adopt   s     ??$$$$<<jjl&&s+y		#t}}"!MDM==#t+++MM  Q.r   c                 `   UR                   R                  US-   5      nU R                  R                  UR                  R                  U5      5        U R                  R	                  UR                  5        U R
                  (       d&  U R                   R	                  UR                   5        gg)z>Merge this node's parent and its right sibling into this node.r%   N)r:   rv   r9   r{   extendr8   )r   rt   rP   rq   s       r   merge_Node.merge   sq    ##EAI.		/0		$||MM  0 r   c                 |    U R                   (       a  U R                  S   $ U R                  S   R                  5       $ )z"The least element in this subtree.r   )r8   r9   r:   minimumr   s    r   r   _Node.minimum  s/    <<99Q<==#++--r   c                 |    U R                   (       a  U R                  S   $ U R                  S   R                  5       $ )z%The greatest element in this subtree.)r8   r9   r:   maximumr   s    r   r   _Node.maximum  s/    <<99R= ==$,,..r   c                    UR                   (       a   eU R                  X5      (       a  gU R                  X5      (       a  gUS:X  a  U R                  X5        gUR	                  US-
  5      nUR                  XS-
  5        g)zThis Node is minimal, and we want to make it non-minimal so we can delete.
We try to steal from our siblings, and if that doesn't work we will merge
with one of them.Nr   r%   )r8   rx   r`   r   rV   )r   rt   rP   ra   s       r   balance_Node.balance  sr     >>!!v--..A:JJv% ))%!)4DJJvqy)r   exactc                 P   Ub  U R                  5       (       a   eU R                  U5      u  pESnU(       a  Ub  U R                  U   ULa  [        S5      eU R                  (       a  U R                  R                  U5      $ SnUnU R                  US-      R                  5       nUR                  5       nUS-   nU R                  (       a  Ub  [        S5      egU R                  U5      nUR                  5       (       aS  UR                  X5        U R                  U5      u  pEU(       a   eU R                  U   nUR                  5       (       a   eUR                  XU5      n	Ub<  U R                  U5      u  pU
c   eU	c   eU
R                  U   nXR                  U'   Un	U	$ )zDelete an element matching *key* if it exists.  If *exact* is not ``None``
then it must be an exact match with that element.  The Node must not be
minimal unless it is the root.Nz'exact delete did not match existing eltr%   zexact delete had no match)rE   rN   r9   
ValueErrorr8   rv   r:   r   r   rV   r   deleterZ   )r   r   rt   r   rI   rK   original_keyleast_successorrT   rw   noderl   s               r   r   _Node.delete&  s    ~T__%6%666&&s+ TYYq\%> !JKK||yy}}Q'' EL"mmAE2::<O!%%'CAA<<  !<==$$Q'MM$"**3/HA9MM!$E''))))ll3e,#nn\2GD###?"?99Q<DIIaLC
r   visitc                    [        U R                  5       H<  u  p#U R                  (       d  U R                  U   R	                  U5        U" U5        M>     U R                  (       d  U R                  S   R	                  U5        gg)z-Call *visit* on all of the elements in order.r   N)	enumerater9   r8   r:   visit_in_order)r   r   rI   rw   s       r   r   _Node.visit_in_orderU  sa    		*FA<<a //6#J + ||MM",,U3 r   c                     U" U 5        U R                   (       d%  U R                   H  nUR                  U5        M     gg)z?Visit nodes in preorder.  This method is only used for testing.N)r8   r:   _visit_preorder_by_node)r   r   rT   s      r   r   _Node._visit_preorder_by_node^  s1    d||--e4 ' r   c                 D    U R                   ULa  U R                  U5      $ g)zReturn a clone of this Node if it was not created by *creator*, or ``None``
otherwise (i.e. copy for copy-on-write if we haven't already copied it).N)r7   clone)r   r7   s     r   rS   _Node.maybe_cowe  s"     <<w&::g&&r   c                    U R                  U R                  XR                  5      nUR                  R	                  U R                  5        U R                  (       d%  UR
                  R	                  U R
                  5        U$ )z+Make a shallow-copy duplicate of this node.)ro   r#   r8   r9   r   r:   )r   r7   rU   s      r   r   _Node.clonem  sR    >499%||OO""4==1r   c           	          U R                   (       d;  SSR                  U R                   Vs/ s H  n[        U5      S PM     sn5      -   nOSn[        U 5      S SU R                   SU R
                   U 3$ s  snf )N r/    )r8   joinr:   r0   r7   r9   )r   cr:   s      r   r1   _Node.__str__u  sm    ||SXXT]]&K]"Q%|]&KLLHHT(1Qt||nAdii[
CC 'Ls   A9
)r:   r7   r9   r8   r#   )'r   r   r   r   r   	__slots__intr,   boolr=   rA   rE   r   tuplerN   rV   r	   r   rZ   r!   r]   rb   rj   ri   rx   r`   rh   r   r   r   r   r   r   r   r   rS   r   r1   r    r   r   r   r4   r4   4   s7   
 @I0# 0 04 0.D .
.D .
" sDy)9 :S _ (R (E(?*CS*H$I ( -r -b4i -  $b D R$Y 2
#u_b/AB 
#_ S T &o c d &// /2 /o /RV /"1O 1C 1D 1. ./ /*o *c *d *&--'8-ACd-	d-^4HbT4Z$8 4T 45X6G6M-N 5SW 5 h.G X / Dr   r4   c                       \ rS rSrSrSS jrSS jrSS jrS rS	 r	S\
S-  4S
 jrS\
S-  4S jrS\S\SS4S jrSS\S\SS4S jjrSS jrSS jrS rS rSrg)Cursori}  zA seekable cursor for a BTree.

If you are going to use a cursor on a mutable BTree, you should use it
in a ``with`` block so that any mutations of the BTree automatically park
the cursor.
c                     Xl         S U l        SU l        SU l        SU l        / U l        SU l        S U l        SU l        g )Nr   FT)	btreecurrent_nodecurrent_indexrecurse
increasingparentsparkedparking_keyparking_key_read)r   r   s     r   r=   Cursor.__init__  sE    
*. #$02&* %r   r   Nc                 r   U R                   c   eU R                   R                  (       d  U R                  R                  U R                   U R                  45        U R                   R
                  U R                     U l         U R                   c   eSU l        U R                   R                  (       d  M  g g rY   )r   r8   r   r{   r   r:   r   s    r   _seek_leastCursor._seek_least  s       ,,,##++LL!2!2D4F4F GH $ 1 1 : :4;M;M ND$$000!"D	 ##+++r   c                    U R                   c   eU R                   R                  (       d  U R                  R                  U R                   U R                  45        U R                   R
                  U R                     U l         U R                   c   e[        U R                   R                  5      U l        U R                   R                  (       d  M  g g rR   )r   r8   r   r{   r   r:   r@   r9   r   s    r   _seek_greatestCursor._seek_greatest  s       ,,,##++LL!2!2D4F4F GH $ 1 1 : :4;M;M ND$$000!$T%6%6%;%;!<D	 ##+++r   c                 6    U R                   (       d  SU l         gg)aw  Park the cursor.

A cursor must be "parked" before mutating the BTree to avoid undefined behavior.
Cursors created in a ``with`` block register with their BTree and will park
automatically.  Note that a parked cursor may not observe some changes made when
it is parked; for example a cursor being iterated with next() will not see items
inserted before its current position.
TN)r   r   s    r   parkCursor.park  s     {{DK r   c                    U R                   (       ay  U R                  b]  U R                  nU R                  (       a  U R                  (       + nOU R                  nU R	                  U R                  U5        Xl        SU l         S U l        g g NF)r   r   r   r   seek)r   r   befores      r   _maybe_unparkCursor._maybe_unpark  sm    ;;+!__
(( "&0F
 "__F		$**F3",DK#D! r   c                 z   U R                  5         SU l        U R                  c|  U R                  S:X  a  gU R                  S:X  d   eU R                  R
                  U l        [        U R                  R
                  R                  5      U l        U R                  5          U R                  (       a(  U R                  (       d  U R                  5         SU l	        SU l
        U =R                  S-  sl        U R                  S:  ac  U R                  R                  U R                     nU R                  R                  (       d  SU l	        UR                  5       U l        SU l        U$ [        U R                  5      S:  a(  U R                  R                  5       u  U l        U l        OSU l        SU l        gGM  )zAGet the previous element, or return None if on the left boundary.Nr   r%   TF)r   r   r   r   r   rootr@   r9   r   r   r   r8   r   r   r   rv   r   rw   s     r   prevCursor.prev  s_   $!!Q&))Q... %)JJOO!%()=)=%>"##%|| '')$#DO!#!!Q&'',,T-?-?@((00#'DL#&779 (,%
t||$q(<@LL<L<L<N9D%t'9(,D%)*D&- r   c                 f   U R                  5         SU l        U R                  cU  U R                  S:X  a  gU R                  S:X  d   eU R                  R
                  U l        SU l        U R                  5          U R                  (       a(  U R                  (       a  U R                  5         SU l        SU l        U R                  [        U R                  R                  5      :  ax  U R                  R                  U R                     nU =R                  S-  sl        U R                  R                  (       d  SU l        UR                  5       U l        SU l        U$ [        U R                  5      S:  a(  U R                  R                  5       u  U l        U l        OSU l        SU l        gGM8  )z>Get the next element, or return None if on the right boundary.Nr%   r   TF)r   r   r   r   r   r   r   r   r   r@   r9   r8   r   r   r   rv   r   s     r   nextCursor.next  s]   $!!Q&))Q... %)JJOO!%&"  "||?? $$&$"DO!!C(9(9(>(>$??'',,T-?-?@""a'"((00#'DL#&779 (,%
t||$q(<@LL<L<L<N9D%t'9(,D%)*D&- r   r   rI   c                 4    U(       a  X l         g US-   U l         g )Nr%   )r   )r   r   rI   s      r   _adjust_for_beforeCursor._adjust_for_before  s    !"!"QDr   r   c                    U R                   R                  U l        U R                  c   eSU l        / U l        X l        SU l        Xl        SU l        U R                  R                  (       d  U R                  R                  U5      u  p4U(       a:  U R                  X#5        U(       a  U R                  5         gU R                  5         gU R                  R                  U R                  U45        U R                  R                  U   U l        U R                  c   eU R                  R                  (       d  M  U R                  R                  U5      u  p4U(       a  U R                  X#5        gX0l        g)a  Seek to the specified key.

If *before* is ``True`` (the default) then the cursor is positioned just
before *key* if it exists, or before its least successor if it doesn't.  A
subsequent next() will retrieve this value.  If *before* is ``False``, then
the cursor is positioned just after *key* if it exists, or its greatest
precessessor if it doesn't.  A subsequent prev() will return this value.
NF)r   r   r   r   r   r   r   r   r   r8   rN   r   r   r   r{   r:   r   )r   r   r   rI   rK   s        r   r   Cursor.seek  s8    !JJOO  ,,,  %##++((77<HA''2'')  $$&LL!2!2A 67 $ 1 1 : :1 =D$$000 ##+++ $$33C8##F.!"r   c                 f    SU l         SU l        SU l        SU l        / U l        SU l        SU l        g)zSeek to the left boundary (i.e. just before the least element).

A subsequent next() will return the least element if the BTree isn't empty.Nr   FTr   r   r   r   r   r   r   r   s    r   
seek_firstCursor.seek_first?  s8     !r   c                 f    SU l         SU l        SU l        SU l        / U l        SU l        SU l        g)zSeek to the right boundary (i.e. just after the greatest element).

A subsequent prev() will return the greatest element if the BTree isn't empty.
Nr%   Fr   r   s    r   	seek_lastCursor.seek_lastK  s8    
 !r   c                 <    U R                   R                  U 5        U $ rR   )r   register_cursorr   s    r   	__enter__Cursor.__enter__X  s    

""4(r   c                 :    U R                   R                  U 5        gr   )r   deregister_cursor)r   exc_type	exc_value	tracebacks       r   __exit__Cursor.__exit__\  s    

$$T*r   )	r   r   r   r   r   r   r   r   r   )r   zBTree[KT, ET]r   N)T)r   r   r   r   r   r=   r   r   r   r   r!   r   r   r   r   r   r   r   r   r   r   r   r    r   r   r   r   r   }  s    &#=
$&& b4i & P& b4i & P' '# '$ '!# !#D !#D !#F
  r   r   c                       \ rS rSrSrSrg)	Immutableia  zThe BTree is immutable.r   N)r   r   r   r   r   r    r   r   r   r   r   a  s    !r   r   c                   ^   \ rS rSrSr\SS.S\S\S    4S jjrS r	S S
 jr
S!S\S\S	\S-  4S jjrS\S	\S-  4S jrS\S\S-  S	\S-  4S jrS\S	\S-  4S jrS\S	\S-  4S jrS rS\\/S4   S	S4S jrS\\/S4   S	S4S jrS	\\\4   4S jrS\S	S4S jrS\S	S4S jrS rS rSrg)"BTreeie  z2An in-memory BTree with copy-on-write and cursors.Nr#   originalr#   r   c                   [        5       U l        SU l        U   U   U   [        5       U l        UbP  UR                  (       d  [        S5      eUR                  U l        UR                  U l        UR                  U l        gUS:  a  [        S5      eXl        [        U R                  U R                  S5      U l        SU l        g)zCreate a BTree.

If *original* is not ``None``, then the BTree is shallow-cloned from
*original* using copy-on-write.  Otherwise a new BTree with the specified
*t* value is created.

The BTree is not thread-safe.
FNzoriginal BTree is not immutabler<   zt must be >= 3Tr   )
r,   r7   
_immutablesetcursorsr   r#   r   sizer4   )r   r#   r   s      r   r=   BTree.__init__h  s      z$'E&& !BCCZZDF DI DI1u !122FdffdllD9DIDIr   c                 6    U R                   (       d  SU l         gg)zMake the BTree immutable.

Attempts to alter the BTree after making it immutable will raise an
Immutable exception.  This operation cannot be undone.
TN)r   r   s    r   make_immutableBTree.make_immutable  s     "DO r   r   c                 x    U R                   (       a  [        eU R                   H  nUR                  5         M     g rR   )r   r   r   r   r   cursors     r   _check_mutable_and_parkBTree._check_mutable_and_park  s%    ??OllFKKM #r   rw   re   c                    U R                  5         U R                  R                  U R                  5      nU(       a  X0l        U R                  R	                  5       (       aY  U R                  n[        U R                  U R                  S5      U l        U R                  R                  " UR                  5       6   U R                  R                  X5      nUc  U =R                  S-  sl
        U$ )a  Insert the element into the BTree.

If *in_order* is ``True``, then extra work will be done to make left siblings
full, which optimizes storage space when the the elements are inserted in-order
or close to it.

Returns the previously existing element at the element's key or ``None``.
Fr%   )r   r   rS   r7   rA   r4   r#   rh   ri   rj   r   )r   rw   re   rU   old_rootrl   s         r   insert_elementBTree.insert_element  s     	$$&$$T\\2I99!!yyHdffdllE:DIIIOOX^^-.yy''6<IINIr   r   c                 8    U R                   R                  U5      $ )zXGet the element matching *key* from the BTree, or return ``None`` if it
does not exist.
)r   r]   r   r   s     r   get_elementBTree.get_element  s     yy}}S!!r   r   c                    U R                  5         U R                  R                  U R                  5      nU(       a  X0l        U R                  R	                  US U5      nUb  U =R
                  S-  sl        [        U R                  R                  5      S:X  a^  U R                  R                  (       dC  [        U R                  R                  5      S:X  d   eU R                  R                  S   U l        U$ )Nr%   r   )
r   r   rS   r7   r   r   r@   r9   r8   r:   )r   r   r   rU   rw   s        r   _deleteBTree._delete  s    $$&$$T\\2IiisD%0?IINI499>>"a' yy((tyy112a777 $		 2 21 5DI
r   c                 &    U R                  US5      $ )zrDelete the element matching *key* from the BTree.

Returns the matching element or ``None`` if it does not exist.
N)r  r  s     r   
delete_keyBTree.delete_key  s    
 ||C&&r   rd   c                 R    U R                  UR                  5       U5      nX!L d   eU$ )zgDelete *element* from the BTree.

Returns the matching element or ``None`` if it was not in the BTree.
)r  r   )r   rd   delts      r   delete_exactBTree.delete_exact  s)    
 ||GKKM73r   c                     U R                   $ rR   )r   r   s    r   __len__BTree.__len__      yyr   r   c                 :    U R                   R                  U5        g)zBCall *visit*(element) on all elements in the tree in sorted order.N)r   r   r   r   s     r   r   BTree.visit_in_order  s    		  'r   c                 :    U R                   R                  U5        g rR   )r   r   r  s     r   r   BTree._visit_preorder_by_node  s    		))%0r   c                     [        U 5      $ )zCreate a cursor.)r   r   s    r   r   BTree.cursor  s    d|r   r   c                 :    U R                   R                  U5        g)z4Register a cursor for the automatic parking service.N)r   addr   s     r   r   BTree.register_cursor  s     r   c                 :    U R                   R                  U5        g)z7Deregister a cursor from the automatic parking service.N)r   discardr   s     r   r   BTree.deregister_cursor  s    V$r   c                      U R                  U S9$ )N)r   ro   r   s    r   __copy__BTree.__copy__  s    ~~t~,,r   c              #      #    U R                  5        n UR                  5       nUc  OUR                  5       v   M(  S S S 5        g ! , (       d  f       g = f7frR   )r   r   r   )r   r   rw   s      r   __iter__BTree.__iter__  s?     [[]fkkm;ggi	  ]]s   A*A	A
AA)r   r7   r   r   r   r#   r   )F)r   r   r   r   r   	DEFAULT_Tr   r   r=   r   r   r!   r   r   r   r  r  r  r  r  r   r   r4   r   r   r   r   r   r!  r$  r    r   r   r   r   r   e  s6   <#,D S 8I <#"  d ."r "b4i "2 b4i BI "'b 'R$Y 'B 29 (HbT4Z$8 (T (1Xugtm-D 1 1r2v !f ! !% %4 %- r   r   VTc                   R    \ rS rSrSrS\S\4S jrS\4S jrS\4S jr	S	 r
S
 rSrg)KVi  z/The BTree element type used in a ``BTreeDict``.r   valuec                     Xl         X l        g rR   _key_value)r   r   r*  s      r   r=   KV.__init__  s    	r   r   c                     U R                   $ rR   r-  r   s    r   r   KV.key  r  r   c                     U R                   $ rR   )r.  r   s    r   r*  KV.value  s    {{r   c                 <    SU R                    SU R                   S3$ NzKV(z, )r,  r   s    r   r1   
KV.__str__	      TYYKr$++a00r   c                 <    SU R                    SU R                   S3$ r6  r,  r   s    r   __repr__KV.__repr__  r9  r   r,  N)r   r   r   r   r   r   r'  r=   r   r*  r1   r;  r    r   r   r   r)  r)    s:    9B r R r 11r   r)  c                      ^  \ rS rSrSr\SSS.S\S\S-  S\4U 4S	 jjjr	S
\
S\4S jrS
\
S\SS4S jrS
\
SS4S jrSrU =r$ )	BTreeDicti  zwA MutableMapping implemented with a BTree.

Unlike a normal Python dict, the BTreeDict may be mutated while iterating.
NFr#   r   re   r#   r   re   c                ,   > [         TU ]  XS9  X0l        g Nr   superr=   re   r   r#   r   re   ro   s       r   r=   BTreeDict.__init__       	10 r   r   r   c                 r    U R                  U5      nUc  [        e[        [        U5      R	                  5       $ rR   )r  KeyErrorr   r)  r*  )r   r   rw   s      r   __getitem__BTreeDict.__getitem__   s1    s#;NC=&&((r   r*  c                 P    [        X5      nU R                  X0R                  5        g rR   )r)  r   re   )r   r   r*  rw   s       r   __setitem__BTreeDict.__setitem__'  s    nC/r   c                 4    U R                  U5      c  [        eg rR   )r  rH  r  s     r   __delitem__BTreeDict.__delitem__+  s    ??3'N (r   re   )r   r   r   r   r   r&  r   r   r   r=   r   r'  rI  rL  rO  r    __classcell__r   s   @r   r>  r>    s     !%! ! $,	!
 ! !)r )b )0r 0" 0 0r d  r   r>  c                   4    \ rS rSrSrS\4S jrS\4S jrSrg)	Memberi0  z.The BTree element type used in a ``BTreeSet``.r   c                     Xl         g rR   r1  r  s     r   r=   Member.__init__3  s    	r   r   c                     U R                   $ rR   r1  r   s    r   r   
Member.key6  r  r   r1  N)	r   r   r   r   r   r   r=   r   r    r   r   r   rT  rT  0  s    8B R r   rT  c                      ^  \ rS rSrSr\SSS.S\S\S-  S\4U 4S	 jjjr	S
\
S\4S jrS\SS4S jrS\SS4S jrSrU =r$ )BTreeSeti:  zqA MutableSet implemented with a BTree.

Unlike a normal Python set, the BTreeSet may be mutated while iterating.
NFr?  r#   r   re   c                ,   > [         TU ]  XS9  X0l        g rA  rB  rD  s       r   r=   BTreeSet.__init__@  rF  r   r   r   c                 (    U R                  U5      S L$ rR   )r  r  s     r   __contains__BTreeSet.__contains__J  s    $D00r   r*  c                 P    [        U5      nU R                  X R                  5        g rR   )rT  r   re   )r   r*  rw   s      r   r  BTreeSet.addM  s    UmC/r   c                 &    U R                  U5        g rR   )r  )r   r*  s     r   r  BTreeSet.discardQ  s    r   rQ  )r   r   r   r   r   r&  r   r   r   r=   r   r^  r   r  r  r    rR  r   s   @r   rZ  rZ  :  s}     !%! ! $,	!
 ! !1 1 10 0 0R D  r   rZ  N)r   collections.abcr   r   typingr   r   r   r   r	   r
   r   r&  r   r   r!   r   r'   r*   r,   r4   r   	Exceptionr   r   r'  r)  r>  rT  rZ  r   r   r   <module>rg     sE   7 I I I	T]"gbk " T!C C C C 
	 	FDGBFO FDR
aWRV_ aH"	 "Q GBFO Q h T]1'"b&/ 1(Br2b"f:~!6r2v8N @Wgbk ugbk:b> r   