| 1 |
#ifndef _FUNCTOR_H_
|
| 2 |
#define _FUNCTOR_H_
|
| 3 |
|
| 4 |
/**
|
| 5 |
* Abstract class of object function which have an overloaded method
|
| 6 |
* to calculate the gradient
|
| 7 |
*/
|
| 8 |
|
| 9 |
class ObjFunctor{
|
| 10 |
|
| 11 |
public:
|
| 12 |
virtual double operator()(vector<double>&, vector<double>&)=0;
|
| 13 |
|
| 14 |
};
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
//PtrFunctor class wraps a pointer which points to an objct function.
|
| 19 |
// PtrFunctor can be invoked by
|
| 20 |
// functor(vector<double>&, vector<double>&)
|
| 21 |
class PtrFunctor : ObjFunctor{
|
| 22 |
|
| 23 |
public:
|
| 24 |
|
| 25 |
PtrFunctor(double (*thePtrFunc)(vector<double>&, vector<double>&)){
|
| 26 |
ptrFunc = thePtrFunc;
|
| 27 |
}
|
| 28 |
|
| 29 |
virtual double operator()(vector<double>& arg, vector<double>& grad){
|
| 30 |
return (*ptrFunc)(arg, grad);
|
| 31 |
};
|
| 32 |
|
| 33 |
protected:
|
| 34 |
double (*ptrFunc)(vector<double>&, vector<double>&);
|
| 35 |
};
|
| 36 |
|
| 37 |
//ClassMemObjFunctor class wraps a pointer pointing to a member function of a class
|
| 38 |
//
|
| 39 |
template<typename TClass>
|
| 40 |
class ClassMemObjFunctor : public ObjFunctor{
|
| 41 |
public:
|
| 42 |
ClassMemObjFunctor(TClass* thePtrClass, double (TClass::*thePtrFunc)(vector<double>&, vector<double>&)){
|
| 43 |
ptrClass = thePtrClass;
|
| 44 |
ptrFunc = thePtrFunc;
|
| 45 |
}
|
| 46 |
|
| 47 |
double operator()(vector<double>&, vector<double>&){
|
| 48 |
return (*ptrClass.*ptrFunc))(arg, grad);
|
| 49 |
}
|
| 50 |
protected:
|
| 51 |
|
| 52 |
double (TClass::*ptrFunc)(vector<double>&, vector<double>&);
|
| 53 |
TClass* ptrClass;
|
| 54 |
};
|
| 55 |
|
| 56 |
#endif
|