
    
3ji#                       S SK rS SKrS SKJrJr  SSKJr  SSKJr  \R                  " S5      r	\R                  " S5      r
\R                  " S5      rS	\R                  \R                     S
\4S jr " S S\R                   \
   5      r " S S\\	\
4   5      r " S S\\	\
4   5      r " S S\\	\
4   5      r " S S\\	\
4   5      r " S S\\	\
4   5      r " S S\\	\
4   5      r " S S\\	\
4   5      rg)    N)datetime	timedelta   )_core)BaseCacheImplKTVTDTitemsreturnc                     US::  a  SSR                  S U  5       5      -   S-   $ Sn/ nX!:  a9  [        U 5      u  pEUS::  a  UR                  U< SU< 35        OOUS-  nX!:  a  M9  S	SR                  U5      X-
  4-  $ )
N2   {z, c              3   8   #    U  H  u  pU< S U< 3v   M     g7f): N ).0kvs      L/home/wildlama/miniconda3/lib/python3.13/site-packages/cachebox/_cachebox.py	<genexpr> _items_to_str.<locals>.<genexpr>   s     A541!b5s   }r   r   r   z{%s, ... %d more ...})joinnextappend)r   lengthcleftr   r   s         r   _items_to_strr       s    |TYYA5AAACGG	AD
*E{7KK1%r!( 	Q * #diiovz%BBB    c                   n    \ rS rSrSrS\R                  \/\R                  4   4S jr	S r
S\4S jrSrg	)
IteratorView"   iteratorfuncr'   c                     Xl         X l        g Nr%   )selfr&   r'   s      r   __init__IteratorView.__init__%   s     	r!   c                 D    U R                   R                  5       U l         U $ r)   )r&   __iter__r*   s    r   r.   IteratorView.__iter__)   s    ..0r!   r   c                 T    U R                  U R                  R                  5       5      $ r)   )r'   r&   __next__r/   s    r   r2   IteratorView.__next__-   s    yy//122r!   )r'   r&   N)__name__
__module____qualname____firstlineno__	__slots__typingCallabletupleAnyr+   r.   r	   r2   __static_attributes__r   r!   r   r#   r#   "   s8    $Ivw

7J'K 3" 3r!   r#   c                      \ rS rSrSrSr S4SSS.S\S\R                  \	\R                  \   S4   S	\S
\SS4
S jjjr\S\4S j5       r\S\4S j5       rS\4S jrS\4S jrS\4S jrS rS\S\4S jrS\4S jrS\4S jrS\4S jrS\S\S\R6                  \   4S jrS4S\S\R6                  \   S\R                  \\4   4S jjrS4S\S\R6                  \   S\R                  \\4   4S jjrS4S\S\R6                  \   S\R                  \\4   4S jjr S\RB                  4S jr"S\S\RB                  4S  jr#S\R                  \	\R                  \   4   SS4S! jr$S\S\SS4S" jr%S\S\4S# jr&S\SS4S$ jr'S\4S% jr(S\4S& jr)S5S' jr*S(S).S*\SS4S+ jjr+S\,\RZ                  \\4      4S, jr.S\,\   4S- jr/S\,\   4S. jr0S6S/ jr1S6S0 jr2S6S1 jr3S\,\   4S2 jr4S\54S3 jr6Sr7g)7Cache1   aN  
A thread-safe, memory-efficient hashmap-like cache with configurable maximum size.

Provides a flexible key-value storage mechanism with:
- Configurable maximum size (zero means unlimited)
- Lower memory usage compared to standard dict
- Thread-safe operations
- Useful memory management methods

Differs from standard dict by:
- Being thread-safe
- Unordered storage
- Size limitation
- Memory efficiency
- Additional cache management methods

Supports initialization with optional initial data and capacity,
and provides dictionary-like access with additional cache-specific operations.
_rawNr   capacity	maxmemorymaxsizeiterablerD   rE   r   c                b    [         R                  " XUS9U l        Ub  U R                  U5        gg)a  
Initialize a new Cache instance.

Args:
    maxsize (int): Maximum number of elements the cache can hold. Zero means unlimited.
    iterable (Union[Cache, dict, tuple, Generator, None], optional): Initial data to populate the cache. Defaults to None.
    capacity (int, optional): Pre-allocate hash table capacity to minimize reallocations. Defaults to 0.
    maxmemory (int, optional): Maximum memory (bytes) allowed for cached entries. Zero means unlimited.
                               On PyPy, it works same as `maxsize` if objects do not support `__sizeof__`
                               method.

Creates a new cache with specified size constraints and optional initial data. The cache can be pre-sized
to improve performance when the number of expected elements is known in advance.
rC   N)r   r?   rB   updater*   rF   rG   rD   rE   s        r   r+   Cache.__init__H   s-    , KKiP	KK!  r!   c                 6    U R                   R                  5       $ r)   rB   rF   r/   s    r   rF   Cache.maxsizec       yy  ""r!   c                 6    U R                   R                  5       $ r)   rB   rE   r/   s    r   rE   Cache.maxmemoryg       yy""$$r!   c                 6    U R                   R                  5       $ zEReturns the number of elements the map can hold without reallocating.rB   rD   r/   s    r   rD   Cache.capacityk       yy!!##r!   c                 6    U R                   R                  5       $ zDReturns the total estimated memory usage of cached entries in bytes.rB   memoryr/   s    r   r\   Cache.memoryo       yy!!r!   c                 ,    [        U R                  5      $ r)   lenrB   r/   s    r   __len__Cache.__len__s       499~r!   c                 6    U R                   R                  5       $ r)   rB   
__sizeof__r/   s    r   rg   Cache.__sizeof__v       yy##%%r!   keyc                     XR                   ;   $ r)   rA   r*   rj   s     r   __contains__Cache.__contains__y       iir!   c                 ,    U R                  5       (       + $ r)   is_emptyr/   s    r   __bool__Cache.__bool__|       ==?""r!   c                 6    U R                   R                  5       $ r)   rB   rr   r/   s    r   rr   Cache.is_empty       yy!!##r!   c                 6    U R                   R                  5       $ r)   rB   is_fullr/   s    r   r|   Cache.is_full       yy  ""r!   valuec                 8    U R                   R                  X5      $ )am  
Equals to `self[key] = value`, but returns a value:

- If the cache did not have this key present, None is returned.
- If the cache did have this key present, the value is updated,
  and the old value is returned. The key is not updated, though;

