
    .h'                       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r4d 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y)    )annotations)OrderedDict)MutableMapping)TYPE_CHECKINGAnyTypeVaroverload)
no_defaultN)	ItemsViewIterableIteratorKeysView
ValuesView)	NoDefault)      )SelfDKVc                     e Zd ZddZddZddZddZddZddZddZ	ddZ
dd	Zd d
Zed!d"d       Zed#d$d       Zd!d%dZed&d       Zd'dZd(dZedd       Zej*                  d)d       Zefd*dZd+dZd,dZy)-LRUCachec                0    t               | _        || _        y)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   _itemsmaxsize)selfr   s     Q/var/www/html/ai-service/venv/lib/python3.12/site-packages/polars/_utils/cache.py__init__zLRUCache.__init__   s    0 *5    c                ,    t        | j                        S )z8Returns True if the cache is not empty, False otherwise.)boolr   r   s    r   __bool__zLRUCache.__bool__5   s    DKK  r   c                    || j                   v S )z!Check if the key is in the cache.r   )r   keys     r   __contains__zLRUCache.__contains__9   s    dkk!!r   c                Z    || j                   vr|d}t        |      | j                   |= y)z6Remove the item with the specified key from the cache. not found in cacheN)r   KeyErrorr   r&   msgs      r   __delitem__zLRUCache.__delitem__=   s1    dkk!G./C3-KKr   c                    || j                   vr|d}t        |      | j                   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   sH    dkk!G./C3- 	${{3r   c              #  8   K   | j                   E d{    y7 w)z#Iterate over the keys in the cache.Nr%   r"   s    r   __iter__zLRUCache.__iter__N   s     ;;s   c                ,    t        | j                        S )zNumber of items in the cache.)lenr   r"   s    r   __len__zLRUCache.__len__R   s    4;;r   c                   | j                   dk(  ryt        |       | j                   k\  r)| j                          t        |       | j                   k\  r)|| v r| j                  j	                  |       || j                  |<   y)zInsert a value into the cache.r   N)	_max_sizer4   popitemr   r/   )r   r&   values      r   __setitem__zLRUCache.__setitem__V   sc    >>Q$i4>>)LLN $i4>>)$;KK##C( Cr   c           	     t   t        | j                  j                               }t        |       dkD  r;dj	                  d |dd D              dz   dj	                  d |dd D              z   }ndj	                  d	 |D              }| j
                  j                   d
| d| j                   dt        |        dS )z,Return a string representation of the cache.   z, c              3  0   K   | ]  \  }}|d |  ywz: N .0kvs      r   	<genexpr>z$LRUCache.__repr__.<locals>.<genexpr>f   s     DdaQEA5/D   N   z ..., c              3  0   K   | ]  \  }}|d |  ywr>   r?   r@   s      r   rD   z$LRUCache.__repr__.<locals>.<genexpr>h   s     G1qe2aUOGrE   c              3  0   K   | ]  \  }}|d |  ywr>   r?   r@   s      r   rD   z$LRUCache.__repr__.<locals>.<genexpr>k   s     D$!QRuoDrE   z({z}, maxsize=z, currsize=))listr   itemsr4   join	__class____name__r7   )r   	all_itemsrL   s      r   __repr__zLRUCache.__repr__a   s    **,-	t9q=		DimDD))G	"#GGH  IID)DDE..))*#eWL@PP[\_`d\e[ffghhr   c                8    | j                   j                          y)z$Clear the cache, removing all items.N)r   clearr"   s    r   rS   zLRUCache.clearn   s    r   Nc                     y Nr?   r   r&   defaults      r   getzLRUCache.getr   s    =@r   c                     y rU   r?   rV   s      r   rX   zLRUCache.getu   s    69r   c                b    || v r*| j                   j                  |       | j                   |   S |S )zJReturn value associated with `key` if present, otherwise return `default`.)r   r/   rV   s      r   rX   zLRUCache.getx   s/    $;KK##C(;;s##r   c               .     | |      }|D ]  }|||<   	 |S )zGInitialize cache with keys from an iterable, all set to the same value.r?   )clsr   keysr9   cacher&   s         r   fromkeyszLRUCache.fromkeys   s*     G 	CE#J	r   c                6    | j                   j                         S )z?Return an iterable view of the cache's items (keys and values).)r   rL   r"   s    r   rL   zLRUCache.items   s    {{  ""r   c                6    | j                   j                         S )z,Return an iterable view of the cache's keys.)r   r]   r"   s    r   r]   zLRUCache.keys   s    {{!!r   c                    | j                   S rU   )r7   r"   s    r   r   zLRUCache.maxsize   s    ~~r   c                    |dk  rd| }t        |      t        |       |kD  r| j                          t        |       |kD  r|| _        y)zASet new maximum cache size; cache is trimmed if value is smaller.r   z$`maxsize` cannot be negative; found N)
ValueErrorr4   r8   r7   )r   nr,   s      r   r   zLRUCache.maxsize   sI     q58<CS/!$i!mLLN $i!mr   c                n    | j                   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&   rW   itemr,   s        r   rg   zLRUCache.pop   s<     KKOOC11Dj@G./C3-r   c                :    | j                   j                  d      S )zHRemove the least recently used value; raises KeyError if cache is empty.F)last)r   r8   r"   s    r   r8   zLRUCache.popitem   s    {{"""..r   c                6    | j                   j                         S )z.Return an iterable view of the cache's values.)r   valuesr"   s    r   rl   zLRUCache.values   s    {{!!##r   )r   intreturnNone)rn   r!   )r&   r   rn   r!   )r&   r   rn   ro   )r&   r   rn   r   )rn   zIterator[K])rn   rm   )r&   r   r9   r   rn   ro   )rn   str)rn   ro   rU   )r&   r   rW   ro   rn   zV | None).)r&   r   rW   r   rn   V | D)r&   r   rW   zD | V | Nonern   zV | D | None)r   rm   r]   zIterable[K]r9   r   rn   r   )rn   zItemsView[K, V])rn   zKeysView[K])re   rm   rn   ro   )r&   r   rW   zD | NoDefaultrn   rq   )rn   ztuple[K, V])rn   zValuesView[V])rO   
__module____qualname__r   r#   r'   r-   r0   r2   r5   r:   rQ   rS   r	   rX   classmethodr_   rL   r]   propertyr   setterr
   rg   r8   rl   r?   r   r   r   r      s    6!"  	!i @ @9 9  #"   ^^  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   r?   r   r   <module>r      sl    " # * 8 8 ,SS/
7"*CLCLCLW$~ad# W$r   