ó
 ,µ[c           @   sœ   d  Z  d Z d d l m Z m Z d d l m Z d d l Z d d d g Z	 d e
 f d	 „  ƒ  YZ d
 „  Z d e f d „  ƒ  YZ d e f d „  ƒ  YZ d S(   s   
Min-heaps.
s&   ysitu <ysitu@users.noreply.github.com>iÿÿÿÿ(   t   heappopt   heappush(   t   countNt   MinHeapt   PairingHeapt
   BinaryHeapc           B   s{   e  Z d  Z d e f d „  ƒ  YZ d „  Z d „  Z d „  Z d d „ Z	 e
 d „ Z d „  Z d	 „  Z d
 „  Z d „  Z RS(   sú   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.
    t   _Itemc           B   s&   e  Z d  Z d Z d „  Z d „  Z RS(   s;   Used by subclassess to represent a key-value pair.
        t   keyt   valuec         C   s   | |  _  | |  _ d  S(   N(   R   R   (   t   selfR   R   (    (    s3   lib/python2.7/site-packages/networkx/utils/heaps.pyt   __init__   s    	c         C   s   t  |  j |  j f ƒ S(   N(   t   reprR   R   (   R	   (    (    s3   lib/python2.7/site-packages/networkx/utils/heaps.pyt   __repr__"   s    (   R   R   (   t   __name__t
   __module__t   __doc__t	   __slots__R
   R   (    (    (    s3   lib/python2.7/site-packages/networkx/utils/heaps.pyR      s   	c         C   s   i  |  _  d S(   s#   Initialize a new min-heap.
        N(   t   _dict(   R	   (    (    s3   lib/python2.7/site-packages/networkx/utils/heaps.pyR
   %   s    c         C   s
   t  ‚ d S(   s   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(   t   NotImplementedError(   R	   (    (    s3   lib/python2.7/site-packages/networkx/utils/heaps.pyt   min*   s    c         C   s
   t  ‚ d S(   s  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	   (    (    s3   lib/python2.7/site-packages/networkx/utils/heaps.pyt   pop9   s    c         C   s
   t  ‚ d S(   sˆ  Return 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   t   default(    (    s3   lib/python2.7/site-packages/networkx/utils/heaps.pyt   getH   s    c         C   s
   t  ‚ d S(   s<  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   t   allow_increase(    (    s3   lib/python2.7/site-packages/networkx/utils/heaps.pyt   insert[   s    c         C   s   t  |  j ƒ S(   s*   Return whether the heap if empty.
        (   t   boolR   (   R	   (    (    s3   lib/python2.7/site-packages/networkx/utils/heaps.pyt   __nonzero__r   s    c         C   s   t  |  j ƒ S(   s*   Return whether the heap if empty.
        (   R   R   (   R	   (    (    s3   lib/python2.7/site-packages/networkx/utils/heaps.pyt   __bool__w   s    c         C   s   t  |  j ƒ S(   s:   Return the number of key-value pairs in the heap.
        (   t   lenR   (   R	   (    (    s3   lib/python2.7/site-packages/networkx/utils/heaps.pyt   __len__|   s    c         C   s   | |  j  k S(   s    Return whether a key exists in the heap.

        Parameters
        ----------
        key : any hashable object.
            The key to be looked up.
        (   R   (   R	   R   (    (    s3   lib/python2.7/site-packages/networkx/utils/heaps.pyt   __contains__   s    N(   R   R   R   t   objectR   R
   R   R   t   NoneR   t   FalseR   R   R   R   R   (    (    (    s3   lib/python2.7/site-packages/networkx/utils/heaps.pyR      s   						c            s   ‡  f d †  } | S(   s;   Decorator for inheriting docstrings from base classes.
    c            s   ˆ  j  |  j j |  _ |  S(   N(   t   __dict__R   R   (   t   fn(   t   cls(    s3   lib/python2.7/site-packages/networkx/utils/heaps.pyt   func   s    (    (   R$   R%   (    (   R$   s3   lib/python2.7/site-packages/networkx/utils/heaps.pyt   _inherit_docŒ   s    c           B   s¥   e  Z d  Z d e j f d „  ƒ  YZ d „  Z e e ƒ d „  ƒ Z e e ƒ d „  ƒ Z	 e e ƒ d d „ ƒ Z e e ƒ e d „ ƒ Z d „  Z d	 „  Z d
 „  Z RS(   s   A pairing heap.
    t   _Nodec           B   s   e  Z d  Z d Z d „  Z RS(   sŠ   A node in a pairing heap.

        A tree in a pairing heap is stored using the left-child, right-sibling
        representation.
        t   leftt   nextt   prevt   parentc         C   sD   t  t j |  ƒ j | | ƒ d  |  _ d  |  _ d  |  _ d  |  _ d  S(   N(	   t   superR   R'   R
   R    R(   R)   R*   R+   (   R	   R   R   (    (    s3   lib/python2.7/site-packages/networkx/utils/heaps.pyR
   ¡   s
    			(   R(   R)   R*   R+   (   R   R   R   R   R
   (    (    (    s3   lib/python2.7/site-packages/networkx/utils/heaps.pyR'   ™   s   c         C   s    t  t |  ƒ j ƒ  d |  _ d S(   s#   Initialize a pairing heap.
        N(   R,   R   R
   R    t   _root(   R	   (    (    s3   lib/python2.7/site-packages/networkx/utils/heaps.pyR
   ¬   s    c         C   s7   |  j  d  k r! t j d ƒ ‚ n  |  j  j |  j  j f S(   Ns   heap is empty.(   R-   R    t   nxt   NetworkXErrorR   R   (   R	   (    (    s3   lib/python2.7/site-packages/networkx/utils/heaps.pyR   ²   s    c         C   s\   |  j  d  k r! t j d ƒ ‚ n  |  j  } |  j |  j  ƒ |  _  |  j | j =| j | j f S(   Ns   heap is empty.(   R-   R    R.   R/   t   _merge_childrenR   R   R   (   R	   t   min_node(    (    s3   lib/python2.7/site-packages/networkx/utils/heaps.pyR   ¸   s    	c         C   s)   |  j  j | ƒ } | d  k	 r% | j S| S(   N(   R   R   R    R   (   R	   R   R   t   node(    (    s3   lib/python2.7/site-packages/networkx/utils/heaps.pyR   Á   s    c         C   s/  |  j  j | ƒ } |  j } | d  k	 rá | | j k  r† | | _ | | k	 r‚ | | j j k  r‚ |  j | ƒ |  j | | ƒ |  _ n  t S| rÝ | | j k rÝ | | _ |  j	 | ƒ } | d  k	 rÝ |  j |  j | ƒ |  _ qÝ n  t
 S|  j | | ƒ } | |  j  | <| d  k	 r|  j | | ƒ n | |  _ t Sd  S(   N(   R   R   R-   R    R   R+   t   _cutt   _linkt   TrueR0   R!   R'   (   R	   R   R   R   R2   t   roott   child(    (    s3   lib/python2.7/site-packages/networkx/utils/heaps.pyR   Æ   s&    			'c         C   sk   | j  | j  k  r" | | } } n  | j } | | _ | d k	 rL | | _ n  d | _ | | _ | | _ | S(   s_   Link two nodes, making the one with the smaller value the parent of
        the other.
        N(   R   R(   R)   R    R*   R+   (   R	   R6   t   otherR)   (    (    s3   lib/python2.7/site-packages/networkx/utils/heaps.pyR4   ë   s    					c         C   sõ   | j  } d | _  | d k	 rñ |  j } d } xi t r˜ | j } | d k rX | | _ Pn  | j } | | | ƒ } | | _ | } | d k r Pn  | } q0 W| j } x. | d k	 rÒ | j } | | | ƒ } | } q¥ Wd | _ d | _ d | _ n  | S(   s„   Merge the subtrees of the root using the standard two-pass method.
        The resulting subtree is detached from the root.
        N(   R(   R    R4   R5   R)   R*   R+   (   R	   R6   R2   t   linkR*   R)   t	   next_nextt	   prev_prev(    (    s3   lib/python2.7/site-packages/networkx/utils/heaps.pyR0   ú   s4    								
		
		c         C   sm   | j  } | j } | d k	 r* | | _ n | | j _ d | _  | d k	 r` | | _  d | _ n  d | _ d S(   s$   Cut a node from its parent.
        N(   R*   R)   R    R+   R(   (   R	   R2   R*   R)   (    (    s3   lib/python2.7/site-packages/networkx/utils/heaps.pyR3      s    				N(   R   R   R   R   R   R'   R
   R&   R   R   R    R   R!   R   R4   R0   R3   (    (    (    s3   lib/python2.7/site-packages/networkx/utils/heaps.pyR   •   s   				$		&c           B   sq   e  Z d  Z d „  Z e e ƒ d „  ƒ Z e e ƒ d „  ƒ Z e e ƒ d d „ ƒ Z	 e e ƒ e
 d „ ƒ Z RS(   s   A binary heap.
    c         C   s,   t  t |  ƒ j ƒ  g  |  _ t ƒ  |  _ d S(   s"   Initialize a binary heap.
        N(   R,   R   R
   t   _heapR   t   _count(   R	   (    (    s3   lib/python2.7/site-packages/networkx/utils/heaps.pyR
   4  s    	c         C   s„   |  j  } | s! t j d ƒ ‚ n  |  j } t } xG t ry | d \ } } } | | k rl | | | k rl Pn  | | ƒ q3 W| | f S(   Ns   heap is emptyi    (   R   R.   R/   R<   R    R5   (   R	   t   dictt   heapR   R   t   _R   (    (    s3   lib/python2.7/site-packages/networkx/utils/heaps.pyR   ;  s    			c         C   s‹   |  j  } | s! t j d ƒ ‚ n  |  j } t } xG t ry | d \ } } } | | ƒ | | k r3 | | | k r3 Pq3 q3 W| | =| | f S(   Ns   heap is emptyi    (   R   R.   R/   R<   R    R5   (   R	   R>   R?   R   R   R@   R   (    (    s3   lib/python2.7/site-packages/networkx/utils/heaps.pyR   K  s    			
c         C   s   |  j  j | | ƒ S(   N(   R   R   (   R	   R   R   (    (    s3   lib/python2.7/site-packages/networkx/utils/heaps.pyR   \  s    c         C   s«   |  j  } | | k rw | | } | | k  s= | rs | | k rs | | | <t |  j | t |  j ƒ | f ƒ | | k  St S| | | <t |  j | t |  j ƒ | f ƒ t Sd  S(   N(   R   R   R<   R)   R=   R!   R5   (   R	   R   R   R   R>   t	   old_value(    (    s3   lib/python2.7/site-packages/networkx/utils/heaps.pyR   `  s    	

"

"N(   R   R   R   R
   R&   R   R   R   R    R   R!   R   (    (    (    s3   lib/python2.7/site-packages/networkx/utils/heaps.pyR   0  s   			(   R   t
   __author__t   heapqR    R   t	   itertoolsR   t   networkxR.   t   __all__R   R   R&   R   R   (    (    (    s3   lib/python2.7/site-packages/networkx/utils/heaps.pyt   <module>   s   {		›