Note: raises `OverflowError` if the cache reached the maxsize limit,
because this class does not have any algorithm.
rB   insertr*   rj   r   s      r   r   Cache.insert   s     yy++r!   defaultc                 r     U R                   R                  U5      $ ! [        R                   a    Us $ f = fa  
Retrieves the value for a given key from the cache.

Returns the value associated with the key if present, otherwise returns the specified default value.
Equivalent to `self[key]`, but provides a fallback default if the key is not found.

Args:
    key: The key to look up in the cache.
    default: The value to return if the key is not present in the cache. Defaults to None.

Returns:
    The value associated with the key, or the default value if the key is not found.
rB   getr   CoreKeyErrorr*   rj   r   s      r   r   	Cache.get   4    	99==%%!! 	N	    66c                 r     U R                   R                  U5      $ ! [        R                   a    Us $ f = fzk
Removes specified key and return the corresponding value. If the key is not found, returns the `default`.
rB   remover   r   r   s      r   pop	Cache.pop   6    	99##C((!! 	N	r   c                 8    U R                   R                  X5      $ )z
Inserts key with a value of default if key is not in the cache. Return the value for key if key is
in the cache, else `default`.
rB   
setdefaultr   s      r   r   Cache.setdefault   s    
 yy##C11r!   c                     [        5       er)   NotImplementedErrorr/   s    r   popitemCache.popitem       !##r!   nc                     [        5       er)   r   r*   r   s     r   drainCache.drain   r   r!   c                 |    [        US5      (       a  UR                  5       nU R                  R                  U5        g)z
Updates the cache with elements from a dictionary or an iterable object of key/value pairs.

Note: raises `OverflowError` if the cache reached the maxsize limit.
r   Nhasattrr   rB   rI   r*   rG   s     r   rI   Cache.update   s/     8W%%~~'H		"r!   c                 &    U R                  X5        g r)   r   r   s      r   __setitem__Cache.__setitem__       Cr!   c                      U R                   R                  U5      $ ! [        R                   a    [	        U5      S ef = fr)   rB   r   r   r   KeyErrorrl   s     r   __getitem__Cache.__getitem__   ;    	*99==%%!! 	*3-T)	*    !>c                      U R                   R                  U5        g ! [        R                   a    [	        U5      S ef = fr)   rB   r   r   r   r   rl   s     r   __delitem__Cache.__delitem__   :    	*IIS!!! 	*3-T)	*    !?c                 `    [        U[        5      (       d  gU R                  UR                  :H  $ NF
isinstancer?   rB   r*   others     r   __eq__Cache.__eq__   %    %''yyEJJ&&r!   c                 `    [        U[        5      (       d  gU R                  UR                  :g  $ r   r   r   s     r   __ne__Cache.__ne__   r   r!   c                 8    U R                   R                  5         gz,Shrinks the cache to fit len(self) elements.NrB   shrink_to_fitr/   s    r   r   Cache.shrink_to_fit       		!r!   Freuser   c                :    U R                   R                  U5        gzf
Removes all items from cache.

If reuse is True, will not free the memory for reusing in the future.
NrB   clearr*   r   s     r   r   Cache.clear        			r!   c                 L    [        U R                  R                  5       S 5      $ )
Returns an iterable object of the cache's items (key-value pairs).

Notes:
- You should not make any changes in cache while using this iterable object.
- Items are not ordered.
c                     U $ r)   r   xs    r   <lambda>Cache.items.<locals>.<lambda>       r!   r#   rB   r   r/   s    r   r   Cache.items        DIIOO-{;;r!   c                 L    [        U R                  R                  5       S 5      $ )
Returns an iterable object of the cache's keys.

Notes:
- You should not make any changes in cache while using this iterable object.
- Keys are not ordered.
c                     U S   $ Nr   r   r   s    r   r   Cache.keys.<locals>.<lambda>       1r!   r   r/   s    r   keys
Cache.keys        DIIOO-~>>r!   c                 L    [        U R                  R                  5       S 5      $ )
Returns an iterable object of the cache's values.

Notes:
- You should not make any changes in cache while using this iterable object.
- Values are not ordered.
c                     U S   $ Nr   r   r   s    r   r   Cache.values.<locals>.<lambda>	  r   r!   r   r/   s    r   valuesCache.values  r   r!   c                 "    U R                  5       $ z#Returns a shallow copy of the cache__copy__r/   s    r   copy
Cache.copy      }}r!   c                     [        U 5      nUR                  U5      n[        R                  " U R                  5      Ul        U$ r)   type__new__	_std_copyr   rB   r*   clscopieds      r   r   Cache.__copy__  2    4jS!nnTYY/r!   c                     [        U 5      nUR                  U5      n[        R                  " U R                  U5      Ul        U$ r)   r   r   r   deepcopyrB   r*   memor   r   s       r   __deepcopy__Cache.__deepcopy__  6    4jS!((D9r!   c                 "    U R                  5       $ r)   r   r/   s    r   r.   Cache.__iter__      yy{r!   c                    [        U 5      nSUR                  UR                  [        U R                  5      U R                  R                  5       [        U R                  R                  5       [        U R                  5      5      4-  $ Nz%s.%s[%d/%d](%s)r   r5   r4   ra   rB   rF   r    r   r*   r   s     r   __repr__Cache.__repr__  c    4j!NNLL		NII$))//+S^<%
 
 	
r!   r)   r   N)r   zCache[KT, VT])8r4   r5   r6   r7   __doc__r8   intr9   UniondictIterabler;   r+   propertyrF   rE   rD   r\   rb   rg   r   boolrm   rs   rr   r|   r	   Optionalr   r
   r   r   r   NoReturnr   r   rI   r   r   r   r   r   r   r   r#   Tupler   r   r   r   r   r  r.   strr  r=   r   r!   r   r?   r?   1   s   ( I
 FJ"
 "" ,,tV__U%;TAB"
 " " 
"6 # # # %3 % %$# $" " &   t  #$ #$$ $# #," ,R ,FOOB,? ,r FOOB$7 6<<PRTVPVCW &r FOOB$7 6<<PRTVPVCW 2b 26??2+> 2&,,WY[]W]J^ 2$ $$s $v $	#v||D&//%2H,HI 	#d 	# r  "    *r *b **r *d *'t ''t '" &+ d t <|FLLR$89 <?l2& ??R( ?,r* 	
# 	
r!   r?   c                   V   \ rS rSrSrSr S6SSS.S\S\R                  \R                  \	\R                  \   4   S4   S	\S
\SS4
S jjjr\S\4S j5       r\S\4S j5       rS\4S jrS\4S jrS\4S jrS rS\S\4S jrS\4S jrS\4S jrS\4S jrS\S\S\R6                  \   4S jrS6S\S\R6                  \   S\R                  \\4   4S jjrS6S\S\R6                  \   S\R                  \\4   4S jjrS6S\S\R6                  \   S\R                  \\4   4S jjr S\RB                  \\4   4S jr"S\S\4S  jr#S\R                  \	\R                  \   4   SS4S! jr$S\S\SS4S" jr%S\S\4S# jr&S\SS4S$ jr'S\4S% jr(S\4S& jr)S7S' jr*S(S).S*\SS4S+ jjr+S\,\RB                  \\4      4S, jr-S\,\   4S- jr.S\,\   4S. jr/S8S\S\R6                  \   4S/ jjr0S\R6                  \   4S0 jr1S9S1 jr2S9S2 jr3S9S3 jr4S\,\   4S4 jr5S\64S5 jr7Sr8g):	FIFOCachei*  a  
A First-In-First-Out (FIFO) cache implementation with configurable maximum size and optional initial capacity.

This cache provides a fixed-size container that automatically removes the oldest items when the maximum size is reached.
Supports various operations like insertion, retrieval, deletion, and iteration.

Attributes:
    maxsize: The maximum number of items the cache can hold.
    capacity: The initial capacity of the cache before resizing.

Key features:
- Deterministic item eviction order (oldest items removed first)
- Efficient key-value storage and retrieval
- Supports dictionary-like operations
- Allows optional initial data population
rA   Nr   rC   rF   rG   rD   rE   r   c                b    [         R                  " XUS9U l        Ub  U R                  U5        gg)a&  
Initialize a new FIFOCache instance.

Args:
    maxsize: The maximum number of items the cache can hold.
    iterable: Optional initial data to populate the cache. Can be another FIFOCache,
              a dictionary, tuple, generator, or None.
    capacity: Optional initial capacity of the cache before resizing. Defaults to 0.
    maxmemory: Maximum memory (bytes) allowed for cached entries. Zero means unlimited.
               When maxmemory is set, updating an existing key can evict the updated key
               if it is the oldest entry.
rC   N)r   r#  rB   rI   rJ   s        r   r+   FIFOCache.__init__>  s-    ( OOG)T	KK!  r!   c                 6    U R                   R                  5       $ r)   rM   r/   s    r   rF   FIFOCache.maxsizeW  rO   r!   c                 6    U R                   R                  5       $ r)   rQ   r/   s    r   rE   FIFOCache.maxmemory[  rS   r!   c                 6    U R                   R                  5       $ rU   rV   r/   s    r   rD   FIFOCache.capacity_  rX   r!   c                 6    U R                   R                  5       $ rZ   r[   r/   s    r   r\   FIFOCache.memoryc  r^   r!   c                 ,    [        U R                  5      $ r)   r`   r/   s    r   rb   FIFOCache.__len__g  rd   r!   c                 6    U R                   R                  5       $ r)   rf   r/   s    r   rg   FIFOCache.__sizeof__j  ri   r!   rj   c                     XR                   ;   $ r)   rA   rl   s     r   rm   FIFOCache.__contains__m  ro   r!   c                 ,    U R                  5       (       + $ r)   rq   r/   s    r   rs   FIFOCache.__bool__p  ru   r!   c                 6    U R                   R                  5       $ r)   rw   r/   s    r   rr   FIFOCache.is_emptys  ry   r!   c                 6    U R                   R                  5       $ r)   r{   r/   s    r   r|   FIFOCache.is_fullv  r~   r!   r   c                 8    U R                   R                  X5      $ a
  
Inserts a key-value pair into the cache, returning the previous value if the key existed.

Equivalent to `self[key] = value`, but with additional return value semantics:

- If the key was not previously in the cache, returns None.
- If the key was already present, updates the value and returns the old value.
  The key itself is not modified.

Args:
    key: The key to insert.
    value: The value to associate with the key.

Returns:
    The previous value associated with the key, or None if the key was not present.
r   r   s      r   r   FIFOCache.inserty      " yy++r!   r   c                 r     U R                   R                  U5      $ ! [        R                   a    Us $ f = fr   r   r   s      r   r   FIFOCache.get  r   r   c                 r     U R                   R                  U5      $ ! [        R                   a    Us $ f = fr   r   r   s      r   r   FIFOCache.pop  r   r   c                 8    U R                   R                  X5      $ z
Inserts key with a value of default if key is not in the cache.

Return the value for key if key is in the cache, else default.
r   r   s      r   r   FIFOCache.setdefault       yy##C11r!   c                 ~     U R                   R                  5       $ ! [        R                   a    [	        5       Sef = fz;Removes the element that has been in the cache the longest.NrB   r   r   r   r   r/   s    r   r   FIFOCache.popitem  9    	'99$$&&!! 	'*$&	'     <r   c                     US::  a  g[        U5       H  n U R                  R                  5         M      W$ ! [        R                   a    Us s  $ f = fzBDoes the `popitem()` `n` times and returns count of removed items.r   rangerB   r   r   r   r*   r   is      r   r   FIFOCache.drain  R    6qA		!!#   %%    7AAc                 |    [        US5      (       a  UR                  5       nU R                  R                  U5        g[Updates the cache with elements from a dictionary or an iterable object of key/value pairs.r   Nr   r   s     r   rI   FIFOCache.update  -    8W%%~~'H		"r!   c                 &    U R                  X5        g r)   r   r   s      r   r   FIFOCache.__setitem__  r   r!   c                      U R                   R                  U5      $ ! [        R                   a    [	        U5      S ef = fr)   r   rl   s     r   r   FIFOCache.__getitem__  r   r   c                      U R                   R                  U5        g ! [        R                   a    [	        U5      S ef = fr)   r   rl   s     r   r   FIFOCache.__delitem__  r   r   c                 `    [        U[        5      (       d  gU R                  UR                  :H  $ r   r   r#  rB   r   s     r   r   FIFOCache.__eq__  %    %++yyEJJ&&r!   c                 `    [        U[        5      (       d  gU R                  UR                  :g  $ r   ra  r   s     r   r   FIFOCache.__ne__  rc  r!   c                 8    U R                   R                  5         gr   r   r/   s    r   r   FIFOCache.shrink_to_fit  r   r!   Fr   r   c                :    U R                   R                  U5        gr   r   r   s     r   r   FIFOCache.clear  r   r!   c                 L    [        U R                  R                  5       S 5      $ )
Returns an iterable object of the cache's items (key-value pairs).

Notes:
- You should not make any changes in cache while using this iterable object.
c                     U $ r)   r   r   s    r   r   !FIFOCache.items.<locals>.<lambda>  r   r!   r   r/   s    r   r   FIFOCache.items       DIIOO-{;;r!   c                 L    [        U R                  R                  5       S 5      $ )
Returns an iterable object of the cache's keys.

Notes:
- You should not make any changes in cache while using this iterable object.
c                     U S   $ r   r   r   s    r   r    FIFOCache.keys.<locals>.<lambda>  r   r!   r   r/   s    r   r   FIFOCache.keys       DIIOO-~>>r!   c                 L    [        U R                  R                  5       S 5      $ )
Returns an iterable object of the cache's values.

Notes:
- You should not make any changes in cache while using this iterable object.
c                     U S   $ r   r   r   s    r   r   "FIFOCache.values.<locals>.<lambda>  r   r!   r   r/   s    r   r   FIFOCache.values  ru  r!   c                     US:  a  [        U R                  5      U-   nUS:  a  gU R                  R                  U5      $ z
Returns the first key in cache; this is the one which will be removed by `popitem()` (if n == 0).

By using `n` parameter, you can browse order index by index.
r   Nra   rB   	get_indexr   s     r   firstFIFOCache.first  <     q5DII"Aq5yy""1%%r!   c                 d    U R                   R                  [        U R                   5      S-
  5      $ z<
Returns the last key in cache. Equals to `self.first(-1)`.
r   rB   r~  ra   r/   s    r   lastFIFOCache.last  &     yy""3tyy>A#566r!   c                 "    U R                  5       $ r   r   r/   s    r   r   FIFOCache.copy!  r   r!   c                     [        U 5      nUR                  U5      n[        R                  " U R                  5      Ul        U$ r)   r   r   s      r   r   FIFOCache.__copy__%  r  r!   c                     [        U 5      nUR                  U5      n[        R                  " U R                  U5      Ul        U$ r)   r  r  s       r   r  FIFOCache.__deepcopy__+  r
  r!   c                 "    U R                  5       $ r)   r  r/   s    r   r.   FIFOCache.__iter__1  r  r!   c                    [        U 5      nSUR                  UR                  [        U R                  5      U R                  R                  5       [        U R                  R                  5       [        U R                  5      5      4-  $ r  r  r  s     r   r  FIFOCache.__repr__4  r  r!   r)   r  r   )r   zFIFOCache[KT, VT])9r4   r5   r6   r7   r  r8   r  r9   r  r  r  r;   r+   r  rF   rE   rD   r\   rb   rg   r   r  rm   rs   rr   r|   r	   r  r   r
   r   r   r   r   r   r   rI   r   r   r   r   r   r   r   r#   r   r   r   r  r  r   r   r  r.   r!  r  r=   r   r!   r   r#  r#  *  s6   " I
 TX"
 "" ,,v||D&//%2H,HI4OP"
 " " 
"2 # # # %3 % %$# $" " &   t  #$ #$$ $# #," ,R ,FOOB,? ,&r FOOB$7 6<<PRTVPVCW &r FOOB$7 6<<PRTVPVCW 2b 26??2+> 2&,,WY[]W]J^ 2'b"f- 's s #v||D&//%2H,HI #d # r  "    *r *b **r *d *'t ''t '" &+ d t <|FLLR$89 <?l2& ??R( ?&s &6??2#6 &7foob) 7,r* 	
# 	
r!   r#  c                      \ rS rSrSrSr S5SSS.S\S\R                  \R                  \	\R                  \   4   S4   S	\S
\SS4
S jjjr\S\4S j5       r\S\4S j5       rS\4S jrS\4S jrS\4S jrS rS\S\4S jrS\4S jrS\4S jrS\4S jrS\S\S\R6                  \   4S jrS5S\S\R6                  \   S\R                  \\4   4S jjrS5S\S\R6                  \   S\R                  \\4   4S jjrS5S\S\R6                  \   S\R                  \\4   4S jjr S\RB                  \\4   4S jr"S\S\4S  jr#S\R                  \	\R                  \   4   SS4S! jr$S\4S" jr%S\S\SS4S# jr&S\S\4S$ jr'S\SS4S% jr(S\4S& jr)S\4S' jr*S6S( jr+S)S*.S+\SS4S, jjr,S\-\RB                  \\4      4S- jr.S\-\   4S. jr/S\-\   4S/ jr0S7S0 jr1S7S1 jr2S7S2 jr3S\-\   4S3 jr4S\54S4 jr6Sr7g)8RRCachei@  a;  
A thread-safe cache implementation with Random Replacement (RR) policy.

