o
    ˷e#                     @   s2   d Z ddlZdgZG dd dZG dd dZdS )z0Priority queue class with updatable priorities.
    NMappedQueuec                   @   sX   e Zd ZdZg dZdd Zdd Zdd Zd	d
 Zdd Z	dd Z
dd Zdd ZdS )_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                 C   s   || _ || _t|| _d S N)r   r   hashr   )selfr   r    r
   R/var/www/ideatree/venv/lib/python3.10/site-packages/networkx/utils/mapped_queue.py__init__"   s   z_HeapElement.__init__c                 C   sH   z|j }W n ty   | j |k  Y S w | j |kr| j|jk S | j |k S r   r   AttributeErrorr   r	   otherother_priorityr
   r
   r   __lt__'      


z_HeapElement.__lt__c                 C   sH   z|j }W n ty   | j |k Y S w | j |kr| j|jk S | j |kS r   r   r   r
   r
   r   __gt__1   r   z_HeapElement.__gt__c                 C   s,   z| j |j kW S  ty   | j |k Y S w r   )r   r   )r	   r   r
   r
   r   __eq__;   s
   z_HeapElement.__eq__c                 C   s   | j S r   )r   r	   r
   r
   r   __hash__A   s   z_HeapElement.__hash__c                 C   s   |dkr| j S | j|d  S )Nr      r   r   )r	   indxr
   r
   r   __getitem__D   s   z_HeapElement.__getitem__c                 c   s:    | j V  z	| jE d H  W d S  ty   | jV  Y d S w r   )r   r   	TypeErrorr   r
   r
   r   __iter__G   s   z_HeapElement.__iter__c                 C   s   d| j  d| j dS )Nz_HeapElement(z, )r   r   r
   r
   r   __repr__N   s   z_HeapElement.__repr__N)__name__
__module____qualname____doc__	__slots__r   r   r   r   r   r   r   r   r
   r
   r
   r   r   	   s    

r   c                   @   s`   e Zd ZdZg fddZdd Zdd Zdd	d
Zdd ZdddZ	dd Z
dd Zdd ZdS )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.

    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
    >>> [q.pop() for i in range(len(q.heap))]
    [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)
    >>> [q.pop() for i in range(len(q.heap))]
    [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rdd | D | _nt|| _t | _|   dS )z/Priority queue class with updatable priorities.c                 S   s   g | ]	\}}t ||qS r
   )r   ).0kvr
   r
   r   
<listcomp>   s    z(MappedQueue.__init__.<locals>.<listcomp>N)
isinstancedictitemsheaplistposition_heapify)r	   datar
   r
   r   r   }   s
   

zMappedQueue.__init__c                 C   sB   t | j dd t| jD | _t| jt| jkrtddS )z+Restore heap invariant and recalculate map.c                 S   s   i | ]\}}||qS r
   r
   )r%   poseltr
   r
   r   
<dictcomp>   s    z(MappedQueue._heapify.<locals>.<dictcomp>z Heap contains duplicate elementsN)heapqheapifyr,   	enumerater.   lenAssertionErrorr   r
   r
   r   r/      s
   zMappedQueue._heapifyc                 C   s
   t | jS r   )r7   r,   r   r
   r
   r   __len__   s   
zMappedQueue.__len__Nc                 C   sP   |dur	t ||}|| jv rdS t| j}| j| || j|< | d| dS )zAdd an element to the queue.NFr   T)r   r.   r7   r,   append	_siftdown)r	   r2   r   r1   r
   r
   r   push   s   



zMappedQueue.pushc                 C   sZ   | j d }| j|= t| j dkr| j   |S | j  }|| j d< d| j|< | d |S )z4Remove and return the smallest element in the queue.r   r   )r,   r.   r7   pop_siftup)r	   r2   lastr
   r
   r   r=      s   





zMappedQueue.popc                 C   sF   |dur	t ||}| j| }|| j|< | j|= || j|< | | dS )z/Replace an element in the queue with a new one.N)r   r.   r,   r>   )r	   r2   newr   r1   r
   r
   r   update   s   



zMappedQueue.updatec                 C   st   z| j | }| j |= W n ty    w |t| jd kr$| j  dS | j }|| j|< || j |< | | dS )z!Remove an element from the queue.r   N)r.   KeyErrorr7   r,   r=   r>   )r	   r2   r1   r?   r
   r
   r   remove   s   




zMappedQueue.removec                 C   s   | j | j}}t|}|}|| }|d> d }||k rG|| }|d }	|	|k r3||	 }
||
k s3|
}|	}|||< |||< |}|d> d }||k s|dkrh|d d? }|| }||k sZn|||< |||< |}|dksK|||< |||< dS )zMove smaller child up until hitting a leaf.

        Built to mimic code for heapq._siftup
        only updating position dict too.
        r   r   N)r,   r.   r7   )r	   r1   r,   r.   end_posstartposnewitem	child_poschild	right_posright
parent_posparentr
   r
   r   r>      s:   zMappedQueue._siftupc                 C   sl   | j | j}}|| }||kr,|d d? }|| }||k sn|||< |||< |}||ks|||< |||< dS )zRestore invariant. keep swapping with parent until smaller.

        Built to mimic code for heapq._siftdown
        only updating position dict too.
        r   N)r,   r.   )r	   	start_posr1   r,   r.   rF   rK   rL   r
   r
   r   r;      s   zMappedQueue._siftdownr   )r    r!   r"   r#   r   r/   r9   r<   r=   rA   rC   r>   r;   r
   r
   r
   r   r   R   s    *	

')r#   r4   __all__r   r   r
   r
   r
   r   <module>   s
    I