
    H                     `    d dl mZ d dlmZ d dlmZ dZ G d de      Z G d de	      Z
dd	Zy
)    )unicode_literals)dequewraps)SimpleCacheFastDictCachememoizedc                   $    e Zd ZdZddZd Zd Zy)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                 l    t        |t              r|dkD  sJ i | _        t               | _        || _        y Nr   )
isinstanceint_datar   _keysmaxsize)selfr   s     'lib/third_party/prompt_toolkit/cache.py__init__zSimpleCache.__init__   s/    '3'GaK77
W
    c                 T   	 | j                   |   S # t        $ r  |       }|| j                   |<   | j                  j                  |       t	        | j                         | j
                  kD  r5| j                  j                         }|| j                   v r| j                   |= |cY S w xY 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   keygetter_funcvaluekey_to_removes        r   getzSimpleCache.get   s    	::c?" 	ME#DJJsOJJc" 4::- $

 2 2 4 DJJ.

=1L	s    BB'&B'c                 0    i | _         t               | _        y)z Clear cache. N)r   r   r   )r   s    r   clearzSimpleCache.clear1   s    
W
r   N)   )__name__
__module____qualname____doc__r   r   r!    r   r   r   r      s    .r   r   c                       e Zd ZdZddZd Zy)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.
    Nc                     t        |      sJ t        |t              r|dkD  sJ t               | _        || _        || _        y r   )callabler   r   r   r   	get_valuesize)r   r+   r,   s      r   r   zFastDictCache.__init__K   s=    	"""$$11W
"	r   c                     t        |       | j                  kD  r!| j                  j                         }|| v r| |=  | j                  | }|| |<   | j                  j                  |       |S N)r   r,   r   r   r+   r   )r   r   r   results       r   __missing__zFastDictCache.__missing__S   sd    t9tyy  JJ..0M$'%S	

#r   )Ni@B )r#   r$   r%   r&   r   r0   r'   r   r   r   r   7   s    
&
r   r   c                 *    t        |       fd}|S )zI
    Momoization decorator for immutable classes and pure functions.
    )r   c                 2     t                fd       }|S )Nc                  p      fd} t        j                               f}j                  ||      S )Nc                        i S r.   r'   )akwobjs   r   
create_newzEmemoized.<locals>.decorator.<locals>.new_callable.<locals>.create_newi   s    A}}$r   )tupleitemsr   )r5   r6   r8   r   cacher7   s   ``  r   new_callablez1memoized.<locals>.decorator.<locals>.new_callableg   s0    % eBHHJ'(C99S*--r   r   )r7   r<   r;   s   ` r   	decoratorzmemoized.<locals>.decoratorf   s!    	s	. 
	. r   )r   )r   r=   r;   s     @r   r	   r	   `   s     (E r   N)i   )
__future__r   collectionsr   	functoolsr   __all__objectr   dictr   r	   r'   r   r   <module>rD      s4    '  (& (V&D &Rr   