This cache randomly selects and removes elements when the cache reaches its maximum size,
ensuring a simple and efficient caching mechanism with configurable capacity.

Supports operations like insertion, retrieval, deletion, and iteration.
rA   Nr   rC   rF   rG   rD   rE   r   c                b    [         R                  " XUS9U l        Ub  U R                  U5        gg)au  
Initialize a new RRCache instance.

Args:
    maxsize (int): Maximum size of the cache. A value of zero means unlimited capacity.
    iterable (dict or Iterable[tuple], optional): Initial data to populate the cache. Defaults to None.
    capacity (int, optional): Preallocated capacity for the cache to minimize reallocations. Defaults to 0.
    maxmemory (int, optional): Maximum memory (bytes) allowed for cached entries. Zero means unlimited.
                               When maxmemory is set, updates can evict any key, including the updated key.
                               On PyPy. In PyPy, the size of each object is assumed to be 1 if the object
                               does not have a `__sizeof__` method.

Note:
    - The cache size limit is immutable after initialization.
    - If an iterable is provided, the cache will be populated using the update method.
rC   N)r   r  rB   rI   rJ   s        r   r+   RRCache.__init__L  s-    0 MM'	R	KK!  r!   c                 6    U R                   R                  5       $ r)   rM   r/   s    r   rF   RRCache.maxsizei  rO   r!   c                 6    U R                   R                  5       $ r)   rQ   r/   s    r   rE   RRCache.maxmemorym  rS   r!   c                 6    U R                   R                  5       $ rU   rV   r/   s    r   rD   RRCache.capacityq  rX   r!   c                 6    U R                   R                  5       $ rZ   r[   r/   s    r   r\   RRCache.memoryu  r^   r!   c                 ,    [        U R                  5      $ r)   r`   r/   s    r   rb   RRCache.__len__y  rd   r!   c                 6    U R                   R                  5       $ r)   rf   r/   s    r   rg   RRCache.__sizeof__|  ri   r!   rj   c                     XR                   ;   $ r)   rA   rl   s     r   rm   RRCache.__contains__  ro   r!   c                 ,    U R                  5       (       + $ r)   rq   r/   s    r   rs   RRCache.__bool__  ru   r!   c                 6    U R                   R                  5       $ r)   rw   r/   s    r   rr   RRCache.is_empty  ry   r!   c                 6    U R                   R                  5       $ r)   r{   r/   s    r   r|   RRCache.is_full  r~   r!   r   c                 8    U R                   R                  X5      $ r;  r   r   s      r   r   RRCache.insert  r=  r!   r   c                 r     U R                   R                  U5      $ ! [        R                   a    Us $ f = fr   r   r   s      r   r   RRCache.get  r   r   c                 r     U R                   R                  U5      $ ! [        R                   a    Us $ f = fr   r   r   s      r   r   RRCache.pop  r   r   c                 8    U R                   R                  X5      $ rC  r   r   s      r   r   RRCache.setdefault  rE  r!   c                 ~     U R                   R                  5       $ ! [        R                   a    [	        5       Sef = f)z@Randomly selects and removes a (key, value) pair from the cache.NrH  r/   s    r   r   RRCache.popitem  rJ  rK  r   c                     US::  a  g[        U5       H  n U R                  R                  5         M      W$ ! [        R                   a    Us s  $ f = frM  rN  rP  s      r   r   RRCache.drain  rS  rT  c                 |    [        US5      (       a  UR                  5       nU R                  R                  U5        grV  r   r   s     r   rI   RRCache.update  rY  r!   c                 ~     U R                   R                  5       $ ! [        R                   a    [	        5       Sef = f)z]
Randomly selects and returns a key from the cache.
Raises `KeyError` If the cache is empty.
N)rB   
random_keyr   r   r   r/   s    r   r  RRCache.random_key  s9    
	'99''))!! 	'*$&	'rK  c                 &    U R                  X5        g r)   r   r   s      r   r   RRCache.__setitem__  r   r!   c                      U R                   R                  U5      $ ! [        R                   a    [	        U5      S ef = fr)   r   rl   s     r   r   RRCache.__getitem__  r   r   c                      U R                   R                  U5        g ! [        R                   a    [	        U5      S ef = fr)   r   rl   s     r   r   RRCache.__delitem__  r   r   c                 `    [        U[        5      (       d  gU R                  UR                  :H  $ r   r   r  rB   r   s     r   r   RRCache.__eq__  %    %))yyEJJ&&r!   c                 `    [        U[        5      (       d  gU R                  UR                  :g  $ r   r  r   s     r   r   RRCache.__ne__  r  r!   c                 8    U R                   R                  5         gr   r   r/   s    r   r   RRCache.shrink_to_fit  r   r!   Fr   r   c                :    U R                   R                  U5        gr   r   r   s     r   r   RRCache.clear  r   r!   c                 L    [        U R                  R                  5       S 5      $ )r   c                     U $ r)   r   r   s    r   r   RRCache.items.<locals>.<lambda>  r   r!   r   r/   s    r   r   RRCache.items  r   r!   c                 L    [        U R                  R                  5       S 5      $ )r   c                     U S   $ r   r   r   s    r   r   RRCache.keys.<locals>.<lambda>   r   r!   r   r/   s    r   r   RRCache.keys  r   r!   c                 L    [        U R                  R                  5       S 5      $ )r   c                     U S   $ r   r   r   s    r   r    RRCache.values.<locals>.<lambda>*  r   r!   r   r/   s    r   r   RRCache.values"  r   r!   c                 "    U R                  5       $ r   r   r/   s    r   r   RRCache.copy,  r   r!   c                     [        U 5      nUR                  U5      n[        R                  " U R                  5      Ul        U$ r)   r   r   s      r   r   RRCache.__copy__0  r  r!   c                     [        U 5      nUR                  U5      n[        R                  " U R                  U5      Ul        U$ r)   r  r  s       r   r  RRCache.__deepcopy__6  r
  r!   c                 "    U R                  5       $ r)   r  r/   s    r   r.   RRCache.__iter__<  r  r!   c                    [        U 5      nSUR                  UR                  [        U R                  5      U R                  R                  5       [        U R                  R                  5       [        U R                  5      5      4-  $ r  r  r  s     r   r  RRCache.__repr__?  r  r!   r)   r  )r   zRRCache[KT, VT])8r4   r5   r6   r7   r  r8   r  r9   r  r  r  r;   r+   r  rF   rE   rD   r\   rb   rg   r   r  rm   rs   rr   r|   r	   r  r   r
   r   r   r   r   r   r   rI   r  r   r   r   r   r   r   r   r#   r   r   r   r   r   r  r.   r!  r  r=   r   r!   r   r  r  @  s    I
 TX"
 "" ,,v||D&//%2H,HI4OP"
 " " 
