Ë
    Q5ýjWx  ã            
       óÖ  — d Z ddlmZmZmZ ddlmZmZmZ dZ	 ed«      Z
 G d„ dee
   «      Z ede¬	«      Zd
edefd„Zd
edefd„Z G d„ d«      Z G d„ dee
ef   «      Z G d„ dee
ef   «      Z G d„ de«      Z G d„ dee
ef   «      Z ed«      Z G d„ deee
ef   «      Z G d„ dee
ef   ee
ee
ef   f   ee
ef   «      Z G d„ deee
   «      Z G d„ d eee
   ee
   «      Zy!)"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.
é    )ÚCallableÚMutableMappingÚ
MutableSet)ÚGenericÚTypeVarÚcasté   ÚKTc                   ó   — e Zd ZdZdefd„Zy)ÚElementz+All items stored in the BTree are Elements.Úreturnc                 ó   — t         ‚)zFThe key for this element; the returned type must implement comparison.)ÚNotImplementedError©Úselfs    úC/root/telegram-saas/.venv/lib/python3.12/site-packages/dns/btree.pyÚkeyzElement.key   s   € ä!Ð!ó    N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r
   r   © r   r   r   r      s   „ Ù5ð"�Rô "r   r   ÚET)ÚboundÚtr   c                 ó   — | dz
  S )z[The minimum number of keys in a non-root node for a BTree with the specified
    ``t``
    é   r   ©r   s    r   Ú_MINr       s   € ð ˆq‰5€Lr   c                 ó   — d| z  dz
  S )zGThe maximum number of keys in node for a BTree with the specified ``t``é   r   r   r   s    r   Ú_MAXr#   #   s   € àˆq‰5�1‰9Ðr   c                   ó   — e Zd ZdZd„ Zy)Ú_CreatorzÓA _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                 ó   — t        | «      d›S )NÚx)Úidr   s    r   Ú__str__z_Creator.__str__0   s   € Ü�T“(˜1�Ðr   N)r   r   r   r   r)   r   r   r   r%   r%   (   s   „ ñór   r%   c            	       óâ  — e Zd ZdZg d¢Zdededefd„Zdefd„Z	defd	„Z
d
edeeef   fd„Zdeddfd„Zd
ededef   fd„Zd
ededz  fd„Zdeddfd„Zdedededz  fd„Zdededf   fd„Zdddedefd„Zdddedefd„Zdddeddddfd„Zdddeddfd„Zdefd „Zdefd!„Zdddeddfd"„Zd
eddd#edz  dedz  fd$„Zd%eegdf   ddfd&„Zd%edgdf   ddfd'„Zdeddfd(„Z deddfd)„Z!d*„ Z"y)+Ú_NodezFA Node in the BTree.

    A Node (leaf or internal) of the BTree.
    ©r   ÚcreatorÚis_leafÚeltsÚchildrenr   r-   r.   c                 óX   — |dk\  sJ ‚|| _         || _        || _        g | _        g | _        y )Né   r,   )r   r   r-   r.   s       r   Ú__init__z_Node.__init__<   s0   € Ø�AŠvˆˆvØˆŒØˆŒØˆŒØ ˆŒ	Ø-/ˆ�r   r   c                 ó²   — t        | j                  «      t        | j                  «      k  sJ ‚t        | j                  «      t        | j                  «      k(  S )z/Does this node have the maximal number of keys?)Úlenr/   r#   r   r   s    r   Ú
