o
    œÇh—(  ã                   @   sb   d Z ddlmZmZ ddlmZ ddlZg d¢ZG dd„ dƒZ	G dd	„ d	e	ƒZ
G d
d„ de	ƒZdS )z
Min-heaps.
é    )ÚheappopÚheappush)ÚcountN)ÚMinHeapÚPairingHeapÚ
BinaryHeapc                   @   sj   e Zd ZdZG dd„ dƒZdd„ Zdd„ Zdd	„ Zddd„Zddd„Z	dd„ Z
dd„ Zdd„ Zdd„ Zd
S )r   zúBase class for min-heaps.

    A MinHeap stores a collection of key-value pairs ordered by their values.
    It supports querying the minimum pair, inserting a new pair, decreasing the
    value in an existing pair and deleting the minimum pair.
    c                   @   s$   e Zd ZdZdZdd„ Zdd„ ZdS )zMinHeap._Itemz2Used by subclassess to represent a key-value pair.©ÚkeyÚvaluec                 C   s   || _ || _d S ©Nr   ©Úselfr	   r
   © r   úh/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/networkx/utils/heaps.pyÚ__init__   s   
zMinHeap._Item.__init__c                 C   s   t | j| jfƒS r   )Úreprr	   r
   ©r   r   r   r   Ú__repr__   s   zMinHeap._Item.__repr__N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ú	__slots__r   r   r   r   r   r   Ú_Item   s
    r   c                 C   s
   i | _ dS )zInitialize a new min-heap.N©Ú_dictr   r   r   r   r   !   ó   
zMinHeap.__init__c                 C   ó   t ‚)a   Query the minimum key-value pair.

        Returns
        -------
        key, value : tuple
            The key-value pair with the minimum value in the heap.

        Raises
        ------
        NetworkXError
            If the heap is empty.
        ©ÚNotImplementedErrorr   r   r   r   Úmin%   ó   zMinHeap.minc                 C   r   )a  Delete the minimum pair in the heap.

        Returns
        -------
        key, value : tuple
            The key-value pair with the minimum value in the heap.

        Raises
        ------
        NetworkXError
            If the heap is empty.
        r   r   r   r   r   Úpop4   r!   zMinHeap.popNc                 C   r   )a‰  Returns the value associated with a key.

        Parameters
        ----------
        key : hashable object
            The key to be looked up.

        default : object
            Default value to return if the key is not present in the heap.
            Default value: None.

        Returns
        -------
        value : object.
            The value associated with the key.
        r   ©r   r	   Údefaultr   r   r   ÚgetC   s   zMinHeap.getFc                 C   r   )a<  Insert a new key-value pair or modify the value in an existing
        pair.

        Parameters
        ----------
        key : hashable object
            The key.

        value : object comparable with existing values.
            The value.

        allow_increase : bool
            Whether the value is allowed to increase. If False, attempts to
            increase an existing value have no effect. Default value: False.

        Returns
        -------
        decreased : bool
            True if a pair is inserted or the existing value is decreased.
        r   )r   r	   r
   Úallow_increaser   r   r   ÚinsertV   s   zMinHeap.insertc                 C   ó
   t | jƒS ©z"Returns whether the heap if empty.©Úboolr   r   r   r   r   Ú__nonzero__m   r   zMinHeap.__nonzero__c                 C   r(   r)   r*   r   r   r   r   Ú__bool__q   r   zMinHeap.__bool__c                 C   r(   )z2Returns the number of key-value pairs in the heap.)Úlenr   r   r   r   r   Ú__len__u   r   zMinHeap.__len__c                 C   s
   || j v S )z¡Returns whether a key exists in the heap.

        Parameters
        ----------
        key : any hashable object.
            The key to be looked up.
        r   )r   r	   r   r   r   Ú__contains__y   s   
zMinHeap.__contains__r   ©F)r   r   r   r   r   r   r    r"   r%   r'   r,   r-   r/   r0   r   r   r   r   r      s    

r   c                       sn   e Zd ZdZG dd„ dejƒZ‡ fdd„Zdd„ Zdd	„ Z	ddd„Z
ddd„Zdd„ Zdd„ Zdd„ Z‡  ZS )r   zA pairing heap.c                       s$   e Zd ZdZdZ‡ fdd„Z‡  ZS )zPairingHeap._NodezŠA node in a pairing heap.

        A tree in a pairing heap is stored using the left-child, right-sibling
        representation.
        )ÚleftÚnextÚprevÚparentc                    s*   t ƒ  ||¡ d | _d | _d | _d | _d S r   )Úsuperr   r2   r3   r4   r5   r   ©Ú	__class__r   r   r      s
   
