Util--  1.1
Public Member Functions | Protected Member Functions | List of all members
utilmm::cached_fn< Arg, Ret, Hash, Equal > Class Template Referenceabstract

A functor with cache. More...

#include <utilmm/functional/cached_fn.hh>

Inheritance diagram for utilmm::cached_fn< Arg, Ret, Hash, Equal >:
Inheritance graph
[legend]
Collaboration diagram for utilmm::cached_fn< Arg, Ret, Hash, Equal >:
Collaboration graph
[legend]

Public Member Functions

 cached_fn ()
 Default constructor. More...
 
virtual ~cached_fn ()
 Destructor. More...
 
Ret const & operator() (arg_type arg)
 Call operator. More...
 
void empty_cache ()
 clear the cache More...
 
size_t cache_size () const
 Cache size. More...
 

Protected Member Functions

virtual Ret call (Arg const &arg)=0
 the real function. More...
 

Detailed Description

template<typename Arg, typename Ret, class Hash = hash<Arg>, class Equal = std::equal_to<Arg>>
class utilmm::cached_fn< Arg, Ret, Hash, Equal >

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.

Parameters
ArgThe argument type of the functor
RetThe return type of the functor
Hashhashing functor used to hash argument keys
Equalequality 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.

Author
Frédéric Py fpy@l.nosp@m.aas..nosp@m.fr
Todo:
Current implementation uses inheritance polymorphism and may be more efficient to use static polymorphism to ease the encapsulation of non cached functors into this cached one.
Todo:
It could be interresting to generalize the argument number of this functor using boost::tuple or another boost utility

Constructor & Destructor Documentation

template<typename Arg , typename Ret , class Hash = hash<Arg>, class Equal = std::equal_to<Arg>>
utilmm::cached_fn< Arg, Ret, Hash, Equal >::cached_fn ( )
inline

Default constructor.

Create a new instance with an empty cache

template<typename Arg , typename Ret , class Hash = hash<Arg>, class Equal = std::equal_to<Arg>>
virtual utilmm::cached_fn< Arg, Ret, Hash, Equal >::~cached_fn ( )
inlinevirtual

Member Function Documentation

template<typename Arg , typename Ret , class Hash = hash<Arg>, class Equal = std::equal_to<Arg>>
size_t utilmm::cached_fn< Arg, Ret, Hash, Equal >::cache_size ( ) const
inline

Cache size.

Returns
the number of calls stored in cache

References utilmm::hash_map< Key, Data, Hash, Equal >::size().

template<typename Arg , typename Ret , class Hash = hash<Arg>, class Equal = std::equal_to<Arg>>
virtual Ret utilmm::cached_fn< Arg, Ret, Hash, Equal >::call ( Arg const &  arg)
protectedpure 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.

Parameters
argThe argument of the function
Returns
the result of the function
template<typename Arg , typename Ret , class Hash = hash<Arg>, class Equal = std::equal_to<Arg>>
void utilmm::cached_fn< Arg, Ret, Hash, Equal >::empty_cache ( )
inline

clear the cache

Thsi function clear the cache

Postcondition
The cache is empty

References utilmm::hash_map< Key, Data, Hash, Equal >::clear().

template<typename Arg , typename Ret , class Hash = hash<Arg>, class Equal = std::equal_to<Arg>>
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

Parameters
argThe argument of the function
Returns
The value attached to arg
Postcondition
The cache is not empty
See also
call(Arg const &arg)

Referenced by utilmm::cached_fn< Arg, Ret, Hash, Equal >::~cached_fn().


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

Generated on Mon Oct 8 2018 08:38:45 for Util-- by doxygen 1.8.11
SourceForge.net Project Page