is_maximalz_Node.is_maximalD   ó;   € ä�4—9‘9‹~¤ d§f¡f£Ò-Ð-Ð-Ü�4—9‘9‹~¤ d§f¡f£Ñ-Ð-r   c                 ó²   — t        | j                  «      t        | j                  «      k\  sJ ‚t        | j                  «      t        | j                  «      k(  S )z/Does this node have the minimal number of keys?)r5   r/   r    r   r   s    r   Ú
is_minimalz_Node.is_minimalI   r7   r   r   c                 óh  — t        | j                  «      }|dkD  r'|| j                  |dz
     j                  «       kD  r|dfS d}t        | j                  «      }|dz
  }d}||k  rK||z   dz  }| j                  |   j                  «       }||k(  r	|}d}	 ||fS ||k  r|}|dz
  }n|dz   }||k  rŒK||fS )zýGet 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.
        :rtype: tuple[int, bool]
        r   r   Fr"   T)r5   r/   r   )r   r   ÚlÚiÚrÚequalÚmÚks           r   Úsearch_in_nodez_Node.search_in_nodeN   sÛ   € ô �—	‘	‹NˆØˆqŠ5�S˜4Ÿ9™9 Q¨¡UÑ+×/Ñ/Ó1Ò1à�e�8ˆOØˆÜ�—	‘	‹NˆØ�‰EˆØˆØ�1ŠfØ�Q‘˜1‘ˆAØ—	‘	˜!‘× Ñ Ó"ˆAØ�aŠxØ�Ø�Øð �%ˆxˆð �q’Ø�Ø˜‘E‘à˜‘E�ð �1‹fð �%ˆxˆr   Úindexz_Node[KT, ET]c                 óœ   — | j                   rJ ‚| j                  |   }|j                  | j                  «      }|r|| j                  |<   |S |S ©N)r.   r0   Ú	maybe_cowr-   )r   rB   ÚchildÚcloneds       r   Úmaybe_cow_childz_Node.maybe_cow_childl   sJ   € Ø—<’<ÐÐØ—‘˜eÑ$ˆØ—‘ §¡Ó.ˆÙØ#)ˆD�M‰M˜%Ñ ØˆMàˆLr   z_Node[KT, ET] | Nonec                 ó”   — | j                  |«      \  }}|r| |fS | j                  ry| j                  |«      }|j                  |«      S )zØGet 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 ``(None, 0)``
            if the key was not found.
        ©Nr   )rA   r.   rH   Ú	_get_node)r   r   r<   r>   rF   s        r   rK   z_Node._get_nodev   sN   € ð ×&Ñ& sÓ+‰ˆˆ5ÙØ˜!�9ÐØ�\Š\Øà×(Ñ(¨Ó+ˆEØ—?‘? 3Ó'Ð'r   Nc                 ó¢   — | j                  |«      \  }}|r| j                  |   S | j                  ry| j                  |   j	                  |«      S )z8Get the element associated with *key* or return ``None``N)rA   r/   r.   r0   Úget)r   r   r<   r>   s       r   rM   z	_Node.get†   sK   € à×&Ñ& sÓ+‰ˆˆ5ÙØ—9‘9˜Q‘<ÐØ�\Š\Øà—=‘= Ñ#×'Ñ'¨Ó,Ð,r   c                 ó�  — |dk(  ry| j                   |dz
     }t        |j                  «      t        | j                  «      k(  ry| j                  |dz
  «      }t        |j                  «      t        | j                  «      k  rC|j                  | |dz
  «      syt        |j                  «      t        | j                  «      k  rŒByy)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   )r0   r5   r/   r#   r   rH   Útry_right_steal)r   rB   Úlefts      r   Úoptimize_in_order_insertionz!_Node.optimize_in_order_insertion�   s˜   € ð �AŠ:ØØ�}‰}˜U Q™YÑ'ˆÜˆt�y‰y‹>œT $§&¡&›\Ò)ØØ×#Ñ# E¨A¡IÓ.ˆÜ�$—)‘)‹nœt D§F¡F›|Ò+Ø×'Ñ'¨¨e°a©iÔ8Øô �$—)‘)‹nœt D§F¡F›|Õ+r   ÚelementÚin_orderc                 óÒ  — | j                  «       rJ ‚	 |j                  «       }| j                  |«      \  }}|r | j                  |   }|| j                  |<   |S | j                  r| j                  j                  ||«       y | j                  |«      }|j                  «       r | j                  |j                  «       Ž  Œ¯|j                  ||«      }|r| j                  |«       |S rD   )r6   r   rA   r/   r.   ÚinsertrH   ÚadoptÚsplitÚinsert_nonfullrQ   )	r   rR   rS   r   r<   r>   ÚoldrF   Úoelts	            r   rX   z_Node.insert_nonfull¢   sÑ   € Ø—?‘?Ô$Ð$Ð$ØØ—+‘+“-ˆCØ×*Ñ*¨3Ó/‰HˆAˆuÙà—i‘i ‘l�Ø&�—	‘	˜!‘Ø�
Ø—’Ø—	‘	× Ñ   GÔ,Øà×,Ñ,¨QÓ/�Ø×#Ñ#Ô%Ø�D—J‘J §¡£Ñ.ð Ø×+Ñ+¨G°XÓ>�ÙØ×4Ñ4°QÔ7Ø�r   c                 ó€  — | j                  «       sJ ‚| j                  | j                  | j                  | j                  «      }t        | j                  t        | j                  «      dz   d «      |_        | j                  t        | j                  «         }t        | j                  dt        | j                  «       «      | _        | j                  sft        | j                  t        | j                  «      dz   d «      |_        t        | j                  dt        | j                  «      dz    «      | _        | ||fS )zASplit a maximal node into two minimal ones and a central element.r   N)	r6   Ú	__class__r   r-   r.   Úlistr/   r    r0   )r   ÚrightÚmiddles      r   rW   z_Node.split»   sè   € à�‰Ô Ð Ð Ø—‘˜tŸv™v t§|¡|°T·\±\ÓBˆÜ˜$Ÿ)™)¤D¨¯©£L°1Ñ$4Ð$6Ð7Ó8ˆŒ
Ø—‘œ4 §¡›<Ñ(ˆÜ˜Ÿ™ >¤T¨$¯&©&£\Ð2Ó3ˆŒ	Ø�|Š|Ü! $§-¡-´°T·V±V³¸qÑ0@Ð0BÐ"CÓDˆEŒNÜ  §¡Ð/A´°d·f±f³ÀÑ1AÐ!BÓCˆDŒMØ�V˜UÐ"Ð"r   Úparentc                 óÌ  — |dk7  rß|j                   |dz
     }|j                  «       s½|j                  |dz
  «      }|j                  |dz
     }|j                  j	                  «       |j                  |dz
  <   | j                  j                  d|«       |j                  sD| j                  rJ ‚|j                   j	                  «       }| j                   j                  d|«       yy)z«Try to steal from this Node's left sibling for balancing purposes.

        :returns: ``True`` if the theft was successful, ``False`` if not.
        :rtype: bool
        r   r   TF)r0   r9   rH   r/   ÚpoprU   r.   )r   r`   rB   rP   ÚeltrF   s         r   Útry_left_stealz_Node.try_left_stealÇ   s½   € ð �AŠ:Ø—?‘? 5¨1¡9Ñ-ˆDØ—?‘?Ô$Ø×-Ñ-¨e°a©iÓ8�Ø—k‘k %¨!¡)Ñ,�Ø)-¯©¯©«�—‘˜E A™IÑ&Ø—	‘	× Ñ   CÔ(Ø—|’|Ø#Ÿ|š|Ð+Ð+Ø ŸM™M×-Ñ-Ó/�EØ—M‘M×(Ñ(¨¨EÔ2ØØr   c                 óì  — |dz   t        |j                  «      k  rÙ|j                  |dz      }|j                  «       s·|j                  |dz   «      }|j                  |   }|j                  j                  d«      |j                  |<   | j                  j                  |«       |j                  sD| j                  rJ ‚|j                  j                  d«      }| j                  j                  |«       yy)z¬Try to steal from this Node's right sibling for balancing purposes.

        :returns: ``True`` if the theft was successful, ``False`` if not.
        :rtype: bool
        r   r   TF)r5   r0   r9   rH   r/   rb   Úappendr.   )r   r`   rB   r^   rc   rF   s         r   rO   z_Node.try_right_stealÛ   sÆ   € ð �1‰9”s˜6Ÿ?™?Ó+Ò+Ø—O‘O E¨A¡IÑ.ˆEØ×#Ñ#Ô%Ø×.Ñ.¨u°q©yÓ9�Ø—k‘k %Ñ(�Ø%*§Z¡Z§^¡^°AÓ%6�—‘˜EÑ"Ø—	‘	× Ñ  Ô%Ø—}’}Ø#Ÿ|š|Ð+Ð+Ø!ŸN™N×.Ñ.¨qÓ1�EØ—M‘M×(Ñ(¨Ô/ØØr   rP   r_   r^   c                 óv  — | j                  «       rJ ‚| j                  rJ ‚|j                  «       }| j                  |«      \  }}|rJ ‚| j                  j                  ||«       t        | j                  «      dk(  r
