pelib  2.0.0
src/exprtk.cpp
Go to the documentation of this file.
00001 /*
00002  Copyright 2015 Nicolas Melot
00003 
00004  This file is part of Pelib.
00005 
00006  Pelib is free software: you can redistribute it and/or modify
00007  it under the terms of the GNU General Public License as published by
00008  the Free Software Foundation, either version 3 of the License, or
00009  (at your option) any later version.
00010 
00011  Pelib is distributed in the hope that it will be useful,
00012  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00014  GNU General Public License for more details.
00015 
00016  You should have received a copy of the GNU General Public License
00017  along with Pelib. If not, see <http://www.gnu.org/licenses/>.
00018 */
00019 
00020 #include <sstream>
00021 
00022 #include <pelib/exprtk.hpp>
00023 #include <pelib/pelib_exprtk.hpp>
00024 
00025 using namespace std;
00026 
00027 namespace pelib
00028 {
00029         struct print : public exprtk::ifunction<double>
00030         {
00031                 public:
00032                         print(ostream &stream)
00033                                 : exprtk::ifunction<double>(1), stream(stream)
00034                         {}  
00035 
00036                         inline double operator()(const double &value) 
00037                         {   
00038                                 stream << value << endl;
00039                                 return value;
00040                         }   
00041 
00042                 private:
00043                         ostream &stream;
00044         };
00045 
00046         struct e: public exprtk::ifunction<double>
00047         {
00048                 public:
00049                         e(const set<Task> &tasks, const Platform &arch)
00050                                 : exprtk::ifunction<double>(2), tasks(tasks), arch(arch)
00051                         {}  
00052 
00053                         inline double operator()(const double &id, const double &p) 
00054                         {
00055                                 set<Task>::const_iterator begin = tasks.begin();
00056                                 std::advance(begin, id - 1);
00057                                 double val = begin->getEfficiency((int)p);
00058                                 return val;
00059                         }   
00060 
00061                 private:
00062                         const set<Task> &tasks;
00063                         const Platform &arch;
00064         };
00065 
00066         double
00067         parseEfficiency(const string &formula, double W, double tau, double p)
00068         {
00069                 print print_cout(cout);
00070                 print print_cerr(cerr);
00071 
00072                 typedef exprtk::expression<double> expression_t;
00073                 typedef exprtk::parser<double> parser_t;
00074 
00075                 expression_t expression;
00076 
00077                 exprtk::symbol_table<double> symbol_table;
00078                 symbol_table.add_variable("W", W);
00079                 symbol_table.add_variable("p", p);
00080                 symbol_table.add_variable("tau", tau);
00081                 symbol_table.add_function("cout", print_cout);
00082                 symbol_table.add_function("cerr", print_cerr);
00083 
00084                 expression.register_symbol_table(symbol_table);
00085 
00086                 parser_t parser;
00087 
00088                 parser.compile(formula, expression);    
00089                 return expression.value();
00090         }
00091 
00092         double
00093         parseDeadline(const string &formula, const set<Task> &tasks, const Platform &pt, vector<double> n, vector<double> p, vector<double> F, vector<double> tau, vector<double> W)
00094         {
00095                 e matrix_e(tasks, pt);
00096 
00097                 print print_cout(cout);
00098                 print print_cerr(cerr);
00099 
00100                 typedef exprtk::expression<double> expression_t;
00101                 typedef exprtk::parser<double> parser_t;
00102                 expression_t expression;
00103 
00104                 exprtk::symbol_table<double> symbol_table;
00105                 symbol_table.add_vector("n", n);
00106                 symbol_table.add_vector("p", p);
00107                 symbol_table.add_vector("F", F);
00108                 symbol_table.add_vector("tau", tau);
00109                 symbol_table.add_vector("W", W);
00110                 symbol_table.add_function("e", matrix_e);
00111                 symbol_table.add_function("cout", print_cout);
00112                 symbol_table.add_function("cerr", print_cerr);
00113 
00114                 expression.register_symbol_table(symbol_table);
00115 
00116                 parser_t parser;
00117 
00118                 parser.compile(formula, expression);    
00119                 return expression.value();
00120         }
00121 }