": # # # %3 % %$# $" " &   t  #$ #$$ $# #," ,R ,FOOB,? ,&r FOOB$7 6<<PRTVPVCW &r FOOB$7 6<<PRTVPVCW 2b 26??2+> 2&,,WY[]W]J^ 2'b"f- 's s #v||D&//%2H,HI #d #'B ' r  "    *r *b **r *d *'t ''t '" &+ d t <|FLLR$89 <?l2& ??R( ?,r* 	
# 	
r!   r  c                      \ rS rSrSrSr S7SSS.S\S\R                  \R                  \	\R                  \   4   S4   S	\S
\SS4
S jjjr\S\4S j5       r\S\4S j5       rS\4S jrS\4S jrS\4S jrS rS\S\4S jrS\4S jrS\4S jrS\4S jrS\S\S\R6                  \   4S jrS7S\S\R6                  \   S\R                  \\4   4S jjrS7S\S\R6                  \   S\R                  \\4   4S jjrS7S\S\R6                  \   S\R                  \\4   4S jjr S7S\S\R6                  \   S\R                  \\4   4S jjr!S\RD                  \\4   4S jr#S \S\4S! jr$S\R                  \	\R                  \   4   SS4S" jr%S\S\SS4S# jr&S\S\4S$ jr'S\SS4S% jr(S\4S& jr)S\4S' jr*S8S( jr+S)S*.S+\SS4S, jjr,S\-\RD                  \\4      4S- jr.S\-\   4S. jr/S\-\   4S/ jr0S\R6                  \   4S0 jr1S\R6                  \   4S1 jr2S9S2 jr3S9S3 jr4S9S4 jr5S\-\   4S5 jr6S\74S6 jr8Sr9g):LRUCacheiK  a  
Thread-safe Least Recently Used (LRU) cache implementation.

Provides a cache that automatically removes the least recently used items when
the cache reaches its maximum size. Supports various operations like insertion,
retrieval, and management of cached items with configurable maximum size and
initial capacity.

