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