Skip to content

Commit

Permalink
Return references in getChildren, getFunction
Browse files Browse the repository at this point in the history
  • Loading branch information
fintarin committed Sep 25, 2023
1 parent 7a0bb49 commit 69aca69
Show file tree
Hide file tree
Showing 20 changed files with 78 additions and 64 deletions.
6 changes: 4 additions & 2 deletions include/fintamath/expressions/Expression.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ class Expression : public IExpressionCRTP<Expression> {

Expression precise(uint8_t precision = FINTAMATH_PRECISION) const;

std::shared_ptr<IFunction> getFunction() const override;
const std::shared_ptr<IFunction> &getFunction() const override;

ArgumentPtrVector getChildren() const override;
const ArgumentPtrVector &getChildren() const override;

void setChildren(const ArgumentPtrVector &childVect) override;

Expand Down Expand Up @@ -139,6 +139,8 @@ class Expression : public IExpressionCRTP<Expression> {
private:
mutable ArgumentPtr child;

mutable ArgumentPtrVector childrenCached = {{}};

mutable std::string stringCached;

mutable bool isSimplified = false;
Expand Down
4 changes: 2 additions & 2 deletions include/fintamath/expressions/IExpression.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ namespace fintamath {

class IExpression : public IArithmetic {
public:
virtual std::shared_ptr<IFunction> getFunction() const = 0;
virtual const std::shared_ptr<IFunction> &getFunction() const = 0;

virtual ArgumentPtrVector getChildren() const = 0;
virtual const ArgumentPtrVector &getChildren() const = 0;

virtual void setChildren(const ArgumentPtrVector &childVect) = 0;

Expand Down
4 changes: 2 additions & 2 deletions include/fintamath/expressions/IExpressionCRTP.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ class IExpressionCRTP_ : public IExpressionBaseCRTP<Derived, isMultiFunction> {
}
}

ArgumentPtrVector lhsChildren = this->getChildren();
ArgumentPtrVector rhsChildren = rhs.getChildren();
const ArgumentPtrVector &lhsChildren = this->getChildren();
const ArgumentPtrVector &rhsChildren = rhs.getChildren();

if (lhsChildren.size() != rhsChildren.size()) {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ class IBinaryExpression : public IExpression {
public:
std::string toString() const override;

std::shared_ptr<IFunction> getFunction() const final;
const std::shared_ptr<IFunction> &getFunction() const final;

ArgumentPtrVector getChildren() const final;
const ArgumentPtrVector &getChildren() const final;

void setChildren(const ArgumentPtrVector &childVect) final;

Expand Down Expand Up @@ -40,6 +40,9 @@ class IBinaryExpression : public IExpression {
ArgumentPtr lhsChild;

ArgumentPtr rhsChild;

private:
mutable ArgumentPtrVector childrenCached = {{}, {}};
};

template <typename Derived, bool isMultiFunction = false>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ class IPolynomExpression : public IExpression {
public:
std::string toString() const override;

std::shared_ptr<IFunction> getFunction() const final;
const std::shared_ptr<IFunction> &getFunction() const final;

ArgumentPtrVector getChildren() const final;
const ArgumentPtrVector &getChildren() const final;

void setChildren(const ArgumentPtrVector &childVect) final;

Expand Down
7 changes: 5 additions & 2 deletions include/fintamath/expressions/interfaces/IUnaryExpression.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ class IUnaryExpression : public IExpression {
public:
std::string toString() const override;

std::shared_ptr<IFunction> getFunction() const final;
const std::shared_ptr<IFunction> &getFunction() const final;

ArgumentPtrVector getChildren() const override;
const ArgumentPtrVector &getChildren() const override;

void setChildren(const ArgumentPtrVector &childVect) override;

Expand Down Expand Up @@ -38,6 +38,9 @@ class IUnaryExpression : public IExpression {
std::shared_ptr<IFunction> func;

ArgumentPtr child;

private:
mutable ArgumentPtrVector childrenCached = {{}};
};

template <typename Derived, bool isMultiFunction = false>
Expand Down
12 changes: 7 additions & 5 deletions src/fintamath/expressions/Expression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -227,8 +227,9 @@ bool Expression::parseTerm(const TermVector &terms, size_t start, size_t end) {
return true;
}

std::shared_ptr<IFunction> Expression::getFunction() const {
return {};
const std::shared_ptr<IFunction> &Expression::getFunction() const {
static const std::shared_ptr<IFunction> func;
return func;
}

Expression &Expression::add(const Expression &rhs) {
Expand Down Expand Up @@ -261,9 +262,10 @@ Expression &Expression::negate() {
return *this;
}

ArgumentPtrVector Expression::getChildren() const {
const ArgumentPtrVector &Expression::getChildren() const {
simplifyMutable();
return {child};
childrenCached.front() = child;
return childrenCached;
}

void Expression::setChildren(const ArgumentPtrVector &childVect) {
Expand Down Expand Up @@ -572,7 +574,7 @@ void Expression::validateFunctionArgs(const IFunction &func, const ArgumentPtrVe

bool Expression::doesArgMatch(const MathObjectType &expectedType, const ArgumentPtr &arg) {
if (const auto childExpr = cast<IExpression>(arg)) {
const std::shared_ptr<IFunction> childFunc = childExpr->getFunction();
const std::shared_ptr<IFunction> &childFunc = childExpr->getFunction();
const MathObjectType childType = childFunc->getReturnType();

if (childType != Variable::getTypeStatic() &&
Expand Down
8 changes: 4 additions & 4 deletions src/fintamath/expressions/ExpressionUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ bool hasVariable(const ArgumentPtr &arg) {
return false;
}

ArgumentPtrVector children = expr->getChildren();
const ArgumentPtrVector &children = expr->getChildren();

return std::ranges::any_of(children, [](const auto &child) {
bool res = false;
Expand All @@ -144,7 +144,7 @@ bool hasVariable(const ArgumentPtr &arg, const Variable &var) {
return false;
}

ArgumentPtrVector children = expr->getChildren();
const ArgumentPtrVector &children = expr->getChildren();

return std::ranges::any_of(children, [&var](const auto &child) {
bool res = false;
Expand All @@ -167,7 +167,7 @@ bool hasInfinity(const ArgumentPtr &arg) {
return false;
}

ArgumentPtrVector children = expr->getChildren();
const ArgumentPtrVector &children = expr->getChildren();

return std::ranges::any_of(children, [](const auto &child) {
bool res = false;
Expand All @@ -190,7 +190,7 @@ bool hasComplex(const ArgumentPtr &arg) {
return false;
}

ArgumentPtrVector children = expr->getChildren();
const ArgumentPtrVector &children = expr->getChildren();

return std::ranges::any_of(children, [](const auto &child) {
bool res = false;
Expand Down
4 changes: 2 additions & 2 deletions src/fintamath/expressions/FunctionExpression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ std::string FunctionExpression::toString() const {
return functionToString(*func, children);
}

std::shared_ptr<IFunction> FunctionExpression::getFunction() const {
const std::shared_ptr<IFunction> &FunctionExpression::getFunction() const {
return func;
}

ArgumentPtrVector FunctionExpression::getChildren() const {
const ArgumentPtrVector &FunctionExpression::getChildren() const {
return children;
}

Expand Down
4 changes: 2 additions & 2 deletions src/fintamath/expressions/FunctionExpression.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ class FunctionExpression : public IExpressionCRTP<FunctionExpression, true> {

std::string toString() const override;

std::shared_ptr<IFunction> getFunction() const override;
const std::shared_ptr<IFunction> &getFunction() const override;

ArgumentPtrVector getChildren() const override;
const ArgumentPtrVector &getChildren() const override;

void setChildren(const ArgumentPtrVector &childVect) override;

Expand Down
5 changes: 2 additions & 3 deletions src/fintamath/expressions/IExpression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,10 @@ std::vector<Variable> IExpression::getVariables() const {
}

void IExpression::setVariables(const std::vector<std::pair<Variable, ArgumentPtr>> &varsToVals) {
auto children = getChildren();

const ArgumentPtrVector &children = getChildren();
ArgumentPtrVector newChildren;

for (auto &child : children) {
for (const auto &child : children) {
if (std::shared_ptr<IExpression> exprChild = cast<IExpression>(child->clone())) {
exprChild->setVariables(varsToVals);
newChildren.emplace_back(exprChild);
Expand Down
2 changes: 1 addition & 1 deletion src/fintamath/expressions/binary/DivExpression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ ArgumentPtr DivExpression::powSimplify(const ArgumentPtr &lhs, const ArgumentPtr

std::pair<ArgumentPtr, ArgumentPtr> DivExpression::getRateValuePair(const ArgumentPtr &rhs) {
if (const auto &powExpr = cast<IExpression>(rhs); powExpr && is<Pow>(powExpr->getFunction())) {
ArgumentPtrVector powExprChildren = powExpr->getChildren();
const ArgumentPtrVector &powExprChildren = powExpr->getChildren();
return {powExprChildren[1], powExprChildren[0]};
}

Expand Down
4 changes: 2 additions & 2 deletions src/fintamath/expressions/binary/PowExpression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,8 @@ ArgumentPtr PowExpression::powSimplify(const IFunction & /*func*/, const Argumen
ArgumentPtr res;

if (auto lhsExpr = cast<IExpression>(lhs); lhsExpr && is<Pow>(lhsExpr->getFunction())) {
auto lhsExprLhsChild = lhsExpr->getChildren().front();
auto lhsExprRhsChild = lhsExpr->getChildren().back();
const ArgumentPtr &lhsExprLhsChild = lhsExpr->getChildren().front();
const ArgumentPtr &lhsExprRhsChild = lhsExpr->getChildren().back();

bool canMul = is<Integer>(rhs) && is<Integer>(lhsExprRhsChild);

Expand Down
8 changes: 5 additions & 3 deletions src/fintamath/expressions/interfaces/IBinaryExpression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ std::string IBinaryExpression::toString() const {
return functionToString(*func, {lhsChild, rhsChild});
}

std::shared_ptr<IFunction> IBinaryExpression::getFunction() const {
const std::shared_ptr<IFunction> &IBinaryExpression::getFunction() const {
return func;
}

ArgumentPtrVector IBinaryExpression::getChildren() const {
return {lhsChild, rhsChild};
const ArgumentPtrVector &IBinaryExpression::getChildren() const {
childrenCached.front() = lhsChild;
childrenCached.back() = rhsChild;
return childrenCached;
}

ArgumentPtr IBinaryExpression::preSimplify() const {
Expand Down
10 changes: 5 additions & 5 deletions src/fintamath/expressions/interfaces/IPolynomExpression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@

namespace fintamath {

std::shared_ptr<IFunction> IPolynomExpression::getFunction() const {
const std::shared_ptr<IFunction> &IPolynomExpression::getFunction() const {
return func;
}

ArgumentPtrVector IPolynomExpression::getChildren() const {
const ArgumentPtrVector &IPolynomExpression::getChildren() const {
return children;
}

Expand Down Expand Up @@ -344,8 +344,8 @@ int IPolynomExpression::comparatorExpressions(const std::shared_ptr<const IExpre
auto lhsOper = cast<IOperator>(lhs->getFunction());
auto rhsOper = cast<IOperator>(rhs->getFunction());

ArgumentPtrVector lhsChildren = lhs->getChildren();
ArgumentPtrVector rhsChildren = rhs->getChildren();
const ArgumentPtrVector &lhsChildren = lhs->getChildren();
const ArgumentPtrVector &rhsChildren = rhs->getChildren();

if (lhsOper &&
lhsOper->getOperatorPriority() == IOperator::Priority::PrefixUnary &&
Expand Down Expand Up @@ -529,7 +529,7 @@ int IPolynomExpression::comparatorVariables(const ArgumentPtr &lhs, const Argume

std::shared_ptr<const Variable> IPolynomExpression::getNextVariable(ExpressionTreePathStack &stack) {
while (!stack.empty()) {
ArgumentPtrVector children = stack.top().first->getChildren();
const ArgumentPtrVector &children = stack.top().first->getChildren();

// TODO: looks weird
size_t &exprIndex = stack.top().second;
Expand Down
7 changes: 4 additions & 3 deletions src/fintamath/expressions/interfaces/IUnaryExpression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@ std::string IUnaryExpression::toString() const {
return functionToString(*func, {child});
}

std::shared_ptr<IFunction> IUnaryExpression::getFunction() const {
const std::shared_ptr<IFunction> &IUnaryExpression::getFunction() const {
return func;
}

ArgumentPtrVector IUnaryExpression::getChildren() const {
return {child};
const ArgumentPtrVector &IUnaryExpression::getChildren() const {
childrenCached.front() = child;
return childrenCached;
}

IUnaryExpression::SimplifyFunctionVector IUnaryExpression::getFunctionsForPreSimplify() const {
Expand Down
30 changes: 15 additions & 15 deletions src/fintamath/expressions/polynomial/AddExpression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,8 @@ ArgumentPtr AddExpression::logSimplify(const IFunction & /*func*/, const Argumen
return {};
}

ArgumentPtrVector lhsChildren = lhsExpr->getChildren();
ArgumentPtrVector rhsChildren = rhsExpr->getChildren();
const ArgumentPtrVector &lhsChildren = lhsExpr->getChildren();
const ArgumentPtrVector &rhsChildren = rhsExpr->getChildren();

if (*lhsChildren.front() == *rhsChildren.front()) {
ArgumentPtr logLhs = lhsChildren.front();
Expand All @@ -168,8 +168,8 @@ ArgumentPtr AddExpression::mulLogSimplify(const IFunction & /*func*/, const Argu
}

if (is<Mul>(lhsExpr->getFunction()) && is<Mul>(rhsExpr->getFunction())) {
ArgumentPtrVector lhsExprChildren = lhsExpr->getChildren();
ArgumentPtrVector rhsExprChildren = rhsExpr->getChildren();
const ArgumentPtrVector &lhsExprChildren = lhsExpr->getChildren();
const ArgumentPtrVector &rhsExprChildren = rhsExpr->getChildren();

std::vector<size_t> lhsLogChildrenIndexes = findLogarithms(lhsExprChildren);
std::vector<size_t> rhsLogChildrenIndexes = findLogarithms(rhsExprChildren);
Expand Down Expand Up @@ -208,7 +208,7 @@ ArgumentPtr AddExpression::mulLogSimplify(const IFunction & /*func*/, const Argu
return {};
}

ArgumentPtrVector mulExprChildren = mulExprChild->getChildren();
const ArgumentPtrVector &mulExprChildren = mulExprChild->getChildren();
std::vector<size_t> logChildrenIndexes = findLogarithms(mulExprChildren);

for (size_t i : logChildrenIndexes) {
Expand All @@ -234,7 +234,7 @@ std::pair<ArgumentPtr, ArgumentPtr> AddExpression::getRateValuePair(const Argume
if (const auto mulExprChild = cast<IExpression>(inChild);
mulExprChild && is<Mul>(mulExprChild->getFunction())) {

const ArgumentPtrVector mulExprChildren = mulExprChild->getChildren();
const ArgumentPtrVector &mulExprChildren = mulExprChild->getChildren();

if (is<INumber>(mulExprChildren.front())) {
rate = mulExprChildren.front();
Expand Down Expand Up @@ -315,19 +315,19 @@ ArgumentPtr AddExpression::sumSimplify(const IFunction & /*func*/, const Argumen
is<Div>(lhsExpr->getFunction()) && is<Div>(rhsExpr->getFunction())) {

if (*lhsExpr->getChildren().back() == *rhsExpr->getChildren().back()) {
ArgumentPtr lhsNumerator = lhsExpr->getChildren().front();
ArgumentPtr rhsNumerator = rhsExpr->getChildren().front();
ArgumentPtr rhsDenominator = rhsExpr->getChildren().back();
const ArgumentPtr &lhsNumerator = lhsExpr->getChildren().front();
const ArgumentPtr &rhsNumerator = rhsExpr->getChildren().front();
const ArgumentPtr &rhsDenominator = rhsExpr->getChildren().back();

ArgumentPtr numerator = addExpr(lhsNumerator, rhsNumerator);
ArgumentPtr denominator = rhsDenominator;
res = divExpr(numerator, denominator);
}
else {
ArgumentPtr lhsNumerator = lhsExpr->getChildren().front();
ArgumentPtr rhsNumerator = rhsExpr->getChildren().front();
ArgumentPtr lhsDenominator = lhsExpr->getChildren().back();
ArgumentPtr rhsDenominator = rhsExpr->getChildren().back();
const ArgumentPtr &lhsNumerator = lhsExpr->getChildren().front();
const ArgumentPtr &rhsNumerator = rhsExpr->getChildren().front();
const ArgumentPtr &lhsDenominator = lhsExpr->getChildren().back();
const ArgumentPtr &rhsDenominator = rhsExpr->getChildren().back();

ArgumentPtr lhsNumeratorMulRhsDenominator = mulExpr(lhsNumerator, rhsDenominator);
ArgumentPtr rhsNumeratorMulLhsDenominator = mulExpr(rhsNumerator, lhsDenominator);
Expand All @@ -338,8 +338,8 @@ ArgumentPtr AddExpression::sumSimplify(const IFunction & /*func*/, const Argumen
}
}
else if (rhsExpr && is<Div>(rhsExpr->getFunction())) {
ArgumentPtr rhsNumerator = rhsExpr->getChildren().front();
ArgumentPtr rhsDenominator = rhsExpr->getChildren().back();
const ArgumentPtr &rhsNumerator = rhsExpr->getChildren().front();
const ArgumentPtr &rhsDenominator = rhsExpr->getChildren().back();

ArgumentPtr lhsMulRhsDenominator = mulExpr(lhsChild, rhsDenominator);

Expand Down
2 changes: 1 addition & 1 deletion src/fintamath/expressions/polynomial/MulExpression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ std::pair<ArgumentPtr, ArgumentPtr> MulExpression::getRateValuePair(const Argume
if (const auto &powExpr = cast<IExpression>(rhsChild);
powExpr && is<Pow>(powExpr->getFunction())) {

ArgumentPtrVector powExprChildren = powExpr->getChildren();
const ArgumentPtrVector &powExprChildren = powExpr->getChildren();
return {powExprChildren[1], powExprChildren[0]};
}

Expand Down
4 changes: 2 additions & 2 deletions src/fintamath/expressions/polynomial/OrExpression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,8 @@ ArgumentPtr OrExpression::andSimplify(const IFunction & /*func*/, const Argument
return {};
}

ArgumentPtrVector lhsChildren = lhsExpr->getChildren();
ArgumentPtrVector rhsChildren = rhsExpr->getChildren();
const ArgumentPtrVector &lhsChildren = lhsExpr->getChildren();
const ArgumentPtrVector &rhsChildren = rhsExpr->getChildren();

if (rhsChildren.size() != lhsChildren.size()) {
return {};
Expand Down
Loading

0 comments on commit 69aca69

Please sign in to comment.