Stroika Library 3.0d16
 
Loading...
Searching...
No Matches
Stroika::Foundation::Cache::Memoizer< RESULT, CACHE, ARGS > Class Template Reference

Cache the results of expensive computations transparently. More...

#include <Memoizer.h>

Public Member Functions

 Memoizer (const function< RESULT(ARGS...)> &f, CACHE< tuple< ARGS... >, RESULT > &&cache=CACHE< tuple< ARGS... >, RESULT >{})
 
nonvirtual RESULT operator() (ARGS... args)
 

Detailed Description

template<typename RESULT, template< typename, typename > class CACHE = LRUCache, typename... ARGS>
class Stroika::Foundation::Cache::Memoizer< RESULT, CACHE, ARGS >

Cache the results of expensive computations transparently.

See also
https://en.wikipedia.org/wiki/Memoization

TODO: o

o

Note
Memoizer works well wtih LRUCache, SynchronizedLRUCache, TimedCache, or SynchronizedTimeCache. But it does NOT work with CallerStalenessCache, because that cache requires the caller to specify an allowed staleness on each call.
Thread-Safety Same as (worse case of) underlying CACHE template argument, and argument function. Since the function will typically be fully reentrant, this comes down to the re-entrancy of the argument Cache. Used with SynchronizedLRUCache and a typical function, for example, this is fully re-entrant

Definition at line 55 of file Memoizer.h.

Constructor & Destructor Documentation

◆ Memoizer()

template<typename RESULT , template< typename, typename > class CACHE, typename... ARGS>
Stroika::Foundation::Cache::Memoizer< RESULT, CACHE, ARGS >::Memoizer ( const function< RESULT(ARGS...)> &  f,
CACHE< tuple< ARGS... >, RESULT > &&  cache = CACHE<tuple<ARGS...>, RESULT>{} 
)
Note
see Tests use of qCompilerAndStdLib_template_template_argument_as_different_template_paramters_Buggy if you get the message "template template argument has different template parameters than its corresponding template template parameter"
Example Usage
unsigned int totalCallsCount{};
EXPECT_TRUE (memoizer (1, 1) == 2 and totalCallsCount == 1);
EXPECT_TRUE (memoizer (1, 1) == 2 and totalCallsCount == 1);
LRUCache implements a simple least-recently-used caching strategy, with optional hashing (of keys) to...
Definition LRUCache.h:94

Definition at line 14 of file Memoizer.inl.

Member Function Documentation

◆ operator()()

template<typename RESULT , template< typename, typename > class CACHE, typename... ARGS>
RESULT Stroika::Foundation::Cache::Memoizer< RESULT, CACHE, ARGS >::operator() ( ARGS...  args)
Note
this function is not const, because it modifies the state of the object/cache.

Definition at line 20 of file Memoizer.inl.


The documentation for this class was generated from the following files: