utilmm::cached_fn< Arg, Ret, Hash, Equal > Class Template Reference

A functor with cache. More...

#include <utilmm/functional/cached_fn.hh>

List of all members.

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.


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:
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.

Author:
Frédéric Py <fpy@laas.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 (  )  [inline, virtual]

Destructor.


Member Function Documentation

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  )  [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.

Parameters:
arg The 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>>
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:
arg The argument of the function
Returns:
The value attached to arg
Postcondition:
The cache is not empty
See also:
call(Arg const &arg)

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

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


The documentation for this class was generated from the following file:
Generated on Tue Feb 19 10:51:01 2008 for Util-- by doxygen 1.5.3
SourceForge.net Project Page