
    dM                        d dl mZ d dlZd dlmZ d dlmZmZ d dlm	Z	m
Z
mZ  ed          Z ed          Z G d d	eeef                   ZdS )
    )annotationsN)OrderedDict)IteratorMutableMapping)AnyCallableTypeVarKVc                      e Zd ZU dZded<   ded<   ded<   ded	<   d
Zd d!dZd"dZd#dZd$dZ	d%dZ
d&dZd'dZed'd            Zej        d(d            ZdS ))LRUCacheal  Thread-safe LRUCache based on an OrderedDict.

    All dict operations (__getitem__, __setitem__, __contains__) update the
    priority of the relevant key and take O(1) time. The dict is iterated over
    in order from the oldest to newest key, which means that a complete pass
    over the dict should not affect the order of any entries.

    When a new item is set and the maximum size of the cache is exceeded, the
    oldest item is dropped and called with ``on_evict(key, value)``.

    The ``maxsize`` property can be used to view or adjust the capacity of
    the cache, e.g., ``cache.maxsize = new_size``.
    zOrderedDict[K, V]_cacheint_maxsizezthreading.RLock_lockCallable[[K, V], Any] | None	_on_evict)r   r   r   r   Nmaxsizeon_evictc                    t          |t                    st          d          |dk     rt          d          || _        t                      | _        t          j                    | _	        || _
        dS )a  
        Parameters
        ----------
        maxsize : int
            Integer maximum number of items to hold in the cache.
        on_evict : callable, optional
            Function to call like ``on_evict(key, value)`` when items are
            evicted.
        zmaxsize must be an integerr   maxsize must be non-negativeN)
isinstancer   	TypeError
ValueErrorr   r   r   	threadingRLockr   r   )selfr   r   s      9lib/python3.11/site-packages/xarray/backends/lru_cache.py__init__zLRUCache.__init__"   si     '3'' 	:8999Q;;;<<<!mm_&&
!    keyr
   returnr   c                    | j         5  | j        |         }| j                            |           |cd d d            S # 1 swxY w Y   d S N)r   r   move_to_endr   r!   values      r   __getitem__zLRUCache.__getitem__5   s    Z 	 	K$EK##C(((	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s   )>AAcapacityNonec                    t          | j                  |k    rU| j                            d          \  }}| j        |                     ||           t          | j                  |k    SdS dS )z9Shrink the cache if necessary, evicting the oldest items.F)lastN)lenr   popitemr   )r   r)   r!   r'   s       r   _enforce_size_limitzLRUCache._enforce_size_limit<   sr    $+)),,%,88JC~)sE*** $+))))))r    r'   c                   | j         5  || j        v r| j        |= || j        |<   nL| j        r(|                     | j        dz
             || j        |<   n| j        |                     ||           d d d            d S # 1 swxY w Y   d S )N   )r   r   r   r/   r   r&   s      r   __setitem__zLRUCache.__setitem__C   s    Z 	+ 	+dk!!K$#(C   +(():;;;#(C  +sE***	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+s   A)A>>BBc                    | j         |= d S r$   )r   )r   r!   s     r   __delitem__zLRUCache.__delitem__Q   s    Kr    Iterator[K]c                D    t          t          | j                            S r$   )iterlistr   r   s    r   __iter__zLRUCache.__iter__T   s     D%%&&&r    c                *    t          | j                  S r$   )r-   r   r9   s    r   __len__zLRUCache.__len__Y   s    4;r    c                    | j         S )z1Maximum number of items can be held in the cache.)r   r9   s    r   r   zLRUCache.maxsize\   s     }r    sizec                    |dk     rt          d          | j        5  |                     |           || _        ddd           dS # 1 swxY w Y   dS )z9Resize the cache, evicting the oldest items if necessary.r   r   N)r   r   r/   r   )r   r>   s     r   r   zLRUCache.maxsizea   s     !88;<<<Z 	! 	!$$T*** DM	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	!s   AAAr$   )r   r   r   r   )r!   r
   r"   r   )r)   r   r"   r*   )r!   r
   r'   r   r"   r*   )r!   r
   r"   r*   )r"   r5   )r"   r   )r>   r   r"   r*   )__name__
__module____qualname____doc____annotations__	__slots__r   r(   r/   r2   r4   r:   r<   propertyr   setter r    r   r   r      s'          MMM++++<I" " " " "&   + + + ++ + + +   ' ' ' '
           X ^! ! ! ^! ! !r    r   )
__future__r   r   collectionsr   collections.abcr   r   typingr   r   r	   r
   r   r   rH   r    r   <module>rM      s    " " " " " "     # # # # # # 4 4 4 4 4 4 4 4 ) ) ) ) ) ) ) ) ) )GCLLGCLL\! \! \! \! \!~ad# \! \! \! \! \!r    