U
    cc                     @   s   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 dddg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eeegef dddZdS )    )deque)wraps)	AnyCallableDequeDictGenericHashableTupleTypeVarcastSimpleCacheFastDictCachememoized_T)bound_Uc                   @   sJ   e Zd ZdZdeddddZeeg ef e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.)
       Nmaxsizereturnc                 C   s$   |dkst i | _t | _|| _d S Nr   )AssertionError_datar   _keysr   )selfr    r   N/var/www/html/project/venv/lib/python3.8/site-packages/prompt_toolkit/cache.py__init__   s    zSimpleCache.__init__)keygetter_funcr   c                 C   sr   z| j | W S  tk
rl   | }|| j |< | j| t| j | jkrd| j }|| j krd| j |= | Y S X 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   clear5   s    zSimpleCache.clear)r   )__name__
__module____qualname____doc__intr   r   r   r   r'   r)   r   r   r   r   r      s   _K._Vc                   @   s<   e Zd ZdZdedef eddddZee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 .N)	get_valuesizer   c                 C   s$   |dkst t | _|| _|| _d S r   )r   r   r   r2   r3   )r   r2   r3   r   r   r   r   T   s    zFastDictCache.__init__)r   r   c                 C   sH   t | | jkr&| j }|| kr&| |= | j| }|| |< | j| |S N)r#   r3   r   r$   r2   r"   )r   r   r&   resultr   r   r   __missing__[   s    

zFastDictCache.__missing__)r1   )
r*   r+   r,   r-   r   r0   r.   r   r/   r6   r   r   r   r   r   ?   s   _F   r   c                    s   t t d fdd}|S )zI
    Memoization decorator for immutable classes and pure functions.
    )objr   c                    s4   t d ttttd fdd}tt|S )Nr   )akwr   c                     s6   t d fdd} tt f}||S )Nr(   c                      s
    S r4   r   r   )r;   r<   r9   r   r   
create_newu   s    zEmemoized.<locals>.decorator.<locals>.new_callable.<locals>.create_new)r   tuplesorteditemsr'   )r;   r<   r=   r   cacher9   )r;   r<   r   new_callables   s    z1memoized.<locals>.decorator.<locals>.new_callable)r   r   r   r   r7   )r9   rC   r:   rA   r   	decoratorp   s    
zmemoized.<locals>.decorator)r7   )r   rD   r   r:   r   r   k   s    N)r8   )collectionsr   	functoolsr   typingr   r   r   r   r   r	   r
   r   r   __all__r   r   r   r/   r0   r   objectr7   r.   r   r   r   r   r   <module>   s   ,,)