Key features:
- Configurable maximum cache size
- Optional initial capacity allocation
- Thread-safe operations
- Efficient key-value pair management
- Supports initialization from dictionaries or iterables
rA   Nr   rC   rF   rG   rD   rE   r   c                b    [         R                  " XUS9U l        Ub  U R                  U5        gg)a  
Initialize a new LRU Cache instance.

Args:
    maxsize (int): Maximum size of the cache. Zero indicates unlimited size.
    iterable (dict | Iterable[tuple], optional): Initial data to populate the cache.
    capacity (int, optional): Pre-allocated capacity for the cache to minimize reallocations.
    maxmemory (int, optional): Maximum memory (bytes) allowed for cached entries. Zero means unlimited.
                               On PyPy. In PyPy, the size of each object is assumed to be 1 if the object
                               does not have a `__sizeof__` method.

Notes:
    - The cache size is immutable after initialization.
    - If an iterable is provided, it will be used to populate the cache.
rC   N)r   r  rB   rI   rJ   s        r   r+   LRUCache.__init__^  s-    . NN7S	KK!  r!   c                 6    U R                   R                  5       $ r)   rM   r/   s    r   rF   LRUCache.maxsizez  rO   r!   c                 6    U R                   R                  5       $ r)   rQ   r/   s    r   rE   LRUCache.maxmemory~  rS   r!   c                 6    U R                   R                  5       $ rU   rV   r/   s    r   rD   LRUCache.capacity  rX   r!   c                 6    U R                   R                  5       $ rZ   r[   r/   s    r   r\   LRUCache.memory  r^   r!   c                 ,    [        U R                  5      $ r)   r`   r/   s    r   rb   LRUCache.__len__  rd   r!   c                 6    U R                   R                  5       $ r)   rf   r/   s    r   rg   LRUCache.__sizeof__  ri   r!   rj   c                     XR                   ;   $ r)   rA   rl   s     r   rm   LRUCache.__contains__  ro   r!   c                 ,    U R                  5       (       + $ r)   rq   r/   s    r   rs   LRUCache.__bool__  ru   r!   c                 6    U R                   R                  5       $ r)   rw   r/   s    r   rr   LRUCache.is_empty  ry   r!   c                 6    U R                   R                  5       $ r)   r{   r/   s    r   r|   LRUCache.is_full  r~   r!   r   c                 8    U R                   R                  X5      $ r;  r   r   s      r   r   LRUCache.insert  r=  r!   r   c                 r     U R                   R                  U5      $ ! [        R                   a    Us $ f = fza
Searches for a key-value in the cache and returns it (without moving the key to recently used).
rB   peekr   r   r   s      r   r  LRUCache.peek  s4    	99>>#&&!! 	N	r   c                 r     U R                   R                  U5      $ ! [        R                   a    Us $ f = fr   r   r   s      r   r   LRUCache.get  r   r   c                 r     U R                   R                  U5      $ ! [        R                   a    Us $ f = fr   r   r   s      r   r   LRUCache.pop  r   r   c                 8    U R                   R                  X5      $ rC  r   r   s      r   r   LRUCache.setdefault  rE  r!   c                 ~     U R                   R                  5       $ ! [        R                   a    [	        5       Sef = f)z
Removes the least recently used item from the cache and returns it as a (key, value) tuple.
Raises KeyError if the cache is empty.
NrH  r/   s    r   r   LRUCache.popitem  s9    
	'99$$&&!! 	'*$&	'rK  r   c                     US::  a  g[        U5       H  n U R                  R                  5         M      W$ ! [        R                   a    Us s  $ f = frM  rN  rP  s      r   r   LRUCache.drain  rS  rT  c                 |    [        US5      (       a  UR                  5       nU R                  R                  U5        grV  r   r   s     r   rI   LRUCache.update  rY  r!   c                 &    U R                  X5        g r)   r   r   s      r   r   LRUCache.__setitem__  r   r!   c                      U R                   R                  U5      $ ! [        R                   a    [	        U5      S ef = fr)   r   rl   s     r   r   LRUCache.__getitem__  r   r   c                      U R                   R                  U5        g ! [        R                   a    [	        U5      S ef = fr)   r   rl   s     r   r   LRUCache.__delitem__  r   r   c                 `    [        U[        5      (       d  gU R                  UR                  :H  $ r   r   r  rB   r   s     r   r   LRUCache.__eq__	  %    %**yyEJJ&&r!   c                 `    [        U[        5      (       d  gU R                  UR                  :g  $ r   r  r   s     r   r   LRUCache.__ne__  r  r!   c                 8    U R                   R                  5         gr   r   r/   s    r   r   LRUCache.shrink_to_fit  r   r!   Fr   r   c                :    U R                   R                  U5        gr   r   r   s     r   r   LRUCache.clear  r   r!   c                 L    [        U R                  R                  5       S 5      $ )rk  c                     U $ r)   r   r   s    r   r    LRUCache.items.<locals>.<lambda>(  r   r!   r   r/   s    r   r   LRUCache.items!  ro  r!   c                 L    [        U R                  R                  5       S 5      $ )rq  c                     U S   $ r   r   r   s    r   r   LRUCache.keys.<locals>.<lambda>1  r   r!   r   r/   s    r   r   LRUCache.keys*  ru  r!   c                 L    [        U R                  R                  5       S 5      $ )rw  c                     U S   $ r   r   r   s    r   r   !LRUCache.values.<locals>.<lambda>:  r   r!   r   r/   s    r   r   LRUCache.values3  ru  r!   c                 6    U R                   R                  5       $ )zN
Returns the key in the cache that has not been accessed in the longest time.
)rB   least_recently_usedr/   s    r   r*  LRUCache.least_recently_used<  s     yy,,..r!   c                 6    U R                   R                  5       $ )zK
Returns the key in the cache that has been accessed in the shortest time.
)rB   most_recently_usedr/   s    r   r-  LRUCache.most_recently_usedB  s     yy++--r!   c                 "    U R                  5       $ r   r   r/   s    r   r   LRUCache.copyH  r   r!   c                     [        U 5      nUR                  U5      n[        R                  " U R                  5      Ul        U$ r)   r   r   s      r   r   LRUCache.__copy__L  r  r!   c                     [        U 5      nUR                  U5      n[        R                  " U R                  U5      Ul        U$ r)   r  r  s       r   r  LRUCache.__deepcopy__R  r
  r!   c                 "    U R                  5       $ r)   r  r/   s    r   r.   LRUCache.__iter__X  r  r!   c                    [        U 5      nSUR                  UR                  [        U R                  5      U R                  R                  5       [        U R                  R                  5       [        U R                  5      5      4-  $ r  r  r  s     r   r  LRUCache.__repr__[  r  r!   r)   r  )r   zLRUCache[KT, VT]):r4   r5   r6   r7   r  r8   r  r9   r  r  r  r;   r+   r  rF   rE   rD   r\   rb   rg   r   r  rm   rs   rr   r|   r	   r  r   r
   r  r   r   r   r   r   r   rI   r   r   r   r   r   r   r   r#   r   r   r   r*  r-  r   r   r  r.   r!  r  r=   r   r!   r   r  r  K  sb     I
 TX"
 "" ,,v||D&//%2H,HI4OP"
 " " 
"8 # # # %3 % %$# $" " &   t  #$ #$$ $# #," ,R ,FOOB,? ,& V__R%8 FLLQSUWQWDX r FOOB$7 6<<PRTVPVCW &r FOOB$7 6<<PRTVPVCW 2b 26??2+> 2&,,WY[]W]J^ 2'b"f- 's s #v||D&//%2H,HI #d # r  "    *r *b **r *d *'t ''t '" &+ d t <|FLLR$89 <?l2& ??R( ?/V__R%8 /.FOOB$7 .,r* 	
# 	
r!   r  c                      \ rS rSrSrSr S7SSS.S\S\R                  \R                  \	\R                  \   4   S4   S	\S
\SS4
S jjjr\S\4S j5       r\S\4S j5       rS\4S jrS\4S jrS\4S jrS rS\S\4S jrS\4S jrS\4S jrS\4S jrS\S\S\R6                  \   4S jr S7S\S\R6                  \   S\R                  \\4   4S jjrS7S\S\R6                  \   S\R                  \\4   4S jjrS7S\S\R6                  \   S\R                  \\4   4S jjr S7S\S\R6                  \   S\R                  \\4   4S jjr!S\RD                  \\4   4S jr#S \S\4S! jr$S\R                  \	\R                  \   4   SS4S" jr%S\S\SS4S# jr&S\S\4S$ jr'S\SS4S% jr(S\4S& jr)S\4S' jr*S8S( jr+S)S*.S+\SS4S, jjr,S\-\RD                  \\4      4S- jr.S\-\RD                  \\\4      4S. jr/S\-\   4S/ jr0S\-\   4S0 jr1S9S \S\R6                  \   4S1 jjr2S:S2 jr3S:S3 jr4S:S4 jr5S\-\   4S5 jr6S\74S6 jr8Sr9g);LFUCacheig  a  
A thread-safe Least Frequently Used (LFU) cache implementation.

This cache removes elements that have been accessed the least number of times,
regardless of their access time. It provides methods for inserting, retrieving,
and managing cache entries with configurable maximum size and initial capacity.

Key features:
- Thread-safe cache with LFU eviction policy
- Configurable maximum size and initial capacity
- Supports initialization from dictionaries or iterables
- Provides methods for key-value management similar to dict
rA   Nr   rC   rF   rG   rD   rE   r   c                b    [         R                  " XUS9U l        Ub  U R                  U5        gg)a  
Initialize a new Least Frequently Used (LFU) cache.

Args:
    maxsize (int): Maximum size of the cache. A value of zero means unlimited size.
    iterable (dict or Iterable[tuple], optional): Initial data to populate the cache.
    capacity (int, optional): Initial hash table capacity to minimize reallocations. Defaults to 0.
    maxmemory (int, optional): Maximum memory (bytes) allowed for cached entries. Zero means unlimited.
                               On PyPy. In PyPy, the size of each object is assumed to be 1 if the object
                               does not have a `__sizeof__` method.

The cache uses a thread-safe LFU eviction policy, removing least frequently accessed items when the cache reaches its maximum size.
rC   N)r   r:  rB   rI   rJ   s        r   r+   LFUCache.__init__x  s-    * NN7S	KK!  r!   c                 6    U R                   R                  5       $ r)   rM   r/   s    r   rF   LFUCache.maxsize  rO   r!   c                 6    U R                   R                  5       $ r)   rQ   r/   s    r   rE   LFUCache.maxmemory  rS   r!   c                 6    U R                   R                  5       $ rU   rV   r/   s    r   rD   LFUCache.capacity  rX   r!   c                 6    U R                   R                  5       $ rZ   r[   r/   s    r   r\   LFUCache.memory  r^   r!   c                 ,    [        U R                  5      $ r)   r`   r/   s    r   rb   LFUCache.__len__  rd   r!   c                 6    U R                   R                  5       $ r)   rf   r/   s    r   rg   LFUCache.__sizeof__  ri   r!   rj   c                     XR                   ;   $ r)   rA   rl   s     r   rm   LFUCache.__contains__  ro   r!   c                 ,    U R                  5       (       + $ r)   rq   r/   s    r   rs   LFUCache.__bool__  ru   r!   c                 6    U R                   R                  5       $ r)   rw   r/   s    r   rr   LFUCache.is_empty  ry   r!   c                 6    U R                   R                  5       $ r)   r{   r/   s    r   r|   LFUCache.is_full  r~   r!   r   c                 8    U R                   R                  X5      $ r;  r   r   s      r   r   LFUCache.insert  r=  r!   r   c                 r     U R                   R                  U5      $ ! [        R                   a    Us $ f = fr  r  r   s      r   r  LFUCache.peek  s4    	99>>#&&!! 	N	r   c                 r     U R                   R                  U5      $ ! [        R                   a    Us $ f = fr   r   r   s      r   r   LFUCache.get  r   r   c                 r     U R                   R                  U5      $ ! [        R                   a    Us $ f = fr   r   r   s      r   r   LFUCache.pop  r   r   c                 8    U R                   R                  X5      $ rC  r   r   s      r   r   LFUCache.setdefault  rE  r!   c                 ~     U R                   R                  5       $ ! [        R                   a    [	        5       Sef = f)zJ
Removes and returns the least frequently used (LFU) item from the cache.
NrH  r/   s    r   r   LFUCache.popitem  s9    	'99$$&&!! 	'*$&	'rK  r   c                     US::  a  g[        U5       H  n U R                  R                  5         M      W$ ! [        R                   a    Us s  $ f = frM  rN  rP  s      r   r   LFUCache.drain  rS  rT  c                 |    [        US5      (       a  UR                  5       nU R                  R                  U5        grV  r   r   s     r   rI   LFUCache.update  rY  r!   c                 &    U R                  X5        g r)   r   r   s      r   r   LFUCache.__setitem__  r   r!   c                      U R                   R                  U5      $ ! [        R                   a    [	        U5      S ef = fr)   r   rl   s     r   r   LFUCache.__getitem__  r   r   c                      U R                   R                  U5        g ! [        R                   a    [	        U5      S ef = fr)   r   rl   s     r   r   LFUCache.__delitem__  r   r   c                 `    [        U[        5      (       d  gU R                  UR                  :H  $ r   r   r:  rB   r   s     r   r   LFUCache.__eq__"  r  r!   c                 `    [        U[        5      (       d  gU R                  UR                  :g  $ r   rh  r   s     r   r   LFUCache.__ne__(  r  r!   c                 8    U R                   R                  5         gr   r   r/   s    r   r   LFUCache.shrink_to_fit.  r   r!   Fr   r   c                :    U R                   R                  U5        gr   r   r   s     r   r   LFUCache.clear2  r   r!   c                 L    [        U R                  R                  5       S 5      $ )rk  c                     U S   U S   4$ )Nr   r   r   r   s    r   r    LFUCache.items.<locals>.<lambda>A  s    !A$!r!   r   r/   s    r   r   LFUCache.items:  s     DIIOO-/EFFr!   c                 L    [        U R                  R                  5       S 5      $ )a  
Returns an iterable view - containing tuples of `(key, value, frequency)` - of the cache's items along with their access frequency.

Notes:
    - The returned iterator should not be used to modify the cache.
    - Frequency represents how many times the item has been accessed.
c                     U $ r)   r   r   s    r   r   /LFUCache.items_with_frequency.<locals>.<lambda>K  r   r!   r   r/   s    r   items_with_frequencyLFUCache.items_with_frequencyC  r   r!   c                 L    [        U R                  R                  5       S 5      $ )rq  c                     U S   $ r   r   r   s    r   r   LFUCache.keys.<locals>.<lambda>T  r   r!   r   r/   s    r   r   LFUCache.keysM  ru  r!   c                 L    [        U R                  R                  5       S 5      $ )rw  c                     U S   $ r   r   r   s    r   r   !LFUCache.values.<locals>.<lambda>]  r   r!   r   r/   s    r   r   LFUCache.valuesV  ru  r!   c                     US:  a  [        U R                  5      U-   nUS:  a  gU R                  R                  U5      $ )a  
Returns the key in the cache that has been accessed the least, regardless of time.

