diff --git a/main.cpp b/main.cpp index 173efc05..6324aedb 100644 --- a/main.cpp +++ b/main.cpp @@ -41,6 +41,30 @@ public: } }; int main() { + PIEvaluator eval, eval2; + piCout << "start"; + + eval.setVariable("x", complexd(1, 2)); + PITimeMeasurer tm; + eval.check("e2eelg(10ex"); + double us = tm.elapsed_u(); piCout << "check" << us; + + tm.reset(); + PIByteArray ba = eval.save(); + us = tm.elapsed_u(); piCout << " save" << us; + + piCout << ba.size(); + + tm.reset(); + eval2.load(ba); + us = tm.elapsed_u(); piCout << " load" << us; + + //eval.setVariable("x", complexd(-2, 0)); + eval2.setVariable("x", complexd(-2, 0)); + + piCout << eval.expression() << "=" << eval.evaluate(); + piCout << eval2.expression() << "=" << eval2.evaluate(); + /*A a; CONNECTU(PICout::Notifier::object(), finished, &a, pcf); PIString buffer = "my buff:"; @@ -177,27 +201,3 @@ int main(int argc, char *argv[]) { piCout << "server" << st << ba.toHex() << ba.size();*/ return 0; } - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/src_main/math/pievaluator.cpp b/src_main/math/pievaluator.cpp index 72a47c24..e1754ca3 100755 --- a/src_main/math/pievaluator.cpp +++ b/src_main/math/pievaluator.cpp @@ -699,8 +699,8 @@ void PIEvaluator::convert() { -const PIString & PIEvaluator::preprocess(const PIString & string) { - static PIString ret; +PIString PIEvaluator::preprocess(const PIString & string) { + PIString ret; int lind; ret = prepare(string); convert(); @@ -1180,6 +1180,7 @@ inline void PIEvaluator::execFunction(const PIEvaluatorTypes::Instruction & ci) inline bool PIEvaluator::execInstructions() { + kvars = &(content.variables); PIEvaluatorTypes::Instruction ci; int oi; complexd tmp; @@ -1266,3 +1267,16 @@ complexd PIEvaluator::evaluate() { if (fabs(out.imag()) < 1E-300) out = complexd(out.real(), 0.); return out; } + + +PIByteArray PIEvaluator::save() const { + PIByteArray ret; + ret << content << elements << currentVariables << unknownVars << variables << instructions << currentString << lastError << out << correct; + return ret; +} + + +void PIEvaluator::load(PIByteArray ba) { + if (ba.size() <= 4) return; + ba >> content >> elements >> currentVariables >> unknownVars >> variables >> instructions >> currentString >> lastError >> out >> correct; +} diff --git a/src_main/math/pievaluator.h b/src_main/math/pievaluator.h index d676d0c6..ce4513fe 100755 --- a/src_main/math/pievaluator.h +++ b/src_main/math/pievaluator.h @@ -94,6 +94,8 @@ namespace PIEvaluatorTypes { class PIP_EXPORT PIEvaluatorContent { friend class PIEvaluator; + friend inline PIByteArray & operator <<(PIByteArray & s, const PIEvaluatorContent & v); + friend inline PIByteArray & operator >>(PIByteArray & s, PIEvaluatorContent & v); public: PIEvaluatorContent(); ~PIEvaluatorContent() {;} @@ -187,11 +189,17 @@ public: //! Returns last result of \a evaluate() const complexd & lastResult() const {return out;} + //! Save to %PIByteArray evaluator state (expression, variables, errors, compiled instructions) + PIByteArray save() const; + + //! Restore from %PIByteArray evaluator state (expression, variables, errors, compiled instructions) + void load(PIByteArray ba); + PIEvaluatorContent content; private: const PIString & prepare(const PIString & string); - const PIString & preprocess(const PIString & string); + PIString preprocess(const PIString & string); int parse(const PIString & string, int offset = 0); void convert(); void checkBrackets(); @@ -226,4 +234,41 @@ private: inline bool operator ==(PIEvaluatorTypes::Element e1, PIEvaluatorTypes::Element e2) {return (e1.type == e2.type && e1.num == e2.num);} inline bool operator !=(PIEvaluatorTypes::Element e1, PIEvaluatorTypes::Element e2) {return (e1.type != e2.type || e1.num != e2.num);} +inline PIByteArray & operator <<(PIByteArray & s, const PIEvaluatorTypes::Instruction & v) { + s << ((int)v.operation) << v.operators << v.out << v.function; + return s; +} +inline PIByteArray & operator >>(PIByteArray & s, PIEvaluatorTypes::Instruction & v) { + int i(0); + s >> i >> v.operators >> v.out >> v.function; + v.operation = (PIEvaluatorTypes::Operation)i; + return s; +} +inline PIByteArray & operator <<(PIByteArray & s, const PIEvaluatorTypes::Element & v) { + s << ((int)v.type) << v.num << v.var_num; + return s; +} +inline PIByteArray & operator >>(PIByteArray & s, PIEvaluatorTypes::Element & v) { + int i(0); + s >> i >> v.num >> v.var_num; + v.type = (PIEvaluatorTypes::eType)i; + return s; +} +inline PIByteArray & operator <<(PIByteArray & s, const PIEvaluatorTypes::Variable & v) { + s << v.name << v.value; + return s; +} +inline PIByteArray & operator >>(PIByteArray & s, PIEvaluatorTypes::Variable & v) { + s >> v.name >> v.value; + return s; +} +inline PIByteArray & operator <<(PIByteArray & s, const PIEvaluatorContent & v) { + s << v.variables << v.cv_count; + return s; +} +inline PIByteArray & operator >>(PIByteArray & s, PIEvaluatorContent & v) { + s >> v.variables >> v.cv_count; + return s; +} + #endif // PIEVALUATOR_H