a
    õ£Beö  ã                   @  sâ   d dl mZ d dlmZ d dlmZ d dlmZmZm	Z	m
Z
mZmZmZmZmZ g d¢Zeded�ZedƒZG d	d
„ d
eeef ƒZedeedf d�ZedƒZG dd„ de
eef ƒZededef d�Zddddœdd„ZdS )é    )Úannotations)Údeque)Úwraps)	ÚAnyÚCallableÚDequeÚDictÚGenericÚHashableÚTupleÚTypeVarÚcast)ÚSimpleCacheÚFastDictCacheÚmemoizedÚ_T)ÚboundÚ_Uc                   @  sB   e Zd ZdZddddœdd„Zdd	d
dœdd„Zddœdd„ZdS )r   z§
    Very simple cache that discards the oldest item when the cache size is
    exceeded.

    :param maxsize: Maximum size of the cache. (Don't make it too big.)
    é   ÚintÚNone©ÚmaxsizeÚreturnc                 C  s$   |dksJ ‚i | _ tƒ | _|| _d S ©Nr   )Ú_datar   Ú_keysr   )Úselfr   © r   úW/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/prompt_toolkit/cache.pyÚ__init__   s    zSimpleCache.__init__r   zCallable[[], _U]r   )ÚkeyÚgetter_funcr   c                 C  sp   z| j | W S  tyj   |ƒ }|| j |< | j |¡ t| j ƒ| jkrb| j ¡ }|| j v rb| j |= | Y S 0 dS )zš
        Get object from the cache.
        If not found, call `getter_func` to resolve it, and put that on the top
        of the cache instead.
        N)r   ÚKeyErrorr   ÚappendÚlenr   Úpopleft)r   r!   r"   ÚvalueÚkey_to_remover   r   r   Úget    s    


zSimpleCache.get©r   c                 C  s   i | _ tƒ | _dS )zClear cache.N)r   r   r   )r   r   r   r   Úclear7   s    zSimpleCache.clearN)r   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r    r)   r+   r   r   r   r   r      s   r   Ú_K.Ú_Vc                   @  s4   e Zd ZdZdddddœdd„Zd	d
dœdd„ZdS )r   a¿  
    Fast, lightweight cache which keeps at most `size` items.
    It will discard the oldest items in the cache first.

    The cache is a dictionary, which doesn't keep track of access counts.
    It is perfect to cache little immutable objects which are not expensive to
    create, but where a dictionary lookup is still much faster than an object
    instantiation.

    :param get_value: Callable that's called in case of a missing key.
    é@B zCallable[..., _V]r   r   )Ú	get_valueÚsizer   c                 C  s$   |dksJ ‚t ƒ | _|| _|| _d S r   )r   r   r3   r4   )r   r3   r4   r   r   r   r    V   s    zFastDictCache.__init__r0   r1   )r!   r   c                 C  sH   t | ƒ| jkr&| j ¡ }|| v r&| |= | j|Ž }|| |< | j |¡ |S ©N)r%   r4   r   r&   r3   r$   )r   r!   r(   Úresultr   r   r   Ú__missing__]   s    

zFastDictCache.__missing__N)r2   )r,   r-   r.   r/   r    r7   r   r   r   r   r   A   s   r   Ú_Fé   r   zCallable[[_F], _F]r   c                   s   dddœ‡ fdd„}|S )zI
    Memoization decorator for immutable classes and pure functions.
    r8   )Úobjr   c                   s4   t ˆd�‰ tˆƒddddœ‡ ‡fdd„ƒ}tt|ƒS )N©r   r   )ÚaÚkwr   c                    s6   ddœ‡ ‡‡fdd„}ˆ t tˆ ¡ ƒƒf}ˆ ||¡S )Nr   r*   c                     s   ˆˆ i ˆ¤ŽS r5   r   r   )r<   r=   r:   r   r   Ú
create_neww   s    zEmemoized.<locals>.decorator.<locals>.new_callable.<locals>.create_new)ÚtupleÚsortedÚitemsr)   )r<   r=   r>   r!   ©Úcacher:   )r<   r=   r   Únew_callableu   s    z1memoized.<locals>.decorator.<locals>.new_callable)r   r   r   r8   )r:   rD   r;   rB   r   Ú	decoratorr   s    
zmemoized.<locals>.decoratorr   )r   rE   r   r;   r   r   m   s    r   N)r9   )Ú
__future__r   Úcollectionsr   Ú	functoolsr   Útypingr   r   r   r   r	   r
   r   r   r   Ú__all__r   r   r   r0   r1   r   Úobjectr8   r   r   r   r   r   Ú<module>   s   ,,)