||g| _        y| j                  |   |k(  sJ ‚| j                  j                  |dz   |«       y)zÒAdopt 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)r6   r.   r   rA   r/   rU   r5   r0   )r   rP   r_   r^   r   r<   r>   s          r   rV   z_Node.adoptï   s§   € ð —?‘?Ô$Ð$Ð$Ø—<’<ÐÐØ�j‰j‹lˆØ×&Ñ& sÓ+‰ˆˆ5ÙÐˆyØ�	‰	×Ñ˜˜FÔ#Üˆt�}‰}Ó Ò"à! 5˜MˆD�Mà—=‘= Ñ# tÒ+Ð+Ð+Ø�M‰M× Ñ   Q¡¨Õ.r   c                 óV  — |j                   j                  |dz   «      }| j                  j                  |j                  j                  |«      «       | j                  j	                  |j                  «       | j
                  s&| j                   j	                  |j                   «       yy)z>Merge this node's parent and its right sibling into this node.r   N)r0   rb   r/   rf   Úextendr.   )r   r`   rB   r^   s       r   Úmergez_Node.merge   sq   € à—‘×#Ñ# E¨A¡IÓ.ˆØ�	‰	×Ñ˜Ÿ™Ÿ™¨Ó/Ô0Ø�	‰	×Ñ˜Ÿ™Ô$Ø�|Š|Ø�M‰M× Ñ  §¡Õ0ð r   c                 ór   — | j                   r| j                  d   S | j                  d   j                  «       S )z"The least element in this subtree.r   )r.   r/   r0   Úminimumr   s    r   rl   z_Node.minimum  s/   € à�<Š<Ø—9‘9˜Q‘<Ðà—=‘= Ñ#×+Ñ+Ó-Ð-r   c                 ór   — | j                   r| j                  d   S | j                  d   j                  «       S )z%The greatest element in this subtree.éÿÿÿÿ)r.   r/   r0   Úmaximumr   s    r   ro   z_Node.maximum  s/   € à�<Š<Ø—9‘9˜R‘=Ð à—=‘= Ñ$×,Ñ,Ó.Ð.r   c                 óî   — |j                   rJ ‚| j                  ||«      ry| j                  ||«      ry|dk(  r| j                  ||«       y|j	                  |dz
  «      }|j                  ||dz
  «       y)z¶This 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   )r.   rd   rO   rj   rH   )r   r`   rB   rP   s       r   Úbalancez_Node.balance  st   € ð —>’>Ð!Ð!Ø×Ñ˜v uÔ-ØØ×Ñ ¨Ô.Øà�AŠ:à�J‰J�v˜uÕ%ð ×)Ñ)¨%°!©)Ó4ˆDØ�J‰J�v˜u q™yÕ)r   Úexactc                 ó  — |�| j                  «       rJ ‚| j                  |«      \  }}d}|r~|�| j                  |   |urt        d«      ‚| j                  r| j                  j                  |«      S d}|}| j                  |dz      j                  «       }|j                  «       }|dz   }| j                  r|�t        d«      ‚y| j                  |«      }|j                  «       rK|j                  | |«       | j                  |«      \  }}|rJ ‚| j                  |   }|j                  «       rJ ‚|j                  || |«      }	|�<| j                  |«      \  }
}|
€J ‚|	€J ‚|
j                  |   }|	|
j                  |<   |}	|	S )zÁDelete 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)r9   rA   r/   Ú
ValueErrorr.   rb   r0   rl   r   rH   rq   ÚdeleterK   )r   r   r`   rr   r<   r>   Úoriginal_keyÚleast_successorrF   rc   ÚnoderZ   s               r   ru   z_Node.delete)  s“  € ð ˆ~ T§_¡_Ô%6Ð6Ð6Ø×&Ñ& sÓ+‰ˆˆ5ØˆÙàÐ  T§Y¡Y¨q¡\¸Ñ%>Ü Ð!JÓKÐKØ�|Š|Ø—y‘y—}‘} QÓ'Ð'ð ˆEØˆLØ"Ÿm™m¨A°©EÑ2×:Ñ:Ó<ˆOØ!×%Ñ%Ó'ˆCØ�A‘ˆAØ�<Š<àÐ Ü Ð!<Ó=Ð=Øà×$Ñ$ QÓ'ˆØ×ÑÔØ�M‰M˜$ Ô"à×*Ñ*¨3Ó/‰HˆAˆuÙÐ�9Ø—M‘M !Ñ$ˆEØ×'Ñ'Ô)Ð)Ð)Ø�l‰l˜3  eÓ,ˆØÐ#Ø—n‘n \Ó2‰GˆD�!ØÐ#Ð#Ð#Ø�?Ð"�?Ø—9‘9˜Q‘<ˆDØˆD�I‰I�a‰LØˆCØˆ
r   Úvisitc                 óø   — t        | j                  «      D ]7  \  }}| j                  s| j                  |   j	                  |«        ||«       Œ9 | j                  s| j                  d   j	                  |«       yy)z-Call *visit* on all of the elements in order.rn   N)Ú	enumerater/   r.   r0   Úvisit_in_order)r   ry   r<   rc   s       r   r|   z_Node.visit_in_orderX  sh   € ä §	¡	Ó*ò 	‰FˆAˆsØ—<’<Ø—‘˜aÑ ×/Ñ/°Ô6Ù�#�Jð	ð �|Š|Ø�M‰M˜"Ñ×,Ñ,¨UÕ3ð r   c                 ór   —  || «       | j                   s#| j                  D ]  }|j                  |«       Œ yy)z?Visit nodes in preorder.  This method is only used for testing.N)r.   r0   Ú_visit_preorder_by_node)r   ry   rF   s      r   r~   z_Node._visit_preorder_by_nodea  s6   € áˆdŒØ�|Š|ØŸ™ò 5�Ø×-Ñ-¨eÕ4ñ5ð r   c                 óB   — | j                   |ur| j                  |«      S y)zœReturn 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)r-   Úclone)r   r-   s     r   rE   z_Node.maybe_cowh  s"   € ð �<‰<˜wÑ&Ø—:‘:˜gÓ&Ð&àr   c                 ó   — | j                  | j                  || j                  «      }|j                  j	                  | j                  «       | j                  s%|j
                  j	                  | j
                  «       |S )z+Make a shallow-copy duplicate of this node.)r\   r   r.   r/   ri   r0   )r   r-   rG   s      r   r€   z_Node.clonep  sT   € à—‘ §¡¨°·±Ó>ˆØ�‰×Ñ˜4Ÿ9™9Ô%Ø�|Š|Ø�O‰O×"Ñ" 4§=¡=Ô1Øˆr   c           	      óì   — | j                   s8ddj                  | j                  D �cg c]  }t        |«      d›‘Œ c}«      z   }nd}t        | «      d›d| j                  › d| j
                  › |› �S c c}w )Nú r'   Ú )r.   Újoinr0   r(   r-   r/   )r   Úcr0   s      r   r)   z_Node.__str__x  si   € Ø�|Š|Ø˜SŸX™X¸T¿]¹]Ö&K¸¬"¨Q«%°¨¡|Ò&KÓLÑL‰HàˆHÜ�T“(˜1�˜Q˜tŸ|™|˜n¨A¨d¯i©i¨[¸¸