If n is given, returns the nth least frequently used key.

Notes:
    - This method may re-sort the cache which can cause iterators to be stopped.
    - Do not use this method while using iterators.
r   N)ra   rB   least_frequently_usedr   s     r   r  LFUCache.least_frequently_used_  s<     q5DII"Aq5yy..q11r!   c                 "    U R                  5       $ r   r   r/   s    r   r   LFUCache.copyq  r   r!   c                     [        U 5      nUR                  U5      n[        R                  " U R                  5      Ul        U$ r)   r   r   s      r   r   LFUCache.__copy__u  r  r!   c                     [        U 5      nUR                  U5      n[        R                  " U R                  U5      Ul        U$ r)   r  r  s       r   r  LFUCache.__deepcopy__{  r
  r!   c                 "    U R                  5       $ r)   r  r/   s    r   r.   LFUCache.__iter__  r  r!   c                     [        U 5      nSUR                  UR                  [        U R                  5      U R                  R                  5       [        U R                  5       [        U R                  5      5      4-  $ r  r  r  s     r   r  LFUCache.__repr__  s^    4j!NNLL		NII$**,DII7%
 
 	
r!   r)   r  r  )r   zLFUCache[KT, VT]):r4   r5   r6   r7   r  r8   r  r9   r  r  r  r;   r+   r  rF   rE   rD   r\   rb   rg   r   r  rm   rs   rr   r|   r	   r  r   r
   r  r   r   r   r   r   r   rI   r   r   r   r   r   r   r   r#   r   rw  r   r   r  r   r   r  r.   r!  r  r=   r   r!   r   r:  r:  g  s{    I
 TX"
 "" ,,v||D&//%2H,HI4OP"
 " " 
"4 # # # %3 % %$# $" " &   t  #$ #$$ $# #," ,R ,FOOB,? ,( 7;		 & 3		b"f		r FOOB$7 6<<PRTVPVCW &r FOOB$7 6<<PRTVPVCW 2b 26??2+> 2&,,WY[]W]J^ 2'b"f- 's s #v||D&//%2H,HI #d # r  "    *r *b **r *d *'t ''t '" &+ d t G|FLLR$89 G<l6<<B3L&M <?l2& ??R( ?2s 26??23F 2$,r* 

# 

r!   r:  c                      \ rS rSrSrSr S=SSS.S\S\R                  \	\
4   S	\R                  \R                  \\R                  \   4   S4   S
\S\SS4S jjjr\S\4S j5       r\S\4S j5       r\S\	4S j5       rS\4S jrS\4S jrS\4S jrS rS\S\4S jrS\4S jrS\4S jrS\4S jrS\S\S\R<                  \   4S jrS=S\S\R<                  \    S\R                  \\ 4   4S jjr! S=S\S\R<                  \    S\RD                  \R                  \\ 4   \	4   4S jjr#S=S\S\R<                  \    S\R                  \\ 4   4S jjr$ S=S\S\R<                  \    S\RD                  \R                  \\ 4   \	4   4S  jjr%S=S\S\R<                  \    S\R                  \\ 4   4S! jjr&S\RD                  \\4   4S" jr'S\RD                  \\\	4   4S# jr(S$\S\4S% jr)S	\R                  \\R                  \   4   SS4S& jr*S\S\SS4S' jr+S\S\4S( jr,S\SS4S) jr-S\4S* jr.S\4S+ jr/S>S, jr0S-S..S/\SS4S0 jjr1S\2\RD                  \\\	4      4S1 jr3S\2\RD                  \\4      4S2 jr4S\2\   4S3 jr5S\2\   4S4 jr6S?S$\S\R<                  \   4S5 jjr7S\R<                  \   4S6 jr8S>S7 jr9S@S8 jr:S@S9 jr;S@S: jr<S\2\   4S; jr=S\>4S< jr?Sr@g)ATTLCachei  a  
A thread-safe Time-To-Live (TTL) cache implementation with configurable maximum size and expiration.

This cache automatically removes elements that have expired based on their time-to-live setting.
Supports various operations like insertion, retrieval, and iteration.
rA   Nr   rC   rF   ttlrG   rD   rE   r   c                    [        U[        5      (       a  UR                  5       nUS::  a  [        S5      e[        R
                  " XXES9U l        Ub  U R                  U5        gg)a  
Initialize a new TTL cache instance.

Args:
    maxsize: Maximum number of elements the cache can hold.
    ttl: Time-to-live for cache entries, either as seconds or a timedelta.
    iterable: Optional initial items to populate the cache, can be a dict or iterable of tuples.
    capacity: Optional initial capacity for the underlying cache storage. Defaults to 0.
    maxmemory: Maximum memory (bytes) allowed for cached entries. Zero means unlimited.
               On PyPy. In PyPy, the size of each object is assumed to be 1 if the object
               does not have a `__sizeof__` method.

Raises:
    ValueError: If the time-to-live (ttl) is not a positive number.
r   z*ttl must be a positive number and non-zerorC   N)r   r   total_seconds
ValueErrorr   r  rB   rI   )r*   rF   r  rG   rD   rE   s         r   r+   TTLCache.__init__  s[    0 c9%%##%C!8IJJNN7(X	KK!  r!   c                 6    U R                   R                  5       $ r)   rM   r/   s    r   rF   TTLCache.maxsize  rO   r!   c                 6    U R                   R                  5       $ r)   rQ   r/   s    r   rE   TTLCache.maxmemory  rS   r!   c                 6    U R                   R                  5       $ r)   )rB   r  r/   s    r   r  TTLCache.ttl  s    yy}}r!   c                 6    U R                   R                  5       $ rU   rV   r/   s    r   rD   TTLCache.capacity  rX   r!   c                 6    U R                   R                  5       $ rZ   r[   r/   s    r   r\   TTLCache.memory  r^   r!   c                 ,    [        U R                  5      $ r)   r`   r/   s    r   rb   TTLCache.__len__  rd   r!   c                 6    U R                   R                  5       $ r)   rf   r/   s    r   rg   TTLCache.__sizeof__  ri   r!   rj   c                     XR                   ;   $ r)   rA   rl   s     r   rm   TTLCache.__contains__  ro   r!   c                 ,    U R                  5       (       + $ r)   rq   r/   s    r   rs   TTLCache.__bool__  ru   r!   c                 6    U R                   R                  5       $ r)   rw   r/   s    r   rr   TTLCache.is_empty  ry   r!   c                 6    U R                   R                  5       $ r)   r{   r/   s    r   r|   TTLCache.is_full  r~   r!   r   c                 8    U R                   R                  X5      $ r;  r   r   s      r   r   TTLCache.insert  r=  r!   r   c                      U R                   R                  U5      R                  5       $ ! [        R                   a    Us $ f = fr   rB   r   r   r   r   r   s      r   r   TTLCache.get  =    	99==%++--!! 	N	   (+ AAc                      U R                   R                  U5      nUR                  5       UR                  5       4$ ! [        R
                   a    US4s $ f = fa  
Retrieves the value and expiration duration for a given key from the cache.

Returns a tuple containing the value associated with the key and its duration.
If the key is not found, returns the default value and 0.0 duration.

Args:
    key: The key to look up in the cache.
    default: The value to return if the key is not present in the cache. Defaults to None.

Returns:
    A tuple of (value, duration), where value is the cached value or default,
    and duration is the time-to-live for the key (or 0.0 if not found).
        rB   r   r   durationr   r   r*   rj   r   pairs       r   get_with_expireTTLCache.get_with_expire
  S    "	399==%D JJL$--/22 !! 	 C<	    = AAc                      U R                   R                  U5      R                  5       $ ! [        R                   a    Us $ f = fr   rB   r   r   r   r   r   s      r   r   TTLCache.pop"  ?    	99##C(..00!! 	N	r  c                      U R                   R                  U5      nUR                  5       UR                  5       4$ ! [        R
                   a    US4s $ f = fa  
Removes the specified key from the cache and returns its value and expiration duration.

If the key is not found, returns the default value and 0.0 duration.

Args:
    key: The key to remove from the cache.
    default: The value to return if the key is not present in the cache. Defaults to None.

Returns:
    A tuple of (value, duration), where value is the cached value or default,
    and duration is the time-to-live for the key (or 0.0 if not found).
r  rB   r   r   r  r   r   r  s       r   pop_with_expireTTLCache.pop_with_expire+  U     	399##C(D JJL$--/22 !! 	 C<	 r  c                 8    U R                   R                  X5      $ rC  r   r   s      r   r   TTLCache.setdefaultB  rE  r!   c                      U R                   R                  5       nUR                  5       $ ! [        R                   a    [        5       Sef = frG  rB   r   pack2r   r   r   r*   vals     r   r   TTLCache.popitemJ  sH    	))##%C 99; !! 	'*$&	'	   ,  Ac                      U R                   R                  5       nUR                  5       $ ! [        R                   a    [        5       Sef = f)af  
Removes and returns the element that has been in the cache the longest, along with its key and expiration duration.

If the cache is empty, raises a KeyError.

Returns:
    A tuple of (key, value, duration), where:
    - key is the key of the removed item
    - value is the value of the removed item
    - duration is the time-to-live for the removed item
NrB   r   pack3r   r   r   r  s     r   popitem_with_expireTTLCache.popitem_with_expireS  sH    	))##%C 99; !! 	'*$&	'r  r   c                     US::  a  g[        U5       H  n U R                  R                  5         M      W$ ! [        R                   a    Us s  $ f = frM  rN  rP  s      r   r   TTLCache.drainf  rS  rT  c                 |    [        US5      (       a  UR                  5       nU R                  R                  U5        grV  r   r   s     r   rI   TTLCache.updates  rY  r!   c                 &    U R                  X5        g r)   r   r   s      r   r   TTLCache.__setitem__z  r   r!   c                      U R                   R                  U5      R                  5       $ ! [        R                   a    [        U5      S ef = fr)   rB   r   r   r   r   r   rl   s     r   r   TTLCache.__getitem__}  D    	*99==%++--!! 	*3-T)	*	   (+ !Ac                      U R                   R                  U5        g ! [        R                   a    [	        U5      S ef = fr)   r   rl   s     r   r   TTLCache.__delitem__  r   r   c                 `    [        U[        5      (       d  gU R                  UR                  :H  $ r   r   r  rB   r   s     r   r   TTLCache.__eq__  r  r!   c                 `    [        U[        5      (       d  gU R                  UR                  :g  $ r   r  r   s     r   r   TTLCache.__ne__  r  r!   c                 8    U R                   R                  5         gr   r   r/   s    r   r   TTLCache.shrink_to_fit  r   r!   Fr   r   c                :    U R                   R                  U5        gr   r   r   s     r   r   TTLCache.clear  r   r!   c                 L    [        U R                  R                  5       S 5      $ )
Returns an iterable object of the cache's items (key-value pairs along with their expiration duration).

Notes:
- You should not make any changes in cache while using this iterable object.
c                 "    U R                  5       $ r)   r  r   s    r   r   ,TTLCache.items_with_expire.<locals>.<lambda>  
    r!   r   r/   s    r   items_with_expireTTLCache.items_with_expire       DIIOO-/BCCr!   c                 L    [        U R                  R                  5       S 5      $ )rk  c                 "    U R                  5       $ r)   r  r   s    r   r    TTLCache.items.<locals>.<lambda>  r  r!   r   r/   s    r   r   TTLCache.items  r  r!   c                 L    [        U R                  R                  5       S 5      $ )rq  c                 "    U R                  5       $ r)   rj   r   s    r   r   TTLCache.keys.<locals>.<lambda>  
    r!   r   r/   s    r   r   TTLCache.keys       DIIOO-/@AAr!   c                 L    [        U R                  R                  5       S 5      $ )rw  c                 "    U R                  5       $ r)   r   r   s    r   r   !TTLCache.values.<locals>.<lambda>  r  r!   r   r/   s    r   r   TTLCache.values  r  r!   c                     US:  a  [        U R                  5      U-   nUS:  a  gU R                  R                  U5      $ r|  r}  r   s     r   r  TTLCache.first  r  r!   c                 d    U R                   R                  [        U R                   5      S-
  5      $ r  r  r/   s    r   r  TTLCache.last  r  r!   c                 8    U R                   R                  5         gz
Manually removes expired key-value pairs from memory and releases their memory.

Notes:
    - This operation is typically automatic and does not require manual invocation.
NrB   expirer/   s    r   r  TTLCache.expire       			r!   c                 "    U R                  5       $ r   r   r/   s    r   r   TTLCache.copy  r   r!   c                     [        U 5      nUR                  U5      n[        R                  " U R                  5      Ul        U$ r)   r   r   s      r   r   TTLCache.__copy__  r  r!   c                     [        U 5      nUR                  U5      n[        R                  " U R                  U5      Ul        U$ r)   r  r  s       r   r  TTLCache.__deepcopy__  r
  r!   c                 "    U R                  5       $ r)   r  r/   s    r   r.   TTLCache.__iter__  r  r!   c                 2   [        U 5      nSUR                  UR                  [        U R                  5      U R                  R                  5       U R                  R                  5       [        U R                  5       [        U R                  5      5      4-  $ )Nz%s.%s[%d/%d, ttl=%f](%s))	r   r5   r4   ra   rB   rF   r  r    r   r  s     r   r  TTLCache.__repr__  sk    4j)NNLL		NIIIIMMO$**,DII7-
 
 	
r!   r)   r  r  )r   zTTLCache[KT, VT])Ar4   r5   r6   r7   r  r8   r  r9   r  floatr   r  r  r;   r+   r  rF   rE   r  rD   r\   rb   rg   r   r  rm   rs   rr   r|   r	   r  r   r
   r   r   r  r   r  r   r   r  r   rI   r   r   r   r   r   r   r   r#   r  r   r   r   r  r  r  r   r   r  r.   r!  r  r=   r   r!   r   r  r    s>    I TX	!" !"!" \\%*+!" ,,v||D&//%2H,HI4OP	!" !" !" 
