ó
X–Tc           @   sL   d  Z  d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d S(   s   This module contains classes to handle memoization, a time-saving method that
caches previously seen results from function calls.t   Memoizec           B   s    e  Z d  Z d „  Z d „  Z RS(   s
  
    This is a decorator to allow memoization of function calls. It is a
    completely dumb cache, and will cache anything given to it indefinitely.

    :param object func: Any function (although any object will work).
    .. warning:: This may be deprecated.
    c         C   s   | |  _  i  |  _ d  S(   N(   t   funct   cache(   t   selfR   (    (    s2   /usr/lib/python2.7/site-packages/spyral/memoize.pyt   __init__   s    	c         G   sc   y |  j  | SWnM t k
 r? |  j | Œ  } | |  j  | <| St k
 r^ d GH|  j | Œ  SXd S(   s¶   
        Attempts to return the results of this function call from the cache;
        if it can't find it, then it will execute the function and add it to the
        cache.
        sK   WARNING: Unhashable type passed to memoize.Reconsider using this decorator.N(   R   t   KeyErrorR   t	   TypeError(   R   t   argst   res(    (    s2   /usr/lib/python2.7/site-packages/spyral/memoize.pyt   __call__   s    (   t   __name__t
   __module__t   __doc__R   R	   (    (    (    s2   /usr/lib/python2.7/site-packages/spyral/memoize.pyR       s   	t   SmartMemoizec           B   s    e  Z d  Z d „  Z d „  Z RS(   s  
    This is a decorator to allow memoization of function calls. Its cache
    is cleared on scene changes, and also clears items from the cache which
    haven't been used in at least 250 frames.

    :param object func: Any function (although any object will work).
    c         C   s(   | |  _  i  |  _ d  |  _ d |  _ d  S(   Ni    (   R   R   t   Nonet   scenet
   last_clear(   R   R   (    (    s2   /usr/lib/python2.7/site-packages/spyral/memoize.pyR   )   s    			c   	      G   sB  d d l  m } | j ƒ  } | j ƒ  |  j k	 rL | j ƒ  |  _ i  |  _ n  | |  j d k rº xL |  j j ƒ  D]; \ } } | \ } } | | d k ro |  j j | ƒ qo qo W| |  _ n  y. |  j | \ } } | | f |  j | <| SWnS t	 k
 r|  j
 | Œ  } | | f |  j | <| St k
 r=d GH|  j
 | Œ  SXd S(   s¶   
        Attempts to return the results of this function call from the cache;
        if it can't find it, then it will execute the function and add it to the
        cache.
        iÿÿÿÿ(   t   directorid   iú   sO   WARNING: Unhashable type passed to SmartMemoize.Reconsider using this decoratorN(   t   spyralR   t   get_tickt	   get_sceneR   R   R   t   itemst   popR   R   R   (	   R   R   R   t   framet   keyt   valuet   datat   oldframeR   (    (    s2   /usr/lib/python2.7/site-packages/spyral/memoize.pyR	   /   s,    (   R
   R   R   R   R	   (    (    (    s2   /usr/lib/python2.7/site-packages/spyral/memoize.pyR   !   s   	t   _ImageMemoizec           B   s   e  Z d  Z d „  Z RS(   sŸ   
    A subclass of SmartMemoise that is built explicitly for image related calls.
    It allows images to be cleared from its cache when they are updated.
    c            s,   t  ‡  f d †  |  j j ƒ  Dƒ ƒ |  _ d S(   s£   
        Removes the given image from the cache.
        :param clear_image: The image to remove.
        :type clear_image: :class:`Image <spyral.Image>`
        c         3   s-   |  ]# \ } } | ˆ  k r | | f Vq d  S(   N(    (   t   .0t   imaget   scale(   t   clear_image(    s2   /usr/lib/python2.7/site-packages/spyral/memoize.pys	   <genexpr>X   s    	N(   t   dictR   t	   iteritems(   R   R    (    (   R    s2   /usr/lib/python2.7/site-packages/spyral/memoize.pyt   clearR   s    (   R
   R   R   R#   (    (    (    s2   /usr/lib/python2.7/site-packages/spyral/memoize.pyR   M   s   N(   R   t   objectR    R   R   (    (    (    s2   /usr/lib/python2.7/site-packages/spyral/memoize.pyt   <module>   s   ,