From 4ba0603e0c9e250d57e268aaacd9c6db9f483c8c Mon Sep 17 00:00:00 2001 From: Gabriel Ozouf Date: Wed, 23 Sep 2020 12:22:29 +0200 Subject: [PATCH] [apps/calculation] Duplicate additional result Fix duplicate additional results on units by comparing serialization. Change-Id: Ia360ae7846716a801253b60fcf000240834528bc --- .../additional_outputs/unit_list_controller.cpp | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/apps/calculation/additional_outputs/unit_list_controller.cpp b/apps/calculation/additional_outputs/unit_list_controller.cpp index af0cc851f..f3463b248 100644 --- a/apps/calculation/additional_outputs/unit_list_controller.cpp +++ b/apps/calculation/additional_outputs/unit_list_controller.cpp @@ -46,18 +46,25 @@ void UnitListController::setExpression(Poincare::Expression e) { Shared::PoincareHelpers::Simplify(&expressions[numberOfExpressions], App::app()->localContext(), ExpressionNode::ReductionTarget::User, Poincare::ExpressionNode::SymbolicComputation::ReplaceAllDefinedSymbolsWithDefinition, Poincare::ExpressionNode::UnitConversion::InternationalSystem); numberOfExpressions++; - // 3. Get rid of duplicates + /* 3. Get rid of duplicates + * We find duplicates by comparing the serializations, to eliminate + * expressions that only differ by the types of their number nodes. */ Expression reduceExpression = m_expression.clone(); // Make m_expression comparable to expressions (turn BasedInteger into Rational for instance) Shared::PoincareHelpers::Simplify(&reduceExpression, App::app()->localContext(), ExpressionNode::ReductionTarget::User, Poincare::ExpressionNode::SymbolicComputation::ReplaceAllDefinedSymbolsWithDefinition, Poincare::ExpressionNode::UnitConversion::None); - int currentExpressionIndex = 1; + int currentExpressionIndex = 0; while (currentExpressionIndex < numberOfExpressions) { bool duplicateFound = false; + constexpr int buffersSize = Constant::MaxSerializedExpressionSize; + char buffer1[buffersSize]; + int size1 = PoincareHelpers::Serialize(expressions[currentExpressionIndex], buffer1, buffersSize); for (int i = 0; i < currentExpressionIndex + 1; i++) { // Compare the currentExpression to all previous expressions and to m_expression Expression comparedExpression = i == currentExpressionIndex ? reduceExpression : expressions[i]; assert(!comparedExpression.isUninitialized()); - if (comparedExpression.isIdenticalTo(expressions[currentExpressionIndex])) { + char buffer2[buffersSize]; + int size2 = PoincareHelpers::Serialize(comparedExpression, buffer2, buffersSize); + if (size1 == size2 && strcmp(buffer1, buffer2) == 0) { numberOfExpressions--; // Shift next expressions for (int j = currentExpressionIndex; j < numberOfExpressions; j++) {