From bf313fff764735020a66d7bd97e6a00c99d514f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89milie=20Feral?= Date: Thu, 2 Nov 2017 14:26:59 +0100 Subject: [PATCH] [poincare] Use Dynamic hierarchy shortcut constructor Change-Id: I5a10ad7fd34d6aae09b5bfc9c7b4ada8451a7e5d --- poincare/src/addition.cpp | 3 +-- poincare/src/division.cpp | 3 +-- poincare/src/logarithm.cpp | 9 +++------ poincare/src/multiplication.cpp | 22 +++++++++------------- poincare/src/opposite.cpp | 6 +++--- poincare/src/power.cpp | 15 +++++---------- poincare/src/subtraction.cpp | 6 ++---- poincare/src/trigonometry.cpp | 9 +++------ 8 files changed, 27 insertions(+), 46 deletions(-) diff --git a/poincare/src/addition.cpp b/poincare/src/addition.cpp index e463b45fc..d49e3b396 100644 --- a/poincare/src/addition.cpp +++ b/poincare/src/addition.cpp @@ -134,8 +134,7 @@ void Addition::factorizeOperands(Expression * e1, Expression * e2, Context & con } e1->shallowSimplify(context, angleUnit); } else { - const Expression * operands[2] = {r, e1}; - Multiplication * m = new Multiplication(operands, 2, true); + Multiplication * m = new Multiplication(r, e1, true); e1->replaceWith(m, true); m->shallowSimplify(context, angleUnit); } diff --git a/poincare/src/division.cpp b/poincare/src/division.cpp index ce0db326c..5e8a65005 100644 --- a/poincare/src/division.cpp +++ b/poincare/src/division.cpp @@ -25,8 +25,7 @@ Expression * Division::clone() const { Expression * Division::shallowSimplify(Context& context, AngleUnit angleUnit) { const Expression * powOperands[2] = {operand(1), new Rational(Integer(-1))}; Power * p = new Power(powOperands, false); - const Expression * multOperands[2] = {operand(0), p}; - Multiplication * m = new Multiplication(multOperands, 2, false); + Multiplication * m = new Multiplication(operand(0), p, false); p->deepSimplify(context, angleUnit); detachOperands(); replaceWith(m, true); diff --git a/poincare/src/logarithm.cpp b/poincare/src/logarithm.cpp index 4acb8a9ec..956d45f03 100644 --- a/poincare/src/logarithm.cpp +++ b/poincare/src/logarithm.cpp @@ -52,8 +52,7 @@ Expression * Logarithm::shallowSimplify(Context& context, AngleUnit angleUnit) { } Expression * n = splitInteger(r->numerator(), false, context, angleUnit); Expression * d = splitInteger(r->denominator(), true, context, angleUnit); - const Expression * addOp[2] = {n, d}; - Addition * a = new Addition(addOp, 2, false); + Addition * a = new Addition(n, d, false); replaceWith(a, true); return a->shallowSimplify(context, angleUnit); } @@ -74,8 +73,7 @@ Expression * Logarithm::splitInteger(Integer i, bool isDenominator, Context & co if (!isDenominator) { return e; } - const Expression * multOperands[2] = {new Rational(Integer(-1)), e}; - Multiplication * m = new Multiplication(multOperands, 2, false); + Multiplication * m = new Multiplication(new Rational(Integer(-1)), e, false); return m; } Integer factors[Arithmetic::k_maxNumberOfPrimeFactors]; @@ -89,8 +87,7 @@ Expression * Logarithm::splitInteger(Integer i, bool isDenominator, Context & co } Expression * e = clone(); e->replaceOperand(e->operand(0), new Rational(factors[index]), true); - const Expression * multOperands[2] = {new Rational(coefficients[index]), e}; - Multiplication * m = new Multiplication(multOperands, 2, false); + Multiplication * m = new Multiplication(new Rational(coefficients[index]), e, false); const Expression * addNewOperand[1] = {m}; a->addOperands(addNewOperand, 1); m->shallowSimplify(context, angleUnit); diff --git a/poincare/src/multiplication.cpp b/poincare/src/multiplication.cpp index 0f329e86c..402a7a935 100644 --- a/poincare/src/multiplication.cpp +++ b/poincare/src/multiplication.cpp @@ -186,13 +186,11 @@ bool Multiplication::resolveSquareRootAtDenominator(Context & context, AngleUnit Integer factor1 = Integer::Multiplication( Integer::Multiplication(n1, d1), Integer::Multiplication(Integer::Power(d2, Integer(2)), q2)); - const Expression * mult1Operands[2] = {new Rational(factor1), sqrt1}; - Multiplication * m1 = new Multiplication(mult1Operands, 2, false); + Multiplication * m1 = new Multiplication(new Rational(factor1), sqrt1, false); Integer factor2 = Integer::Multiplication( Integer::Multiplication(n2, d2), Integer::Multiplication(Integer::Power(d1, Integer(2)), q1)); - const Expression * mult2Operands[2] = {new Rational(factor2), sqrt2}; - Multiplication * m2 = new Multiplication(mult2Operands, 2, false); + Multiplication * m2 = new Multiplication(new Rational(factor2), sqrt2, false); const Expression * subOperands[2] = {m1, m2}; if (denominator.isNegative()) { denominator.setNegative(false); @@ -238,16 +236,14 @@ void Multiplication::factorize(Context & context, AngleUnit angleUnit) { } void Multiplication::factorizeBase(Expression * e1, Expression * e2, Context & context, AngleUnit angleUnit) { - const Expression * addOperands[2] = {CreateExponent(e1), CreateExponent(e2)}; + Expression * s = new Addition(CreateExponent(e1), CreateExponent(e2), false); removeOperand(e2, true); - Expression * s = new Addition(addOperands, 2, false); if (e1->type() == Type::Power) { e1->replaceOperand(e1->operand(1), s, true); s->shallowSimplify(context, angleUnit); e1->shallowSimplify(context, angleUnit); } else { - const Expression * operands[2] = {e1, s}; - Power * p = new Power(operands, false); + Power * p = new Power(e1, s, false); s->shallowSimplify(context, angleUnit); replaceOperand(e1, p, false); p->shallowSimplify(context, angleUnit); @@ -255,11 +251,12 @@ void Multiplication::factorizeBase(Expression * e1, Expression * e2, Context & c } void Multiplication::factorizeExponent(Expression * e1, Expression * e2, Context & context, AngleUnit angleUnit) { - const Expression * multOperands[2] = {e1->operand(0)->clone(), e2->operand(0)}; + const Expression * base1 = e1->operand(0)->clone(); + const Expression * base2 = e2->operand(0); // TODO: remove cast, everything is a hierarchy - static_cast(e2)->detachOperand(e2->operand(0)); + static_cast(e2)->detachOperand(base2); + Expression * m = new Multiplication(base1, base2, false); removeOperand(e2, true); - Expression * m = new Multiplication(multOperands, 2, false); e1->replaceOperand(e1->operand(0), m, true); m->shallowSimplify(context, angleUnit); e1->shallowSimplify(context, angleUnit); @@ -401,8 +398,7 @@ Expression * Multiplication::shallowBeautify(Context & context, AngleUnit angleU static_cast(denominatorOperand)->addOperands(integerDenominator, 1); static_cast(denominatorOperand)->sortOperands(SimplificationOrder); } else { - const Expression * multOperands[2] = {new Rational(r->denominator()), denominatorOperand->clone()}; - Multiplication * m = new Multiplication(multOperands, 2, false); + Multiplication * m = new Multiplication(new Rational(r->denominator()), denominatorOperand->clone(), false); denominatorOperand->replaceWith(m, true); } } diff --git a/poincare/src/opposite.cpp b/poincare/src/opposite.cpp index 4905597d3..e3439a00d 100644 --- a/poincare/src/opposite.cpp +++ b/poincare/src/opposite.cpp @@ -29,9 +29,9 @@ Complex Opposite::compute(const Complex c, AngleUnit angleUnit) { } Expression * Opposite::shallowSimplify(Context& context, AngleUnit angleUnit) { - const Expression * multOperands[2] = {new Rational(Integer(-1)), operand(0)}; - detachOperand(operand(0)); - Multiplication * m = new Multiplication(multOperands, 2, false); + const Expression * op = operand(0); + detachOperand(op); + Multiplication * m = new Multiplication(new Rational(Integer(-1)), op, false); replaceWith(m, true); return m->shallowSimplify(context, angleUnit); } diff --git a/poincare/src/power.cpp b/poincare/src/power.cpp index dc5aef1b5..6d6a9c6c7 100644 --- a/poincare/src/power.cpp +++ b/poincare/src/power.cpp @@ -197,8 +197,7 @@ Expression * Power::shallowSimplify(Context& context, AngleUnit angleUnit) { m->shallowSimplify(context, angleUnit); const Expression * powOperands[2] = {factor, rCopy}; Power * p = new Power(powOperands, false); - const Expression * multOperands[2] = {p, clone()}; - Multiplication * root = new Multiplication(multOperands, 2, false); + Multiplication * root = new Multiplication(p, clone(), false); p->shallowSimplify(context, angleUnit); root->editableOperand(1)->shallowSimplify(context, angleUnit); replaceWith(root, true); @@ -214,8 +213,7 @@ Expression * Power::shallowSimplify(Context& context, AngleUnit angleUnit) { Power * p1 = static_cast(clone()); replaceOperand(a, a->editableOperand(1), true); Power * p2 = static_cast(clone()); - const Expression * multOperands[2] = {p1, p2}; - Multiplication * m = new Multiplication(multOperands, 2, false); + Multiplication * m = new Multiplication(p1, p2, false); simplifyRationalRationalPower(p1, static_cast(p1->editableOperand(0)), static_cast(p1->editableOperand(1)->editableOperand(0)), context, angleUnit); replaceWith(m, true); return m->shallowSimplify(context, angleUnit); @@ -228,8 +226,7 @@ Expression * Power::simplifyPowerPower(Power * p, Expression * e, Context& conte Expression * p0 = p->editableOperand(0); Expression * p1 = p->editableOperand(1); p->detachOperands(); - const Expression * multOperands[2] = {p1, e}; - Multiplication * m = new Multiplication(multOperands, 2, false); + Multiplication * m = new Multiplication(p1, e, false); replaceOperand(e, m, false); replaceOperand(p, p0, true); m->shallowSimplify(context, angleUnit); @@ -263,8 +260,7 @@ Expression * Power::simplifyRationalRationalPower(Expression * result, Rational n = CreateSimplifiedIntegerRationalPower(a->numerator(), b, false); d = CreateSimplifiedIntegerRationalPower(a->denominator(), b, true); } - const Expression * multOp[2] = {n, d}; - Multiplication * m = new Multiplication(multOp, 2, false); + Multiplication * m = new Multiplication(n, d, false); result->replaceWith(m, true); return m->shallowSimplify(context, angleUnit); } @@ -304,8 +300,7 @@ Expression * Power::CreateSimplifiedIntegerRationalPower(Integer i, Rational * r return p; } Rational * r3 = isDenominator ? new Rational(Integer(1), r1) : new Rational(r1); - const Expression * multOp[2] = {r3, p}; - Multiplication * m = new Multiplication(multOp, 2, false); + Multiplication * m = new Multiplication(r3, p, false); if (r2.isOne()) { m->removeOperand(p); } diff --git a/poincare/src/subtraction.cpp b/poincare/src/subtraction.cpp index b82f6f252..7975ddabf 100644 --- a/poincare/src/subtraction.cpp +++ b/poincare/src/subtraction.cpp @@ -29,10 +29,8 @@ Complex Subtraction::compute(const Complex c, const Complex d) { } Expression * Subtraction::shallowSimplify(Context& context, AngleUnit angleUnit) { - const Expression * multOperands[2] = {new Rational(Integer(-1)), operand(1)}; - Multiplication * m = new Multiplication(multOperands, 2, false); - const Expression * addOperands[2] = {operand(0), m}; - Addition * a = new Addition(addOperands, 2, false); + Multiplication * m = new Multiplication(new Rational(Integer(-1)), operand(1), false); + Addition * a = new Addition(operand(0), m, false); m->shallowSimplify(context, angleUnit); detachOperands(); replaceWith(a, true); diff --git a/poincare/src/trigonometry.cpp b/poincare/src/trigonometry.cpp index ea244a7a1..ac9f20d88 100644 --- a/poincare/src/trigonometry.cpp +++ b/poincare/src/trigonometry.cpp @@ -35,8 +35,7 @@ Expression * Trigonometry::shallowSimplifyDirectFunction(Expression * e, Context if (e->type() == Expression::Type::Cosine) { return e->shallowSimplify(context, angleUnit); } else { - const Expression * multOperands[2] = {new Rational(Integer(-1)), e->clone()}; - Multiplication * m = new Multiplication(multOperands, 2, false); + Multiplication * m = new Multiplication(new Rational(Integer(-1)), e->clone(), false); m->editableOperand(1)->shallowSimplify(context, angleUnit); return e->replaceWith(m, true)->shallowSimplify(context, angleUnit); } @@ -65,8 +64,7 @@ Expression * Trigonometry::shallowSimplifyDirectFunction(Expression * e, Context unaryCoefficient *= -1; } Expression * simplifiedCosine = e->shallowSimplify(context, angleUnit); // recursive - const Expression * multOperands[2] = {new Rational(Integer(unaryCoefficient)), simplifiedCosine->clone()}; - Multiplication * m = new Multiplication(multOperands, 2, false); + Multiplication * m = new Multiplication(new Rational(Integer(unaryCoefficient)), simplifiedCosine->clone(), false); return simplifiedCosine->replaceWith(m, true)->shallowSimplify(context, angleUnit); } assert(r->sign() == Expression::Sign::Positive); @@ -129,8 +127,7 @@ Expression * Trigonometry::shallowSimplifyInverseFunction(Expression * e, Contex s->editableOperand(1)->shallowSimplify(context, angleUnit); return e->replaceWith(s, true)->shallowSimplify(context, angleUnit); } else { - const Expression * multOperands[2] = {new Rational(Integer(-1)), e->clone()}; - Multiplication * m = new Multiplication(multOperands, 2, false); + Multiplication * m = new Multiplication(new Rational(Integer(-1)), e->clone(), false); m->editableOperand(1)->shallowSimplify(context, angleUnit); return e->replaceWith(m, true)->shallowSimplify(context, angleUnit); }