
    i|1                         S SK r S SKJr  S SKJr  S SKJrJrJr  S SK	J
r
  \" S5      r\" S5      r\" SS	S
9 " S S\\\4   5      5       r " S S\\\4   5      r " S S\\\4   5      r " S S\\\4   5      rg)    N)OrderedDict)	dataclass)AnyGenericTypeVar)InvalidArgumentKVTF)slotsfrozenc                   d    \ rS rSr% \\S'   \\S'   \\S'   Sr\\S'   \	S\
\S4   4S	 j5       rS
rg)Entry   keyvaluescorer   pinsreturn.c                 @    U R                   S:X  a  SU R                  4$ g)Nr   )   )r   r   selfs    X/var/www/html/ai-image-ml/venv/lib/python3.13/site-packages/hypothesis/internal/cache.pysort_keyEntry.sort_key   s     99>tzz?"      N)__name__
__module____qualname____firstlineno__r	   __annotations__r
   intr   propertytupler   __static_attributes__r   r   r   r   r      s;    	
FHJD#M%S/  r   r   c                      \ rS rSrSrSrS\4S jr\S\	\
\4   4S j5       r\S\\\
\4      4S j5       rS\4S	 jrS
\
S\4S jrS
\
S\4S jrS
\
S\SS4S jrS rS
\
S\SS4S jrS
\
SS4S jrS
\
S\4S jrS!S jrS\4S jrS
\
S\S\4S jrS
\
S\S\S\4S jrS
\
S\S\S\4S jrS!S jr S\SS4S jr!S\S\SS4S jr"S\SS4S jr#S\S\S\4S  jr$Sr%g)"GenericCache(   a  Generic supertype for cache implementations.

Defines a dict-like mapping with a maximum size, where as well as mapping
to a value, each key also maps to a score. When a write would cause the
dict to exceed its maximum size, it first evicts the existing key with
the smallest score, then adds the new key to the map. If due to pinning
no key can be evicted, ValueError is raised.

A key has the following lifecycle:

1. key is written for the first time, the key is given the score
   self.new_entry(key, value)
2. whenever an existing key is read or written, self.on_access(key, value,
   score) is called. This returns a new score for the key.
3. After a key is evicted, self.on_evict(key, value, score) is called.

The cache will be in a valid state in all of these cases.

Implementations are expected to implement new_entry and optionally
on_access and on_evict to implement a specific scoring strategy.
_threadlocalmax_sizer,   c                 f    US::  a  [        S5      eXl        [        R                  " 5       U l        g )Nr   z Cache size must be at least one.)r   r,   	threadinglocalr+   r   r,   s     r   __init__GenericCache.__init__A   s+    q=!"DEE  &OO-r   r   c                      U R                   R                  $ ! [         a*    0 U R                   l        U R                   R                  s $ f = fN)r+   keys_to_indicesAttributeErrorr   s    r   r5   GenericCache.keys_to_indicesN   sH    	5$$444 	502D-$$444	5    1AAc                      U R                   R                  $ ! [         a*    / U R                   l        U R                   R                  s $ f = fr4   )r+   datar6   r   s    r   r:   GenericCache.dataV   sH    	*$$))) 	*%'D"$$)))	*r8   c                     [        U R                  5      [        U R                  5      :X  d   e[        U R                  5      $ r4   )lenr5   r:   r   s    r   __len__GenericCache.__len__^   s1    4''(C		N:::499~r   r   c                     XR                   ;   $ r4   )r5   r   r   s     r   __contains__GenericCache.__contains__b   s    ****r   c                 x    U R                   U   nU R                  U   nU R                  U5        UR                  $ r4   )r5   r:   !_GenericCache__entry_was_accessedr   )r   r   iresults       r   __getitem__GenericCache.__getitem__e   s7      %1!!!$||r   r   Nc           	      Z   S n U R                   U   nU R                  U   nUR                  U:X  d   eX%l        U R	                  U5        Ubm  U R                  S   ULa)  UR                  U R                  S   R                  ::  d   eU R                  UR                  UR                  UR                   5        g g ! [
         a    [        XU R                  X5      5      n[        U R                  5      U R                  :  aS  U R                  S   nUR                  S:  a  [        S5      S eU R                   UR                  	 SnXPR                  S'   O0[        U R                  5      nU R                  R                  U5        X@R                   U'   U R                  U5         GN^f = f)Nr   z>Cannot increase size of cache where all keys have been pinned.)r5   r:   r   r   rE   KeyErrorr   	new_entryr=   r,   r   
ValueErrorappend_GenericCache__balancer   on_evictr   )r   r   r   evictedrF   entrys         r   __setitem__GenericCache.__setitem__k   sl   	)$$S)A$ IIaLE99###K%%a(yy|5(''499Q<+@+@@@@MM'++w}}gmmD -  	#dnnS&@AE499~.))A,<<!#$X  ((5$		!		N		  '()  %NN1	s   B< <C*F*)F*c                 ,    [        U R                  5      $ r4   )iterr5   r   s    r   __iter__GenericCache.__iter__   s    D(())r   c                     X U'   U R                   U   nU R                  U   nU=R                  S-  sl        UR                  S:X  a  U R                  U5        gg)a  Mark ``key`` as pinned (with the given value). That is, it may not
be evicted until ``unpin(key)`` has been called. The same key may be
pinned multiple times, possibly changing its value, and will not be
unpinned until the same number of calls to unpin have been made.
r   N)r5   r:   r   rO   )r   r   r   rF   rR   s        r   pinGenericCache.pin   sQ     S	  %		!