!"F # # # %3 % % U  $# $" " &   t  #$ #$$ $# #," ,R ,FOOB,? ,&r FOOB$7 6<<PRTVPVCW ( 7;33 & 33	fll2r6*E1	230r FOOB$7 6<<PRTVPVCW  7;33 & 33	fll2r6*E1	23.2b 26??2+> 2&,,WY[]W]J^ 2b"f- V\\"b%-%@ &s s #v||D&//%2H,HI #d # r  "    *r *b **r *d *'t ''t '" &+ d t D<RU]0K#L DD|FLLR$89 DBl2& BDR( D&s &6??2#6 &7foob) 7,r* 

# 

r!   r  c                      \ rS rSrSrSr  S:SSS.S\S\R                  \R                  \	\R                  \   4   S4   S	\R                  \\\S4   S
\S\SS4S jjjr\S\4S j5       r\S\4S j5       rS\4S jrS\4S jrS\4S jrS rS\S\4S jrS\4S jrS\4S jrS\4S jr S;S\S\S	\R                  \\\S4   S\R<                  \   4S jjrS;S\S\R<                  \    S\R                  \\ 4   4S jjr! S;S\S\R<                  \    S\RD                  \R                  \\ 4   \4   4S jjr#S;S\S\R<                  \    S\R                  \\ 4   4S jjr$ S;S\S\R<                  \    S\RD                  \R                  \\ 4   \4   4S jjr%  S:S\S\R<                  \    S	\R                  \\\S4   S\R                  \\ 4   4S  jjr&S\RD                  \\4   4S! jr'S\RD                  \\\4   4S" jr(S#\S\4S$ jr) S;S\R                  \	\R                  \   4   S	\R                  \\\S4   SS4S% jjr*S\S\SS4S& jr+S\S\4S' jr,S\SS4S( jr-S\4S) jr.S\4S* jr/S<S+ jr0S,S-.S.\SS4S/ jjr1S\2\RD                  \\\4      4S0 jr3S\2\RD                  \\4      4S1 jr4S\2\   4S2 jr5S\2\   4S3 jr6S<S4 jr7S=S5 jr8S=S6 jr9S=S7 jr:S\2\   4S8 jr;S\<4S9 jr=Sr>g)>	VTTLCachei  a:  
A thread-safe, time-to-live (TTL) cache implementation with per-key expiration policy.

This cache allows storing key-value pairs with optional expiration times. When an item expires,
it is automatically removed from the cache. The cache supports a maximum size and provides
various methods for inserting, retrieving, and managing cached items.

Key features:
- Per-key time-to-live (TTL) support
- Configurable maximum cache size
- Thread-safe operations
- Automatic expiration of items