ÐCÐCùò 'Ls   §A1
)#r   r   r   r   Ú	__slots__Úintr%   Úboolr3   r6   r9   r
   ÚtuplerA   rH   rK   r   rM   rQ   rX   rW   rd   rO   rV   rj   rl   ro   rq   ru   r   r|   r~   rE   r€   r)   r   r   r   r+   r+   4   s*  „ ñò
 @€Ið0˜#ð 0¨ð 0¸4ó 0ð.˜Dó .ð
.˜Dó .ð
 "ð ¨¨s°D¨yÑ)9ó ð< Sð ¨_ó ð(˜Rð ( EÐ*@À#Ð*EÑ$Fó (ð -�rð -˜b 4™ió -ð°ð ¸ó ð$ bð °Dð ¸RÀ$¹Yó ð2
#�u˜_¨b°/ÐAÑBó 
#ð _ð ¸Sð ÀTó ð( oð ¸cð Àdó ð(/˜/ð /°2ð /¸oð /ÐRVó /ð"1˜Oð 1°Cð 1¸Dó 1ð.˜ó .ð/˜ó /ð*˜oð *°cð *¸dó *ð&-Øð-Ø5ð-Ø>@À4¹ið-à	ˆd‰ó-ð^4 H¨b¨T°4¨ZÑ$8ð 4¸Tó 4ð5¨X°Ð6GÈÐ6MÑ-Nð 5ÐSWó 5ð ð Ð.Dó ð˜Xð ¨/ó óDr   r+   c                   óž   — e Zd ZdZdd„Zdd„Zdd„Zd„ Zd„ Zde	dz  fd	„Z
de	dz  fd
„Zdededdfd„Zddededdfd„Zdd„Zdd„Zd„ Zd„ Zy)ÚCursorzÍA 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                 ó‚   — || _         d | _        d| _        d| _        d| _        g | _        d| _        d | _        d| _        y )Nr   FT)	ÚbtreeÚcurrent_nodeÚcurrent_indexÚrecurseÚ
increasingÚparentsÚparkedÚparking_keyÚparking_key_read)r   rŽ   s     r   r3   zCursor.__init__ˆ  sG   € ØˆŒ
Ø*.ˆÔð #$ˆÔØˆŒØˆŒØ02ˆŒØˆŒØ&*ˆÔØ %ˆÕr   r   Nc                 óX  — | j                   €J ‚| j                   j                  s†| j                  j                  | j                   | j                  f«       | j                   j
                  | j                     | _         | j                   €J ‚d| _        | j                   j                  sŒ…y y rJ   )r�   r.   r“   rf   r�   r0   r   s    r   Ú_seek_leastzCursor._seek_least–  s’   € ð × Ñ Ð,Ð,Ð,Ø×#Ñ#×+Ò+Ø�L‰L×Ñ ×!2Ñ!2°D×4FÑ4FÐ GÔHØ $× 1Ñ 1× :Ñ :¸4×;MÑ;MÑ NˆDÔØ×$Ñ$Ð0Ð0Ð0Ø!"ˆDÔð	 ×#Ñ#×+Õ+r   c                 ó’  — | j                   €J ‚| j                   j                  s£| j                  j                  | j                   | j                  f«       | j                   j
                  | j                     | _         | j                   €J ‚t        | j                   j                  «      | _        | j                   j                  sŒ¢y y rD   )r�   r.   r“   rf   r�   r0   r5   r/   r   s    r   Ú_seek_greatestzCursor._seek_greatest   s£   € ð × Ñ Ð,Ð,Ð,Ø×#Ñ#×+Ò+Ø�L‰L×Ñ ×!2Ñ!2°D×4FÑ4FÐ GÔHØ $× 1Ñ 1× :Ñ :¸4×;MÑ;MÑ NˆDÔØ×$Ñ$Ð0Ð0Ð0Ü!$ T×%6Ñ%6×%;Ñ%;Ó!<ˆDÔð	 ×#Ñ#×+Õ+r   c                 ó,   — | j                   sd| _         yy)a§  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   ÚparkzCursor.parkª  s   € ð �{Š{ØˆD�Kð r   c                 óü   — | j                   rp| j                  �U| j                  }| j                  r| j                   }n| j                  }| j	                  | j                  |«       || _        d| _         d | _        y y ©NF)r”   r•   r’   r–   Úseek)r   r’   Úbefores      r   Ú_maybe_unparkzCursor._maybe_unpark¶  so   € Ø�;Š;Ø×ÑÐ+à!Ÿ_™_�
