From ceeb43cd5fbc12d8f8de0bf4041cc3c64ab65733 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89milie=20Feral?= Date: Mon, 25 Sep 2017 14:36:41 +0200 Subject: [PATCH] [poincare] Change comparesTo + virtual nodeCompareTo -> virtual compareTo Change-Id: Ie54b3652ad1d5845f084b4b49ca0eb96198b853a --- poincare/include/poincare/expression.h | 3 +-- poincare/include/poincare/integer.h | 2 +- poincare/include/poincare/symbol.h | 2 +- poincare/src/expression.cpp | 25 ++++++++------------- poincare/src/integer.cpp | 8 +++---- poincare/src/symbol.cpp | 30 +++++++++++++------------- 6 files changed, 31 insertions(+), 39 deletions(-) diff --git a/poincare/include/poincare/expression.h b/poincare/include/poincare/expression.h index 863cccd9d..d3d056ce6 100644 --- a/poincare/include/poincare/expression.h +++ b/poincare/include/poincare/expression.h @@ -111,7 +111,7 @@ public: /* Sorting */ virtual bool isCommutative() const { return false; } virtual void sort(); - int comparesTo(const Expression * e) const; + virtual int compareTo(const Expression * e) const; /* Layout Engine */ ExpressionLayout * createLayout(FloatDisplayMode floatDisplayMode = FloatDisplayMode::Default, ComplexFormat complexFormat = ComplexFormat::Default) const; // Returned object must be deleted @@ -135,7 +135,6 @@ protected: * This behavior makes sense for value-less nodes (addition, product, fraction * power, etc… For nodes with a value (Integer, Complex), this must be over- * -riden. */ - virtual int nodeComparesTo(const Expression * e) const; private: /* Layout Engine */ virtual ExpressionLayout * privateCreateLayout(FloatDisplayMode floatDisplayMode, ComplexFormat complexFormat) const = 0; diff --git a/poincare/include/poincare/integer.h b/poincare/include/poincare/integer.h index 75912e4a2..f03241dcc 100644 --- a/poincare/include/poincare/integer.h +++ b/poincare/include/poincare/integer.h @@ -32,7 +32,7 @@ public: Type type() const override; Expression * clone() const override; int checksum() const override; - int nodeComparesTo(const Expression * e) const override; + int compareTo(const Expression * e) const override; bool isEqualTo(const Integer & other) const; bool isLowerThan(const Integer & other) const; diff --git a/poincare/include/poincare/symbol.h b/poincare/include/poincare/symbol.h index de708e2e3..8635772f4 100644 --- a/poincare/include/poincare/symbol.h +++ b/poincare/include/poincare/symbol.h @@ -35,9 +35,9 @@ public: Type type() const override; Expression * clone() const override; int checksum() const override; + int compareTo(const Expression * e) const override; bool isMatrixSymbol() const; private: - int nodeComparesTo(const Expression * e) const override; Evaluation * privateEvaluate(SinglePrecision p, Context& context, AngleUnit angleUnit) const override { return templatedEvaluate(context, angleUnit); } Evaluation * privateEvaluate(DoublePrecision p, Context& context, AngleUnit angleUnit) const override { return templatedEvaluate(context, angleUnit); } template Evaluation * templatedEvaluate(Context& context, AngleUnit angleUnit) const; diff --git a/poincare/src/expression.cpp b/poincare/src/expression.cpp index 52b38d403..fafab993a 100644 --- a/poincare/src/expression.cpp +++ b/poincare/src/expression.cpp @@ -88,7 +88,7 @@ void Expression::sort() { for (int i = numberOfOperands()-1; i > 0; i--) { bool isSorted = true; for (int j = 0; j < numberOfOperands()-1; j++) { - if (operand(j)->comparesTo(operand(j+1)) > 0) { + if (operand(j)->compareTo(operand(j+1)) > 0) { swapOperands(j, j+1); isSorted = false; } @@ -104,17 +104,20 @@ int Expression::checksum() const { return 0; } -int Expression::comparesTo(const Expression * e) const { - if (this->nodeComparesTo(e) != 0) { - return this->nodeComparesTo(e); +int Expression::compareTo(const Expression * e) const { + if (e->type() > this->type()) { + return 1; + } + if (e->type() < this->type()) { + return -1; } for (int i = 0; i < this->numberOfOperands(); i++) { // The NULL node is the least node type. if (e->numberOfOperands() <= i) { return 1; } - if (this->operand(i)->comparesTo(e->operand(i)) != 0) { - return this->operand(i)->comparesTo(e->operand(i)); + if (this->operand(i)->compareTo(e->operand(i)) != 0) { + return this->operand(i)->compareTo(e->operand(i)); } } // The NULL node is the least node type. @@ -157,16 +160,6 @@ template T Expression::epsilon() { return epsilon; } -int Expression::nodeComparesTo(const Expression * e) const { - if (e->type() == this->type()) { - return 0; - } - if (e->type() > this->type()) { - return 1; - } - return -1; -} - void Expression::recursivelySetAsParentOfChildren() { for (int i=0; i(operand(i)); diff --git a/poincare/src/integer.cpp b/poincare/src/integer.cpp index 0f7cb129f..a42ad8214 100644 --- a/poincare/src/integer.cpp +++ b/poincare/src/integer.cpp @@ -142,8 +142,8 @@ Expression * Integer::clone() const { // Comparison -int Integer::nodeComparesTo(const Expression * e) const { - int typeComparison = Expression::nodeComparesTo(e); +int Integer::compareTo(const Expression * e) const { + int typeComparison = Expression::compareTo(e); if (typeComparison != 0) { return typeComparison; } @@ -160,11 +160,11 @@ int Integer::nodeComparesTo(const Expression * e) const { } bool Integer::isEqualTo(const Integer & other) const { - return (nodeComparesTo(&other) == 0); + return (compareTo(&other) == 0); } bool Integer::isLowerThan(const Integer & other) const { - return (nodeComparesTo(&other) < 0); + return (compareTo(&other) < 0); } // Arithmetic diff --git a/poincare/src/symbol.cpp b/poincare/src/symbol.cpp index 2fdc130a0..ba6427048 100644 --- a/poincare/src/symbol.cpp +++ b/poincare/src/symbol.cpp @@ -57,6 +57,21 @@ int Symbol::checksum() const { return m_name; } +int Symbol::compareTo(const Expression * e) const { + int typeComparison = Expression::compareTo(e); + if (typeComparison != 0) { + return typeComparison; + } + assert(e->type() == Expression::Type::Symbol); + if (m_name == ((Symbol *)e)->m_name) { + return 0; + } + if ((m_name > ((Symbol *)e)->m_name)) { + return 1; + } + return -1; +} + template Evaluation * Symbol::templatedEvaluate(Context& context, AngleUnit angleUnit) const { if (context.expressionForSymbol(this) != nullptr) { @@ -104,21 +119,6 @@ ExpressionLayout * Symbol::privateCreateLayout(FloatDisplayMode floatDisplayMode return new StringLayout(&m_name, 1); } -int Symbol::nodeComparesTo(const Expression * e) const { - int typeComparison = Expression::nodeComparesTo(e); - if (typeComparison != 0) { - return typeComparison; - } - assert(e->type() == Expression::Type::Symbol); - if (m_name == ((Symbol *)e)->m_name) { - return 0; - } - if ((m_name > ((Symbol *)e)->m_name)) { - return 1; - } - return -1; -} - bool Symbol::isMatrixSymbol() const { if (m_name >= (char)SpecialSymbols::M0 && m_name <= (char)SpecialSymbols::M9) { return true;