$search

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

A functor with cache. More...

#include <cached_fn.hh>

List of all members.

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

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

Definition at line 60 of file cached_fn.hh.


Member Typedef Documentation

template<typename Arg , typename Ret , class Hash = hash<Arg>, class Equal = std::equal_to<Arg>>
typedef arg_traits<Arg>::type utilmm::cached_fn< Arg, Ret, Hash, Equal >::arg_type [private]

Definition at line 64 of file cached_fn.hh.

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


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

Definition at line 85 of file cached_fn.hh.

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.

Definition at line 87 of file cached_fn.hh.


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

Definition at line 119 of file cached_fn.hh.

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>>
void utilmm::cached_fn< Arg, Ret, Hash, Equal >::empty_cache (  )  [inline]

clear the cache

Thsi function clear the cache

Postcondition:
The cache is empty

Definition at line 111 of file cached_fn.hh.

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)

Member Data Documentation

template<typename Arg , typename Ret , class Hash = hash<Arg>, class Equal = std::equal_to<Arg>>
cache_type utilmm::cached_fn< Arg, Ret, Hash, Equal >::the_cache [private]

Definition at line 66 of file cached_fn.hh.


The documentation for this class was generated from the following file:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines


utilmm
Author(s): Sylvain Joyeux/sylvain.joyeux@m4x.org
autogenerated on Fri Mar 1 16:28:52 2013