a
::?NN1 r   c                     U R                   U   nU R                  U   nUR                  S:X  a  [        SU< S35      eU=R                  S-  sl        UR                  S:X  a  U R	                  U5        gg)z~Undo one previous call to ``pin(key)``. The value stays the same.
Once all calls are undone this key may be evicted as normal.r   zKey z has not been pinnedr   N)r5   r:   r   rM   rO   )r   r   rF   rR   s       r   unpinGenericCache.unpin   sj       %		!::?tC7*>?@@

a
::?NN1 r   c                 X    U R                   U   nU R                  U   R                  S:  $ )z,Returns True if the key is currently pinned.r   )r5   r:   r   )r   r   rF   s      r   	is_pinnedGenericCache.is_pinned   s+      %yy|  1$$r   c                 V    U R                   SS2	 U R                  R                  5         g)z3Remove all keys, regardless of their pinned status.N)r:   r5   clearr   s    r   rc   GenericCache.clear   s    IIaL""$r   c                 R    SSR                  S U R                   5       5      -   S-   $ )N{z, c              3   Z   #    U  H!  oR                   < S UR                  < 3v   M#     g7f)z: N)r   r   ).0es     r   	<genexpr>(GenericCache.__repr__.<locals>.<genexpr>   s!     L)Q%%"QWWK8)s   )+})joinr:   r   s    r   __repr__GenericCache.__repr__   s%    TYYL$))LLLsRRr   c                     [         e)zvCalled when a key is written that does not currently appear in the
map.

Returns the score to associate with the key.
)NotImplementedErrorr   r   r   s      r   rL   GenericCache.new_entry   s
     "!r   r   c                     U$ )zkCalled every time a key that is already in the map is read or
written.