Supports dictionary-like operations such as get, insert, update, and iteration.
rA   Nr   rC   rF   rG   r  rD   rE   r   c                b    [         R                  " XUS9U l        Ub  U R                  X#5        gg)a  
Initialize a new VTTLCache instance.

Args:
    maxsize (int): Maximum size of the cache. Zero indicates unlimited size.
    iterable (dict or Iterable[tuple], optional): Initial data to populate the cache.
    ttl (float or timedelta or datetime, optional): Time-to-live duration for `iterable` items.
    capacity (int, optional): Preallocated capacity for the cache to minimize reallocations.
    maxmemory (int, optional): Maximum memory (bytes) allowed for cached entries. Zero means unlimited.
                               On PyPy. In PyPy, the size of each object is assumed to be 1 if the object
                               does not have a `__sizeof__` method.

Raises:
    ValueError: If provided TTL is zero or negative.
rC   N)r   r  rB   rI   )r*   rF   rG   r  rD   rE   s         r   r+   VTTLCache.__init__  s-    0 OOG)T	KK&  r!   c                 6    U R                   R                  5       $ r)   rM   r/   s    r   rF   VTTLCache.maxsize2  rO   r!   c                 6    U R                   R                  5       $ r)   rQ   r/   s    r   rE   VTTLCache.maxmemory6  rS   r!   c                 6    U R                   R                  5       $ rU   rV   r/   s    r   rD   VTTLCache.capacity:  rX   r!   c                 6    U R                   R                  5       $ rZ   r[   r/   s    r   r\   VTTLCache.memory>  r^   r!   c                 ,    [        U R                  5      $ r)   r`   r/   s    r   rb   VTTLCache.__len__B  rd   r!   c                 6    U R                   R                  5       $ r)   rf   r/   s    r   rg   VTTLCache.__sizeof__E  ri   r!   rj   c                     XR                   ;   $ r)   rA   rl   s     r   rm   VTTLCache.__contains__H  ro   r!   c                 ,    U R                  5       (       + $ r)   rq   r/   s    r   rs   VTTLCache.__bool__K  ru   r!   c                 6    U R                   R                  5       $ r)   rw   r/   s    r   rr   VTTLCache.is_emptyN  ry   r!   c                 6    U R                   R                  5       $ r)   r{   r/   s    r   r|   VTTLCache.is_fullQ  r~   r!   r   c                 $   Ubr  [        U[        5      (       a  UR                  5       nO;[        U[        5      (       a&  U[        R                  " 5       -
  R                  5       nUS::  a  [        S5      eU R                  R                  XU5      $ )a  
Insert a key-value pair into the cache with an optional time-to-live (TTL).
Returns the previous value associated with the key, if it existed.

Args:
    key (KT): The key to insert.
    value (VT): The value to associate with the key.
    ttl (float or timedelta or datetime, optional): Time-to-live duration for the item.
        If a timedelta or datetime is provided, it will be converted to seconds.

Raises:
    ValueError: If the provided TTL is zero or negative.
r   !ttl must be positive and non-zero)r   r   r  r   nowr  rB   r   )r*   rj   r   r  s       r   r   VTTLCache.insertT  st    & ?#y))'')C**X\\^+::<ax !DEEyyC00r!   r   c                      U R                   R                  U5      R                  5       $ ! [        R                   a    Us $ f = fr   r  r   s      r   r   VTTLCache.gets  r  r  c                      U R                   R                  U5      nUR                  5       UR                  5       4$ ! [        R
                   a    US4s $ f = fr  r  r  s       r   r  VTTLCache.get_with_expire  r  r  c                      U R                   R                  U5      R                  5       $ ! [        R                   a    Us $ f = fr   r  r   s      r   r   VTTLCache.pop  r  r  c                      U R                   R                  U5      nUR                  5       UR                  5       4$ ! [        R
                   a    US4s $ f = fr  r  r  s       r   r  VTTLCache.pop_with_expire  r  r  c                 $   Ubr  [        U[        5      (       a  UR                  5       nO;[        U[        5      (       a&  U[        R                  " 5       -
  R                  5       nUS::  a  [        S5      eU R                  R                  XU5      $ )a  
Inserts a key-value pair into the cache with an optional time-to-live (TTL).

If the key is not in the cache, it will be inserted with the default value.
If the key already exists, its current value is returned.

Args:
    key: The key to insert or retrieve from the cache.
    default: The value to insert if the key is not present. Defaults to None.
    ttl: Optional time-to-live for the key. Can be a float (seconds), timedelta, or datetime.
         If not specified, the key will not expire.

Returns:
    The value associated with the key, either existing or the default value.

Raises:
    ValueError: If the provided TTL is not a positive value.
r   r3  )r   r   r  r   r4  r  rB   r   )r*   rj   r   r  s       r   r   VTTLCache.setdefault  st    0 ?#y))'')C**X\\^+::<ax !DEEyy##C#66r!   c                      U R                   R                  5       nUR                  5       $ ! [        R                   a    [        5       Sef = f)z
Removes and returns the key-value pair that is closest to expiration.

Returns:
    A tuple containing the key and value of the removed item.

Raises:
    KeyError: If the cache is empty.
Nr  r  s     r   r   VTTLCache.popitem  H    	))##%C 99; !! 	'*$&	'r  c                      U R                   R                  5       nUR                  5       $ ! [        R                   a    [        5       Sef = f)z
Removes and returns the key-value pair that is closest to expiration, along with its expiration duration.

Returns:
    A tuple containing the key, value, and expiration duration of the removed item.

Raises:
    KeyError: If the cache is empty.
Nr  r  s     r   r  VTTLCache.popitem_with_expire  rB  r  r   c                     US::  a  g[        U5       H  n U R                  R                  5         M      W$ ! [        R                   a    Us s  $ f = frM  rN  rP  s      r   r   VTTLCache.drain  rS  rT  c                 f   [        US5      (       a  UR                  5       nUbr  [        U[        5      (       a  UR	                  5       nO;[        U[
        5      (       a&  U[
        R                  " 5       -
  R	                  5       nUS::  a  [        S5      eU R                  R                  X5        g)rW  r   Nr   r3  )
r   r   r   r   r  r   r4  r  rB   rI   )r*   rG   r  s      r   rI   VTTLCache.update  s     8W%%~~'H?#y))'')C**X\\^+::<ax !DEE		'r!   c                 (    U R                  XS 5        g r)   r   r   s      r   r   VTTLCache.__setitem__&  s    C%r!   c                      U R                   R                  U5      R                  5       $ ! [        R                   a    [        U5      S ef = fr)   r  rl   s     r   r   VTTLCache.__getitem__)  r  r  c                      U R                   R                  U5        g ! [        R                   a    [	        U5      S ef = fr)   r   rl   s     r   r   VTTLCache.__delitem__/  r   r   c                 `    [        U[        5      (       d  gU R                  UR                  :H  $ r   r   r  rB   r   s     r   r   VTTLCache.__eq__5  rc  r!   c                 `    [        U[        5      (       d  gU R                  UR                  :g  $ r   rP  r   s     r   r   VTTLCache.__ne__;  rc  r!   c                 8    U R                   R                  5         gr   r   r/   s    r   r   VTTLCache.shrink_to_fitA  r   r!   Fr   r   c                :    U R                   R                  U5        gr   r   r   s     r   r   VTTLCache.clearE  r   r!   c                 L    [        U R                  R                  5       S 5      $ )r  c                 "    U R                  5       $ r)   r  r   s    r   r   -VTTLCache.items_with_expire.<locals>.<lambda>T  r  r!   r   r/   s    r   r  VTTLCache.items_with_expireM  r  r!   c                 L    [        U R                  R                  5       S 5      $ )rk  c                 "    U R                  5       $ r)   r  r   s    r   r   !VTTLCache.items.<locals>.<lambda>]  r  r!   r   r/   s    r   r   VTTLCache.itemsV  r  r!   c                 L    [        U R                  R                  5       S 5      $ )rq  c                 "    U R                  5       $ r)   r  r   s    r   r    VTTLCache.keys.<locals>.<lambda>f  r  r!   r   r/   s    r   r   VTTLCache.keys_  r  r!   c                 L    [        U R                  R                  5       S 5      $ )rw  c                 "    U R                  5       $ r)   r  r   s    r   r   "VTTLCache.values.<locals>.<lambda>o  r  r!   r   r/   s    r   r   VTTLCache.valuesh  r  r!   c                 8    U R                   R                  5         gr
  r  r/   s    r   r  VTTLCache.expireq  r  r!   c                 "    U R                  5       $ r   r   r/   s    r   r   VTTLCache.copyz  r   r!   c                     [        U 5      nUR                  U5      n[        R                  " U R                  5      Ul        U$ r)   r   r   s      r   r   VTTLCache.__copy__~  r  r!   c                     [        U 5      nUR                  U5      n[        R                  " U R                  U5      Ul        U$ r)   r  r  s       r   r  VTTLCache.__deepcopy__  r
  r!   c                 "    U R                  5       $ r)   r  r/   s    r   r.   VTTLCache.__iter__  r  r!   c                     [        U 5      nSUR                  UR                  [        U R                  5      U R                  R                  5       [        U R                  5       [        U R                  5      5      4-  $ r  r  r  s     r   r  VTTLCache.__repr__  s^    4j!NNLL		NII$**,DII7%
 
 	
r!   )NNr)   r  )r   zVTTLCache[KT, VT])?r4   r5   r6   r7   r  r8   r  r9   r  r  r  r;   r  r   r   r+   r  rF   rE   rD   r\   rb   rg   r   r  rm   rs   rr   r|   r	   r  r   r
   r   r   r  r   r  r   r   r  r   rI   r   r   r   r   r   r   r   r#   r  r   r   r   r  r   r   r  r.   r!  r  r=   r   r!   r   r  r    sp     I
 TX>B	' '' ,,v||D&//%2H,HI4OP' \\%Hd:;	' ' ' 
': # # # %3 % %$# $" " &   t  #$ #$$ $# # ?C	11 1 \\%Hd:;	1
 
	1>r FOOB$7 6<<PRTVPVCW ( 7;33 & 33	fll2r6*E1	230r FOOB$7 6<<PRTVPVCW  7;33 & 33	fll2r6*E1	234 (,>B	"7"7 $"7 \\%Hd:;	"7
 
b"f	"7Hb"f- "V\\"b%-%@ "s s   ?C(,,tV__U%;;<( \\%Hd:;( 
	(*&r &" & &*r *b **r *d *'t ''t '" &+ d t D<RU]0K#L DD|FLLR$89 DBl2& BDR( D,r* 	
# 	
r!   r  )r   r   r9   r   r    r   r   TypeVarr   r	   r
   r  r<   r!  r    Genericr#   r?   r#  r  r  r:  r  r  r   r!   r   <module>rw     s     (   ^^D^^D^^DC4 C C*36>>"% 3v
M"b&! v
rS
b"f% S
lH
mBF# H
VY
}RV$ Y
xg
}RV$ g
T	n
}RV$ n
bT
b"f% T
r!   