cp-library

This documentation is automatically generated by online-judge-tools/verification-helper

View the Project on GitHub kobejean/cp-library

:warning: cp_library/misc/decorators/list_cache_fn.py

Code

from functools import wraps
from typing import Any, Callable, TypeVar

F = TypeVar('F', bound=Callable[..., Any])

def list_cache(max_size: int = 16):
    def decorator(func: F) -> F:
        K, V = [], []
        @wraps(func)
        def wrapper(arg):
            if len(K) > max_size: return func(arg)
            try:
                return V[K.index(arg)]
            except:
                result = func(arg)
                K.append(arg); V.append(result)
                return result
        return wrapper
    return decorator
from functools import wraps
from typing import Any, Callable, TypeVar

F = TypeVar('F', bound=Callable[..., Any])

def list_cache(max_size: int = 16):
    def decorator(func: F) -> F:
        K, V = [], []
        @wraps(func)
        def wrapper(arg):
            if len(K) > max_size: return func(arg)
            try:
                return V[K.index(arg)]
            except:
                result = func(arg)
                K.append(arg); V.append(result)
                return result
        return wrapper
    return decorator
Back to top page