ó
 ,µ[c           @   s5   d  Z  d d l Z d g Z d e f d „  ƒ  YZ d S(   s0   Priority queue class with updatable priorities.
iÿÿÿÿNt   MappedQueuec           B   sb   e  Z d  Z g  d „ Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 d	 „  Z RS(
   sÂ  The MappedQueue class implements an efficient minimum heap. The
    smallest element can be popped in O(1) time, new elements can be pushed
    in O(log n) time, and any element can be removed or updated in O(log n)
    time. The queue cannot contain duplicate elements and an attempt to push an
    element already in the queue will have no effect.

    MappedQueue complements the heapq package from the python standard
    library. While MappedQueue is designed for maximum compatibility with
    heapq, it has slightly different functionality.

    Examples
    --------

    A `MappedQueue` can be created empty or optionally given an array of
    initial elements. Calling `push()` will add an element and calling `pop()`
    will remove and return the smallest element.

    >>> q = MappedQueue([916, 50, 4609, 493, 237])
    >>> q.push(1310)
    True
    >>> x = [q.pop() for i in range(len(q.h))]
    >>> x
    [50, 237, 493, 916, 1310, 4609]

    Elements can also be updated or removed from anywhere in the queue.

    >>> q = MappedQueue([916, 50, 4609, 493, 237])
    >>> q.remove(493)
    >>> q.update(237, 1117)
    >>> x = [q.pop() for i in range(len(q.h))]
    >>> x
    [50, 916, 1117, 4609]

    References
    ----------
    .. [1] Cormen, T. H., Leiserson, C. E., Rivest, R. L., & Stein, C. (2001).
       Introduction to algorithms second edition.
    .. [2] Knuth, D. E. (1997). The art of computer programming (Vol. 3).
       Pearson Education.
    c         C   s)   t  | ƒ |  _ t ƒ  |  _ |  j ƒ  d S(   s8   Priority queue class with updatable priorities.
        N(   t   listt   ht   dictt   dt   _heapify(   t   selft   data(    (    s:   lib/python2.7/site-packages/networkx/utils/mapped_queue.pyt   __init__A   s    c         C   s   t  |  j ƒ S(   N(   t   lenR   (   R   (    (    s:   lib/python2.7/site-packages/networkx/utils/mapped_queue.pyt   __len__H   s    c         C   sx   t  j |  j ƒ t g  t |  j ƒ D] \ } } | | f ^ q# ƒ |  _ t |  j ƒ t |  j ƒ k rt t d ƒ ‚ n  d S(   s+   Restore heap invariant and recalculate map.s    Heap contains duplicate elementsN(   t   heapqt   heapifyR   R   t	   enumerateR   R	   t   AssertionError(   R   t   post   elt(    (    s:   lib/python2.7/site-packages/networkx/utils/mapped_queue.pyR   K   s    7c         C   sP   | |  j  k r t St |  j ƒ } |  j j | ƒ | |  j  | <|  j | ƒ t S(   s   Add an element to the queue.(   R   t   FalseR	   R   t   appendt	   _siftdownt   True(   R   R   R   (    (    s:   lib/python2.7/site-packages/networkx/utils/mapped_queue.pyt   pushR   s    c         C   s†   |  j  d } |  j | =t |  j  ƒ d k r= |  j  j ƒ  | S|  j  j ƒ  } | |  j  d <d |  j | <|  j d ƒ } |  j | ƒ | S(   s4   Remove and return the smallest element in the queue.i    i   (   R   R   R	   t   popt   _siftupR   (   R   R   t   lastR   (    (    s:   lib/python2.7/site-packages/networkx/utils/mapped_queue.pyR   _   s    
c         C   sQ   |  j  | } | |  j | <|  j  | =| |  j  | <|  j | ƒ } |  j | ƒ d S(   s/   Replace an element in the queue with a new one.N(   R   R   R   R   (   R   R   t   newR   (    (    s:   lib/python2.7/site-packages/networkx/utils/mapped_queue.pyt   updater   s    
c         C   s¥   y |  j  | } |  j  | =Wn t k
 r1 ‚  n X| t |  j ƒ d k r\ |  j j ƒ  d S|  j j ƒ  } | |  j | <| |  j  | <|  j | ƒ } |  j | ƒ d S(   s!   Remove an element from the queue.i   N(   R   t   KeyErrorR	   R   R   R   R   (   R   R   R   R   (    (    s:   lib/python2.7/site-packages/networkx/utils/mapped_queue.pyt   remove}   s    c   
      C   sF  |  j  |  j } } | | } t | ƒ } | d >d } x| | k  rA| | } y• | d } | | }	 |	 | k  r­ |	 | | | <| | <| | } } | | | | <| |	 <n7 | | | | <| | <| | } } | | | | <| | <WnH t k
 r/| | | | <| | <| | } } | | | | <| | <n X| d >d } q: W| S(   sU   Move element at pos down to a leaf by repeatedly moving the smaller
        child up.i   (   R   R   R	   t
   IndexError(
   R   R   R   R   R   t   end_post   left_post   leftt	   right_post   right(    (    s:   lib/python2.7/site-packages/networkx/utils/mapped_queue.pyR   ’   s,    



c         C   s’   |  j  |  j } } | | } xn | d k r | d d ?} | | } | | k r‰ | | | | <| | <| | } } | | | <| | | <q  Pq  W| S(   sW   Restore invariant by repeatedly replacing out-of-place element with
        its parent.i    i   (   R   R   (   R   R   R   R   R   t
   parent_post   parent(    (    s:   lib/python2.7/site-packages/networkx/utils/mapped_queue.pyR   ²   s    


(   t   __name__t
   __module__t   __doc__R   R
   R   R   R   R   R   R   R   (    (    (    s:   lib/python2.7/site-packages/networkx/utils/mapped_queue.pyR       s   (							 (   R'   R   t   __all__t   objectR    (    (    (    s:   lib/python2.7/site-packages/networkx/utils/mapped_queue.pyt   <module>   s   	