
    Cj$                        S r SSKJr  SSKJrJrJrJrJrJ	r	  \" S5      r
\" S5      r\" S5      rSS/r " S	 S\\
\4   5      r " S
 S\\
\4   5      rg)z

Cache classes are dict-like containers used to avoid recalculating expensive operations such as rendering.

You can also use them in your own apps for similar reasons.

    )annotations)TYPE_CHECKINGDictGenericKeysViewTypeVaroverloadCacheKey
CacheValueDefaultValueLRUCache	FIFOCachec                  8  ^  \ rS rSrSr/ SQrSU 4S jjr\SS j5       r\R                  SS j5       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\r\(       a  \SS j5       r\      SS j5       r S     SS jjrS S jrS!S jrS"S jrSrU =r$ )#r      a  
A dictionary-like container with a maximum size.

If an additional item is added when the LRUCache is full, the least
recently used key is discarded to make room for the new item.

The implementation is similar to functools.lru_cache, which uses a (doubly)
linked list to keep track of the most recently used items.

Each entry is stored as [PREV, NEXT, KEY, VALUE] where PREV is a reference
to the previous entry, and NEXT is a reference to the next value.

Note that stdlib's @lru_cache is implemented in C and faster! It's best to use
@lru_cache where you are caching things that are fairly quick and called many times.
Use LRUCache where you want increased flexibility and you are caching slow operations
where the overhead of the cache is a small fraction of the total processing time.
)_maxsize_cache_full_headhitsmissesc                t   > Xl         0 U l        SU l        / U l        SU l        SU l        [        TU ]  5         g)zfInitialize a LRUCache.

Args:
    maxsize: Maximum size of the cache, before old items are discarded.
Fr   N)r   r   r   r   r   r   super__init__)selfmaxsize	__class__s     _/home/rurouni/.local/share/pipx/venvs/strix-agent/lib/python3.13/site-packages/textual/cache.pyr   LRUCache.__init__0   s8      46
#%
	    c                    U R                   $ )z?int: Maximum size of cache, before new values evict old values.r   r   s    r   r   LRUCache.maxsize>   s     }}r   c                    Xl         g Nr!   r   r   s     r   r   r#   C   s    r   c                ,    [        U R                  5      $ r%   boolr   r"   s    r   __bool__LRUCache.__bool__G       DKK  r   c                ,    [        U R                  5      $ r%   lenr   r"   s    r   __len__LRUCache.__len__J       4;;r   c           	     n    S[        U 5       SU R                   SU R                   SU R                   S3	$ )Nz<LRUCache size=z	 maxsize= hits= misses=>)r/   r   r   r   r"   s    r   __repr__LRUCache.__repr__M   s9     T9T]]O6$))T\]a]h]h\iijkkr   c                :    [        U R                  U5      U l        g)z\Grow the maximum size to at least `maxsize` elements.

Args:
    maxsize: New maximum size.
N)maxr   r&   s     r   growLRUCache.growP   s     4<<1r   c                T    U R                   R                  5         SU l        / U l        g)Clear the cache.FN)r   clearr   r   r"   s    r   r?   LRUCache.clearX   s     

r   c                6    U R                   R                  5       $ zGet cache keys.r   keysr"   s    r   rD   LRUCache.keys^        {{!!r   c                   U R                   R                  U5      c  U R                  nU(       d  X3X/U R                  SS& O.US   X1U/U l        U R                  US   S'   U R                  US'   U R                  U R                   U'   U R                  (       d#  [	        U R                   5      U R
                  :  a8  SU l        U R                  nUS   nX4S   S'   US   US'   U R                   US   	 ggg)4Set a value.

Args:
    key: Key.
    value: Value.
Nr      T   )r   getr   r   r/   r   )r   keyvalueheadlasts        r   setLRUCache.setc   s     ;;??3'::D!%S 8

1 #1gt%8
!ZZQ
**Q#zzDKKzzS-=!
zzAw!Q
q'QKKQ( > (r   c                    g r%    r   rL   s     r   rK   LRUCache.get       ;>r   c                    g r%   rS   r   rL   defaults      r   rK   rU           ),r   c                @   U R                   R                  U5      =nc  U =R                  S-  sl        U$ X0R                  LaB  US   US   S'   US   US   S'   U R                  nUS   US'   XCS'   U=U l        =US   S'   US'   U =R                  S-  sl        US   $ )Get a value from the cache, or return a default if the key is not present.

Args:
    key: Key
    default: Default to return if key is not present.

Returns:
    Either the value or a default.