Ø×(Ò(ð "&§¡Ð0‘Fð
 "Ÿ_™_�FØ—	‘	˜$×*Ñ*¨FÔ3Ø",�”ØˆDŒKØ#ˆDÕð! r   c                 óP  — | j                  «        d| _        | j                  €z| j                  dk(  ry| j                  dk(  sJ ‚| j                  j
                  | _        t        | j                  j
                  j                  «      | _        | j                  «        	 | j                  r#| j                  s| j                  «        d| _	        d| _
        | xj                  dz  c_        | j                  dk\  r^| j                  j                  | j                     }| j                  j                  sd| _	        |j                  «       | _        d| _        |S t        | j                  «      dkD  r(| j                  j                  «       \  | _        | _        nd| _        d| _        y�Œ	)zAGet the previous element, or return None if on the left boundary.Nr   r   TF)r¡   r•   r�   r�   rŽ   Úrootr5   r/   rš   r‘   r’   r.   r   r–   r“   rb   ©r   rc   s     r   ÚprevzCursor.prevÉ  s_  € à×ÑÔØˆÔØ×ÑÐ$à×!Ñ! QÒ&àà×)Ñ)¨QÒ.Ð.Ð.ð %)§J¡J§O¡O�Ô!Ü%(¨¯©¯©×)=Ñ)=Ó%>�Ô"Ø×#Ñ#Ô%ØØ�|Š|Ø—’ð ×'Ñ'Ô)Ø$�”Ø#ˆDŒOØ×Ò !Ñ#ÕØ×!Ñ! QÒ&Ø×'Ñ'×,Ñ,¨T×-?Ñ-?Ñ@�Ø×(Ñ(×0Ò0Ø#'�D”LØ#&§7¡7£9�Ô Ø(,�Ô%Ø�
ä�t—|‘|Ó$ qÒ(Ø<@¿L¹L×<LÑ<LÓ<NÑ9�DÔ% tÕ'9à(,�DÔ%Ø)*�DÔ&Øñ- r   c                 ó<  — | j                  «        d| _        | j                  €S| j                  dk(  ry| j                  dk(  sJ ‚| j                  j
                  | _        d| _        | j                  «        	 | j                  r#| j                  r| j                  «        d| _        d| _        | j                  t        | j                  j                  «      k  rs| j                  j                  | j                     }| xj                  dz  c_        | j                  j                  sd| _        |j                  «       | _        d| _        |S t        | j                  «      dkD  r(| j                  j                  «       \  | _        | _        nd| _        d| _        y�Œ&)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’   r5   r/   r.   r   r–   r“   rb   r¤   s     r   ÚnextzCursor.nextñ  s]  € à×ÑÔØˆÔØ×ÑÐ$à×!Ñ! QÒ&àà×)Ñ)¨QÒ.Ð.Ð.ð %)§J¡J§O¡O�Ô!Ø%&�Ô"Ø× Ñ Ô"ØØ�|Š|Ø—?’?ð ×$Ñ$Ô&Ø$�”Ø"ˆDŒOØ×!Ñ!¤C¨×(9Ñ(9×(>Ñ(>Ó$?Ò?Ø×'Ñ'×,Ñ,¨T×-?Ñ-?Ñ@�Ø×"Ò" aÑ'Õ"Ø×(Ñ(×0Ò0Ø#'�D”LØ#&§7¡7£9�Ô Ø(,�Ô%Ø�
ä�t—|‘|Ó$ qÒ(Ø<@¿L¹L×<LÑ<LÓ<NÑ9�DÔ% tÕ'9à(,�DÔ%Ø)*�DÔ&Øñ- r   r    r<   c                 ó,   — |r|| _         y |dz   | _         y )Nr   )r�   )r   r    r<   s      r   Ú_adjust_for_beforezCursor._adjust_for_before  s   € ÙØ!"ˆDÕà!" Q¡ˆDÕr   r   c                 óÊ  — | j                   j                  | _        | j                  €J ‚d| _        g | _        || _        d| _        || _        d| _        | j                  j                  sÀ| j                  j                  |«      \  }}|r6| j                  ||«       |r| j                  «        y| j                  «        y| j                  j                  | j                  |f«       | j                  j                  |   | _        | j                  €J ‚| j                  j                  sŒÀ| j                  j                  |«      \  }}|r| j                  ||«       y|| _        y)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–   r.   rA   r©   rš   r˜   rf   r0   r�   )r   r   r    r<   r>   s        r   rŸ   zCursor.seek  sC  € ð !ŸJ™JŸO™OˆÔØ× Ñ Ð,Ð,Ð,ØˆŒØˆŒØ ˆŒØˆŒØˆÔØ %ˆÔØ×#Ñ#×+Ò+Ø×(Ñ(×7Ñ7¸Ó<‰HˆAˆuÙØ×'Ñ'¨°Ô2ÙØ×'Ñ'Ô)ð ð ×$Ñ$Ô&ØØ�L‰L×Ñ ×!2Ñ!2°AÐ 6Ô7Ø $× 1Ñ 1× :Ñ :¸1Ñ =ˆDÔØ×$Ñ$Ð0Ð0Ð0ð ×#Ñ#×+Ó+ð ×$Ñ$×3Ñ3°CÓ8‰ˆˆ5ÙØ×#Ñ# F¨AÕ.à!"ˆDÕr   c                 óf   — d| _         d| _        d| _        d| _        g | _        d| _        d| _        y)z”Seek 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_firstzCursor.seek_firstB  s8   € ð !ˆÔØˆÔØˆŒØˆŒØˆŒØˆŒØˆÕr   c                 óf   — d| _         d| _        d| _        d| _        g | _        d| _        d| _        y)z£Seek 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_lastzCursor.seek_lastN  s8   € ð
 !ˆÔØˆÔØˆŒØˆŒØˆŒØˆŒØˆÕr   c                 ó<   — | j                   j                  | «       | S rD   )rŽ   Úregister_cursorr   s    r   Ú	__enter__zCursor.__enter__[  s   € Ø�
‰
×"Ñ" 4Ô(Øˆr   c                 ó:   — | j                   j                  | «       yrž   )rŽ   Úderegister_cursor)r   Úexc_typeÚ	exc_valueÚ	tracebacks       r   Ú__exit__zCursor.__exit___  s   € Ø�
‰
×$Ñ$ TÔ*Ør   )rŽ   zBTree[KT, ET]©r   N)T)r   r   r   r   r3   r˜   rš   rœ   r¡   r   r¥   r§   r‰   rˆ   r©   r
   rŸ   r­   r¯   r²   r¸   r   r   r   rŒ   rŒ   €  s‹   „ ñó&ó#ó=ò
