#include <utilmm/functional/cached_fn.hh>
Public Member Functions | |
| cached_fn () | |
| Default constructor. | |
| virtual | ~cached_fn () |
| Destructor. | |
| Ret const & | operator() (arg_type arg) |
| Call operator. | |
| void | empty_cache () |
| clear the cache | |
| size_t | cache_size () const |
| Cache size. | |
Protected Member Functions | |
| virtual Ret | call (Arg const &arg)=0 |
| the real function. | |
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 |
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.
| utilmm::cached_fn< Arg, Ret, Hash, Equal >::cached_fn | ( | ) | [inline] |
Default constructor.
Create a new instance with an empty cache
| virtual utilmm::cached_fn< Arg, Ret, Hash, Equal >::~cached_fn | ( | ) | [inline, virtual] |
Destructor.
| 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 |
| 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 |
| void utilmm::cached_fn< Arg, Ret, Hash, Equal >::empty_cache | ( | ) | [inline] |
clear the cache
Thsi function clear the cache
| size_t utilmm::cached_fn< Arg, Ret, Hash, Equal >::cache_size | ( | ) | const [inline] |
Cache size.
1.5.3