a
    İa                     @   s`   d dl Z d dlmZ d dlmZmZmZmZmZm	Z	 e	dZ
e	dZG dd dee
ef ZdS )    N)OrderedDict)AnyCallableIteratorMutableMappingOptionalTypeVarKVc                   @   s   e Zd ZU dZded< eed< ejed< ee	e
egef  ed< dZd ee	e
egef d	d
dZe
edddZeddddZe
eddddZe
ddddZee
 dddZedddZeedddZejedd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_maxsize_lock	_on_evict)r   r   r   r   N)maxsizeon_evictc                 C   sD   t |tstd|dk r"td|| _t | _t | _	|| _
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)
isinstanceint	TypeError
ValueErrorr   r   r   	threadingRLockr   r   )selfr   r    r   8lib/python3.9/site-packages/xarray/backends/lru_cache.py__init__   s    


zLRUCache.__init__)keyreturnc                 C   sD   | j * | j| }| j| |W  d    S 1 s60    Y  d S N)r   r   move_to_endr   r   valuer   r   r   __getitem__2   s    
zLRUCache.__getitem__)capacityr   c                 C   s<   t | j|kr8| jjdd\}}| jdur | || q dS )z9Shrink the cache if necessary, evicting the oldest items.F)ZlastN)lenr   popitemr   )r   r$   r   r"   r   r   r   _enforce_size_limit9   s    
zLRUCache._enforce_size_limit)r   r"   r   c                 C   s   | j f || jv r&| j|= || j|< n8| jrH| | jd  || j|< n| jd ur^| || W d    n1 sr0    Y  d S )N   )r   r   r   r'   r   r!   r   r   r   __setitem__@   s    

zLRUCache.__setitem__c                 C   s   | j |= d S r   )r   )r   r   r   r   r   __delitem__N   s    zLRUCache.__delitem__)r   c                 C   s   t t| jS r   )iterlistr   r   r   r   r   __iter__Q   s    zLRUCache.__iter__c                 C   s
   t | jS r   )r%   r   r-   r   r   r   __len__V   s    zLRUCache.__len__c                 C   s   | j S )z1Maximum number of items can be held in the cache.)r   r-   r   r   r   r   Y   s    zLRUCache.maxsize)sizer   c                 C   sJ   |dk rt d| j  | | || _W d   n1 s<0    Y  dS )z9Resize the cache, evicting the oldest items if necessary.r   r   N)r   r   r'   r   )r   r0   r   r   r   r   ^   s
    
)N)__name__
__module____qualname____doc____annotations__r   r   r   r   r   r	   r
   r   	__slots__r   r#   r'   r)   r*   r   r.   r/   propertyr   setterr   r   r   r   r   	   s"   

r   )r   collectionsr   typingr   r   r   r   r   r   r	   r
   r   r   r   r   r   <module>   s
    