#include <cached_fn.hh>
Public Member Functions | |
size_t | cache_size () const |
Cache size. | |
cached_fn () | |
Default constructor. | |
void | empty_cache () |
clear the cache | |
Ret const & | operator() (arg_type arg) |
Call operator. | |
virtual | ~cached_fn () |
Destructor. | |
Protected Member Functions | |
virtual Ret | call (Arg const &arg)=0 |
the real function. | |
Private Types | |
typedef arg_traits< Arg >::type | arg_type |
typedef hash_map< Arg, Ret, Hash, Equal > | cache_type |
Private Attributes | |
cache_type | the_cache |
A functor with cache.
This class is the basis for functors needing a cache. It allows programmer to implement easily a functor that will use a cache to store previously computed results. It can be usefull when the function is complex and may be called many times with the same argument.
Arg | The argument type of the functor |
Ret | The return type of the functor |
Hash | hashing functor used to hash argument keys |
Equal | equality test for arguments |
The cache of this functor is based on a utilmm::hash_map
with Arg as key and ret as attached value.
A small example illustrating the way to use it and showing an alternative way to use it is the id generator :
#include "utilmm/cached_fn.hh" template< class Obj, class Hash=utilmm::hash<Obj>, class Eq = std::equal_to<Obj> > class id_gen :public cached_fn<Obj, size_t, Hash, Eq> { private: typedef cached_fn<Obj, size_t, Hash, Eq>::mother_class; size_t call(Obj const &arg) { return mother_class::cache_size()+1; } };
This functor will then attach a unique id to an Obj
instance and will return this id until will clear the cache.
Definition at line 60 of file cached_fn.hh.
typedef arg_traits<Arg>::type utilmm::cached_fn< Arg, Ret, Hash, Equal >::arg_type [private] |
Definition at line 64 of file cached_fn.hh.
typedef hash_map<Arg, Ret, Hash, Equal> utilmm::cached_fn< Arg, Ret, Hash, Equal >::cache_type [private] |
Definition at line 63 of file cached_fn.hh.
utilmm::cached_fn< Arg, Ret, Hash, Equal >::cached_fn | ( | ) | [inline] |
Default constructor.
Create a new instance with an empty cache
Definition at line 85 of file cached_fn.hh.
virtual utilmm::cached_fn< Arg, Ret, Hash, Equal >::~cached_fn | ( | ) | [inline, virtual] |
Destructor.
Definition at line 87 of file cached_fn.hh.
size_t utilmm::cached_fn< Arg, Ret, Hash, Equal >::cache_size | ( | ) | const [inline] |
Cache size.
Definition at line 119 of file cached_fn.hh.
virtual Ret utilmm::cached_fn< Arg, Ret, Hash, Equal >::call | ( | Arg const & | arg | ) | [protected, pure virtual] |
the real function.
This pure virtual function will embed the function code. It is called by operator()
if and only if the cache does not include any cached result for arg.
arg | The argument of the function |
void utilmm::cached_fn< Arg, Ret, Hash, Equal >::empty_cache | ( | ) | [inline] |
clear the cache
Thsi function clear the cache
Definition at line 111 of file cached_fn.hh.
Ret const& utilmm::cached_fn< Arg, Ret, Hash, Equal >::operator() | ( | arg_type | arg | ) |
Call operator.
This is the public interface used to call the function. It will check if the function was allready called with this argument. If it was called it returns the value in cache else it compute the new result and store it in cache
arg | The argument of the function |
cache_type utilmm::cached_fn< Arg, Ret, Hash, Equal >::the_cache [private] |
Definition at line 66 of file cached_fn.hh.