ò$ð&& �b˜4‘ió & ðP& �b˜4‘ió & ðP'¨ð '°#ð '¸$ó 'ñ!#˜ð !# Dð !#°Dó !#óF
 ó òór   rŒ   c                   ó   — e Zd ZdZy)Ú	ImmutablezThe BTree is immutable.N)r   r   r   r   r   r   r   r»   r»   d  s   „ Ú!r   r»   c                   ó4  — e Zd ZdZeddœdeddfd„Zd„ Zdd
„Zd de	de
d	e	dz  fd„Zded	e	dz  fd„Zdede	dz  d	e	dz  fd„Zded	e	dz  fd„Zde	d	e	dz  fd„Zd„ Zdee	gdf   d	dfd„Zdeegdf   d	dfd„Zd	eee	f   fd„Zded	dfd„Zded	dfd„Zd„ Zd„ Zy)!ÚBTreez2An in-memory BTree with copy-on-write and cursors.N©r   Úoriginalr   r¿   zBTree | Nonec                ó|  — t        «       | _        d| _        |  |  |  t        «       | _        |�K|j                  st        d«      ‚|j                  | _        |j                  | _        |j                  | _        y|dk  rt        d«      ‚|| _        t        | j                  | j                  d«      | _        d| _        y)zýCreate 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 immutabler2   zt must be >= 3Tr   )
r%   r-   Ú
_immutableÚsetÚcursorsrt   r   r£   Úsizer+   )r   r   r¿   s      r   r3   zBTree.__init__k  s›   € ô  “zˆŒØˆŒÙÙÙÜ$'£EˆŒØÐØ×&Ò&Ü Ð!BÓCÐCØ—Z‘ZˆDŒFØ Ÿ™ˆDŒIØ Ÿ™ˆD�Ià�1ŠuÜ Ð!1Ó2Ð2ØˆDŒFÜ˜dŸf™f d§l¡l°DÓ9ˆDŒIØˆD�Ir   c                 ó,   — | j                   sd| _         yy)z®Make 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_immutablezBTree.make_immutable‰  s   € ð �ŠØ"ˆD�Oð r   r   c                 ój   — | j                   rt        ‚| j                  D ]  }|j                  «        Œ y rD   )rÁ   r»   rÃ   rœ   ©r   Úcursors     r   Ú_check_mutable_and_parkzBTree._check_mutable_and_park’  s*   € Ø�?Š?ÜˆOØ—l‘lò 	ˆFØ�K‰K�Mñ	r   rc   rS   c                 óÎ  — | j                  «        | j                  j                  | j                  «      }|r|| _        | j                  j	                  «       rY| j                  }t        | j                  | j                  d«      | _         | j                  j                  |j                  «       Ž  | j                  j                  ||«      }|€| xj                  dz  c_
        |S )aJ  Insert the element into the BTree.

        :param bool in_order: If ``True``, extra work will be done to make left
            siblings full, which optimizes storage space when elements are inserted
            in-order or close to it.
        :returns: The previously existing element at the element's key, or ``None``.
        Fr   )rÊ   r£   rE   r-   r6   r+   r   rV   rW   rX   rÄ   )r   rc   rS   rG   Úold_rootrZ   s         r   Úinsert_elementzBTree.insert_elementœ  s©   € ð 	×$Ñ$Ô&Ø—‘×$Ñ$ T§\¡\Ó2ˆÙØˆDŒIØ�9‰9×ÑÔ!Ø—y‘yˆHÜ˜dŸf™f d§l¡l°EÓ:ˆDŒIØˆD�I‰I�O‰O˜XŸ^™^Ó-Ñ.Ø�y‰y×'Ñ'¨¨XÓ6ˆØˆ<à�IŠI˜‰N�IØˆr   r   c                 ó8   — | j                   j                  |«      S )zhGet the element matching *key* from the BTree, or return ``None`` if it
        does not exist.
        )r£   rM   ©r   r   s     r   Úget_elementzBTree.get_element²  s   € ð �y‰y�}‰}˜SÓ!Ð!r   rr   c                 óÞ  — | j                  «        | j                  j                  | j                  «      }|r|| _        | j                  j	                  |d |«      }|��| xj
                  dz  c_        t        | j                  j                  «      dk(  rX| j                  j                  sBt        | j                  j                  «      dk(  sJ ‚| j                  j                  d   | _        |S )Nr   r   )
