o
    �õ±jý  ã                   @  sØ   d dl mZ d dlmZ d dlmZmZ d dlmZ d dl	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)ÚCallableÚHashable)Úwraps)ÚAnyÚGenericÚ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   ú‡/root/aizidognhua/tmp/workspace/projects/ec89d86c-575f-41c9-af57-ac45cbdbf775/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   Úclear8   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   r/   r1   )r   r/   r1   r   r   r   r   W   s   
zFastDictCache.__init__r   r,   r-   c                 C  sH   t | ƒ| jkr| j ¡ }|| v r| |= | j|Ž }|| |< | j |¡ |S ©N)r"   r1   r   r#   r/   r!   )r   r   r%   Úresultr   r   r   Ú__missing__^   s   

zFastDictCache.__missing__N)r.   )r/   r0   r1   r   r   r   )r   r,   r   r-   )r(   r)   r*   r+   r   r4   r   r   r   r   r   B   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.
    Úobjr5   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 r2   r   r   )r:   r;   r8   r   r   Ú
create_newx   s   zEmemoized.<locals>.decorator.<locals>.new_callable.<locals>.create_new)r   r   )ÚtupleÚsortedÚitemsr&   )r:   r;   r<   r   ©Úcacher8   )r:   r;   r   Únew_callablev   s   z1memoized.<locals>.decorator.<locals>.new_callable)r:   r   r;   r   r   r   )r   r   r
   r5   )r8   rB   r9   r@   r   Ú	decorators   s   

zmemoized.<locals>.decoratorN)r8   r5   r   r5   r   )r   rC   r   r9   r   r   n   s   r   N)r6   )r   r   r   r7   )Ú
__future__r   Úcollectionsr   Úcollections.abcr   r   Ú	functoolsr   Útypingr   r   r	   r
   Ú__all__r   r   r   r=   r,   r-   Údictr   Úobjectr5   r   r   r   r   r   Ú<module>   s    ,)