B
    't\T                 @   s\   d dl mZ d dlmZ d dlmZ dddgZG dd deZG dd de	Z
dd
dZdS )    )unicode_literals)deque)wrapsSimpleCacheFastDictCachememoizedc               @   s*   e Zd ZdZd
ddZdd Z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.)
       c             C   s.   t |tr|dksti | _t | _|| _d S )Nr   )
isinstanceintAssertionError_datar   _keysmaxsize)selfr    r   3lib/python3.7/site-packages/prompt_toolkit/cache.py__init__   s    zSimpleCache.__init__c             C   sl   y
| j | S  tk
rf   | }|| j |< | j| t| j | jkrb| j }|| j krb| j |= |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   keyZgetter_func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   clear1   s    zSimpleCache.clearN)r   )__name__
__module____qualname____doc__r   r   r   r   r   r   r   r      s   
c               @   s"   e Zd ZdZdddZ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.
    N@B c             C   s:   t |stt|tr|dks"tt | _|| _|| _d S )Nr   )callabler   r	   r
   r   r   	get_valuesize)r   r"   r#   r   r   r   r   K   s
    zFastDictCache.__init__c             C   sH   t | | jkr&| j }|| kr&| |= | j| }|| |< | j| |S )N)r   r#   r   r   r"   r   )r   r   r   resultr   r   r   __missing__S   s    

zFastDictCache.__missing__)Nr    )r   r   r   r   r   r%   r   r   r   r   r   7   s   	
   c                s    fdd}|S )zI
    Memoization decorator for immutable classes and pure functions.
    c                s$   t d t fdd}|S )N)r   c                 s0    fdd} t t f}||S )Nc                  s
    S )Nr   r   )akwobjr   r   
create_newi   s    zEmemoized.<locals>.decorator.<locals>.new_callable.<locals>.create_new)tuplesorteditemsr   )r'   r(   r*   r   )cacher)   )r'   r(   r   new_callableg   s    z1memoized.<locals>.decorator.<locals>.new_callable)r   r   )r)   r/   )r   )r.   r)   r   	decoratord   s    
zmemoized.<locals>.decoratorr   )r   r0   r   )r   r   r   `   s    N)r&   )Z
__future__r   collectionsr   	functoolsr   __all__objectr   dictr   r   r   r   r   r   <module>   s   +)