B
    Nô`£*  ã               @   sl   d Z ddlmZmZ ddlmZ ddlZdddgZG dd„ dƒZ	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._Itemz;Used by subclassess to represent a key-value pair.
        )ÚkeyÚvaluec             C   s   || _ || _d S )N)r   r	   )Úselfr   r	   © r   ú8/tmp/pip-unpacked-wheel-fc63gmtm/networkx/utils/heaps.pyÚ__init__   s    zMinHeap._Item.__init__c             C   s   t | j| jfƒS )N)Ú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)Ú_dict)r
   r   r   r   r   !   s    zMinHeap.__init__c             C   s   t ‚dS )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.
        N)ÚNotImplementedError)r
   r   r   r   Úmin&   s    zMinHeap.minc             C   s   t ‚dS )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.
        N)r   )r
   r   r   r   Úpop5   s    zMinHeap.popNc             C   s   t ‚dS )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.
        N)r   )r
   r   Údefaultr   r   r   ÚgetD   s    zMinHeap.getFc             C   s   t ‚dS )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.
        N)r   )r
   r   r	   Úallow_increaser   r   r   ÚinsertW   s    zMinHeap.insertc             C   s
   t | jƒS )z+Returns whether the heap if empty.
        )Úboolr   )r
   r   r   r   Ú__nonzero__n   s    zMinHeap.__nonzero__c             C   s
   t | jƒS )z+Returns whether the heap if empty.
        )r   r   )r
   r   r   r   Ú__bool__s   s    zMinHeap.__bool__c             C   s
   t | jƒS )z;Returns the number of key-value pairs in the heap.
        )Úlenr   )r
   r   r   r   Ú__len__x   s    zMinHeap.__len__c             C   s
   || j k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__}   s    zMinHeap.__contains__)N)F)r   r   r   r   r   r   r   r   r   r   r   r    r"   r#   r   r   r   r   r      s   

c                s   ‡ fdd„}|S )z;Decorator for inheriting docstrings from base classes.
    c                s   ˆ j | j j| _| S )N)Ú__dict__r   r   )Úfn)Úclsr   r   ÚfuncŒ   s    z_inherit_doc.<locals>.funcr   )r&   r'   r   )r&   r   Ú_inherit_docˆ   s    r(   c                   sŽ   e Zd ZdZG dd„ dejƒZ‡ fdd„Zeeƒdd„ ƒZ	eeƒdd	„ ƒZ
eeƒddd„ƒZeeƒ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                s0   t tj| ƒ ||¡ d | _d | _d | _d | _d S )N)Úsuperr   Ú_Noder   r)   r*   r+   r,   )r
   r   r	   )Ú	__class__r   r   r       s
    zPairingHeap._Node.__init__)r   r   r   r   r   r   Ú__classcell__r   r   )r/   r   r.   —   s   r.   c                s   t ƒ  ¡  d| _dS )z#Initialize a pairing heap.
        N)r-   r   Ú_root)r
   )r/   r   r   r   «   s    
zPairingHeap.__init__c             C   s$   | j d krt d¡‚| j j| j jfS )Nzheap is empty.)r1   ÚnxÚNetworkXErrorr   r	   )r
   r   r   r   r   ±   s    

zPairingHeap.minc             C   s>   | j d krt d¡‚| j }|  | j ¡| _ | j|j= |j|jfS )Nzheap is empty.)r1   r2   r3   Ú_merge_childrenr   r   r	   )r
   Zmin_noder   r   r   r   ·   s    


zPairingHeap.popNc             C   s   | j  |¡}|d k	r|jS |S )N)r   r   r	   )r
   r   r   Únoder   r   r   r   À   s    zPairingHeap.getFc             C   sÌ   | j  |¡}| j}|d k	r”||jk rZ||_||k	rV||jjk rV|  |¡ |  ||¡| _dS |r�||jkr�||_|  |¡}|d k	r�|  | j|¡| _dS |  ||¡}|| j |< |d k	r¾|  ||¡n|| _dS d S )NTF)	r   r   r1   r	   r,   Ú_cutÚ_linkr4   r.   )r
   r   r	   r   r5   ÚrootÚchildr   r   r   r   Å   s&    



zPairingHeap.insertc             C   sF   |j |j k r|| }}|j}||_|dk	r0||_d|_||_||_|S )z_Link two nodes, making the one with the smaller value the parent of
        the other.
        N)r	   r)   r*   r+   r,   )r
   r8   Úotherr*   r   r   r   r7   ê   s    
zPairingHeap._linkc             C   s    |j }d|_ |dk	rœ| j}d}xB|j}|dkr6||_P |j}|||ƒ}||_|}|dkrZP |}q W |j}x |dk	rˆ|j}|||ƒ}|}qjW d|_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)r)   r7   r*   r+   r,   )r
   r8   r5   Úlinkr+   r*   Z	next_nextZ	prev_prevr   r   r   r4   ù   s4    


zPairingHeap._merge_childrenc             C   sH   |j }|j}|dk	r||_n||j_d|_ |dk	r>||_ d|_d|_dS )z$Cut a node from its parent.
        N)r+   r*   r,   r)   )r
   r5   r+   r*   r   r   r   r6     s    zPairingHeap._cut)N)F)r   r   r   r   r   r   r.   r   r(   r   r   r   r   r7   r4   r6   r0   r   r   )r/   r   r   “   s   	$&c                   sd   e Zd ZdZ‡ fdd„Zeeƒdd„ ƒZeeƒdd„ ƒZeeƒdd	d
„ƒZ	eeƒddd„ƒZ
‡  ZS )r   zA binary heap.
    c                s   t ƒ  ¡  g | _tƒ | _dS )z"Initialize a binary heap.
        N)r-   r   Ú_heapr   Ú_count)r
   )r/   r   r   r   3  s    
zBinaryHeap.__init__c             C   sX   | j }|st d¡‚| j}t}x0|d \}}}||krD||| krDP ||ƒ q W ||fS )Nzheap is emptyr   )r   r2   r3   r<   r   )r
   ÚdictÚheapr   r	   Ú_r   r   r   r   r   :  s    
zBinaryHeap.minc             C   s^   | j }|st d¡‚| j}t}x0|d \}}}||ƒ ||kr ||| kr P q W ||= ||fS )Nzheap is emptyr   )r   r2   r3   r<   r   )r
   r>   r?   r   r	   r@   r   r   r   r   r   J  s    
zBinaryHeap.popNc             C   s   | j  ||¡S )N)r   r   )r
   r   r   r   r   r   r   [  s    zBinaryHeap.getFc             C   s~   | j }||krV|| }||k s*|rR||krR|||< t| j|t| jƒ|fƒ ||k S dS |||< t| j|t| jƒ|fƒ dS d S )NFT)r   r   r<   r*   r=   )r
   r   r	   r   r>   Ú	old_valuer   r   r   r   _  s    zBinaryHeap.insert)N)F)r   r   r   r   r   r(   r   r   r   r   r   r0   r   r   )r/   r   r   /  s   )r   Úheapqr   r   Ú	itertoolsr   Znetworkxr2   Ú__all__r   r(   r   r   r   r   r   r   Ú<module>   s   
| 