a
    zn„fR  ã                   @  sP   d Z ddlmZ ddlZddlmZ ddlZG dd„ deƒZG dd„ deƒZdS )	a„  
This module defines simple, thread-safe cache classes satisfying the
``MutableMapping`` protocol.

The :doc:`uproot.cache.LRUCache` implements a least-recently used eviction
policy that limits the number of items in the cache (used as an
``object_cache``).

The :doc:`uproot.cache.LRUArrayCache` implements the same policy, limiting the
total number of bytes, as reported by ``nbytes``.
é    )ÚannotationsN)ÚMutableMappingc                   @  s”   e Zd ZdZedd„ ƒZdd„ Zdd„ Zdd	„ Zd
d„ Z	e
dd„ ƒZe
dd„ ƒZdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zd S )!ÚLRUCachea±  
    Args:
        limit (None or int): Number of objects to allow in the cache before
            evicting the least-recently used. If None, this cache never evicts.

    LRUCache is a ``MutableMapping`` that evicts the least-recently used
    objects when the ``current`` number of objects exceeds the ``limit``.

    Get and set (or explicitly remove) items with ``MutableMapping`` syntax:
    square bracket subscripting.

    LRUCache is thread-safe for all options: getting, setting, deleting,
    iterating, listing keys, values, and items.

    This cache is insensitive to the size of the objects it stores, and hence
    is a better ``object_cache`` than an ``array_cache``.
    c                 C  s   dS )zL
        The "size of" an object in this cache is always exactly 1.
        é   © )ÚclsÚobjr   r   úO/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/uproot/cache.pyÚsizeof)   s    zLRUCache.sizeofc                 C  s&   || _ d| _g | _i | _t ¡ | _d S ©Nr   ©Ú_limitÚ_currentÚ_orderÚ_dataÚ	threadingÚLockÚ_lock©ÚselfÚlimitr   r   r	   Ú__init__0   s
    zLRUCache.__init__c                 C  s
   d| j iS )Nr   ©r   ©r   r   r   r	   Ú__getstate__7   s    ÿzLRUCache.__getstate__c                 C  s*   |d | _ d| _g | _i | _t ¡ | _d S )Nr   r   r   )r   Ústater   r   r	   Ú__setstate__<   s
    
zLRUCache.__setstate__c                 C  s>   | j d u rd}nd| j› d| j › d�}d|› dt| ƒd›d�S )	Nú
(no limit)ú(ú/z full)z
<LRUCache ú at 0xÚ012xú>©r   r   Úidr   r   r   r	   Ú__repr__C   s    
zLRUCache.__repr__c                 C  s   | j S )zŒ
        Number of objects to allow in the cache before evicting the
        least-recently used. If None, this cache never evicts.
        r   r   r   r   r	   r   J   s    zLRUCache.limitc                 C  s   | j S )z7
        Current number of items in the cache.
        ©r   r   r   r   r	   ÚcurrentR   s    zLRUCache.currentc                 C  s4   | j � t| jƒW  d  ƒ S 1 s&0    Y  dS )aQ  
        Returns a copy of the keys currently in the cache, in least-recently
        used order.

        The list ascends from least-recently used to most-recently used: index
        ``0`` is the least-recently used and index ``-1`` is the most-recently
        used.

        (Calling this method does not change the order.)
        N©r   Úlistr   r   r   r   r	   ÚkeysY   s    zLRUCache.keysc                   s>   ˆ j �$ ‡ fdd„ˆ jD ƒW  d  ƒ S 1 s00    Y  dS )aS  
        Returns a copy of the values currently in the cache, in least-recently
        used order.

        The list ascends from least-recently used to most-recently used: index
        ``0`` is the least-recently used and index ``-1`` is the most-recently
        used.

        (Calling this method does not change the order.)
        c                   s   g | ]}ˆ j | ‘qS r   ©r   ©Ú.0Úwherer   r   r	   Ú
<listcomp>s   ó    z#LRUCache.values.<locals>.<listcomp>N©r   r   r   r   r   r	   Úvaluesg   s    zLRUCache.valuesc                   s>   ˆ j �$ ‡ fdd„ˆ jD ƒW  d  ƒ S 1 s00    Y  dS )aR  
        Returns a copy of the items currently in the cache, in least-recently
        used order.

        The list ascends from least-recently used to most-recently used: index
        ``0`` is the least-recently used and index ``-1`` is the most-recently
        used.

        (Calling this method does not change the order.)
        c                   s   g | ]}|ˆ j | f‘qS r   r+   r,   r   r   r	   r/   �   r0   z"LRUCache.items.<locals>.<listcomp>Nr1   r   r   r   r	   Úitemsu   s    zLRUCache.itemsc                 C  sP   | j �6 | j| }| j |¡ | j |¡ |W  d   ƒ S 1 sB0    Y  d S ©N)r   r   r   ÚremoveÚappend)r   r.   Úoutr   r   r	   Ú__getitem__ƒ   s
    