rÊ   r£   rE   r-   ru   rÄ   r5   r/   r.   r0   )r   r   rr   rG   rc   s        r   Ú_deletezBTree._delete¸  s¶   € Ø×$Ñ$Ô&Ø—‘×$Ñ$ T§\¡\Ó2ˆÙØˆDŒIØ�i‰i×Ñ˜s D¨%Ó0ˆØˆ?à�IŠI˜‰N�IÜ�4—9‘9—>‘>Ó" aÒ'ð —y‘y×(Ò(Ü˜tŸy™y×1Ñ1Ó2°aÒ7Ð7Ð7Ø $§	¡	× 2Ñ 2°1Ñ 5�D”IØˆ
r   c                 ó&   — | j                  |d«      S )z…Delete the element matching *key* from the BTree.

        :returns: The matching element, or ``None`` if it does not exist.
        N)rÒ   rÏ   s     r   Ú
delete_keyzBTree.delete_keyÉ  s   € ð
 �|‰|˜C Ó&Ð&r   rR   c                 óR   — | j                  |j                  «       |«      }||u sJ ‚|S )zzDelete *element* from the BTree.

        :returns: The matching element, or ``None`` if it was not in the BTree.
        )rÒ   r   )r   rR   Údelts      r   Údelete_exactzBTree.delete_exactÐ  s+   € ð
 �|‰|˜GŸK™K›M¨7Ó3ˆØ�w‰ÐˆØˆr   c                 ó   — | j                   S rD   )rÄ   r   s    r   Ú__len__zBTree.__len__Ù  ó   € Ø�y‰yÐr   ry   c                 ó:   — | j                   j                  |«       y)zBCall *visit*(element) on all elements in the tree in sorted order.N)r£   r|   ©r   ry   s     r   r|   zBTree.visit_in_orderÜ  s   € à�	‰	× Ñ  Õ'r   c                 ó:   — | j                   j                  |«       y rD   )r£   r~   rÜ   s     r   r~   zBTree._visit_preorder_by_nodeà  s   € Ø�	‰	×)Ñ)¨%Õ0r   c                 ó   — t        | «      S )zCreate a cursor.)rŒ   r   s    r   rÉ   zBTree.cursorã  s   € ä�d‹|Ðr   rÉ   c                 ó:   — | j                   j                  |«       y)z4Register a cursor for the automatic parking service.N)rÃ   ÚaddrÈ   s     r   r±   zBTree.register_cursorç  s   € à�‰×Ñ˜Õ r   c                 ó:   — | j                   j                  |«       y)z7Deregister a cursor from the automatic parking service.N)rÃ   ÚdiscardrÈ   s     r   r´   zBTree.deregister_cursorë  s   € à�‰×Ñ˜VÕ$r   c                 ó&   — | j                  | ¬«      S )N)r¿   ©r\   r   s    r   Ú__copy__zBTree.__copy__ï  s   € Ø�~‰~ tˆ~Ó,Ð,r   c              #   ó¦   K  — | j                  «       5 }	 |j                  «       }|€n|j                  «       –— Œ&	 d d d «       y # 1 sw Y   y xY w­wrD   )rÉ   r§   r   )r   rÉ   rc   s      r   Ú__iter__zBTree.__iter__ò  sP   è ø€ Ø�[‰[‹]ð 	 ˜fØØ—k‘k“m�Ø�;ØØ—g‘g“i’ð	 ð ÷		 ÷ 	 ñ 	 üs   ‚A“(A¼	AÁAÁ
Ar¹   )F)r   r   r   r   Ú	DEFAULT_Trˆ   r3   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½   h  s1  „ Ù<à#,Èò ˜Sð ¸ó ò<#óñ "ð °ð ÀÀdÁó ð,"˜rð " b¨4¡ió "ð˜2ð  b¨4¡ið °B¸±Ió ð"'˜bð ' R¨$¡Yó 'ð Bð ¨2°©9ó òð( H¨b¨T°4¨ZÑ$8ð (¸Tó (ð1¨X°u°g¸t°mÑ-Dð 1Èó 1ð˜˜r 2˜v™ó ð! fð !°ó !ð%¨ð %°4ó %ò-ó r   r½   ÚVTc                   óD   — e Zd ZdZdedefd„Zdefd„Zdefd„Zd„ Z	d	„ Z
y
)ÚKVz/The BTree element type used in a ``BTreeDict``.r   Úvaluec                 ó    — || _         || _        y rD   ©Ú_keyÚ_value)r   r   rì   s      r   r3   zKV.__init__  s   € ØˆŒ	Øˆ�r   r   c                 ó   — | j                   S rD   ©rï   r   s    r   r   zKV.key  rÚ   r   c                 ó   — | j                   S rD   )rð   r   s    r   rì   zKV.value  s   € Ø�{‰{Ðr   c                 ó<   — d| j                   › d| j                  › d�S ©NzKV(z, ú)rî   r   s    r   r)   z
KV.__str__  ó   € Ø�T—Y‘Y�K˜r $§+¡+ ¨aÐ0Ð0r   c                 ó<   — d| j                   › d| j                  › d�S rõ   rî   r   s    r   Ú__repr__zKV.__repr__  r÷   r   N)r   r   r   r   r
   ré   r3   r   rì   r)   rù   r   r   r   rë   rë   þ  s:   „ Ù9ð˜Bð  ró ð�Ró ð�ró ò1ó1r   rë   c                   ót   ‡ — e Zd ZdZedddœdededz  defˆ fd„Zd	e	d