Returns the new score for the key.
r   r   r   r   r   s       r   	on_accessGenericCache.on_access   s	     r   c                     g)zXCalled after a key has been evicted, with the score it had had at
the point of eviction.Nr   ru   s       r   rP   GenericCache.on_evict   s    r   c                    [        U R                  5      [        U R                  5      :X  d   e[        U R                  5       H  u  pU R                  UR                     U:X  d   eUS-  S-   US-  S-   4 HV  nU[        U R                  5      :  d  M  UR
                  U R                  U   R
                  ::  a  MG   U R                  5       e   M     g)zDebugging method for use in tests.

Asserts that all of the cache's invariants hold. When everything
is working correctly this should be an expensive no-op.
   r   N)r=   r5   r:   	enumerater   r   )r   rF   ri   js       r   check_validGenericCache.check_valid   s     4''(C		N:::dii(DA''.!333!eaiQ+s499~%::1)>)>>I		I> , )r   rF   c                     U R                   U   nU R                  UR                  UR                  UR                  5      nX2R                  :w  a)  X2l        UR
                  S:X  a  U R                  U5        g g g Nr   )r:   rv   r   r   r   r   rO   )r   rF   rR   	new_scores       r   __entry_was_accessed!GenericCache.__entry_was_accessed   s_    		!NN599ekk5;;G	##K zzQq! 	 $r   r}   c                    X:  d   eU R                   U   R                  U R                   U   R                  :  d   eU R                   U   U R                   U   sU R                   U'   U R                   U'   XR                  U R                   U   R                  '   X R                  U R                   U   R                  '   g r4   )r:   r   r5   r   r   rF   r}   s      r   __swapGenericCache.__swap   s    uuyy|$$tyy|'<'<<<<%)YYq\499Q<"		!diil12TYYq\--.12TYYq\--.r   c                 ,  ^  US-
  S-  =nS:  a;  T R                  X!5      (       a  T R                  X!5        UnOOUS-
  S-  =nS:  a  M;  SU-  S-   SU-  S-   4 Vs/ s H  o3[        T R                  5      :  d  M  UPM!     sn=n(       a~  [	        UU 4S jS9nT R                  X5      (       a  T R                  X5        UnOgSU-  S-   SU-  S-   4 Vs/ s H  o3[        T R                  5      :  d  M  UPM!     sn=n(       a  M}  ggs  snf s  snf )zWhen we have made a modification to the heap such that
the heap property has been violated locally around i but previously
held for all other indexes (and no other values have been modified),
this fixes the heap so that the heap property holds everywhere.r   r{   r   c                 6   > TR                   U    R                  $ r4   r:   r   )r}   r   s    r   <lambda>(GenericCache.__balance.<locals>.<lambda>   s    19N9Nr   )r   N)_GenericCache__out_of_order_GenericCache__swapr=   r:   min)r   rF   parentr}   childrensmallest_childs   `     r   	__balanceGenericCache.__balance   s    a%A%v!+""6--F& a%A%v!+ ()1uqy!a%!)&<S&<C		N@R1&<SShS /NON""155A." ()1uqy!a%!)&<S&<C		N@R1&<SShSSSSs   D=DD8Dc                     XS-
  S-  :X  d   eU R                   U   R                  U R                   U   R                  :  $ )zUReturns True if the indices i, j are in the wrong order.

i must be the parent of j.
r   r{   r   r   s      r   __out_of_orderGenericCache.__out_of_order   s?    
 UqL   yy|$$tyy|'<'<<<r   r   N)&r   r   r    r!   __doc__	__slots__r#   r1   r$   dictr	   r5   listr   r
   r:   r>   boolrB   rH   rS   rW   rZ   r]   r`   rc   strrn   rL   r   rv   rP   r~   rE   r   rO   r   r&   r   r   r   r(   r(   (   s   , -I. . 5af 5 5 *d5A;' * * + +d +q Q Eq E Et E>*q  t 	 	t 	%Q %4 %
%
S# S"Q "q "S "Q q   "A "a " " "J"c "d "3 3 3 33 4 *= = = =r   r(   c                   v   ^  \ rS rSrSrSrS\4U 4S jjrS\4S jrS\	S	\
S\4S
 jrS\	S	\
S\S\4S jrSrU =r$ )LRUReusedCachei  ac  The only concrete implementation of GenericCache we use outside of tests
currently.

Adopts a modified least-recently used eviction policy: It evicts the key
that has been used least recently, but it will always preferentially evict
keys that have never been accessed after insertion. Among keys that have been
accessed, it ignores the number of accesses.

This retains most of the benefits of an LRU cache, but adds an element of
scan-resistance to the process: If we end up scanning through a large
number of keys without reusing them, this does not evict the existing
entries in preference for the new ones.
)__tickr,   c                 2   > [         TU ]  U5        SU l        g r   )superr1   _LRUReusedCache__tick)r   r,   	__class__s     r   r1   LRUReusedCache.__init__  s    "r   r   c                 D    U =R                   S-  sl         U R                   $ Nr   )r   r   s    r   tickLRUReusedCache.tick  s    q{{r   r   r   c                 &    SU R                  5       4$ r   r   rr   s      r   rL   LRUReusedCache.new_entry      499;r   r   c                 &    SU R                  5       4$ )Nr{   r   ru   s       r   rv   LRUReusedCache.on_access  r   r   )r   r   r    r!   r   r   r#   r1   r   r	   r
   r   rL   rv   r&   __classcell__)r   s   @r   r   r     sg     I c  Q  q  S   Q  q        r   r   c                       \ rS rSrSrS\SS4S jr\S\\	\
