
    wg'                     >    d Z ddlZdgZ G d d      Z G d d      Zy)/Priority queue class with updatable priorities.    NMappedQueuec                   H    e Zd ZdZg dZd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zy)_HeapElementa  This proxy class separates the heap element from its priority.

    The idea is that using a 2-tuple (priority, element) works
    for sorting, but not for dict lookup because priorities are
    often floating point values so round-off can mess up equality.

    So, we need inequalities to look at the priority (for sorting)
    and equality (and hash) to look at the element to enable
    updates to the priority.

    Unfortunately, this class can be tricky to work with if you forget that
    `__lt__` compares the priority while `__eq__` compares the element.
    In `greedy_modularity_communities()` the following code is
    used to check that two _HeapElements differ in either element or priority:

        if d_oldmax != row_max or d_oldmax.priority != row_max.priority:

    If the priorities are the same, this implementation uses the element
    as a tiebreaker. This provides compatibility with older systems that
    use tuples to combine priority and elements.
    )priorityelement_hashc                 @    || _         || _        t        |      | _        y N)r   r   hashr	   )selfr   r   s      `/home/mcse/projects/flask/flask-venv/lib/python3.12/site-packages/networkx/utils/mapped_queue.py__init__z_HeapElement.__init__!   s     ']
    c                     	 |j                   }| j                   |k(  r	 | j                  |j                  k  S | j                   |k  S # t        $ r | j                   |k  cY S w xY w# t        $ r}t        d      d }~ww xY wNzCConsider using a tuple, with a priority value that can be compared.r   AttributeErrorr   	TypeErrorr   otherother_priorityerrs       r   __lt__z_HeapElement.__lt__&       	)"^^N ==N*||emm33
 }}~--  	)==5((	)  Y (   A A$ A! A!$	A=-A88A=c                     	 |j                   }| j                   |k(  r	 | j                  |j                  kD  S | j                   |kD  S # t        $ r | j                   |kD  cY S w xY w# t        $ r}t        d      d }~ww xY wr   r   r   s       r   __gt__z_HeapElement.__gt__5   r   r   c                 r    	 | j                   |j                   k(  S # t        $ r | j                   |k(  cY S w xY wr   )r   r   )r   r   s     r   __eq__z_HeapElement.__eq__D   s8    	)<<5==00 	)<<5((	)s    66c                     | j                   S r   )r	   r   s    r   __hash__z_HeapElement.__hash__J   s    zzr   c                 H    |dk(  r| j                   S | j                  |dz
     S )Nr      r   r   )r   indxs     r   __getitem__z_HeapElement.__getitem__M   s$     $	t}}Et||D1H/EEr   c              #      K   | j                    	 | j                  E d {    y 7 # t        $ r | j                   Y y w xY wwr   )r   r   r   r"   s    r   __iter__z_HeapElement.__iter__P   s:     mm	||## 	,,	s1   A) ') A) A AAAc                 <    d| j                    d| j                   dS )Nz_HeapElement(z, )r&   r"   s    r   __repr__z_HeapElement.__repr__W   s    t}}oR~Q??r   N)__name__
__module____qualname____doc__	__slots__r   r   r   r    r#   r(   r*   r-    r   r   r   r      s8    , 1I#
..)F@r   r   c                   L    e Zd ZdZddZd Zd ZddZd ZddZ	d	 Z
d
 Zd Zy)r   a  The MappedQueue class implements a min-heap with removal and update-priority.

    The min heap uses heapq as well as custom written _siftup and _siftdown
    methods to allow the heap positions to be tracked by an additional dict
    keyed by element to position. 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 adds element removal, lookup, and priority update.

    Parameters
    ----------
    data : dict or iterable

    Examples
    --------

    A `MappedQueue` can be created empty, or optionally, given a dictionary
    of initial elements and priorities.  The methods `push`, `pop`,
    `remove`, and `update` operate on the queue.

    >>> colors_nm = {"red": 665, "blue": 470, "green": 550}
    >>> q = MappedQueue(colors_nm)
    >>> q.remove("red")
    >>> q.update("green", "violet", 400)
    >>> q.push("indigo", 425)
    True
    >>> [q.pop().element for i in range(len(q.heap))]
    ['violet', 'indigo', 'blue']

    A `MappedQueue` can also be initialized with a list or other iterable. The priority is assumed
    to be the sort order of the items in the list.

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

    An exception is raised if the elements are not comparable.

    >>> q = MappedQueue([100, "a"])
    Traceback (most recent call last):
    ...
    TypeError: '<' not supported between instances of 'int' and 'str'

    To avoid the exception, use a dictionary to assign priorities to the elements.

    >>> q = MappedQueue({100: 0, "a": 1})

    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.
    Nc                     |g | _         nSt        |t              r3|j                         D cg c]  \  }}t	        ||       c}}| _         nt        |      | _         i | _        | j                          yc c}}w )r   N)heap
isinstancedictitemsr   listposition_heapify)r   datakvs       r   r   zMappedQueue.__init__   s\    <DId#8<

E1a+EDIT
DI	 Fs   A6c                    t        j                  | j                         t        | j                        D ci c]  \  }}||
 c}}| _        t        | j                        t        | j                        k7  rt        d      yc c}}w )z+Restore heap invariant and recalculate map.z Heap contains duplicate elementsN)heapqheapifyr6   	enumerater;   lenAssertionError)r   poselts      r   r<   zMappedQueue._heapify   se    dii 2;DII2FGhc3cGtyy>S// !CDD 0 Hs   Bc                 ,    t        | j                        S r   )rD   r6   r"   s    r   __len__zMappedQueue.__len__   s    499~r   c                     |t        ||      }|| j                  v ryt        | j                        }| j                  j	                  |       || j                  |<   | j                  d|       y)zAdd an element to the queue.Fr   T)r   r;   rD   r6   append	_siftdown)r   rG   r   rF   s       r   pushzMappedQueue.push   sa    x-C$--$))n		 cq#r   c                 8   | j                   d   }| j                  |= t        | j                         dk(  r| j                   j                          |S | j                   j                         }|| j                   d<   d| j                  |<   | j	                  d       |S )z4Remove and return the smallest element in the queue.r   r%   )r6   r;   rD   pop_siftup)r   rG   lasts      r   rO   zMappedQueue.pop   sy     iilMM#tyy>QIIMMOJyy}}		!dQ
r   c                     |t        ||      }| j                  |   }|| j                  |<   | j                  |= || j                  |<   | j                  |       y)z/Replace an element in the queue with a new one.N)r   r;   r6   rP   )r   rG   newr   rF   s        r   updatezMappedQueue.update   sU    x-CmmC 		#MM# cSr   c                 X   	 | j                   |   }| j                   |= |t        | j                        dz
  k(  r| j                  j	                          y| j                  j	                         }|| j                  |<   || j                   |<   | j                  |       y# t        $ r  w xY w)z!Remove an element from the queue.r%   N)r;   KeyErrorrD   r6   rO   rP   )r   rG   rF   rQ   s       r   removezMappedQueue.remove   s    	--$Cc"
 #dii.1$$IIMMOyy}}		#!dS  		s   B B)c                 H   | j                   | j                  }}t        |      }|}||   }|dz  dz   }||k  r7||   }|dz   }	|	|k  r||	   }
||
k  s|
}|	}|||<   |||<   |}|dz  dz   }||k  r7|dkD  r%|dz
  dz	  }||   }||k  sn|||<   |||<   |}|dkD  r%|||<   |||<   y)zMove smaller child up until hitting a leaf.

        Built to mimic code for heapq._siftup
        only updating position dict too.
        r%   r   N)r6   r;   rD   )r   rF   r6   r;   end_posstartposnewitem	child_poschild	right_posright
parent_posparents                r   rP   zMappedQueue._siftup   s    DMMhd)s)AXN	'!OE!AI7"Yu}!E )IDI!HUOCQI '!  Ag'aJ*%FV#DI"HVC Ag S	r   c                     | j                   | j                  }}||   }||kD  r%|dz
  dz	  }||   }||k  sn|||<   |||<   |}||kD  r%|||<   |||<   y)zRestore invariant. keep swapping with parent until smaller.

        Built to mimic code for heapq._siftdown
        only updating position dict too.
        r%   N)r6   r;   )r   	start_posrF   r6   r;   r[   r`   ra   s           r   rL   zMappedQueue._siftdown  s     DMMhs) Io'aJ*%FV#DI"HVC Io S	r   r   )r.   r/   r0   r1   r   r<   rI   rM   rO   rT   rW   rP   rL   r3   r   r   r   r   [   s7    ;z	E$
(% N r   )r1   rA   __all__r   r   r3   r   r   <module>re      s-    5 /P@ P@fN  N r   