
    j'                        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
 d dlmZ er0d dlZd dlmZmZmZmZmZ d dlmZ ej        d	k    rd d
lmZ nd d
lmZ  e	d          Z e	d          Z e	d          Z G d deeef                   ZdS )    )annotations)OrderedDict)MutableMapping)TYPE_CHECKINGAnyTypeVaroverload)
NO_DEFAULTN)	ItemsViewIterableIteratorKeysView
ValuesView)	NoDefault)      )SelfDKVc                     e Zd Zd3dZd4dZd5d
Zd6dZd7dZd8dZd9dZ	d:dZ
d;dZd<dZed=d>d            Zed?d@d            Zd=dAd!ZedBd%            ZdCd'ZdDd)Zed9d*            Zej        dEd,            ZefdFd.ZdGd0ZdHd2ZdS )ILRUCachemaxsizeintreturnNonec                :    t                      | _        || _        dS )a  
        Initialize an LRU (Least Recently Used) cache with a specified maximum size.

        Parameters
        ----------
        maxsize : int
            The maximum number of items the cache can hold.

        Examples
        --------
        >>> from polars._utils.cache import LRUCache
        >>> cache = LRUCache[str, int](maxsize=3)
        >>> cache["a"] = 1
        >>> cache["b"] = 2
        >>> cache["c"] = 3
        >>> cache["d"] = 4  # evicts the least recently used item ("a"), as maxsize=3
        >>> print(cache["b"])  # accessing "b" marks it as recently used
        2
        >>> print(list(cache.keys()))  # show the current keys in LRU order
        ['c', 'd', 'b']
        >>> cache.get("xyz", "not found")
        'not found'
        N)r   _itemsr   )selfr   s     X/home/longshao/multi-rider-rag/.venv/lib/python3.11/site-packages/polars/_utils/cache.py__init__zLRUCache.__init__   s    0 *5    boolc                *    t          | j                  S )z8Returns True if the cache is not empty, False otherwise.)r#   r   r   s    r    __bool__zLRUCache.__bool__5   s    DK   r"   keyr   c                    || j         v S )z!Check if the key is in the cache.r   )r   r'   s     r    __contains__zLRUCache.__contains__9   s    dk!!r"   r   c                P    || j         vr|d}t          |          | j         |= dS )z6Remove the item with the specified key from the cache. not found in cacheN)r   KeyErrorr   r'   msgs      r    __delitem__zLRUCache.__delitem__=   s9    dk!!///C3--Kr"   r   c                    || j         vr|d}t          |          | j                             |           | j         |         S )z(Raises KeyError if the key is not found.r,   )r   r-   move_to_endr.   s      r    __getitem__zLRUCache.__getitem__D   sN    dk!!///C3-- 	$$${3r"   Iterator[K]c              #  $   K   | j         E d{V  dS )z#Iterate over the keys in the cache.Nr)   r%   s    r    __iter__zLRUCache.__iter__N   s&      ;r"   c                *    t          | j                  S )zNumber of items in the cache.)lenr   r%   s    r    __len__zLRUCache.__len__R   s    4;r"   valuec                    | j         dk    rdS t          |           | j         k    r,|                                  t          |           | j         k    ,|| v r| j                            |           || j        |<   dS )zInsert a value into the cache.r   N)	_max_sizer8   popitemr   r2   )r   r'   r:   s      r    __setitem__zLRUCache.__setitem__V   sz    >QF$ii4>))LLNNN $ii4>))$;;K##C((( Cr"   strc           	        t          | j                                                  }t          |           dk    rSd                    d |dd         D                       dz   d                    d |dd         D                       z   }nd                    d	 |D                       }| j        j         d
