mirror of
https://github.com/aportelli/LatAnalyze.git
synced 2024-11-10 00:45:36 +00:00
function classes: const calls and fonction pointer support
This commit is contained in:
parent
74c11a1da3
commit
4659cdb607
@ -27,30 +27,39 @@ using namespace Latan;
|
||||
/******************************************************************************
|
||||
* Compiled double function implementation *
|
||||
******************************************************************************/
|
||||
// constructor/destructor //////////////////////////////////////////////////////
|
||||
// constructor /////////////////////////////////////////////////////////////////
|
||||
CompiledDoubleFunction::CompiledDoubleFunction(const unsigned nArg)
|
||||
: DoubleFunction(nArg)
|
||||
{}
|
||||
{
|
||||
interpreter_ = new MathInterpreter;
|
||||
context_ = new RunContext;
|
||||
}
|
||||
|
||||
CompiledDoubleFunction::CompiledDoubleFunction(const unsigned nArg,
|
||||
const string &code)
|
||||
: DoubleFunction(nArg)
|
||||
{
|
||||
interpreter_ = new MathInterpreter;
|
||||
context_ = new RunContext;
|
||||
setCode(code);
|
||||
}
|
||||
|
||||
// destructor //////////////////////////////////////////////////////////////////
|
||||
CompiledDoubleFunction::~CompiledDoubleFunction(void)
|
||||
{}
|
||||
{
|
||||
delete interpreter_;
|
||||
delete context_;
|
||||
}
|
||||
|
||||
// access //////////////////////////////////////////////////////////////////////
|
||||
void CompiledDoubleFunction::setCode(const string &code)
|
||||
{
|
||||
interpreter_.setCode(code);
|
||||
StdMath::addStdMathFunc(context_.fTable);
|
||||
interpreter_->setCode(code);
|
||||
StdMath::addStdMathFunc(context_->fTable);
|
||||
}
|
||||
|
||||
// function call ///////////////////////////////////////////////////////////////
|
||||
double CompiledDoubleFunction::operator()(vector<double> &arg)
|
||||
double CompiledDoubleFunction::evaluate(const vector<double> &arg) const
|
||||
{
|
||||
double result;
|
||||
|
||||
@ -62,13 +71,13 @@ double CompiledDoubleFunction::operator()(vector<double> &arg)
|
||||
}
|
||||
for (unsigned int i = 0; i < getNArg(); ++i)
|
||||
{
|
||||
context_.vTable["x_" + strFrom(i)] = arg[i];
|
||||
context_->vTable["x_" + strFrom(i)] = arg[i];
|
||||
}
|
||||
interpreter_(context_);
|
||||
if (!context_.dStack.empty())
|
||||
(*interpreter_)(*context_);
|
||||
if (!context_->dStack.empty())
|
||||
{
|
||||
result = context_.dStack.top();
|
||||
context_.dStack.pop();
|
||||
result = context_->dStack.top();
|
||||
context_->dStack.pop();
|
||||
}
|
||||
else
|
||||
{
|
||||
@ -82,7 +91,8 @@ double CompiledDoubleFunction::operator()(vector<double> &arg)
|
||||
// IO //////////////////////////////////////////////////////////////////////////
|
||||
ostream &Latan::operator<<(ostream &out, CompiledDoubleFunction &f)
|
||||
{
|
||||
out << f.interpreter_;
|
||||
f.interpreter_->compile();
|
||||
out << *(f.interpreter_);
|
||||
|
||||
return out;
|
||||
}
|
||||
|
@ -35,22 +35,22 @@ BEGIN_NAMESPACE
|
||||
class CompiledDoubleFunction: public DoubleFunction
|
||||
{
|
||||
public:
|
||||
// constructor/destructor
|
||||
// constructors
|
||||
explicit CompiledDoubleFunction(const unsigned nArg);
|
||||
explicit CompiledDoubleFunction(const unsigned nArg,
|
||||
const std::string &code);
|
||||
// destructor
|
||||
virtual ~CompiledDoubleFunction(void);
|
||||
// access
|
||||
void setCode(const std::string &code);
|
||||
// function call
|
||||
using DoubleFunction::operator();
|
||||
virtual double operator()(std::vector<double> &arg);
|
||||
virtual double evaluate(const std::vector<double> &arg) const;
|
||||
// IO
|
||||
friend std::ostream &operator<<(std::ostream &out,
|
||||
CompiledDoubleFunction &f);
|
||||
private:
|
||||
MathInterpreter interpreter_;
|
||||
RunContext context_;
|
||||
MathInterpreter* interpreter_;
|
||||
RunContext* context_;
|
||||
};
|
||||
|
||||
END_NAMESPACE
|
||||
|
@ -61,27 +61,38 @@ unsigned int Function::getNArg(void) const
|
||||
/******************************************************************************
|
||||
* DoubleFunction implementation *
|
||||
******************************************************************************/
|
||||
DoubleFunction::DoubleFunction(const unsigned nArg)
|
||||
: Function(nArg), buffer_(nArg)
|
||||
{}
|
||||
DoubleFunction::DoubleFunction(const unsigned nArg, vecFunc *f)
|
||||
: Function(nArg)
|
||||
, f_(f)
|
||||
{
|
||||
buffer_ = new vector<double>(nArg);
|
||||
}
|
||||
|
||||
DoubleFunction::~DoubleFunction(void)
|
||||
{}
|
||||
{
|
||||
delete buffer_;
|
||||
}
|
||||
|
||||
double DoubleFunction::operator()(std::stack<double> &arg)
|
||||
double DoubleFunction::evaluate(const std::vector<double> &arg) const
|
||||
{
|
||||
std::cout << "double()" << endl;
|
||||
return f_(arg);
|
||||
}
|
||||
|
||||
double DoubleFunction::operator()(std::stack<double> &arg) const
|
||||
{
|
||||
for (unsigned int i = 0; i < getNArg(); ++i)
|
||||
{
|
||||
buffer_[getNArg() - i - 1] = arg.top();
|
||||
(*buffer_)[getNArg() - i - 1] = arg.top();
|
||||
arg.pop();
|
||||
}
|
||||
|
||||
return (*this)(buffer_);
|
||||
return this->evaluate(*buffer_);
|
||||
}
|
||||
|
||||
double DoubleFunction::operator()(const double x0, ...)
|
||||
double DoubleFunction::operator()(const double x0, ...) const
|
||||
{
|
||||
buffer_[0] = x0;
|
||||
(*buffer_)[0] = x0;
|
||||
if (getNArg() > 1)
|
||||
{
|
||||
va_list va;
|
||||
@ -89,10 +100,10 @@ double DoubleFunction::operator()(const double x0, ...)
|
||||
va_start(va, x0);
|
||||
for (unsigned int i = 1; i < getNArg(); ++i)
|
||||
{
|
||||
buffer_[i] = va_arg(va, double);
|
||||
(*buffer_)[i] = va_arg(va, double);
|
||||
}
|
||||
va_end(va);
|
||||
}
|
||||
|
||||
return (*this)(buffer_);
|
||||
return this->evaluate(*buffer_);
|
||||
}
|
||||
|
@ -47,16 +47,19 @@ private:
|
||||
******************************************************************************/
|
||||
class DoubleFunction: public Function
|
||||
{
|
||||
private:
|
||||
typedef double vecFunc(const std::vector<double> &);
|
||||
public:
|
||||
// constructor/destructor
|
||||
explicit DoubleFunction(const unsigned nArg);
|
||||
explicit DoubleFunction(const unsigned nArg, vecFunc *f = NULL);
|
||||
virtual ~DoubleFunction(void);
|
||||
// function call
|
||||
virtual double operator()(std::vector<double> &arg) = 0;
|
||||
double operator()(std::stack<double> &arg);
|
||||
double operator()(const double x0, ...);
|
||||
virtual double evaluate(const std::vector<double> &arg) const;
|
||||
double operator()(std::stack<double> &arg) const;
|
||||
double operator()(const double x0, ...) const;
|
||||
private:
|
||||
std::vector<double> buffer_;
|
||||
std::vector<double> *buffer_;
|
||||
vecFunc *f_;
|
||||
};
|
||||
|
||||
END_NAMESPACE
|
||||
|
@ -27,58 +27,54 @@ using namespace Latan;
|
||||
* Standard C functions *
|
||||
******************************************************************************/
|
||||
|
||||
#define DEF_STD_FUNC_1ARG(name, funcName) \
|
||||
name##Function::name##Function(void): DoubleFunction(1) {}\
|
||||
name##Function::~name##Function(void) {}\
|
||||
double name##Function::operator()(std::vector<double> &arg)\
|
||||
#define DEF_STD_FUNC_1ARG(name) \
|
||||
static double name##VecFunc(const vector<double> &arg)\
|
||||
{\
|
||||
return funcName(arg[0]);\
|
||||
return (name)(arg[0]);\
|
||||
}\
|
||||
name##Function STDMATH_NAMESPACE::funcName;
|
||||
DoubleFunction STDMATH_NAMESPACE::name(1, &name##VecFunc);
|
||||
|
||||
#define DEF_STD_FUNC_2ARG(name, funcName) \
|
||||
name##Function::name##Function(void): DoubleFunction(2) {}\
|
||||
name##Function::~name##Function(void) {}\
|
||||
double name##Function::operator()(std::vector<double> &arg)\
|
||||
#define DEF_STD_FUNC_2ARG(name) \
|
||||
static double name##VecFunc(const vector<double> &arg)\
|
||||
{\
|
||||
return funcName(arg[0], arg[1]);\
|
||||
return (name)(arg[0], arg[1]);\
|
||||
}\
|
||||
name##Function STDMATH_NAMESPACE::funcName;
|
||||
DoubleFunction STDMATH_NAMESPACE::name(2, &name##VecFunc);
|
||||
|
||||
// Trigonometric functions
|
||||
DEF_STD_FUNC_1ARG(Cos, cos)
|
||||
DEF_STD_FUNC_1ARG(Sin, sin)
|
||||
DEF_STD_FUNC_1ARG(Tan, tan)
|
||||
DEF_STD_FUNC_1ARG(ACos, acos)
|
||||
DEF_STD_FUNC_1ARG(ASin, asin)
|
||||
DEF_STD_FUNC_1ARG(ATan, atan)
|
||||
DEF_STD_FUNC_2ARG(ATan2, atan2)
|
||||
DEF_STD_FUNC_1ARG(cos)
|
||||
DEF_STD_FUNC_1ARG(sin)
|
||||
DEF_STD_FUNC_1ARG(tan)
|
||||
DEF_STD_FUNC_1ARG(acos)
|
||||
DEF_STD_FUNC_1ARG(asin)
|
||||
DEF_STD_FUNC_1ARG(atan)
|
||||
DEF_STD_FUNC_2ARG(atan2)
|
||||
|
||||
// Hyperbolic functions
|
||||
DEF_STD_FUNC_1ARG(Cosh, cosh)
|
||||
DEF_STD_FUNC_1ARG(Sinh, sinh)
|
||||
DEF_STD_FUNC_1ARG(Tanh, tanh)
|
||||
DEF_STD_FUNC_1ARG(cosh)
|
||||
DEF_STD_FUNC_1ARG(sinh)
|
||||
DEF_STD_FUNC_1ARG(tanh)
|
||||
|
||||
// Exponential and logarithmic functions
|
||||
DEF_STD_FUNC_1ARG(Exp, exp)
|
||||
DEF_STD_FUNC_1ARG(Log, log)
|
||||
DEF_STD_FUNC_1ARG(exp)
|
||||
DEF_STD_FUNC_1ARG(log)
|
||||
|
||||
// Power functions
|
||||
DEF_STD_FUNC_2ARG(Pow, pow)
|
||||
DEF_STD_FUNC_1ARG(Sqrt, sqrt)
|
||||
DEF_STD_FUNC_2ARG(pow)
|
||||
DEF_STD_FUNC_1ARG(sqrt)
|
||||
|
||||
// Rounding and remainder functions
|
||||
DEF_STD_FUNC_1ARG(Ceil, ceil)
|
||||
DEF_STD_FUNC_1ARG(Floor, floor)
|
||||
DEF_STD_FUNC_2ARG(FMod, fmod)
|
||||
DEF_STD_FUNC_1ARG(ceil)
|
||||
DEF_STD_FUNC_1ARG(floor)
|
||||
DEF_STD_FUNC_2ARG(fmod)
|
||||
|
||||
// Minimum, maximum, difference functions
|
||||
DEF_STD_FUNC_2ARG(FDim, fdim)
|
||||
DEF_STD_FUNC_2ARG(FMax, fmax)
|
||||
DEF_STD_FUNC_2ARG(FMin, fmin)
|
||||
DEF_STD_FUNC_2ARG(fdim)
|
||||
DEF_STD_FUNC_2ARG(fmax)
|
||||
DEF_STD_FUNC_2ARG(fmin)
|
||||
|
||||
// Absolute value
|
||||
DEF_STD_FUNC_1ARG(Abs, abs)
|
||||
DEF_STD_FUNC_1ARG(fabs)
|
||||
|
||||
#define ADD_FUNC(func) fTable[#func] = &STDMATH_NAMESPACE::func
|
||||
void STDMATH_NAMESPACE::addStdMathFunc(FunctionTable &fTable)
|
||||
@ -110,5 +106,5 @@ void STDMATH_NAMESPACE::addStdMathFunc(FunctionTable &fTable)
|
||||
ADD_FUNC(fmax);
|
||||
ADD_FUNC(fmin);
|
||||
// Absolute value
|
||||
ADD_FUNC(abs);
|
||||
ADD_FUNC(fabs);
|
||||
}
|
||||
|
@ -32,54 +32,46 @@ BEGIN_NAMESPACE
|
||||
******************************************************************************/
|
||||
#define STDMATH_NAMESPACE StdMath
|
||||
|
||||
#define DECL_STD_FUNC(name, funcName) \
|
||||
class name##Function: public DoubleFunction\
|
||||
{\
|
||||
public:\
|
||||
name##Function(void);\
|
||||
virtual ~name##Function(void);\
|
||||
using DoubleFunction::operator();\
|
||||
virtual double operator()(std::vector<double> &arg);\
|
||||
};\
|
||||
#define DECL_STD_FUNC(name) \
|
||||
namespace STDMATH_NAMESPACE\
|
||||
{\
|
||||
extern name##Function funcName;\
|
||||
extern DoubleFunction name;\
|
||||
}
|
||||
|
||||
// Trigonometric functions
|
||||
DECL_STD_FUNC(Cos, cos)
|
||||
DECL_STD_FUNC(Sin, sin)
|
||||
DECL_STD_FUNC(Tan, tan)
|
||||
DECL_STD_FUNC(ACos, acos)
|
||||
DECL_STD_FUNC(ASin, asin)
|
||||
DECL_STD_FUNC(ATan, atan)
|
||||
DECL_STD_FUNC(ATan2, atan2)
|
||||
DECL_STD_FUNC(cos)
|
||||
DECL_STD_FUNC(sin)
|
||||
DECL_STD_FUNC(tan)
|
||||
DECL_STD_FUNC(acos)
|
||||
DECL_STD_FUNC(asin)
|
||||
DECL_STD_FUNC(atan)
|
||||
DECL_STD_FUNC(atan2)
|
||||
|
||||
// Hyperbolic functions
|
||||
DECL_STD_FUNC(Cosh, cosh)
|
||||
DECL_STD_FUNC(Sinh, sinh)
|
||||
DECL_STD_FUNC(Tanh, tanh)
|
||||
DECL_STD_FUNC(cosh)
|
||||
DECL_STD_FUNC(sinh)
|
||||
DECL_STD_FUNC(tanh)
|
||||
|
||||
// Exponential and logarithmic functions
|
||||
DECL_STD_FUNC(Exp, exp)
|
||||
DECL_STD_FUNC(Log, log)
|
||||
DECL_STD_FUNC(exp)
|
||||
DECL_STD_FUNC(log)
|
||||
|
||||
// Power functions
|
||||
DECL_STD_FUNC(Pow, pow)
|
||||
DECL_STD_FUNC(Sqrt, sqrt)
|
||||
DECL_STD_FUNC(pow)
|
||||
DECL_STD_FUNC(sqrt)
|
||||
|
||||
// Rounding and remainder functions
|
||||
DECL_STD_FUNC(Ceil, ceil)
|
||||
DECL_STD_FUNC(Floor, floor)
|
||||
DECL_STD_FUNC(FMod, fmod)
|
||||
DECL_STD_FUNC(ceil)
|
||||
DECL_STD_FUNC(floor)
|
||||
DECL_STD_FUNC(fmod)
|
||||
|
||||
// Minimum, maximum, difference functions
|
||||
DECL_STD_FUNC(FDim, fdim)
|
||||
DECL_STD_FUNC(FMax, fmax)
|
||||
DECL_STD_FUNC(FMin, fmin)
|
||||
DECL_STD_FUNC(fdim)
|
||||
DECL_STD_FUNC(fmax)
|
||||
DECL_STD_FUNC(fmin)
|
||||
|
||||
// Absolute value
|
||||
DECL_STD_FUNC(Abs, abs)
|
||||
DECL_STD_FUNC(fabs)
|
||||
|
||||
// Add standard math functions to a table for the math compiler
|
||||
namespace STDMATH_NAMESPACE
|
||||
|
Loading…
Reference in New Issue
Block a user