
    &`i                     x    d dl mZmZ d dlmZmZmZmZmZ  ed          Z	 ed          Z
 G d d          ZdS )    )Counterdefaultdict)Dict	GeneratorListOptionalTypeVarTUc                       e Zd ZdZddefdZed             Zed             Zdde	d	e
d
dfdZdde	d	e
d
dfdZde	d
efdZde	ded
efdZde	d
ee         fdZdded
eeddf         fdZdS )_ObjectCachea  Cache up to some maximum count given a grouping key.

    This object cache can e.g. be used to cache Ray Tune trainable actors
    given their resource requirements (reuse_actors=True).

    If the max number of cached objects for a grouping key is reached,
    no more objects for this group will be cached.

    However, if `may_keep_one=True`, one object (globally across all grouping
    keys) may be cached, even if the max number of objects is 0. This is to
    allow to cache an object if the max number of objects of this key
    will increase shortly after (as is the case e.g. in the Ray Tune control
    loop).

    Args:
        may_keep_one: If True, one object (globally) may be cached if no desired
            maximum objects are defined.

    Tmay_keep_onec                 z    d| _         t          t                    | _        t	                      | _        || _        d S )Nr   )_num_cached_objectsr   list_cached_objectsr   _max_num_objects_may_keep_one)selfr   s     o/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/ray/tune/utils/object_cache.py__init__z_ObjectCache.__init__   s4    () 1<T1B1B,3II)    c                     | j         S N)r   r   s    r   num_cached_objectsz_ObjectCache.num_cached_objects&   s    ''r   c                 N    t          | j                                                  S r   )sumr   valuesr   s    r   total_max_objectsz_ObjectCache.total_max_objects*   s!     4(//11222r      keybyreturnNc                 0    | j         |xx         |z  cc<   dS )zIncrease number of max objects for this key.

        Args:
            key: Group key.
            by: Decrease by this amount.
        Nr   r   r"   r#   s      r   increase_maxz_ObjectCache.increase_max/   )     	c"""b("""""r   c                 0    | j         |xx         |z  cc<   dS )zDecrease number of max objects for this key.

        Args:
            key: Group key.
            by: Decrease by this amount.
        Nr&   r'   s      r   decrease_maxz_ObjectCache.decrease_max8   r)   r   c                 6    t          | j        |                   S )zReturn True if at least one cached object exists for this key.

        Args:
            key: Group key.

        Returns:
            True if at least one cached object exists for this key.
        )boolr   r   r"   s     r   has_cached_objectz_ObjectCache.has_cached_objectA   s     D(-...r   objc                 H   t          | j        |                   | j        |         k    rH| j        sdS | j        dk    rdS t          d | j                                        D                       rdS | j        |                             |           | xj        dz  c_        dS )a  Cache object for a given key.

        This will put the object into a cache, assuming the number
        of cached objects for this key is less than the number of
        max objects for this key.

        An exception is made if `max_keep_one=True` and no other
        objects are cached globally. In that case, the object can
        still be cached.

        Args:
            key: Group key.
            obj: Object to cache.

        Returns:
            True if the object has been cached. False otherwise.

        Fr   c              3      K   | ]}|V  d S r    .0vs     r   	<genexpr>z,_ObjectCache.cache_object.<locals>.<genexpr>j   s"      ==1======r   r!   T)lenr   r   r   r   anyr   append)r   r"   r0   s      r   cache_objectz_ObjectCache.cache_objectL   s    ( t#C())T-B3-GGG% u '!++u ==d3::<<===== u 	S!((---  A%  tr   c                     |                      |          sdS | xj        dz  c_        | j        |                             d          S )zGet one cached object for a key.

        This will remove the object from the cache.

        Args:
            key: Group key.

        Returns:
            Cached object.
        Nr!   r   )r/   r   r   popr.   s     r   pop_cached_objectz_ObjectCache.pop_cached_objects   sO     %%c** 	4  A%  #C(,,Q///r   F	force_allc              #     K   | j         o| }| j                                        D ]\  }}|s| j        |         nd}| j        dk    r5|r3t          d | j                                        D                       s dS t          |          |k    r:| xj        dz  c_        |                    d          V  t          |          |k    :dS )a  Return a generator over cached objects evicted from the cache.

        This method yields all cached objects that should be evicted from the
        cache for cleanup by the caller.

        If the number of max objects is lower than the number of
        cached objects for a given key, objects are evicted until
        the numbers are equal.

        If `max_keep_one=True` (and ``force_all=False``), one cached object
        may be retained.

        Objects are evicted FIFO.

        If ``force_all=True``, all objects are evicted.

        Args:
            force_all: If True, all objects are flushed. This takes precedence
                over ``keep_one``.

        Yields:
            Evicted objects to be cleaned up by caller.

        r   r!   c              3      K   | ]}|V  d S r   r3   r4   s     r   r7   z4_ObjectCache.flush_cached_objects.<locals>.<genexpr>   s"      FF!AFFFFFFr   N)	r   r   itemsr   r   r9   r   r8   r=   )r   r?   keep_oner"   objs
max_cacheds         r   flush_cached_objectsz!_ObjectCache.flush_cached_objects   s      4 %7i--3355 	" 	"IC;DK.s33!J (A-- . FFt'<'C'C'E'EFFFFF .
 d))j((((A-((hhqkk!!! d))j((	" 	"r   )T)r!   )F)__name__
__module____qualname____doc__r-   r   propertyr   r    r
   intr(   r+   r/   r   r;   r   r>   r   rF   r3   r   r   r   r   
   su        (* *T * * * * ( ( X( 3 3 X3) ) )s )4 ) ) ) )) ) )s )4 ) ) ) )	/Q 	/4 	/ 	/ 	/ 	/% % %d % % % %N0Q 08A; 0 0 0 0")" )"d )"yDRV?W )" )" )" )" )" )"r   r   N)collectionsr   r   typingr   r   r   r   r	   r
   r   r   r3   r   r   <module>rO      s    , , , , , , , , ; ; ; ; ; ; ; ; ; ; ; ; ; ; GCLLGCLLc" c" c" c" c" c" c" c" c" c"r   