| d| j         dt          |            dS )z,Return a string representation of the cache.   z, c              3  *   K   | ]\  }}|d |V  dS z: N .0kvs      r    	<genexpr>z$LRUCache.__repr__.<locals>.<genexpr>f   s0      DDdaQ//A//DDDDDDr"   N   z ..., c              3  *   K   | ]\  }}|d |V  dS rC   rD   rE   s      r    rI   z$LRUCache.__repr__.<locals>.<genexpr>h   s0      GG1qOOaOOGGGGGGr"   c              3  *   K   | ]\  }}|d |V  dS rC   rD   rE   s      r    rI   z$LRUCache.__repr__.<locals>.<genexpr>k   s0      DD$!QooooDDDDDDr"   z({z}, maxsize=z, currsize=))listr   itemsr8   join	__class____name__r<   )r   	all_itemsrP   s      r    __repr__zLRUCache.__repr__a   s    **,,--	t99q==		DDimDDDDD))GG	"##GGGGGH E IIDD)DDDDDE.)hhehhhh\_`d\e\ehhhhr"   c                8    | j                                          dS )z$Clear the cache, removing all items.N)r   clearr%   s    r    rW   zLRUCache.clearn   s    r"   NdefaultV | Nonec                    d S NrD   r   r'   rX   s      r    getzLRUCache.getr   s    =@Sr"   .r   V | Dc                    d S r[   rD   r\   s      r    r]   zLRUCache.getu   s    69cr"   D | V | NoneV | D | Nonec                \    || v r'| j                             |           | j         |         S |S )zJReturn value associated with `key` if present, otherwise return `default`.)r   r2   r\   s      r    r]   zLRUCache.getx   s3    $;;K##C(((;s##r"   keysIterable[K]r   c               0     | |          }|D ]}|||<   |S )zGInitialize cache with keys from an iterable, all set to the same value.rD   )clsr   rc   r:   cacher'   s         r    fromkeyszLRUCache.fromkeys   s0     G 	 	CE#JJr"   ItemsView[K, V]c                4    | j                                         S )z?Return an iterable view of the cache's items (keys and values).)r   rP   r%   s    r    rP   zLRUCache.items   s    {  """r"   KeysView[K]c                4    | j                                         S )z,Return an iterable view of the cache's keys.)r   rc   r%   s    r    rc   zLRUCache.keys   s    {!!!r"   c                    | j         S r[   )r<   r%   s    r    r   zLRUCache.maxsize   s
    ~r"   nc                    |dk     rd| }t          |          t          |           |k    r'|                                  t          |           |k    '|| _        dS )zASet new maximum cache size; cache is trimmed if value is smaller.r   z$`maxsize` cannot be negative; found N)
ValueErrorr8   r=   r<   )r   rn   r/   s      r    r   zLRUCache.maxsize   s^     q55<<<CS//!$ii!mmLLNNN $ii!mmr"   D | NoDefaultc                v    | j                             ||          x}t          u r|d}t          |          |S )z
        Remove specified key from the cache and return the associated value.

        If the key is not found, `default` is returned (if given).
        Otherwise, a KeyError is raised.
        r,   )r   popr
   r-   )r   r'   rX   itemr/   s        r    rs   zLRUCache.pop   sC     KOOC111Dj@@///C3--r"   tuple[K, V]c                8    | j                             d          S )zHRemove the least recently used value; raises KeyError if cache is empty.F)last)r   r=   r%   s    r    r=   zLRUCache.popitem   s    {"""...r"   ValuesView[V]c                4    | j                                         S )z.Return an iterable view of the cache's values.)r   valuesr%   s    r    rz   zLRUCache.values   s    {!!###r"   )r   r   r   r   )r   r#   )r'   r   r   r#   )r'   r   r   r   )r'   r   r   r   )r   r4   )r   r   )r'   r   r:   r   r   r   )r   r?   )r   r   r[   )r'   r   rX   r   r   rY   ).)r'   r   rX   r   r   r^   )r'   r   rX   r`   r   ra   )r   r   rc   rd   r:   r   r   r   )r   ri   )r   rk   )rn   r   r   r   )r'   r   rX   rq   r   r^   )r   ru   )r   rx   )rS   
__module____qualname__r!   r&   r*   r0   r3   r6   r9   r>   rU   rW   r	   r]   classmethodrh   rP   rc   propertyr   setterr
   rs   r=   rz   rD   r"   r    r   r      s          6! ! ! !" " " "                    	! 	! 	! 	!i i i i    @@@@ X@9999 X9        [# # # #" " " "    X ^   ^ 4> 
 
 
 
 
/ / / /$ $ $ $ $ $r"   r   )
__future__r   collectionsr   collections.abcr   typingr   r   r   r	   polars._utils.variousr
   sysr   r   r   r   r   r   version_infor   typing_extensionsr   r   r   r   rD   r"   r    <module>r      sa   " " " " " " # # # # # # * * * * * * 8 8 8 8 8 8 8 8 8 8 8 8 , , , , , , 	+JJJSSSSSSSSSSSSSS//////
7""******GCLLGCLLGCLLW$ W$ W$ W$ W$~ad# W$ W$ W$ W$ W$r"   