o
    Df                     @  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 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ZdS )    )annotations)deque)wraps)AnyCallableDictGenericHashableTupleTypeVarcast)SimpleCacheFastDictCachememoized_T)bound_Uc                   @  s0   e Zd ZdZddddZdddZ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.)
       maxsizeintreturnNonec                 C  s$   |dksJ i | _ t | _|| _d S Nr   )_datar   _keysr   )selfr    r   M/home/ubuntu/webapp/venv/lib/python3.10/site-packages/prompt_toolkit/cache.py__init__   s   
zSimpleCache.__init__keyr   getter_funcCallable[[], _U]r   c                 C  sl   z| j | W S  ty5   | }|| j |< | j| t| j | jkr1| j }|| j v r1| j |= | Y S w )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.
        )r   KeyErrorr   appendlenr   popleft)r   r   r    valuekey_to_remover   r   r   get    s   


zSimpleCache.getc                 C  s   i | _ t | _dS )zClear cache.N)r   r   r   )r   r   r   r   clear7   s   zSimpleCache.clearN)r   )r   r   r   r   )r   r   r    r!   r   r   )r   r   )__name__
__module____qualname____doc__r   r(   r)   r   r   r   r   r      s
    
r   _K._Vc                   @  s&   e Zd ZdZddd	d
Z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 	get_valueCallable[..., _V]sizer   r   r   c                 C  s$   |dksJ t  | _|| _|| _d S r   )r   r   r1   r3   )r   r1   r3   r   r   r   r   V   s   
zFastDictCache.__init__r   r.   r/   c                 C  sH   t | | jkr| j }|| v r| |= | j| }|| |< | j| |S N)r$   r3   r   r%   r1   r#   )r   r   r'   resultr   r   r   __missing__]   s   

zFastDictCache.__missing__N)r0   )r1   r2   r3   r   r   r   )r   r.   r   r/   )r*   r+   r,   r-   r   r6   r   r   r   r   r   A   s    r   _F   r   r   r   Callable[[_F], _F]c                   s   d fdd}|S )zI
    Memoization decorator for immutable classes and pure functions.
    objr7   r   c                   s,   t d td fdd}tt|S )	Nr   ar   kwr   c                    s2   d fdd} t t f}||S )Nr   r   c                     s    i S r4   r   r   )r<   r=   r:   r   r   
create_neww   s   zEmemoized.<locals>.decorator.<locals>.new_callable.<locals>.create_new)r   r   )tuplesorteditemsr(   )r<   r=   r>   r   cacher:   )r<   r=   r   new_callableu   s   z1memoized.<locals>.decorator.<locals>.new_callable)r<   r   r=   r   r   r   )r   r   r   r7   )r:   rD   r;   rB   r   	decoratorr   s   

zmemoized.<locals>.decoratorN)r:   r7   r   r7   r   )r   rE   r   r;   r   r   m   s   r   N)r8   )r   r   r   r9   )
__future__r   collectionsr   	functoolsr   typingr   r   r   r   r	   r
   r   r   __all__r   r   r   r.   r/   r   objectr7   r   r   r   r   r   <module>   s    (,)