[apps/calculation] Do no display the factor expression if it is

undefined in Integer additional outputs
This commit is contained in:
Émilie Feral
2020-01-13 11:25:02 +01:00
committed by Léa Saviot
parent 90957d137a
commit c441611190
3 changed files with 25 additions and 8 deletions

View File

@@ -29,7 +29,7 @@ protected:
constexpr static int k_maxNumberOfCells = 4;
Poincare::Expression m_expression;
// Memoization of layouts
Poincare::Layout m_layouts[k_maxNumberOfCells];
mutable Poincare::Layout m_layouts[k_maxNumberOfCells];
private:
Poincare::Layout layoutAtIndex(int index);
virtual void computeLayoutAtIndex(int index) = 0;

View File

@@ -1,6 +1,7 @@
#include "integer_list_controller.h"
#include <poincare/based_integer.h>
#include <poincare/integer.h>
#include <poincare/empty_layout.h>
#include <poincare/factor.h>
#include "../app.h"
#include "../../shared/poincare_helpers.h"
@@ -11,7 +12,7 @@ using namespace Shared;
namespace Calculation {
int IntegerListController::numberOfRows() const {
return 4;
return 3 + factorExpressionIsComputable();
}
Integer::Base baseAtIndex(int index) {
@@ -27,14 +28,12 @@ Integer::Base baseAtIndex(int index) {
}
void IntegerListController::computeLayoutAtIndex(int index) {
assert(m_expression.type() == ExpressionNode::Type::BasedInteger);
Poincare::Context * context = App::app()->localContext();
if (index == 3) {
Expression factor = Factor::Builder(m_expression.clone());
PoincareHelpers::Simplify(&factor, context, ExpressionNode::ReductionTarget::User);
m_layouts[index] = PoincareHelpers::CreateLayout(factor);
if (!m_layouts[index].isUninitialized()) {
return;
}
assert(m_expression.type() == ExpressionNode::Type::BasedInteger);
// For index = k_indexOfFactorExpression, the layout is assumed to be alreday memoized because it is needed to compute the numberOfRows
assert(index < k_indexOfFactorExpression);
Integer i = static_cast<BasedInteger &>(m_expression).integer();
m_layouts[index] = i.createLayout(baseAtIndex(index));
}
@@ -52,4 +51,20 @@ I18n::Message IntegerListController::messageAtIndex(int index) {
}
}
bool IntegerListController::factorExpressionIsComputable() const {
if (!m_layouts[k_indexOfFactorExpression].isUninitialized()) {
// The factor expression is already memoized
return !m_layouts[k_indexOfFactorExpression].isEmpty();
}
Poincare::Context * context = App::app()->localContext();
Expression factor = Factor::Builder(m_expression.clone());
PoincareHelpers::Simplify(&factor, context, ExpressionNode::ReductionTarget::User);
if (!factor.isUndefined()) {
m_layouts[k_indexOfFactorExpression] = PoincareHelpers::CreateLayout(factor);
return true;
}
m_layouts[k_indexOfFactorExpression] = EmptyLayout::Builder();
return false;
}
}

View File

@@ -13,8 +13,10 @@ public:
//ListViewDataSource
int numberOfRows() const override;
private:
static constexpr int k_indexOfFactorExpression = 3;
void computeLayoutAtIndex(int index) override;
I18n::Message messageAtIndex(int index) override;
bool factorExpressionIsComputable() const;
};
}