4   4S j5       rS\	S	\
SS4S
 jrS\	S\
4S jrS rS\4S jrS\	S\4S jrSS jrSrg)LRUCachei#  a  
This is a drop-in replacement for a GenericCache (despite the lack of inheritance)
in performance critical environments. It turns out that GenericCache's heap
balancing for arbitrary scores can be quite expensive compared to the doubly
linked list approach of lru_cache or OrderedDict.

This class is a pure LRU and does not provide any sort of affininty towards
the number of accesses beyond recency. If soft-pinning entries which have been
accessed at least once is important, use LRUReusedCache.
r,   r   Nc                 T    US:  d   eXl         [        R                  " 5       U l        g r   )r,   r.   r/   r+   r0   s     r   r1   LRUCache.__init__9  s"    !|| %OO-r   c                      U R                   R                  $ ! [         a2    [        5       U R                   l        U R                   R                  s $ f = fr4   )r+   cacher6   r   r   s    r   r   LRUCache.cache>  sJ    	+$$*** 	+&1mD#$$***	+s    9AAr   r   c                    X R                   U'   U R                   R                  U5        [        U R                   5      U R                  :  a?  U R                   R	                  SS9  [        U R                   5      U R                  :  a  M>  g g )NF)last)r   move_to_endr=   r,   popitemrr   s      r   rS   LRUCache.__setitem__F  s^    

3

s#$**o-JJE* $**o-r   c                 Z    U R                   U   nU R                   R                  U5        U$ r4   )r   r   )r   r   vals      r   rH   LRUCache.__getitem__M  s&    jjo

s#
r   c                 ,    [        U R                  5      $ r4   )rV   r   r   s    r   rW   LRUCache.__iter__R  s    DJJr   c                 ,    [        U R                  5      $ r4   )r=   r   r   s    r   r>   LRUCache.__len__U  s    4::r   c                     XR                   ;   $ r4   )r   rA   s     r   rB   LRUCache.__contains__X  s    jj  r   c                     g r4   r   r   s    r   r~   LRUCache.check_valid\  s    r   r*   r   )r   r   r    r!   r   r#   r1   r$   r   r	   r
   r   rS   rH   rW   r>   r   rB   r~   r&   r   r   r   r   r   #  s    	*. . .
 +{1a4( + ++q + +t +q Q 
  ! !d !r   r   )r.   collectionsr   dataclassesr   typingr   r   r   hypothesis.errorsr   r	   r
   r   r(   r   r   r   r   r   <module>r      s     # ! ( ( -CLCL e$GAqDM  %"X=71a4= X=v \!Q$'  @:wq!t} :r   