U
    /e=                     @  s   d dl mZ d dlZd dlZd dlZd dlmZmZ d dlm	Z	m
Z
mZ d dlmZ d dlmZmZmZ ede
dZG d	d
 d
eZG dd dee ZdS )    )annotationsN)OrderedDictUserDict)CallableHashableIterator)
MutableSet)AnyTypeVarcastT)boundc                      s>   e Zd ZdZdd fddZ fddZ fdd	Z  ZS )
LRUzILimited size mapping, evicting the least recently looked-up key when fullfloat)maxsizec                   s   t    t | _|| _d S N)super__init__r   datar   )selfr   	__class__ ;/tmp/pip-unpacked-wheel-g426oqom/distributed/collections.pyr      s    
zLRU.__init__c                   s"   t  |}tt| j| |S r   )r   __getitem__r   r   r   move_to_endr   keyvaluer   r   r   r      s    zLRU.__getitem__c                   s4   t | | jkr"tt| jjdd t || d S )NF)last)lenr   r   r   r   popitemr   __setitem__r   r   r   r   r"      s    zLRU.__setitem__)__name__
__module____qualname____doc__r   r   r"   __classcell__r   r   r   r   r      s   r   c                   @  s0  e Zd ZU dZdZded< ded< ded< d	ed
< ded< ddddZddddZddddZe	dd	dddddZ
dddddZd	dd d!Zd"d#dd$d%Zd"d#dd&d'Zd"dd(d)Zd	d*d+d,d-Zd"dd.d/Zd"dd0d1Zd"dd2d3Zd*dd4d5Zd*dd6d7Zd#dd8d9Zd:S );HeapSeta  A set-like where the `pop` method returns the smallest item, as sorted by an
    arbitrary key function. Ties are broken by oldest first.

    Values must be compatible with :mod:`weakref`.

    Parameters
    ----------
    key: Callable
        A function that takes a single element of the collection as a parameter and
        returns a sorting key. The key does not need to be hashable and does not need to
        support :mod:`weakref`.

    Note
    ----
    The key returned for each element should not to change over time. If it does, the
    position in the heap won't change, even if the element is re-added, and it *may* not
    change even if it's discarded and then re-added later.
    )r   _data_heap_inc_sortedzCallable[[T], Any]r   zset[T]r)   z%list[tuple[Any, int, weakref.ref[T]]]r*   intr+   boolr,   )r   c                C  s$   || _ t | _d| _g | _d| _d S )Nr   T)r   setr)   r+   r*   r,   )r   r   r   r   r   r   =   s
    zHeapSet.__init__str)returnc                 C  s   dt | j dt|  dS )N<z: z items>)typer#   r    r   r   r   r   __repr__D   s    zHeapSet.__repr__ztuple[Callable, tuple]c                   s*    fdd j D }tj j j|ffS )Nc                   s,   g | ]$\}}}|   j kr||fqS r   r)   ).0kivrefr   vr   r   
<listcomp>H   s      z&HeapSet.__reduce__.<locals>.<listcomp>)r*   r(   	_unpickler   r+   )r   heapr   r;   r   
__reduce__G   s    zHeapSet.__reduce__zlist[tuple[Any, int, T]]z
HeapSet[T])r   incr?   r1   c                 C  sN   t t}| |_dd |D |_||_dd |D |_t|j | |_	|S )Nc                 S  s   h | ]\}}}|qS r   r   )r7   _r<   r   r   r   	<setcomp>Q   s     z$HeapSet._unpickle.<locals>.<setcomp>c                 S  s"   g | ]\}}}||t |fqS r   )weakrefref)r7   r8   r9   r<   r   r   r   r=   S   s     z%HeapSet._unpickle.<locals>.<listcomp>)
object__new__r(   r   r)   r+   r*   heapqheapifyr,   )r   rA   r?   r   r   r   r   r>   K   s    
zHeapSet._unpicklerF   )r   r1   c                 C  s
   || j kS r   r6   r   r   r   r   r   __contains__X   s    zHeapSet.__contains__c                 C  s
   t | jS r   )r    r)   r4   r   r   r   __len__[   s    zHeapSet.__len__r   Nonec                 C  s\   || j krd S | |}t|}t| j|| j|f d| _| j 	| |  jd7  _d S )NF   )
r)   r   rD   rE   rH   heappushr*   r+   r,   add)r   r   r8   r:   r   r   r   rP   ^   s    


zHeapSet.addc                 C  s   | j | | j s|   d S r   )r)   discardclearrJ   r   r   r   rQ   h   s    zHeapSet.discardc                 C  sD   | j std| jd d  }|| j kr,|S t| j d| _qdS )z/Return the smallest element without removing itpeek into empty setr      FN)r)   KeyErrorr*   rH   heappopr,   rJ   r   r   r   peekm   s    
zHeapSet.peekzIterator[T])nr1   c                 c  s>   |dks| sdS |dkr$|   V  nt|  |E dH  dS )zzIterate over the n smallest elements without removing them.
        This is O(1) for n == 1; O(n*logn) otherwise.
        r   NrN   )rW   	itertoolsislicesorted)r   rX   r   r   r   peeknx   s
    zHeapSet.peeknc                 C  sZ   | j stdt| j\}}}d| _| }|| j kr| j | | j sP|   |S qd S )Npop from an empty setF)r)   rU   rH   rV   r*   r,   rQ   rR   r   rB   r:   r   r   r   r   pop   s    
zHeapSet.popc                 C  s:   | j std| jd d  }|| j kr,|S | jd= qdS )zReturn one of the largest elements (not necessarily the largest!) without
        removing it. It's guaranteed that ``self.peekright() >= self.peek()``.
        rS   rT   N)r)   rU   r*   rJ   r   r   r   	peekright   s    
zHeapSet.peekrightc                 C  sR   | j std| j \}}}| }|| j kr| j | | j sH|   |S qdS )zRemove and return one of the largest elements (not necessarily the largest!)
        It's guaranteed that ``self.popright() >= self.peek()``.
        r]   N)r)   rU   r*   r_   rQ   rR   r^   r   r   r   popright   s    
zHeapSet.poprightc                 C  s
   t | jS )zwIterate over all elements. This is a O(n) operation which returns the
        elements in pseudo-random order.
        )iterr)   r4   r   r   r   __iter__   s    zHeapSet.__iter__c                 c  sZ   | j s| j  d| _ t }| jD ]2\}}}| }|| jkr"||kr"|V  || q"dS )zIterate over all elements. This is a O(n*logn) operation which returns the
        elements in order, from smallest to largest according to the key and insertion
        order.
        TN)r,   r*   sortr/   r)   rP   )r   seenrB   r:   r   r   r   r   r[      s    
zHeapSet.sortedc                 C  s   | j   | j  d| _d S )NT)r)   rR   r*   r,   r4   r   r   r   rR      s    

zHeapSet.clearN)r#   r$   r%   r&   	__slots____annotations__r   r5   r@   staticmethodr>   rK   rL   rP   rQ   rW   r\   r_   ra   rb   rd   r[   rR   r   r   r   r   r(   "   s0   

r(   )
__future__r   rH   rY   rD   collectionsr   r   collections.abcr   r   r   typingr   r	   r
   r   r   r   r(   r   r   r   r   <module>   s   