a
    d                     @   s   d Z ddlmZmZ ddlmZ ddlZzddlmZ eddZ	W n e
yZ   e Z	Y n0 zdd	lmZ W n e
y   eZY n0 g d
ZG dd deZG dd deZG dd deZeZdS )a  Python comes with a many great data structures, from :class:`dict`
to :class:`collections.deque`, and no shortage of serviceable
algorithm implementations, from :func:`sorted` to :mod:`bisect`. But
priority queues are curiously relegated to an example documented in
:mod:`heapq`. Even there, the approach presented is not full-featured
and object-oriented. There is a built-in priority queue,
:class:`Queue.PriorityQueue`, but in addition to its austere API, it
carries the double-edged sword of threadsafety, making it fine for
multi-threaded, multi-consumer applications, but high-overhead for
cooperative/single-threaded use cases.

The ``queueutils`` module currently provides two Queue
implementations: :class:`HeapPriorityQueue`, based on a heap, and
:class:`SortedPriorityQueue`, based on a sorted list. Both use a
unified API based on :class:`BasePriorityQueue` to facilitate testing
the slightly different performance characteristics on various
application use cases.

>>> pq = PriorityQueue()
>>> pq.add('low priority task', 0)
>>> pq.add('high priority task', 2)
>>> pq.add('medium priority task 1', 1)
>>> pq.add('medium priority task 2', 1)
>>> len(pq)
4
>>> pq.pop()
'high priority task'
>>> pq.peek()
'medium priority task 1'
>>> len(pq)
3

    )heappushheappopinsortN   )make_sentinel_REMOVED)var_name)BList)PriorityQueueBasePriorityQueueHeapPriorityQueueSortedPriorityQueuec                   @   s|   e Zd ZdZedd ZeZdd Zedd Z	edd	 Z
dddZdd ZdddZefddZefddZdd Zd
S )r   a  The abstract base class for the other PriorityQueues in this
    module. Override the ``_backend_type`` class attribute, as well as
    the :meth:`_push_entry` and :meth:`_pop_entry` staticmethods for
    custom subclass behavior. (Don't forget to use
    :func:`staticmethod`).

    Args:
        priority_key (callable): A function that takes *priority* as
            passed in by :meth:`add` and returns a real number
            representing the effective priority.

    c                 C   s   t | pd S Nr   )float)p r   [/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/boltons/queueutils.py<lambda>k       zBasePriorityQueue.<lambda>c                 K   sB   |   | _i | _t | _|d| j| _|r>t	d|
  d S )NZpriority_keyz unexpected keyword arguments: %r)_backend_type_pq
_entry_map	itertoolscount_counterpop_default_priority_key_get_priority	TypeErrorkeys)selfkwr   r   r   __init__n   s    

zBasePriorityQueue.__init__c                 C   s   d S Nr   backendentryr   r   r   _push_entryv   s    zBasePriorityQueue._push_entryc                 C   s   d S r$   r   r&   r   r   r   
_pop_entryz   s    zBasePriorityQueue._pop_entryNc                 C   sN   |  |}|| jv r| | t| j}|||g}|| j|< | | j| dS )aP  
        Add a task to the queue, or change the *task*'s priority if *task*
        is already in the queue. *task* can be any hashable object,
        and *priority* defaults to ``0``. Higher values representing
        higher priority, but this behavior can be controlled by
        setting *priority_key* in the constructor.
        N)r   r   removenextr   r(   r   )r!   taskpriorityr   r'   r   r   r   add~   s    





zBasePriorityQueue.addc                 C   s   | j |}t|d< dS )zgRemove a task from the priority queue. Raises :exc:`KeyError` if
        the *task* is absent.
        N)r   r   r   )r!   r-   r'   r   r   r   r+      s    zBasePriorityQueue.removeTc                 C   s@   | j r0| j d \}}}|tu r,| | j  q dS |r<tddS )zBRemove entries marked as removed by previous :meth:`remove` calls.r   Nzempty priority queue)r   r   r*   
IndexError)r!   Z	raise_excr.   r   r-   r   r   r   _cull   s    zBasePriorityQueue._cullc                 C   sL   z|    | jd \}}}W n* tyF   |tur:| Y S tdY n0 |S )zRead the next value in the queue without removing it. Returns
        *default* on an empty queue, or raises :exc:`KeyError` if
        *default* is not set.
        r   zpeek on empty queue)r2   r   r1   r   r!   default_r-   r   r   r   peek   s    zBasePriorityQueue.peekc                 C   sV   z&|    | | j\}}}| j|= W n* tyP   |turD| Y S tdY n0 |S )zRemove and return the next value in the queue. Returns *default* on
        an empty queue, or raises :exc:`KeyError` if *default* is not
        set.
        zpop on empty queue)r2   r*   r   r   r1   r   r3   r   r   r   r      s    zBasePriorityQueue.popc                 C   s
   t | jS )z(Return the number of tasks in the queue.)lenr   )r!   r   r   r   __len__   s    zBasePriorityQueue.__len__)N)T)__name__
__module____qualname____doc__staticmethodr   listr   r#   r(   r*   r/   r+   r2   r   r6   r   r8   r   r   r   r   r   ]   s   



r   c                   @   s(   e Zd ZdZedd Zedd ZdS )r   zA priority queue inherited from :class:`BasePriorityQueue`,
    backed by a list and based on the :func:`heapq.heappop` and
    :func:`heapq.heappush` functions in the built-in :mod:`heapq`
    module.
    c                 C   s   t | S r$   )r   r)   r   r   r   r*      s    zHeapPriorityQueue._pop_entryc                 C   s   t | | d S r$   )r   r%   r   r   r   r(      s    zHeapPriorityQueue._push_entryN)r9   r:   r;   r<   r=   r*   r(   r   r   r   r   r      s
   
r   c                   @   s,   e Zd ZdZeZedd Zedd ZdS )r   zA priority queue inherited from :class:`BasePriorityQueue`, based
    on the :func:`bisect.insort` approach for in-order insertion into
    a sorted list.
    c                 C   s
   |  dS r   )r   r)   r   r   r   r*      s    zSortedPriorityQueue._pop_entryc                 C   s   t | | d S r$   r   r%   r   r   r   r(      s    zSortedPriorityQueue._push_entryN)	r9   r:   r;   r<   r
   r   r=   r*   r(   r   r   r   r   r      s   
r   )r<   heapqr   r   bisectr   r   Z	typeutilsr   r   ImportErrorobjectZ	listutilsr
   r>   __all__r   r   r   r   r   r   r   r   <module>!   s"   #
e