From 6b30d993403617a24cb07fe38344b0b383ad7999 Mon Sep 17 00:00:00 2001 From: fintarin Date: Sat, 23 Sep 2023 09:42:10 +0300 Subject: [PATCH] Use std::views --- .../fintamath/expressions/IExpressionCRTP.hpp | 2 +- include/fintamath/functions/IFunctionCRTP.hpp | 2 +- include/fintamath/parser/Parser.hpp | 9 +++-- src/fintamath/expressions/Expression.cpp | 40 +++++++++++++------ src/fintamath/expressions/ExpressionUtils.cpp | 2 +- .../expressions/binary/DivExpression.cpp | 15 ++++--- .../expressions/binary/PowExpression.cpp | 18 +++++---- 7 files changed, 55 insertions(+), 33 deletions(-) diff --git a/include/fintamath/expressions/IExpressionCRTP.hpp b/include/fintamath/expressions/IExpressionCRTP.hpp index 699f4610c..f64c7ae3d 100644 --- a/include/fintamath/expressions/IExpressionCRTP.hpp +++ b/include/fintamath/expressions/IExpressionCRTP.hpp @@ -24,7 +24,7 @@ class IExpressionCRTP_ : public IExpressionBaseCRTP { return false; } - for (size_t i = 0; i < lhsChildren.size(); i++) { + for (auto i : std::views::iota(0U, lhsChildren.size())) { if (lhsChildren[i] != rhsChildren[i] && *lhsChildren[i] != *rhsChildren[i]) { return false; } diff --git a/include/fintamath/functions/IFunctionCRTP.hpp b/include/fintamath/functions/IFunctionCRTP.hpp index e4289218a..63d2c8888 100644 --- a/include/fintamath/functions/IFunctionCRTP.hpp +++ b/include/fintamath/functions/IFunctionCRTP.hpp @@ -116,7 +116,7 @@ class IFunctionCRTP_ : public IFunction { void throwInvalidInputFunctionException(const ArgumentRefVector &argsVect) const { std::vector argNamesVect(argsVect.size()); - for (size_t i = 0; i < argNamesVect.size(); i++) { + for (auto i : std::views::iota(0U, argNamesVect.size())) { argNamesVect[i] = argsVect[i].get().toString(); } diff --git a/include/fintamath/parser/Parser.hpp b/include/fintamath/parser/Parser.hpp index 359a64678..6997e82d3 100644 --- a/include/fintamath/parser/Parser.hpp +++ b/include/fintamath/parser/Parser.hpp @@ -3,6 +3,7 @@ #include #include #include +#include #include #include @@ -71,7 +72,7 @@ class Parser { const auto &valuePairs = parserMap.equal_range(parsedStr); - for (auto pair = valuePairs.first; pair != valuePairs.second; pair++) { + for (auto pair : std::views::iota(valuePairs.first, valuePairs.second)) { if (Return value = pair->second(args...)) { return value; } @@ -88,7 +89,7 @@ class Parser { const auto &valuePairs = parserMap.equal_range(parsedStr); - for (auto pair = valuePairs.first; pair != valuePairs.second; pair++) { + for (auto pair : std::views::iota(valuePairs.first, valuePairs.second)) { if (Return value = pair->second(move(args)...)) { return value; } @@ -105,7 +106,7 @@ class Parser { const auto &valuePairs = parserMap.equal_range(parsedStr); - for (auto pair = valuePairs.first; pair != valuePairs.second; pair++) { + for (auto pair : std::views::iota(valuePairs.first, valuePairs.second)) { if (Return value = pair->second(args...); value && comp(value)) { return value; } @@ -123,7 +124,7 @@ class Parser { const auto &valuePairs = parserMap.equal_range(parsedStr); - for (auto pair = valuePairs.first; pair != valuePairs.second; pair++) { + for (auto pair : std::views::iota(valuePairs.first, valuePairs.second)) { if (Return value = pair->second(move(args)...); value && comp(value)) { return value; } diff --git a/src/fintamath/expressions/Expression.cpp b/src/fintamath/expressions/Expression.cpp index 8d4b82e52..3c292cf07 100644 --- a/src/fintamath/expressions/Expression.cpp +++ b/src/fintamath/expressions/Expression.cpp @@ -83,10 +83,10 @@ bool Expression::parseOperator(const TermVector &terms, size_t start, size_t end IOperator::Priority foundOperPriority = IOperator::Priority::Any; bool isPreviousTermBinaryOper = false; - for (size_t i = start; i < end; i++) { + size_t i = start; + while (i < end) { if (skipBrackets(terms, i)) { isPreviousTermBinaryOper = false; - i--; continue; } @@ -112,6 +112,8 @@ bool Expression::parseOperator(const TermVector &terms, size_t start, size_t end else { isPreviousTermBinaryOper = false; } + + i++; } if (foundOperPos == std::numeric_limits::max()) { @@ -181,7 +183,8 @@ ArgumentPtrVector Expression::parseFunctionArgs(const TermVector &terms, size_t ArgumentPtrVector funcArgs; - for (size_t i = start; i < end; i++) { + size_t i = start; + while (i < end) { if (terms[i]->name == "(") { skipBrackets(terms, i); } @@ -202,6 +205,8 @@ ArgumentPtrVector Expression::parseFunctionArgs(const TermVector &terms, size_t return funcArgs; } + + i++; } funcArgs.emplace_back(parseExpr(terms, start, end)); @@ -281,7 +286,7 @@ TermVector Expression::tokensToTerms(const TokenVector &tokens) { TermVector terms(tokens.size()); - for (size_t i = 0; i < tokens.size(); i++) { + for (auto i : std::views::iota(0U, terms.size())) { if (auto term = Parser::parse(getTermMakers(), (tokens[i]))) { terms[i] = std::move(term); } @@ -300,7 +305,8 @@ TermVector Expression::tokensToTerms(const TokenVector &tokens) { void Expression::insertMultiplications(TermVector &terms) { static const ArgumentPtr mul = Mul().clone(); - for (size_t i = 1; i < terms.size(); i++) { + size_t i = 1; + while (i < terms.size()) { if (canNextTermBeBinaryOperator(*terms[i - 1]) && canPrevTermBeBinaryOperator(*terms[i])) { @@ -308,6 +314,8 @@ void Expression::insertMultiplications(TermVector &terms) { terms.insert(terms.begin() + ptrdiff_t(i), term); i++; } + + i++; } } @@ -334,7 +342,7 @@ void Expression::fixOperatorTypes(TermVector &terms) { return; } - for (size_t i = 1; i < terms.size() - 1; i++) { + for (auto i : std::views::iota(1U, terms.size() - 1)) { const auto &term = terms[i]; const auto &termPrev = terms[i - 1]; @@ -347,7 +355,8 @@ void Expression::fixOperatorTypes(TermVector &terms) { } } - for (size_t i = terms.size() - 2; i > 1; i--) { + // TODO: use reverse(iota(1, terms.size() - 1)) when it is work + for (size_t i = terms.size() - 2; i > 0; i--) { const auto &term = terms[i]; const auto &termNext = terms[i + 1]; @@ -366,7 +375,8 @@ void Expression::fixOperatorTypes(TermVector &terms) { } void Expression::collapseFactorials(TermVector &terms) { - for (size_t i = 1; i < terms.size() - 1; i++) { + size_t i = 1; + while (i < terms.size() - 1) { const auto &term = terms[i]; const auto &termNext = terms[i + 1]; @@ -381,6 +391,8 @@ void Expression::collapseFactorials(TermVector &terms) { terms.erase(terms.begin() + ptrdiff_t(i) + 1); i--; } + + i++; } } @@ -406,7 +418,7 @@ bool Expression::skipBrackets(const TermVector &terms, size_t &openBracketIndex) int64_t brackets = 0; - for (size_t i = openBracketIndex; i < terms.size(); i++) { + for (auto i : std::views::iota(openBracketIndex, terms.size())) { const auto &term = terms[i]; if (term->name == "(") { @@ -479,8 +491,12 @@ void Expression::validateChild(const ArgumentPtr &inChild) { validateFunctionArgs(func, children); } else { - for (size_t i = 0; i + 1 < children.size(); i++) { - for (size_t j = i + 1; j < children.size(); j++) { + if (children.empty()) { + throw InvalidInputFunctionException(func->toString(), {}); + } + + for (auto i : std::views::iota(0U, children.size() - 1)) { + for (auto j : std::views::iota(i + 1, children.size())) { validateFunctionArgs(func, {children[i], children[j]}); } } @@ -502,7 +518,7 @@ void Expression::validateFunctionArgs(const std::shared_ptr &func, co childrenTypes = ArgumentTypeVector(args.size(), childrenTypes.front()); } - for (size_t i = 0; i < args.size(); i++) { + for (auto i : std::views::iota(0U, args.size())) { const ArgumentPtr &arg = args[i]; const MathObjectType Type = childrenTypes[i]; diff --git a/src/fintamath/expressions/ExpressionUtils.cpp b/src/fintamath/expressions/ExpressionUtils.cpp index 1979b8bdd..52bcd61f3 100644 --- a/src/fintamath/expressions/ExpressionUtils.cpp +++ b/src/fintamath/expressions/ExpressionUtils.cpp @@ -233,7 +233,7 @@ bool isNegated(const ArgumentPtr &arg) { std::vector argumentVectorToStringVector(const ArgumentPtrVector &args) { std::vector argStrings(args.size()); - for (size_t i = 0; i < argStrings.size(); i++) { + for (auto i : std::views::iota(0U, argStrings.size())) { argStrings[i] = args[i].get()->toString(); } diff --git a/src/fintamath/expressions/binary/DivExpression.cpp b/src/fintamath/expressions/binary/DivExpression.cpp index 4439c7faf..8479a3ab6 100644 --- a/src/fintamath/expressions/binary/DivExpression.cpp +++ b/src/fintamath/expressions/binary/DivExpression.cpp @@ -160,16 +160,19 @@ ArgumentPtr DivExpression::mulSimplify(const IFunction &func, const ArgumentPtr size_t lhsChildrenSizeInitial = lhsChildren.size(); size_t rhsChildrenSizeInitial = rhsChildren.size(); - for (auto &lhsChild : lhsChildren) { - for (size_t j = 0; j < rhsChildren.size(); j++) { - ArgumentPtr res = constSimplify(func, lhsChild, rhsChildren[j]); + for (auto i : std::views::iota(0U, lhsChildren.size())) { + auto &lhsChild = lhsChildren[i]; + + for (auto j : std::views::iota(0U, rhsChildren.size())) { + const auto &rhsChild = rhsChildren[j]; + ArgumentPtr res = constSimplify(func, lhsChild, rhsChild); if (!res) { - res = callFunction(Div(), {lhsChild, rhsChildren[j]}); + res = callFunction(Div(), {lhsChild, rhsChild}); } if (!res) { - res = powSimplify(lhsChild, rhsChildren[j]); + res = powSimplify(lhsChild, rhsChild); } if (res && !is(res)) { @@ -360,7 +363,7 @@ std::pair DivExpression::mulSumSimplify(const Argument ArgumentPtrVector multiplicator; - for (size_t i = 1; i < rhsChildren.size(); i++) { + for (auto i : std::views::iota(1U, rhsChildren.size())) { multiplicator.emplace_back(mulExpr(rhsChildren[i], result)); } diff --git a/src/fintamath/expressions/binary/PowExpression.cpp b/src/fintamath/expressions/binary/PowExpression.cpp index 57f49e687..6adfca30b 100644 --- a/src/fintamath/expressions/binary/PowExpression.cpp +++ b/src/fintamath/expressions/binary/PowExpression.cpp @@ -98,8 +98,9 @@ PowExpression::SimplifyFunctionVector PowExpression::getFunctionsForPostSimplify Integer PowExpression::generateFirstNum(const Integer &countOfOne) { Integer n = 0; - for (int i = 0; i < countOfOne; i++) { - n = n << 1 | 1; + for ([[maybe_unused]] auto _ : std::views::iota(0U, countOfOne)) { + n <<= 1; + n |= 1; } return n; @@ -147,19 +148,20 @@ ArgumentPtr PowExpression::sumPolynomSimplify(const ArgumentPtr &expr, const Int return {}; } - ArgumentPtrVector newPolynom; - Integer variableCount = int64_t(polynom.size()); - + size_t variableCount = polynom.size(); Integer bitNumber = generateFirstNum(powValue); + Integer combins = combinations(powValue + variableCount - 1, powValue); + + ArgumentPtrVector newPolynom; - for (int i = 0; i < combinations(powValue + variableCount - 1, powValue); i++) { - std::vector vectOfPows = getPartition(bitNumber, variableCount); + for ([[maybe_unused]] auto _ : std::views::iota(0U, combins)) { + std::vector vectOfPows = getPartition(bitNumber, Integer(variableCount)); bitNumber = generateNextNumber(bitNumber); ArgumentPtrVector mulExprPolynom; mulExprPolynom.emplace_back(multinomialCoefficient(powValue, vectOfPows).clone()); - for (size_t j = 0; j < size_t(variableCount); j++) { + for (auto j : std::views::iota(0U, variableCount)) { ArgumentPtr powExprChild = powExpr(polynom[j], vectOfPows[j].clone()); mulExprPolynom.emplace_back(powExprChild); }