rI   r      )r   rK   r   r   r   )r   rL   rY   linkrN   s        r   rK   rU      s     KKOOC((D1KK1KNzz!aDGAJaDGAJ::D1gDGG044DJ4ad1g		Q	Awr   c                   U R                   R                  U5      nU R                   R                  U5      =nc   U =R                  S-  sl        [        U5      eX R                  LaB  US   US   S'   US   US   S'   U R                  nUS   US'   X2S'   U=U l        =US   S'   US'   U =R
                  S-  sl        US   $ )NrI   r   r]   )r   rK   r   KeyErrorr   r   )r   rL   r^   rN   s       r   __getitem__LRUCache.__getitem__   s    {{s#KKOOC((D1KK1K3-zz!aDGAJaDGAJ::D1gDGG044DJ4ad1g		Q	Awr   c                    XR                   ;   $ r%   r   rT   s     r   __contains__LRUCache.__contains__       kk!!r   c                   XR                   ;  a  gU R                   U   nUS   US   S'   US   US   S'   U R                  S   U:X  a.  U R                  S   U l        U R                  S   U:X  a  / U l        U R                   U	 SU l        g)z;Discard item in cache from key.

Args:
    key: Cache key.
NrI   r   rJ   F)r   r   r   )r   rL   r^   s      r   discardLRUCache.discard   s     kk!{{3 !WQ
!WQ
 ::a=CADJzz!}#
KK
r   )r   r   r   r   r   r   r   r   intreturnNonerm   rl   rm   r)   rm   strrm   rn   rm   zKeysView[CacheKey]rL   r
   rM   r   rm   rn   rL   r
   rm   zCacheValue | NonerL   r
   rY   r   rm   zCacheValue | DefaultValuer%   rL   r
   rY   zDefaultValue | Nonerm   z CacheValue | DefaultValue | NonerL   r
   rm   r   rL   r
   rm   r)   )rL   r
   rm   rn   )__name__
__module____qualname____firstlineno____doc__	__slots__r   propertyr   setterr*   r0   r7   r;   r?   rD   rP   __setitem__r   r	   rK   ra   re   ri   __static_attributes____classcell__)r   s   @r   r   r      s    $I   ^^   ! l2"
): K	> 
>		,	,*6	,&	, 
	,
 =A&9	)8" r   c                      \ rS rSrSr/ SQ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\r\(       a  \SS j5       r\      SS j5       r S     SS jjrSS jrSS jrSrg)r      a  A simple cache that discards the first added key when full (First In First Out).

This has a lower overhead than LRUCache, but won't manage a working set as efficiently.
It is most suitable for a cache with a relatively low maximum size that is not expected to
do many lookups.

r   r   r   r   c                :    Xl         0 U l        SU l        SU l        g)z[Initialize a FIFOCache.

Args:
    maxsize: Maximum size of cache before discarding items.
r   Nr   r&   s     r   r   FIFOCache.__init__   s      24	r   c                ,    [        U R                  5      $ r%   r(   r"   s    r   r*   FIFOCache.__bool__   r,   r   c                ,    [        U R                  5      $ r%   r.   r"   s    r   r0   FIFOCache.__len__   r2   r   c                V    SU R                    SU R                   SU R                   S3$ )Nz<FIFOCache maxsize=r4   r5   r6   )r   r   r   r"   s    r   r7   FIFOCache.__repr__   s,    !$--tyyk$++VWX	
r   c                8    U R                   R                  5         g)r>   N)r   r?   r"   s    r   r?   FIFOCache.clear   s    r   c                6    U R                   R                  5       $ rB   rC   r"   s    r   rD   FIFOCache.keys   rF   r   c                    XR                   ;  aQ  [        U R                   5      U R                  :  a.  U R                    H  nU R                   R                  U5          O   X R                   U'   g)rH   N)r   r/   r   pop)r   rL   rM   	first_keys       r   rP   FIFOCache.set  sQ     kk!c$++&6$--&G![[		* ) !Cr   c                    g r%   rS   rT   s     r   rK   FIFOCache.get  rV   r   c                    g r%   rS   rX   s      r   rK   r     rZ   r   Nc                     U R                   U   nU =R                  S-  sl        U$ ! [         a    U =R                  S-  sl        Us $ f = f)r\   rI   r   r   r`   r   )r   rL   rY   results       r   rK   r     sM    	[[%F
 IINIM  	KK1KN	s   ( !AAc                     U R                   U   nU =R                  S-  sl        U$ ! [         a"    U =R                  S-  sl        [        U5      S ef = f)NrI   r   )r   rL   r   s      r   ra   FIFOCache.__getitem__/  sT    	[[%F
 IINIM  	*KK1K3-T)	*s	   ( ,Ac                    XR                   ;   $ r%   rd   rT   s     r   re   FIFOCache.__contains__9  rg   r   )r   r   r   r   rk   rp   ro   rq   rs   rt   ru   rv   rw   r%   rx   ry   rz   )r{   r|   r}   r~   r   r   r   r*   r0   r7   r?   rD   rP   r   r   r	   rK   ra   re   r   rS   r   r   r   r      s    I	! 

"
! K	> 
>		,	,*6	,&	, 
	,
 =A&9	)*"r   N)r   
__future__r   typingr   r   r   r   r   r	   r
   r   r   __all__r   r   rS   r   r   <module>r      sn    # L L:\"
~&{
#{wx+, {|h"*,- h"r   