diff --git a/poincare/Makefile b/poincare/Makefile index 1408138a6..a3d430325 100644 --- a/poincare/Makefile +++ b/poincare/Makefile @@ -25,7 +25,7 @@ objs += $(addprefix poincare/src/layout/,\ ) objs += $(addprefix poincare/src/simplify/,\ simplify_addition_integer.o\ - simplify_addition_merge.o\ + simplify_commutative_merge.o\ simplify_product_zero.o\ ) tests += $(addprefix poincare/test/,\ diff --git a/poincare/src/expression.cpp b/poincare/src/expression.cpp index 77bc0bc8a..934d680a1 100644 --- a/poincare/src/expression.cpp +++ b/poincare/src/expression.cpp @@ -4,7 +4,7 @@ #include "simplify/simplify.h" #include "simplify/simplify_product_zero.h" #include "simplify/simplify_addition_integer.h" -#include "simplify/simplify_addition_merge.h" +#include "simplify/simplify_commutative_merge.h" extern "C" { #include } @@ -13,7 +13,7 @@ extern "C" { static expression_simplifier_t kSimplifiers[] = { &SimplifyProductZero, &SimplifyAdditionInteger, - &SimplifyAdditionMerge, + &SimplifyCommutativeMerge, nullptr }; diff --git a/poincare/src/simplify/simplify_addition_merge.cpp b/poincare/src/simplify/simplify_addition_merge.cpp deleted file mode 100644 index 9add4fc37..000000000 --- a/poincare/src/simplify/simplify_addition_merge.cpp +++ /dev/null @@ -1,43 +0,0 @@ -#include "simplify_addition_merge.h" -#include -extern "C" { -#include -#include -} - -Expression * SimplifyAdditionMerge(Expression * e) { - if (e->type() != Expression::Type::Addition) { - return nullptr; - } - Addition * a = (Addition *)e; - // Let's count how many elements we'll be able to pick up from our children - int mergedNumberOfChildren = 0; - for (int i=0; inumberOfOperands(); i++) { - Expression * operand = a->operand(i); - if (operand->type() == Expression::Type::Addition) { - mergedNumberOfChildren += operand->numberOfOperands(); - } else { - mergedNumberOfChildren += 1; - } - } - assert(mergedNumberOfChildren >= a->numberOfOperands()); - if (mergedNumberOfChildren == a->numberOfOperands()) { - // Nothing to simplify here - return nullptr; - } - Expression ** children = (Expression **)malloc(sizeof(Expression *) * mergedNumberOfChildren); - int childrenIndex = 0; - for (int i=0; inumberOfOperands(); i++) { - Expression * operand = a->operand(i); - if (operand->type() == Expression::Type::Addition) { - for (int j=0; jnumberOfOperands(); j++) { - children[childrenIndex++] = operand->operand(j); - } - } else { - children[childrenIndex++] = operand; - } - } - Addition * result = new Addition(children, mergedNumberOfChildren, true); - free(children); - return result; -} diff --git a/poincare/src/simplify/simplify_addition_merge.h b/poincare/src/simplify/simplify_addition_merge.h deleted file mode 100644 index fb80bcfa6..000000000 --- a/poincare/src/simplify/simplify_addition_merge.h +++ /dev/null @@ -1,16 +0,0 @@ -#ifndef POINCARE_SIMPLIFY_ADDITION_MERGE_H -#define POINCARE_SIMPLIFY_ADDITION_MERGE_H - -#include "simplify.h" - -/* - * + + - * / \ / | \ - * a + -> a c d - * / \ - * c d - */ - -Expression * SimplifyAdditionMerge(Expression * e); - -#endif diff --git a/poincare/src/simplify/simplify_commutative_merge.cpp b/poincare/src/simplify/simplify_commutative_merge.cpp new file mode 100644 index 000000000..328c577b5 --- /dev/null +++ b/poincare/src/simplify/simplify_commutative_merge.cpp @@ -0,0 +1,55 @@ +#include "simplify_commutative_merge.h" +#include +#include +extern "C" { +#include +#include +} + +Expression * SimplifyCommutativeMerge(Expression * e) { + if (e->type() != Expression::Type::Addition && + e->type() != Expression::Type::Product) { + return nullptr; + } + CommutativeOperation * c = (CommutativeOperation *)e; + // Let's count how many elements we'll be able to pick up from our children + int mergedNumberOfChildren = 0; + for (int i=0; inumberOfOperands(); i++) { + Expression * operand = c->operand(i); + if (operand->type() == c->type()) { + mergedNumberOfChildren += operand->numberOfOperands(); + } else { + mergedNumberOfChildren += 1; + } + } + assert(mergedNumberOfChildren >= c->numberOfOperands()); + if (mergedNumberOfChildren == c->numberOfOperands()) { + // Nothing to simplify here + return nullptr; + } + Expression ** children = (Expression **)malloc(sizeof(Expression *) * mergedNumberOfChildren); + int childrenIndex = 0; + for (int i=0; inumberOfOperands(); i++) { + Expression * operand = c->operand(i); + if (operand->type() == c->type()) { + for (int j=0; jnumberOfOperands(); j++) { + children[childrenIndex++] = operand->operand(j); + } + } else { + children[childrenIndex++] = operand; + } + } + CommutativeOperation * result = nullptr; + switch(e->type()) { + case Expression::Type::Addition: + result = new Addition(children, mergedNumberOfChildren, true); + break; + case Expression::Type::Product: + result = new Product(children, mergedNumberOfChildren, true); + break; + default: + assert(false); + } + free(children); + return result; +} diff --git a/poincare/src/simplify/simplify_commutative_merge.h b/poincare/src/simplify/simplify_commutative_merge.h new file mode 100644 index 000000000..057049a37 --- /dev/null +++ b/poincare/src/simplify/simplify_commutative_merge.h @@ -0,0 +1,16 @@ +#ifndef POINCARE_SIMPLIFY_COMMUTATIVE_MERGE_H +#define POINCARE_SIMPLIFY_COMMUTATIVE_MERGE_H + +#include "simplify.h" + +/* + * + + + * / \ / | \ + * a + -> a c d + * / \ + * c d + */ + +Expression * SimplifyCommutativeMerge(Expression * e); + +#endif