| 1 |
#ifndef _FUNCTOR_H_
|
| 2 |
#define _FUNCTOR_H_
|
| 3 |
|
| 4 |
#include <vector>
|
| 5 |
|
| 6 |
using namespace std;
|
| 7 |
|
| 8 |
class ObjFunctor0{
|
| 9 |
public:
|
| 10 |
virtual double operator()(vector<double>&)=0;
|
| 11 |
};
|
| 12 |
|
| 13 |
class PtrFunctor0 : ObjFunctor0{
|
| 14 |
|
| 15 |
public:
|
| 16 |
|
| 17 |
PtrFunctor0(double (*thePtrFunc)(vector<double>&)){
|
| 18 |
ptrFunc = thePtrFunc;
|
| 19 |
}
|
| 20 |
|
| 21 |
virtual double operator()(vector<double>& arg){
|
| 22 |
return (*ptrFunc)(arg);
|
| 23 |
};
|
| 24 |
|
| 25 |
protected:
|
| 26 |
double (*ptrFunc)(vector<double>&);
|
| 27 |
};
|
| 28 |
|
| 29 |
|
| 30 |
//ClassMemObjFunctor class wraps a pointer pointing to a member function of a class
|
| 31 |
//
|
| 32 |
template<typename TClass>
|
| 33 |
class ClassMemObjFunctor0 : public ObjFunctor0{
|
| 34 |
public:
|
| 35 |
ClassMemObjFunctor0(TClass* thePtrClass, double (TClass::*thePtrFunc)(vector<double>&)){
|
| 36 |
ptrClass = thePtrClass;
|
| 37 |
ptrFunc = thePtrFunc;
|
| 38 |
}
|
| 39 |
|
| 40 |
double operator()(vector<double>& arg){
|
| 41 |
return (*ptrClass.*ptrFunc)(arg);
|
| 42 |
}
|
| 43 |
protected:
|
| 44 |
|
| 45 |
double (TClass::*ptrFunc)(vector<double>&);
|
| 46 |
TClass* ptrClass;
|
| 47 |
};
|
| 48 |
|
| 49 |
/**
|
| 50 |
* Abstract class of object function which have an overloaded method
|
| 51 |
* to calculate the gradient
|
| 52 |
*/
|
| 53 |
|
| 54 |
class ObjFunctor1{
|
| 55 |
|
| 56 |
public:
|
| 57 |
virtual double operator()(vector<double>&, vector<double>&)=0;
|
| 58 |
|
| 59 |
};
|
| 60 |
|
| 61 |
//PtrFunctor class wraps a pointer which points to an objct function.
|
| 62 |
// PtrFunctor can be invoked by
|
| 63 |
// functor(vector<double>&, vector<double>&)
|
| 64 |
class PtrFunctor1 : ObjFunctor1{
|
| 65 |
|
| 66 |
public:
|
| 67 |
|
| 68 |
PtrFunctor1(double (*thePtrFunc)(vector<double>&, vector<double>&)){
|
| 69 |
ptrFunc = thePtrFunc;
|
| 70 |
}
|
| 71 |
|
| 72 |
virtual double operator()(vector<double>& arg, vector<double>& grad){
|
| 73 |
return (*ptrFunc)(arg, grad);
|
| 74 |
};
|
| 75 |
|
| 76 |
protected:
|
| 77 |
double (*ptrFunc)(vector<double>&, vector<double>&);
|
| 78 |
};
|
| 79 |
|
| 80 |
//ClassMemObjFunctor class wraps a pointer pointing to a member function of a class
|
| 81 |
//
|
| 82 |
template<typename TClass>
|
| 83 |
class ClassMemObjFunctor1 : public ObjFunctor1{
|
| 84 |
public:
|
| 85 |
ClassMemObjFunctor1(TClass* thePtrClass, double (TClass::*thePtrFunc)(vector<double>&, vector<double>&)){
|
| 86 |
ptrClass = thePtrClass;
|
| 87 |
ptrFunc = thePtrFunc;
|
| 88 |
}
|
| 89 |
|
| 90 |
double operator()(vector<double>&arg, vector<double>&grad){
|
| 91 |
return (*ptrClass.*ptrFunc)(arg, grad);
|
| 92 |
}
|
| 93 |
protected:
|
| 94 |
|
| 95 |
double (TClass::*ptrFunc)(vector<double>&, vector<double>&);
|
| 96 |
TClass* ptrClass;
|
| 97 |
};
|
| 98 |
|
| 99 |
#endif
|