zLRUCache.__getitem__c                 C  s¾   | j �¤ || jv r| j |¡ | j |¡ || j|< |  j|  |¡7  _| jd urœ| j| jkrœt| jƒdkrœ| j 	d¡}|  j|  | j| ¡8  _| j|= qRW d   ƒ n1 s°0    Y  d S r   )
r   r   r   r5   r6   r   r
   r   ÚlenÚpop)r   r.   ÚwhatÚkeyr   r   r	   Ú__setitem__Š   s    


zLRUCache.__setitem__c                 C  sX   | j �> |  j|  | j| ¡8  _| j|= | j |¡ W d   ƒ n1 sJ0    Y  d S r4   )r   r   r
   r   r   r5   )r   r.   r   r   r	   Ú__delitem__˜   s    zLRUCache.__delitem__c                 c  s>   | j � t| jƒ}W d   ƒ n1 s&0    Y  |E d H  d S r4   r(   )r   Úorderr   r   r	   Ú__iter__ž   s    (zLRUCache.__iter__c                 C  s4   | j � t| jƒW  d   ƒ S 1 s&0    Y  d S r4   )r   r9   r   r   r   r   r	   Ú__len__£   s    zLRUCache.__len__N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Úclassmethodr
   r   r   r   r%   Úpropertyr   r'   r*   r2   r3   r8   r=   r>   r@   rA   r   r   r   r	   r      s&   


r   c                      sP   e Zd ZdZdZedd„ ƒZ‡ fdd„Zdd„ Ze	d	d
„ ƒZ
e	dd„ ƒZ‡  ZS )ÚLRUArrayCachea  
    Args:
        limit_bytes (None, int, or str): Amount of data to allow in the cache
            before evicting the least-recently used. An integer is interpreted
            as a number of bytes and a string must be a number followed by a
            unit, such as "100 MB". If None, this cache never evicts.

    LRUArrayCache is a ``MutableMapping`` that evicts the least-recently used
    objects when the ``current`` number of bytes exceeds the ``limit``. The
    size of an object is taken from its ``nbytes`` attribute. If it does not
    have an ``nbytes``, it is presumed to have ``default_nbytes``.

    Get and set (or explicitly remove) items with ``MutableMapping`` syntax:
    square bracket subscripting.

    LRUArrayCache is thread-safe for all options: getting, setting, deleting,
    iterating, listing keys, values, and items.

    This cache is sensitive to the size of the objects it stores, but only if
    those objects have meaningful ``nbytes``. It is therefore a better
    ``array_cache`` than an ``array_cache``.
    i   c                 C  sN   t |dƒr|jS t |dƒrHt|jƒrH| ¡ }t |dƒrHt|jƒrH| ¡ S | jS )a  
        The "size of" an object in this cache is its

        - ``nbytes`` attribute/property if it has one (NumPy and Awkward Arrays,
          Pandas Series),
        - ``memory_usage().sum()`` if it exists (Pandas DataFrames),
        - ``default_nbytes`` otherwise.
        ÚnbytesÚmemory_usageÚsum)ÚhasattrrI   ÚcallablerJ   rK   Údefault_nbytes)r   r;   Útmpr   r   r	   r
   Â   s    

zLRUArrayCache.sizeofc                   s(   |d u rd n
t j |¡}tƒ  |¡ d S r4   )ÚuprootZ_utilZmemory_sizeÚsuperr   )r   Zlimit_bytesr   ©Ú	__class__r   r	   r   Ô   s    zLRUArrayCache.__init__c                 C  s>   | j d u rd}nd| j› d| j › d�}d|› dt| ƒd›d�S )	Nr   r   r   z bytes full)z<LRUArrayCache r    r!   r"   r#   r   r   r   r	   r%   Ø   s    
zLRUArrayCache.__repr__c                 C  s   | j S )zŠ
        Number of bytes to allow in the cache before evicting the
        least-recently used. If None, this cache never evicts.
        r   r   r   r   r	   r   ß   s    zLRUArrayCache.limitc                 C  s   | j S )z7
        Current number of bytes in the cache.
        r&   r   r   r   r	   r'   ç   s    zLRUArrayCache.current)rB   rC   rD   rE   rN   rF   r
   r   r%   rG   r   r'   Ú__classcell__r   r   rR   r	   rH   ¨   s   

rH   )	rE   Ú
__future__r   r   Úcollections.abcr   rP   r   rH   r   r   r   r	   Ú<module>   s    