zPairingHeap._Node.__init__)r   r   r   r   r   r   Ú__classcell__r   r   r7   r   Ú_Node‡   s    r:   c                    s   t ƒ  ¡  d| _dS )zInitialize a pairing heap.N)r6   r   Ú_rootr   r7   r   r   r   ›   s   

zPairingHeap.__init__c                 C   s$   | j d u r
t d¡‚| j j| j jfS ©Nzheap is empty.)r;   ÚnxÚNetworkXErrorr	   r
   r   r   r   r   r        s   

zPairingHeap.minc                 C   s>   | j d u r
t d¡‚| j }|  | j ¡| _ | j|j= |j|jfS r<   )r;   r=   r>   Ú_merge_childrenr   r	   r
   )r   Úmin_noder   r   r   r"   ¥   s   


zPairingHeap.popNc                 C   s   | j  |¡}|d ur|jS |S r   )r   r%   r
   )r   r	   r$   Únoder   r   r   r%   ­   s   zPairingHeap.getFc                 C   sÎ   | j  |¡}| j}|d urJ||jk r-||_||ur+||jjk r+|  |¡ |  ||¡| _dS |rH||jkrH||_|  |¡}|d urH|  | j|¡| _dS |  ||¡}|| j |< |d urb|  ||¡| _dS || _dS )NTF)	r   r%   r;   r
   r5   Ú_cutÚ_linkr?   r:   )r   r	   r
   r&   rA   ÚrootÚchildr   r   r   r'   ±   s*   



ÿzPairingHeap.insertc                 C   sF   |j |j k r||}}|j}||_|dur||_d|_||_||_|S )z_Link two nodes, making the one with the smaller value the parent of
        the other.
        N)r
   r2   r3   r4   r5   )r   rD   Úotherr3   r   r   r   rC   Õ   s   
zPairingHeap._linkc                 C   s    |j }d|_ |durN| j}d}	 |j}|du r||_n|j}|||ƒ}||_|}|du r-n|}q|j}|durE|j}|||ƒ}|}|dus7d|_d|_d|_|S )z„Merge the subtrees of the root using the standard two-pass method.
        The resulting subtree is detached from the root.
        N)r2   rC   r3   r4   r5   )r   rD   rA   Úlinkr4   r3   Ú	next_nextÚ	prev_prevr   r   r   r?   ä   s8   
õ
ýzPairingHeap._merge_childrenc                 C   sH   |j }|j}|dur||_n||j_d|_ |dur||_ d|_d|_dS )zCut a node from its parent.N)r4   r3   r5   r2   )r   rA   r4   r3   r   r   r   rB   
  s   
zPairingHeap._cutr   r1   )r   r   r   r   r   r   r:   r   r    r"   r%   r'   rC   r?   rB   r9   r   r   r7   r   r   „   s    

$&r   c                       sD   e Zd ZdZ‡ fdd„Zdd„ Zdd„ Zdd	d
„Zddd„Z‡  Z	S )r   zA binary heap.c                    s   t ƒ  ¡  g | _tƒ | _dS )zInitialize a binary heap.N)r6   r   Ú_heapr   Ú_countr   r7   r   r   r     s   
zBinaryHeap.__init__c                 C   sV   | j }|s
t d¡‚| j}t}	 |d \}}}||v r&||| kr&	 ||fS ||ƒ q©Nzheap is emptyTr   ©r   r=   r>   rJ   r   ©r   ÚdictÚheapr"   r
   Ú_r	   r   r   r   r    "  s   
ÿüzBinaryHeap.minc                 C   s\   | j }|s
t d¡‚| j}t}	 |d \}}}||ƒ ||v r&||| kr&nq||= ||fS rL   rM   rN   r   r   r   r"   1  s   
üzBinaryHeap.popNc                 C   s   | j  ||¡S r   )r   r%   r#   r   r   r   r%   A  s   zBinaryHeap.getFc                 C   sz   | j }||v r+|| }||k s|r)||kr)|||< t| j|t| jƒ|fƒ ||k S dS |||< t| j|t| jƒ|fƒ dS )NFT)r   r   rJ   r3   rK   )r   r	   r
   r&   rO   Ú	old_valuer   r   r   r'   D  s   zBinaryHeap.insertr   r1   )
r   r   r   r   r   r    r"   r%   r'   r9   r   r   r7   r   r     s    
r   )r   Úheapqr   r   Ú	itertoolsr   Únetworkxr=   Ú__all__r   r   r   r   r   r   r   Ú<module>   s    w 