e
fd„Zd	e	de
d
dfd„Zd	e	d
dfd„Zˆ xZS )Ú	BTreeDictzA MutableMapping implemented with a BTree.

    Unlike a normal Python dict, the BTreeDict may be mutated while iterating.
    NF©r   r¿   rS   r   r¿   rS   c                ó6   •— t         ‰| �  ||¬«       || _        y ©Nr¾   ©Úsuperr3   rS   ©r   r   r¿   rS   r\   s       €r   r3   zBTreeDict.__init__  ó   ø€ ô 	‰Ñ˜1 xÐÔ0Ø ˆ�r   r   r   c                 óp   — | j                  |«      }|€t        ‚t        t        |«      j	                  «       S rD   )rÐ   ÚKeyErrorr   rë   rì   )r   r   rc   s      r   Ú__getitem__zBTreeDict.__getitem__"  s1   € Ø×Ñ˜sÓ#ˆØˆ;ÜˆNäœ˜C“=×&Ñ&Ó(Ð(r   rì   c                 óT   — t        ||«      }| j                  || j                  «       y rD   )rë   rÍ   rS   )r   r   rì   rc   s       r   Ú__setitem__zBTreeDict.__setitem__)  s!   € Ü��e‹nˆØ×Ñ˜C §¡Õ/r   c                 ó2   — | j                  |«      €t        ‚y rD   )rÔ   r  rÏ   s     r   Ú__delitem__zBTreeDict.__delitem__-  s   € Ø�?‰?˜3ÓÐ'ÜˆNð (r   )r   r   r   r   rè   rˆ   r½   r‰   r3   r
   ré   r  r  r	  Ú__classcell__rä   s   @r   rû   rû     sz   ø„ ñð Ø!%Øò!ð ð!ð ˜$‘,ð	!ð
 õ!ð)˜rð ) bó )ð0˜rð 0¨"ð 0°ó 0ð˜rð  d÷ r   rû   c                   ó(   — e Zd ZdZdefd„Zdefd„Zy)ÚMemberz.The BTree element type used in a ``BTreeSet``.r   c                 ó   — || _         y rD   rò   rÏ   s     r   r3   zMember.__init__5  s	   € Øˆ�	r   r   c                 ó   — | j                   S rD   rò   r   s    r   r   z
Member.key8  rÚ   r   N)r   r   r   r   r
   r3   r   r   r   r   r  r  2  s   „ Ù8ð˜Bó ð�Rô r   r  c                   óp   ‡ — e Zd ZdZedddœdededz  defˆ fd„Zd	e	d
efd„Z
ded
dfd„Zded
dfd„Zˆ xZS )ÚBTreeSetzyA MutableSet implemented with a BTree.

    Unlike a normal Python set, the BTreeSet may be mutated while iterating.
    NFrü   r   r¿   rS   c                ó6   •— t         ‰| �  ||¬«       || _        y rþ   rÿ   r  s       €r   r3   zBTreeSet.__init__B  r  r   r'   r   c                 ó(   — | j                  |«      d uS rD   )rÐ   )r   r'   s     r   Ú__contains__zBTreeSet.__contains__L  s   € Ø×Ñ Ó"¨$Ð.Ð.r   rì   c                 óR   — t        |«      }| j                  || j                  «       y rD   )r  rÍ   rS   )r   rì   rc   s      r   rà   zBTreeSet.addO  s   € Ü�U‹mˆØ×Ñ˜C §¡Õ/r   c                 ó&   — | j                  |«       y rD   )rÔ   )r   rì   s     r   râ   zBTreeSet.discardS  s   € Ø�‰˜Õr   )r   r   r   r   rè   rˆ   r½   r‰   r3   Úobjectr  r
   rà   râ   r
  rä   s   @r   r  r  <  ss   ø„ ñð Ø!%Øò!ð ð!ð ˜$‘,ð	!ð
 õ!ð/˜fð /¨ó /ð0˜ð 0 ó 0ð˜Rð  D÷ r   r  N)r   Úcollections.abcr   r   r   Útypingr   r   r   rè   r
   r   r   rˆ   r    r#   r%   r+   rŒ   Ú	Exceptionr»   r½   ré   rë   rû   r  r  r   r   r   ú<module>r     sF  ðñ÷ AÑ @ß )Ñ )à€	áˆTƒ]€ô"ˆg�b‰kô "ñ ˆT˜Ô!€ðˆCð �Có ðˆCð �Có ÷
	ñ 	ôIDˆG�B˜�F‰Oô IDôX
aˆW�R˜�V‰_ô aôH"�	ô "ôP ˆG�B˜�F‰Oô P ñf ˆTƒ]€ô1ˆ�'˜"˜b˜&‘/ô 1ô(�˜˜B˜‘  r¨2¨b°"¨f©: ~Ñ!6¸ÀrÈ2ÀvÑ8Nô ô@ˆW�g˜b‘kô ôˆu�g˜b‘k :¨b¡>õ r   