o
    i'                     @  s   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rPd 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rJd d
lmZ nd d
lmZ e	dZe	dZe	dZG d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                   @  s   e Zd ZdEddZdFdd	ZdGddZdHddZdIddZdJddZdKddZ	dLddZ
dMddZdNdd ZedOdPd$d%ZedQdRd)d%ZdOdSd,d%ZedTd0d1ZdUd3d4ZdVd6d7ZedKd8d9ZejdWd;d9ZefdXd=d>ZdYd@dAZdZdCdDZd!S )[LRUCachemaxsizeintreturnNonec                 C  s   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    r   J/home/app/Keep/.python/lib/python3.10/site-packages/polars/_utils/cache.py__init__   s   
zLRUCache.__init__boolc                 C  
   t | jS )z8Returns True if the cache is not empty, False otherwise.)r    r   r   r   r   r   __bool__5      
zLRUCache.__bool__keyr   c                 C  s
   || j v S )z!Check if the key is in the cache.r   )r   r%   r   r   r   __contains__9   r$   zLRUCache.__contains__r   c                 C  s(   || 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%   msgr   r   r   __delitem__=   s   

zLRUCache.__delitem__r   c                 C  s2   || j vr|d}t|| j | | j | S )z(Raises KeyError if the key is not found.r(   )r   r)   move_to_endr*   r   r   r   __getitem__D   s
   


zLRUCache.__getitem__Iterator[K]c                 c  s    | j E dH  dS )z#Iterate over the keys in the cache.Nr&   r"   r   r   r   __iter__N   s   zLRUCache.__iter__c                 C  r!   )zNumber of items in the cache.)lenr   r"   r   r   r   __len__R   r$   zLRUCache.__len__valuec                 C  sT   | j dkrdS t| | j kr|   t| | j ks|| v r#| j| || j|< dS )zInsert a value into the cache.r   N)	_max_sizer1   popitemr   r-   )r   r%   r3   r   r   r   __setitem__V   s   
zLRUCache.__setitem__strc                 C  s   t | j }t| dkr,ddd |dd D d ddd |d	d D  }n
dd
d |D }| jj d| d| j dt|  dS )z,Return a string representation of the cache.   z, c                 s  "    | ]\}}|d |V  qdS z: Nr   .0kvr   r   r   	<genexpr>f        z$LRUCache.__repr__.<locals>.<genexpr>N   z ..., c                 s  r9   r:   r   r;   r   r   r   r?   h   r@   c                 s  r9   r:   r   r;   r   r   r   r?   k   r@   z({z}, maxsize=z, currsize=))listr   itemsr1   join	__class____name__r4   )r   Z	all_itemsrE   r   r   r   __repr__a   s   &zLRUCache.__repr__c                 C  s   | j   dS )z$Clear the cache, removing all items.N)r   clearr"   r   r   r   rJ   n      zLRUCache.clearNdefaultV | Nonec                 C     d S Nr   r   r%   rL   r   r   r   getr      zLRUCache.get.r   V | Dc                 C  rN   rO   r   rP   r   r   r   rQ   u   rR   D | V | NoneV | D | Nonec                 C  s"   || v r| j | | j | S |S )zJReturn value associated with `key` if present, otherwise return `default`.)r   r-   rP   r   r   r   rQ   x   s   
keysIterable[K]r   c                C  s   | |}|D ]}|||< q|S )zGInitialize cache with keys from an iterable, all set to the same value.r   )clsr   rV   r3   cacher%   r   r   r   fromkeys   s   
zLRUCache.fromkeysItemsView[K, V]c                 C  
   | j  S )z?Return an iterable view of the cache's items (keys and values).)r   rE   r"   r   r   r   rE      r$   zLRUCache.itemsKeysView[K]c                 C  r\   )z,Return an iterable view of the cache's keys.)r   rV   r"   r   r   r   rV      r$   zLRUCache.keysc                 C  s   | j S rO   )r4   r"   r   r   r   r      s   zLRUCache.maxsizenc                 C  sD   |dk rd| }t |t| |kr|   t| |ks|| _dS )zASet new maximum cache size; cache is trimmed if value is smaller.r   z$`maxsize` cannot be negative; found N)
ValueErrorr1   r5   r4   )r   r^   r+   r   r   r   r      s   

D | NoDefaultc                 C  s,   | j || }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%   rL   itemr+   r   r   r   ra      s   
zLRUCache.poptuple[K, V]c                 C  s   | j jddS )zHRemove the least recently used value; raises KeyError if cache is empty.F)last)r   r5   r"   r   r   r   r5      rK   zLRUCache.popitemValuesView[V]c                 C  r\   )z.Return an iterable view of the cache's values.)r   valuesr"   r   r   r   rf      r$   zLRUCache.values)r   r   r   r   )r   r    )r%   r   r   r    )r%   r   r   r   )r%   r   r   r   )r   r/   )r   r   )r%   r   r3   r   r   r   )r   r7   )r   r   rO   )r%   r   rL   r   r   rM   ).)r%   r   rL   r   r   rS   )r%   r   rL   rT   r   rU   )r   r   rV   rW   r3   r   r   r   )r   r[   )r   r]   )r^   r   r   r   )r%   r   rL   r`   r   rS   )r   rc   )r   re   )rH   
__module____qualname__r   r#   r'   r,   r.   r0   r2   r6   rI   rJ   r   rQ   classmethodrZ   rE   rV   propertyr   setterr	   ra   r5   rf   r   r   r   r   r      s6    












	
r   )
__future__r   collectionsr   collections.abcr   typingr   r   r   r   Zpolars._utils.variousr	   sysr
   r   r   r   r   r   version_infor   Ztyping_extensionsr   r   r   r   r   r   r   r   <module>   s     
