#ifndef ROOT_Math_Functor
#define ROOT_Math_Functor
#ifndef ROOT_Math_IFunction
#include "Math/IFunction.h"
#endif
#include <memory>
namespace ROOT {
namespace Math {
template<class ParentFunctor, class Func >
class FunctorHandler : public ParentFunctor::Impl {
typedef typename ParentFunctor::Impl ImplFunc;
typedef typename ImplFunc::BaseFunc BaseFunc;
public:
FunctorHandler(const Func & fun) : fDim(1), fFunc(fun) {}
FunctorHandler(unsigned int dim, const Func & fun ) :
fDim(dim),
fFunc(fun)
{}
BaseFunc * Clone() const {
return new FunctorHandler(*this);
}
unsigned int NDim() const {
return fDim;
}
private :
inline double DoEval (double x) const {
return fFunc(x);
}
inline double DoEval (const double * x) const {
return fFunc(x);
}
inline double DoDerivative (double x) const {
return fFunc.Derivative(x);
}
inline double DoDerivative (const double * x, unsigned int icoord ) const {
return fFunc.Derivative(x,icoord);
}
unsigned int fDim;
mutable Func fFunc;
};
template<class ParentFunctor, class Func, class GradFunc >
class FunctorGradHandler : public ParentFunctor::Impl {
typedef typename ParentFunctor::Impl ImplFunc;
typedef typename ImplFunc::BaseFunc BaseFunc;
public:
FunctorGradHandler(const Func & fun, const GradFunc & gfun) :
fDim(1),
fFunc(fun),
fGradFunc(gfun)
{}
FunctorGradHandler(unsigned int dim, const Func & fun, const GradFunc & gfun) :
fDim(dim),
fFunc(fun),
fGradFunc( gfun )
{}
BaseFunc * Clone() const { return new FunctorGradHandler(*this); }
unsigned int NDim() const {
return fDim;
}
private :
inline double DoEval (double x) const {
return fFunc(x);
}
inline double DoEval (const double * x) const {
return fFunc(x);
}
inline double DoDerivative (double x) const {
return fGradFunc(x);
}
inline double DoDerivative (const double * x, unsigned int icoord ) const {
return fGradFunc(x, icoord);
}
unsigned int fDim;
mutable Func fFunc;
mutable GradFunc fGradFunc;
};
template <class ParentFunctor, typename PointerToObj,
typename PointerToMemFn>
class MemFunHandler : public ParentFunctor::Impl
{
typedef typename ParentFunctor::Impl ImplFunc;
typedef typename ImplFunc::BaseFunc BaseFunc;
public:
MemFunHandler(const PointerToObj& pObj, PointerToMemFn pMemFn)
: fDim(1), fObj(pObj), fMemFn(pMemFn)
{}
MemFunHandler(unsigned int dim, const PointerToObj& pObj, PointerToMemFn pMemFn)
: fDim(dim), fObj(pObj), fMemFn(pMemFn)
{}
BaseFunc * Clone() const { return new MemFunHandler(*this); }
unsigned int NDim() const {
return fDim;
}
private :
inline double DoEval (double x) const {
return ((*fObj).*fMemFn)(x);
}
inline double DoEval (const double * x) const {
return ((*fObj).*fMemFn)(x);
}
unsigned int fDim;
mutable PointerToObj fObj;
PointerToMemFn fMemFn;
};
template <class ParentFunctor, typename PointerToObj,
typename PointerToMemFn, typename PointerToGradMemFn>
class MemGradFunHandler : public ParentFunctor::Impl
{
typedef typename ParentFunctor::Impl ImplFunc;
typedef typename ImplFunc::BaseFunc BaseFunc;
public:
MemGradFunHandler(const PointerToObj& pObj, PointerToMemFn pMemFn, PointerToGradMemFn pGradMemFn)
: fDim(1),
fObj(pObj),
fMemFn(pMemFn),
fGradMemFn(pGradMemFn)
{}
MemGradFunHandler(unsigned int dim,
const PointerToObj& pObj,
PointerToMemFn pMemFn,
PointerToGradMemFn pGradMemFn )
: fDim(dim),
fObj(pObj),
fMemFn(pMemFn),
fGradMemFn(pGradMemFn)
{}
BaseFunc * Clone() const { return new MemGradFunHandler(*this); }
unsigned int NDim() const {
return fDim;
}
private :
inline double DoEval (double x) const {
return ((*fObj).*fMemFn)(x);
}
inline double DoEval (const double * x) const {
return ((*fObj).*fMemFn)(x);
}
inline double DoDerivative (double x) const {
return ((*fObj).*fGradMemFn)(x);
}
inline double DoDerivative (const double * x, unsigned int icoord ) const {
return ((*fObj).*fGradMemFn)(x,icoord);
}
unsigned int fDim;
mutable PointerToObj fObj;
PointerToMemFn fMemFn;
PointerToGradMemFn fGradMemFn;
};
#if defined(__MAKECINT__) || defined(G__DICTIONARY)
template<class ParentFunctor>
class FunctorHandler<ParentFunctor,TRootIOCtor *> : public ParentFunctor::Impl
{
public:
typedef typename ParentFunctor::Impl ImplFunc;
typedef typename ImplFunc::BaseFunc BaseFunc;
FunctorHandler(TRootIOCtor *) {}
double DoEval (double ) const { return 0; }
double DoDerivative (double ) const { return 0; }
BaseFunc * Clone() const { return 0; }
};
#endif
class Functor : public IBaseFunctionMultiDim {
public:
typedef IBaseFunctionMultiDim Impl;
typedef IBaseFunctionMultiDim::BaseFunc ImplBase;
Functor () : fImpl(0) {}
template <class PtrObj, typename MemFn>
Functor(const PtrObj& p, MemFn memFn, unsigned int dim )
: fImpl(new MemFunHandler<Functor, PtrObj, MemFn>(dim, p, memFn))
{}
template <typename Func>
Functor( const Func & f, unsigned int dim ) :
fImpl(new FunctorHandler<Functor,Func>(dim,f) )
{}
#if defined(__CINT__) || defined(G__DICTIONARY) || defined(MAKE_CINT_FUNCTOR)
Functor(void * p, unsigned int dim, const char * className = 0, const char * methodName = 0);
#endif
virtual ~Functor () {}
#ifndef __CINT__
Functor(const Functor & rhs) :
Impl()
{
if (rhs.fImpl.get() != 0)
fImpl = std::auto_ptr<Impl>( (rhs.fImpl)->Clone() );
}
#endif
Functor & operator = (const Functor & rhs) {
Functor copy(rhs);
Impl * p = fImpl.release();
fImpl.reset(copy.fImpl.release());
copy.fImpl.reset(p);
return *this;
}
ImplBase * Clone() const { return new Functor(*this); }
unsigned int NDim() const { return fImpl->NDim(); }
private :
inline double DoEval (const double * x) const {
return (*fImpl)(x);
}
std::auto_ptr<Impl> fImpl;
};
class Functor1D : public IBaseFunctionOneDim {
public:
typedef IBaseFunctionOneDim Impl;
typedef IBaseFunctionOneDim::BaseFunc ImplBase;
Functor1D () : fImpl(0) {}
template <class PtrObj, typename MemFn>
Functor1D(const PtrObj& p, MemFn memFn)
: fImpl(new MemFunHandler<Functor1D, PtrObj, MemFn>(p, memFn))
{}
template <typename Func>
Functor1D(const Func & f) :
fImpl(new FunctorHandler<Functor1D,Func>(f) )
{}
#if defined(__CINT__) || defined(G__DICTIONARY) || defined(MAKE_CINT_FUNCTOR)
Functor1D(void * p, const char * className = 0, const char * methodName = 0);
#endif
virtual ~Functor1D () {}
#ifndef __CINT__
Functor1D(const Functor1D & rhs) :
Impl()
{
if (rhs.fImpl.get() != 0)
fImpl = std::auto_ptr<Impl>( (rhs.fImpl)->Clone() );
}
#endif
Functor1D & operator = (const Functor1D & rhs) {
Functor1D copy(rhs);
Impl * p = fImpl.release();
fImpl.reset(copy.fImpl.release());
copy.fImpl.reset(p);
return *this;
}
ImplBase * Clone() const { return new Functor1D(*this); }
private :
inline double DoEval (double x) const {
return (*fImpl)(x);
}
std::auto_ptr<Impl> fImpl;
};
class GradFunctor : public IGradientFunctionMultiDim {
public:
typedef IGradientFunctionMultiDim Impl;
typedef IGradientFunctionMultiDim::BaseFunc ImplBase;
GradFunctor () : fImpl(0) {}
template <typename Func>
GradFunctor( const Func & f, unsigned int dim ) :
fImpl(new FunctorHandler<GradFunctor,Func>(dim,f) )
{}
template <class PtrObj, typename MemFn, typename GradMemFn>
GradFunctor(const PtrObj& p, MemFn memFn, GradMemFn gradFn, unsigned int dim )
: fImpl(new MemGradFunHandler<GradFunctor, PtrObj, MemFn, GradMemFn>(dim, p, memFn, gradFn))
{}
template <typename Func, typename GradFunc>
GradFunctor(const Func & f, const GradFunc & g, int dim ) :
fImpl(new FunctorGradHandler<GradFunctor,Func,GradFunc>(dim, f, g) )
{ }
#if defined(__CINT__) || defined(G__DICTIONARY) || defined(MAKE_CINT_FUNCTOR)
GradFunctor(void * p1, unsigned int dim, const char * className, const char * methodName, const char * derivName);
GradFunctor(void * p1, void * p2, unsigned int dim);
#endif
virtual ~GradFunctor () {}
#ifndef __CINT__
GradFunctor(const GradFunctor & rhs) :
ImplBase(),
Impl()
{
if (rhs.fImpl.get() != 0)
fImpl = std::auto_ptr<Impl>( dynamic_cast<Impl *>( (rhs.fImpl)->Clone()) );
}
#endif
GradFunctor & operator = (const GradFunctor & rhs) {
GradFunctor copy(rhs);
Impl * p = fImpl.release();
fImpl.reset(copy.fImpl.release());
copy.fImpl.reset(p);
return *this;
}
ImplBase * Clone() const { return new GradFunctor(*this); }
unsigned int NDim() const { return fImpl->NDim(); }
private :
inline double DoEval (const double * x) const {
return (*fImpl)(x);
}
inline double DoDerivative (const double * x, unsigned int icoord ) const {
return fImpl->Derivative(x,icoord);
}
std::auto_ptr<Impl> fImpl;
};
class GradFunctor1D : public IGradientFunctionOneDim {
public:
typedef IGradientFunctionOneDim Impl;
typedef IGradientFunctionOneDim::BaseFunc ImplBase;
GradFunctor1D () : fImpl(0) {}
template <typename Func>
GradFunctor1D(const Func & f) :
fImpl(new FunctorHandler<GradFunctor1D,Func>(f) )
{}
template <class PtrObj, typename MemFn, typename GradMemFn>
GradFunctor1D(const PtrObj& p, MemFn memFn, GradMemFn gradFn)
: fImpl(new MemGradFunHandler<GradFunctor1D, PtrObj, MemFn, GradMemFn>(p, memFn, gradFn))
{}
template <typename Func, typename GradFunc>
GradFunctor1D(const Func & f, const GradFunc & g ) :
fImpl(new FunctorGradHandler<GradFunctor1D,Func, GradFunc>(f, g) )
{}
#if defined(__CINT__) || defined(G__DICTIONARY) || defined(MAKE_CINT_FUNCTOR)
GradFunctor1D(void * p1, const char * className, const char * methodName, const char * derivName);
GradFunctor1D(void * p1, void * p2);
#endif
virtual ~GradFunctor1D () {}
#ifndef __CINT__
GradFunctor1D(const GradFunctor1D & rhs) :
ImplBase(),
Impl()
{
if (rhs.fImpl.get() != 0)
fImpl = std::auto_ptr<Impl>( dynamic_cast<Impl *>( (rhs.fImpl)->Clone() ) );
}
#endif
GradFunctor1D & operator = (const GradFunctor1D & rhs) {
GradFunctor1D copy(rhs);
Impl * p = fImpl.release();
fImpl.reset(copy.fImpl.release());
copy.fImpl.reset(p);
return *this;
}
ImplBase * Clone() const { return new GradFunctor1D(*this); }
private :
inline double DoEval (double x) const {
return (*fImpl)(x);
}
inline double DoDerivative (double x) const {
return fImpl->Derivative(x);
}
std::auto_ptr<Impl> fImpl;
};
}
}
#endif /* ROOT_Math_Functor */
Last change: Wed Jun 25 08:29:37 2008
Last generated: 2008-06-25 08:29
This page has been automatically generated. If you have any comments or suggestions about the page layout send a mail to ROOT support, or contact the developers with any questions or problems regarding ROOT.