PIEvaluator simplify variables sort

This commit is contained in:
2021-04-02 11:42:01 +03:00
parent 58bdcd65a3
commit 8a05a25bca
3 changed files with 45 additions and 68 deletions

View File

@@ -189,9 +189,8 @@ void PIEvaluatorContent::addFunction(const PIString & name, int args) {
int PIEvaluatorContent::addVariable(const PIString & name, const complexd & val) { int PIEvaluatorContent::addVariable(const PIString & name, const complexd & val) {
int ind = variables.size_s(); variables << PIEvaluatorTypes::Variable(name, val);
variables << PIEvaluatorTypes::Variable(name, val, ind); return variables.size_s()-1;
return ind;
} }
@@ -216,7 +215,7 @@ int PIEvaluatorContent::findFunction(const PIString & name) const {
int PIEvaluatorContent::findVariable(const PIString & var_name) const { int PIEvaluatorContent::findVariable(const PIString & var_name) const {
for (uint i = 0; i < variables.size(); i++) for (uint i = 0; i < variables.size(); i++)
if (variables[i].name == var_name) if (variables[i].name == var_name)
return variables[i].index; return i;
return -1; return -1;
} }
@@ -251,7 +250,7 @@ void PIEvaluatorContent::removeVariable(int index) {
void PIEvaluatorContent::clearCustomVariables() { void PIEvaluatorContent::clearCustomVariables() {
variables.clear(); variables.clear();
addVariable(PIStringAscii("i" ), complexd_i); addVariable(PIStringAscii("i" ), complexd_i);
addVariable(PIStringAscii("pi"), atan(1.) * 4.); addVariable(PIStringAscii("pi"), 3.141592653589793238462643383280);
addVariable(PIStringAscii("e" ), exp(1.)); addVariable(PIStringAscii("e" ), exp(1.));
cv_count = variables.size_s(); cv_count = variables.size_s();
} }
@@ -308,8 +307,6 @@ BaseFunctions PIEvaluatorContent::getBaseFunction(const PIString & name) {
void PIEvaluatorContent::dump() { void PIEvaluatorContent::dump() {
for (int i = 0; i < variables.size_s(); ++i) { for (int i = 0; i < variables.size_s(); ++i) {
Variable v(variables[i]); Variable v(variables[i]);
PIString str;
//str << "(ind " << v.index << ", mapped " << var_index.value(v.index, -1) << ")";
piCout << i << v.name << "=" << v.value; piCout << i << v.name << "=" << v.value;
} }
} }
@@ -483,20 +480,19 @@ bool PIEvaluator::fillElements() {
} }
} }
cnum = 0; cnum = 0;
PIMap<int, int> var_index; PIVector<PIPair<int, int> > var_index;
PIVector<PIEvaluatorTypes::Variable> svariables = content.variables; for (int i = 0; i < content.variables.size_s(); ++i)
for (int i = 0; i < svariables.size_s(); ++i) var_index << PIPair<int, int>(i, content.variables[i].name.length());
svariables[i].index = i; var_index.sort([](const PIPair<int, int> * v1, const PIPair<int, int> * v2)->int {return v1->second > v2->second ? -1 : (v1->second == v2->second ? 0 : 1);});
svariables.sort(); for (int i = 0; i < var_index.size_s(); i++) {
for (int i = 0; i < svariables.size_s(); i++) { curfind = content.variables[var_index[i].first].name;
curfind = svariables[i].name;
flen = curfind.length(); flen = curfind.length();
fstart = 0; fstart = 0;
while (fstart >= 0) { while (fstart >= 0) {
fstart = tmps.find(curfind, fstart); fstart = tmps.find(curfind, fstart);
if (fstart < 0) break; if (fstart < 0) break;
for (int j = fstart; j < fstart + flen; j++) { for (int j = fstart; j < fstart + flen; j++) {
elements[j].set(etVariable, cnum, svariables[i].index); elements[j].set(etVariable, cnum, var_index[i].first);
tmps.replace(j, 1, fc); tmps.replace(j, 1, fc);
} }
cnum++; cnum++;

View File

@@ -76,13 +76,12 @@ namespace PIEvaluatorTypes {
short arguments; short arguments;
}; };
struct PIP_EXPORT Variable { struct PIP_EXPORT Variable {
Variable() {value = 0.; index = -1;} Variable() {value = 0.;}
Variable(const PIString & var_name, complexd val, int ind = -1) {name = var_name; value = val; index = ind;} Variable(const PIString & var_name, complexd val) {name = var_name; value = val;}
int index;
PIString name; PIString name;
complexd value; complexd value;
}; };
}; }
/* /*
≠ : ≠ :
≥ } ≥ }
@@ -231,8 +230,6 @@ 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 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 bool operator < (PIEvaluatorTypes::Variable e1, PIEvaluatorTypes::Variable e2) {return e1.name > e2.name;}
inline bool operator ==(PIEvaluatorTypes::Variable e1, PIEvaluatorTypes::Variable e2) {return e1.name == e2.name;}
inline PIByteArray & operator <<(PIByteArray & s, const PIEvaluatorTypes::Instruction & v) { inline PIByteArray & operator <<(PIByteArray & s, const PIEvaluatorTypes::Instruction & v) {
s << PIByteArray::RawData(&v, sizeof(v) - sizeof(v.operators)) << v.operators; s << PIByteArray::RawData(&v, sizeof(v) - sizeof(v.operators)) << v.operators;

View File

@@ -1,53 +1,37 @@
#include "pip.h" #include "pip.h"
#include "pivariantsimple.h"
#pragma pack(push, 1)
struct MM {
int x;
int x2;
double y;
char c[16];
int e2;
int e;
//PIPointd h;
};
#pragma pack(pop)
int Acnt = 0;
class A {
public:
A() {moved = false; i = "constructor"; piCout << "A()"; ++Acnt;}
A(const PIString & s): i(s) {moved = false; piCout << "A(String)"; ++Acnt;}
A(const A & a): i(a.i) {moved = false; piCout << "copy A(&)"; ++Acnt;}
A(A && a): i(std::move(a.i)) {moved = false; a.moved = true; piCout << "copy A(&&)"; ++Acnt;}
~A() {piCout << "~A()" << moved; --Acnt;}
void swap(A & a) {piCout << "swap A()"; piSwap(i, a.i);}
A & operator =(const A & a) {i = a.i; piCout << "= A&"; return *this;}
A & operator =(A && a) {piSwap(i, a.i); a.moved = true; piCout << "= A&&)"; return *this;}
bool operator ==(const A & a) {return true;}
PIString i;
bool moved;
MM m;
static void F(int) {}
};
inline PIByteArray & operator <<(PIByteArray & ba, const A & v) {ba << v.i << v.m; return ba;}
inline PIByteArray & operator >>(PIByteArray & ba, A & v) {ba >> v.i >> v.m; return ba;}
inline PIByteArray & operator <<(PIByteArray & ba, const MM & v) {piCout << "<<"; ba << v.x << v.y << v.c; return ba;}
inline PIByteArray & operator >>(PIByteArray & ba, MM & v) {piCout << ">>"; ba >> v.x >> v.y >> v.c; return ba;}
int main() { int main() {
PIMathVectorT3d v3({1,2,3}); PIVector<int> indexes;
PIMathVectord v(v3); PIEvaluator eval;
PIMathVectord v2({3,2,1}); int dims = 3;
piCout << v; indexes << eval.setVariable("l", complexd(1.));
piCout << v2; indexes << eval.setVariable("mc", complexd(1.));
PIMathMatrixT22d m = PIMathMatrixT22d::identity(); for (int i = 0; i < dims; ++i) {
piCout << m; indexes << eval.setVariable("t" + PIString::fromNumber(i), complexd(1.));
m.rotate(toRad(90.)); indexes << eval.setVariable("tv" + PIString::fromNumber(i), complexd(1.));
piCout << m; }
for (int i = 0; i < dims; ++i) {
indexes << eval.setVariable("m" + PIString::fromNumber(i), complexd(1.));
indexes << eval.setVariable("mv" + PIString::fromNumber(i), complexd(1.));
}
piCout << indexes;
indexes.clear();
indexes << eval.setVariable("l", complexd(1.));
indexes << eval.setVariable("mc", complexd(1.));
for (int i = 0; i < dims; ++i) {
indexes << eval.setVariable("t" + PIString::fromNumber(i), complexd(1.));
indexes << eval.setVariable("tv" + PIString::fromNumber(i), complexd(1.));
}
for (int i = 0; i < dims; ++i) {
indexes << eval.setVariable("m" + PIString::fromNumber(i), complexd(1.));
indexes << eval.setVariable("mv" + PIString::fromNumber(i), complexd(1.));
}
piCout << indexes;
PIEvaluator * eval_x = new PIEvaluator();
piCout << eval_x->setVariable("t");
piCout << eval_x->setVariable("t");
return 0; return 0;
} }