o
    *\                     @   s~   d Z ddlmZ ddlmZ ddlmZ edg dZG dd deZ	e
 feeeeed	geeeefd
dZdddZd	S )aY  
Memoization decorator that caches a function's return value. If later called
with the same arguments then the cached value is returned rather than
reevaluated.

This is a a python 2.x port of `functools.lru_cache
<http://docs.python.org/3/library/functools.html#functools.lru_cache>`_. If
using python 3.2 or later you should use that instead.
    )
namedtuple)update_wrapper)RLock	CacheInfo)hitsmissesmaxsizecurrsizec                   @   s$   e Zd ZdZefddZdd ZdS )
_HashedSeq	hashvaluec                 C   s   || d d < ||| _ d S Nr   )selftuphash r   5/usr/lib/python3/dist-packages/stem/util/lru_cache.py__init__   s   z_HashedSeq.__init__c                 C   s   | j S r   r   )r   r   r   r   __hash__   s   z_HashedSeq.__hash__N)__name__
__module____qualname__	__slots__r   r   r   r   r   r   r   r
      s    r
   Nc	                    s   | }	|r||  }
|	|7 }	|
D ]}|	|7 }	q|r9|	| fdd| D 7 }	|r5|	| fdd|
D 7 }	t|	S ||	dkrK |	d |v rK|	d S t|	S )zGMake a cache key from optionally typed positional and keyword argumentsc                 3   s    | ]} |V  qd S r   r   ).0vtyper   r   	<genexpr>/   s    z_make_key.<locals>.<genexpr>c                 3   s    | ]	\}} |V  qd S r   r   )r   kr   r   r   r   r   1   s       r   )itemsr
   )argskwdstypedkwd_mark	fasttypessortedtupler   lenkeyZsorted_itemsitemr   r   r   	_make_key#   s   
r+   d   Fc                    s    fdd}|S )a  Least-recently-used cache decorator.

    If *maxsize* is set to None, the LRU features are disabled and the cache
    can grow without bound.

    If *typed* is True, arguments of different types will be cached separately.
    For example, f(3.0) and f(3) will be treated as distinct calls with
    distinct results.

    Arguments to the cached function must be hashable.

    View the cache statistics named tuple (hits, misses, maxsize, currsize) with
    f.cache_info().  Clear the cache and statistics with f.cache_clear().
    Access the underlying function with f.__wrapped__.

    See:  http://en.wikipedia.org/wiki/Cache_algorithms#Least_Recently_Used

    c                    s  t  ddgd\ t
jtt 	g d d gd d < gd\dkr7fdd}n(d u rJ 
f	dd}n 	
fdd} 	fdd	}	fd
d}|_||_||_t|S )Nr   )r   r   )r   r         c                     s"   | i |}   d7  < |S Nr   r   )r!   r"   result)MISSESstatsuser_functionr   r   wrapper`   s   z7lru_cache.<locals>.decorating_function.<locals>.wrapperc                     s\   | |}|}|ur   d7  < |S | i |}||<   d7  < |S r/   r   )r!   r"   r)   r0   )	HITSr1   cache	cache_getmake_keyrootr2   r#   r3   r   r   r4   h   s   
c                     s  |sr

| |n| }	F |}|d urM\}|\}}}}||< ||< | }| |< |< ||< ||<    d7  < |W  d    S W d    n1 sWw   Y  | i |}	[ \}|v rnnBkr|}	||	< ||	< |	  }d< | }
d  |< |< |
= |	|< n| }||||g}| |<  |< |<   d7  < W d    |S 1 sw   Y  |S )Nr   r   r   )r!   r"   r)   linkr9   Z	link_prevZ	link_nextr0   ZlastZoldrootZoldkey)r5   KEYr1   NEXTPREVRESULT_lenr6   r7   lockr8   r   nonlocal_rootr2   r#   r3   r   r   r4   v   sN   

c                      sB    t    tW  d   S 1 sw   Y  dS )zReport cache statisticsN)
_CacheInfor(   r   )r5   r1   r6   r@   r   r2   r   r   
cache_info   s   $z:lru_cache.<locals>.decorating_function.<locals>.cache_infoc                     s^   #     d } | | ddg| dd< ddgdd< W d   dS 1 s(w   Y  dS )z$Clear the cache and cache statisticsr   N)clear)r9   )r6   r@   rA   r2   r   r   cache_clear   s   "z;lru_cache.<locals>.decorating_function.<locals>.cache_clear)	dictr+   getr(   r   __wrapped__rC   rE   r   )r3   r4   rC   rE   r   r#   )r5   r;   r1   r<   r=   r>   r?   r6   r7   r@   r8   rA   r9   r2   r3   r   decorating_functionP   s,   *.
z&lru_cache.<locals>.decorating_functionr   )r   r#   rJ   r   rI   r   	lru_cache7   s   frK   )r,   F)__doc__collectionsr   	functoolsr   Z	threadingr   rB   listr
   objectsetintstr	frozensetr   r&   r'   r(   r+   rK   r   r   r   r   <module>   s   

