
    h'                       S SK Jr  S SKJr  S SKJr  S SKJrJrJ	r	J
r
  S SKJr  \(       a5  S SKrS SKJrJrJrJrJr  S SKJr  \R(                  S	:  a  S S
KJr  OS S
KJr  \	" S5      r\	" S5      r\	" S5      r " S S\\\4   5      rg)    )annotations)OrderedDict)MutableMapping)TYPE_CHECKINGAnyTypeVaroverload)
no_defaultN)	ItemsViewIterableIteratorKeysView
ValuesView)	NoDefault)      )SelfDKVc                  >   \ rS rSrSS jrSS jrSS jrSS jrSS jrSS jr	SS jr
S S	 jrS!S
 jrS"S jr\S#S$S jj5       r\S%S&S jj5       rS#S'S jjr\S(S j5       rS)S jrS*S jr\SS j5       r\R,                  S+S j5       r\4S,S jjrS-S jrS.S jrSrg)/LRUCache   c                .    [        5       U l        Xl        g)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     M/home/james-whalen/.local/lib/python3.13/site-packages/polars/_utils/cache.py__init__LRUCache.__init__   s    0 *5    c                ,    [        U R                  5      $ )z8Returns True if the cache is not empty, False otherwise.)boolr   r   s    r   __bool__LRUCache.__bool__5   s    DKK  r!   c                    XR                   ;   $ )z!Check if the key is in the cache.r   )r   keys     r   __contains__LRUCache.__contains__9   s    kk!!r!   c                ^    XR                   ;  a  U< S3n[        U5      eU R                   U	 g)z6Remove the item with the specified key from the cache. not found in cacheN)r   KeyErrorr   r)   msgs      r   __delitem__LRUCache.__delitem__=   s/    kk!G./C3-KKr!   c                    XR                   ;  a  U< S3n[        U5      eU R                   R                  U5        U R                   U   $ )z(Raises KeyError if the key is not found.r-   )r   r.   move_to_endr/   s      r   __getitem__LRUCache.__getitem__D   sF    kk!G./C3- 	${{3r!   c              #  8   #    U R                    Sh  vN   g N7f)z#Iterate over the keys in the cache.Nr(   r$   s    r   __iter__LRUCache.__iter__N   s     ;;s   c                ,    [        U R                  5      $ )zNumber of items in the cache.)lenr   r$   s    r   __len__LRUCache.__len__R   s    4;;r!   c                
   U R                   S:X  a  g[        U 5      U R                   :  a+  U R                  5         [        U 5      U R                   :  a  M+  X;   a  U R                  R	                  U5        X R                  U'   g)zInsert a value into the cache.r   N)	_max_sizer;   popitemr   r4   )r   r)   values      r   __setitem__LRUCache.__setitem__V   s_    >>Q$i4>>)LLN $i4>>);KK##C( Cr!   c           	     v   [        U R                  R                  5       5      n[        U 5      S:  a;  SR	                  S USS  5       5      S-   SR	                  S USS  5       5      -   nOSR	                  S	 U 5       5      nU R
                  R                   S
U SU R                   S[        U 5       S3$ )z,Return a string representation of the cache.   z, c              3  8   #    U  H  u  pU< S U< 3v   M     g7fz: N .0kvs      r   	<genexpr>$LRUCache.__repr__.<locals>.<genexpr>f   s     DmdaQEA5/m   N   z ..., c              3  8   #    U  H  u  pU< S U< 3v   M     g7frG   rH   rI   s      r   rM   rN   h   s     Gqe2aUOrO   c              3  8   #    U  H  u  pU< S U< 3v   M     g7frG   rH   rI   s      r   rM   rN   k   s     D)$!Ruo)rO   z({z}, maxsize=z, currsize=))listr   itemsr;   join	__class____name__r?   )r   	all_itemsrV   s      r   __repr__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    U R                   R                  5         g)z$Clear the cache, removing all items.N)r   clearr$   s    r   r^   LRUCache.clearn   s    r!   Nc                    g NrH   r   r)   defaults      r   getLRUCache.getr   s    =@r!   c                    g ra   rH   rb   s      r   rd   re   u   s    69r!   c                d    X;   a*  U R                   R                  U5        U R                   U   $ U$ )zJReturn value associated with `key` if present, otherwise return `default`.)r   r4   rb   s      r   rd   re   x   s-    ;KK##C(;;s##r!   c               0    U " U5      nU H  nX4U'   M	     U$ )zGInitialize cache with keys from an iterable, all set to the same value.rH   )clsr   keysrA   cacher)   s         r   fromkeysLRUCache.fromkeys   s#     GC#J r!   c                6    U R                   R                  5       $ )z?Return an iterable view of the cache's items (keys and values).)r   rV   r$   s    r   rV   LRUCache.items   s    {{  ""r!   c                6    U R                   R                  5       $ )z,Return an iterable view of the cache's keys.)r   rj   r$   s    r   rj   LRUCache.keys   s    {{!!r!   c                    U R                   $ ra   )r?   r$   s    r   r   LRUCache.maxsize   s    ~~r!   c                    US:  a  SU 3n[        U5      e[        U 5      U:  a!  U R                  5         [        U 5      U:  a  M!  Xl        g)zASet new maximum cache size; cache is trimmed if value is smaller.r   z$`maxsize` cannot be negative; found N)
ValueErrorr;   r@   r?   )r   nr0   s      r   r   rs      sG     q58<CS/!$i!mLLN $i!mr!   c                p    U R                   R                  X5      =n[        L a  U< S3n[        U5      eU$ )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)   rc   itemr0   s        r   rx   LRUCache.pop   s:     KKOOC11Dj@G./C3-r!   c                4    U R                   R                  SS9$ )zHRemove the least recently used value; raises KeyError if cache is empty.F)last)r   r@   r$   s    r   r@   LRUCache.popitem   s    {{"""..r!   c                6    U R                   R                  5       $ )z.Return an iterable view of the cache's values.)r   valuesr$   s    r   r   LRUCache.values   s    {{!!##r!   )r   r?   r   )r   intreturnNone)r   r#   )r)   r   r   r#   )r)   r   r   r   )r)   r   r   r   )r   zIterator[K])r   r   )r)   r   rA   r   r   r   )r   str)r   r   ra   )r)   r   rc   r   r   zV | None).)r)   r   rc   r   r   V | D)r)   r   rc   zD | V | Noner   zV | D | None)r   r   rj   zIterable[K]rA   r   r   r   )r   zItemsView[K, V])r   zKeysView[K])rv   r   r   r   )r)   r   rc   zD | NoDefaultr   r   )r   ztuple[K, V])r   zValuesView[V])rY   
__module____qualname____firstlineno__r   r%   r*   r1   r5   r8   r<   rB   r[   r^   r	   rd   classmethodrl   rV   rj   propertyr   setterr
   rx   r@   r   __static_attributes__rH   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   rH   r!   r   <module>r      sl    " # * 8 8 ,SS/
7"*CLCLCLW$~ad# W$r!   