[poincare] Sort selectors before creating rules

Change-Id: I51da663d83f5759c499c14f603511e3b9cb3f92f
This commit is contained in:
Émilie Feral
2017-09-27 18:52:46 +02:00
parent c5324de471
commit 38ee1ef1df
3 changed files with 133 additions and 43 deletions

View File

@@ -15,62 +15,62 @@ class Evaluation;
class Expression {
public:
enum class Type : uint8_t {
Integer = 0,
Complex,
Symbol,
Parenthesis,
Opposite,
AbsoluteValue = 0,
Addition,
Subtraction,
Multiplication,
Division,
Power,
Sum,
Product,
DivisionQuotient,
DivisionRemainder,
GreatCommonDivisor,
LeastCommonMultiple,
Floor,
Ceiling,
Round,
FracPart,
AbsoluteValue,
Factorial,
ImaginaryPart,
RealPart,
ComplexArgument,
Conjugate,
Logarithm,
NaperianLogarithm,
SquareRoot,
NthRoot,
Cosine,
Sine,
Tangent,
ArcCosine,
ArcSine,
ArcTangent,
HyperbolicCosine,
HyperbolicSine,
HyperbolicTangent,
BinomialCoefficient,
Ceiling,
Complex,
ComplexArgument,
ComplexMatrix,
ConfidenceInterval,
Conjugate,
Cosine,
Derivative,
Determinant,
Division,
DivisionQuotient,
DivisionRemainder,
ExpressionMatrix,
Factorial,
Floor,
FracPart,
GreatCommonDivisor,
HyperbolicArcCosine,
HyperbolicArcSine,
HyperbolicArcTangent,
Derivative,
HyperbolicCosine,
HyperbolicSine,
HyperbolicTangent,
ImaginaryPart,
Integer,
Integral,
BinomialCoefficient,
PermuteCoefficient,
ConfidenceInterval,
PredictionInterval,
ExpressionMatrix,
ComplexMatrix,
LeastCommonMultiple,
Logarithm,
MatrixDimension,
MatrixInverse,
MatrixTrace,
MatrixTranspose,
Determinant,
Multiplication,
NaperianLogarithm,
NthRoot,
Opposite,
Parenthesis,
PermuteCoefficient,
Power,
PredictionInterval,
Product,
RealPart,
Round,
Sine,
SquareRoot,
Store,
Subtraction,
Sum,
Symbol,
Tangent,
Undefined = 255
};
enum class FloatDisplayMode {

View File

@@ -26,6 +26,7 @@ void Node::identifyAnonymousChildren(int * index) {
// Generation
void Node::generateSelector(Rule * rule) {
sort();
int i = 0;
for (Node * child : *m_children) {
@@ -110,6 +111,93 @@ int Node::intValue() const {
return result;
}
int Node::compareTo(Node * n) const {
if (m_name->compare("Any")) {
if (n->m_name->compare("Any")) {
if (identifier().compare(n->identifier()) == 0) {
return 0;
} else if (identifier().compare(n->identifier()) > 0) {
return 1;
} else {
return -1;
}
} else if (n->m_name->compare("SameAs")) {
if (identifier().compare(n->value()) == 0) {
return -1;
} else if (identifier().compare(n->value()) > 0) {
return 1;
} else {
return -1;
}
} else {
return 1;
}
} else if (m_name->compare("SameAs")) {
if (n->m_name->compare("Any")) {
if (value().compare(n->identifier()) == 0) {
return 1;
} else if (value().compare(n->identifier()) > 0) {
return 1;
} else {
return -1;
}
} else if (n->m_name->compare("SameAs")) {
if (value().compare(n->value()) == 0) {
return 0;
} else if (value().compare(n->value()) > 0) {
return 1;
} else {
return -1;
}
} else {
return 1;
}
} else {
if (n->m_name->compare("Any")) {
return -1;
} else if (n->m_name->compare("SameAs")) {
return -1;
} else {
if (name().compare(n->name()) != 0) {
if (name().compare(n->name()) > 0) {
return 1;
} else {
return -1;
}
} else {
if (intValue() == n->intValue()) {
return 0;
} else if (intValue() > n->intValue()) {
return 1;
} else {
return -1;
}
}
}
}
}
void Node::sort() {
if (m_children->size() == 0) {
return;
}
for (Node * child : *m_children) {
child->sort();
}
for (int i = m_children->size()-1; i > 0; i--) {
bool isSorted = true;
for (int j = 0; j < m_children->size()-1; j++) {
if (m_children->at(j)->compareTo(m_children->at(j+1)) > 0) {
std::swap(m_children->at(j), m_children->at(j+1));
isSorted = false;
}
}
if (isSorted) {
return;
}
}
}
#if 0
std::string Node::generateSelectorConstructor(Rule * context) {

View File

@@ -24,6 +24,8 @@ public:
std::string value() const;
int intValue() const;
int numberOfChildren() { return m_children->size(); }
int compareTo(Node * node) const;
void sort();
private:
int selectorCaptureIndexInRule(Rule * rule);
int selectorIndexInRule(Rule * rule);