diff --git a/include/fintamath/core/CoreUtils.hpp b/include/fintamath/core/CoreUtils.hpp index 81abe73e5..5d03f73ed 100644 --- a/include/fintamath/core/CoreUtils.hpp +++ b/include/fintamath/core/CoreUtils.hpp @@ -2,24 +2,27 @@ #include #include -#include namespace fintamath { -namespace stdr = std::ranges; -namespace stdv = std::views; - template concept ConvertibleToAndNotSameAs = std::convertible_to && !std::same_as; -template +template concept TupleLike = requires { std::tuple_size::value; }; -template +template concept Hashable = requires(const T &v) { std::hash{}(v); }; +template +void repeat(const Number &num, Function &&func) { + for (Number i = 0; i < num; ++i) { + func(); + } +} + } diff --git a/include/fintamath/core/Hash.hpp b/include/fintamath/core/Hash.hpp index a41c2d970..f30824e02 100644 --- a/include/fintamath/core/Hash.hpp +++ b/include/fintamath/core/Hash.hpp @@ -7,10 +7,10 @@ namespace fintamath::detail { -template +template struct Hash; -template +template void hashCombine(size_t &seed, const T &v) noexcept { Hash hasher; seed ^= hasher(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2); diff --git a/include/fintamath/expressions/IExpressionCRTP.hpp b/include/fintamath/expressions/IExpressionCRTP.hpp index 68e86bd25..2ccde94db 100644 --- a/include/fintamath/expressions/IExpressionCRTP.hpp +++ b/include/fintamath/expressions/IExpressionCRTP.hpp @@ -24,7 +24,7 @@ class IExpressionCRTP_ : public IExpressionBaseCRTP { return false; } - for (const auto i : stdv::iota(0U, lhsChildren.size())) { + for (size_t i = 0U; i < lhsChildren.size(); i++) { 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 c523e6405..d65a87fb2 100644 --- a/include/fintamath/functions/IFunctionCRTP.hpp +++ b/include/fintamath/functions/IFunctionCRTP.hpp @@ -101,7 +101,7 @@ class IFunctionCRTP_ : public IFunction { bool doAnyArgsMatch(const ArgumentRefVector &argVect) const { using AnyArgsType = typename std::tuple_element_t<0, std::tuple>; - return stdr::all_of(argVect, [](const auto &arg) { + return std::ranges::all_of(argVect, [](const auto &arg) { return is(arg); }); } diff --git a/src/fintamath/core/Tokenizer.cpp b/src/fintamath/core/Tokenizer.cpp index b5302dd89..0f208c099 100644 --- a/src/fintamath/core/Tokenizer.cpp +++ b/src/fintamath/core/Tokenizer.cpp @@ -38,7 +38,7 @@ TokenVector Tokenizer::tokenize(std::string str) { void Tokenizer::registerToken(const Token &token) { auto &tokens = getRegisteredTokens(); - tokens.insert(stdr::upper_bound(tokens, token, [](const Token &lhs, const Token &rhs) { + tokens.insert(std::ranges::upper_bound(tokens, token, [](const Token &lhs, const Token &rhs) { return lhs.size() > rhs.size(); }), token); diff --git a/src/fintamath/expressions/Expression.cpp b/src/fintamath/expressions/Expression.cpp index 377f927c8..45cf78328 100644 --- a/src/fintamath/expressions/Expression.cpp +++ b/src/fintamath/expressions/Expression.cpp @@ -151,7 +151,7 @@ TermVector Expression::tokensToTerms(TokenVector &tokens) { TermVector terms(tokens.size()); - for (const auto i : stdv::iota(0U, terms.size())) { + for (size_t i = 0U; i < terms.size(); i++) { terms[i] = parseTerm(tokens[i]); } @@ -338,7 +338,7 @@ void Expression::fixOperatorTypes(TermVector &terms) { return; } - for (const auto i : stdv::iota(1U, terms.size() - 1)) { + for (size_t i = 1; i + 1 < terms.size(); i++) { auto &term = terms[i]; const auto &termPrev = terms[i - 1]; @@ -351,7 +351,7 @@ void Expression::fixOperatorTypes(TermVector &terms) { } } - for (const auto i : stdv::iota(1U, terms.size() - 1) | stdv::reverse) { + for (size_t i = terms.size() - 1; i > 0; i--) { auto &term = terms[i]; const auto &termNext = terms[i + 1]; @@ -457,7 +457,7 @@ void Expression::validateFunctionArgs(const IFunction &func, const ArgumentPtrVe const bool doesArgSizeMatch = !func.isVariadic() && args.size() == expectedArgTypes.size(); MathObjectClass expectedType = expectedArgTypes.front(); - for (const auto i : stdv::iota(0U, args.size())) { + for (size_t i = 0; i < args.size(); i++) { if (doesArgSizeMatch) { expectedType = expectedArgTypes[i]; } @@ -503,7 +503,7 @@ bool Expression::doesArgMatch(const MathObjectClass &expectedType, const Argumen namespace detail { std::unique_ptr makeExpr(const IFunction &func, ArgumentPtrVector args) { - stdr::transform(args, args.begin(), &Expression::compress); + std::ranges::transform(args, args.begin(), &Expression::compress); Expression::validateFunctionArgs(func, args); if (const auto strToConstr = Expression::getExpressionMaker().find(func.getClass()); diff --git a/src/fintamath/expressions/ExpressionComparator.cpp b/src/fintamath/expressions/ExpressionComparator.cpp index 60fb57dd6..8e57661e6 100644 --- a/src/fintamath/expressions/ExpressionComparator.cpp +++ b/src/fintamath/expressions/ExpressionComparator.cpp @@ -71,7 +71,7 @@ Ordering reverse(Ordering ordering); template size_t getPositionOfFirstChildWithTerm(const ArgumentPtrVector &children) { - for (const auto i : stdv::iota(0U, children.size())) { + for (size_t i = 0; i < children.size(); i++) { if (containsIf(children[i], [](const ArgumentPtr &child) { return is(child); })) { return i; } @@ -429,7 +429,7 @@ ChildrenComparatorResult compareChildren(const ArgumentPtrVector &lhsChildren, auto size = std::min(std::max(lhsStart, rhsStart), std::min(lhsChildren.size(), rhsChildren.size())); - for (const auto i : stdv::iota(0U, size)) { + for (size_t i = 0; i < size; i++) { const Ordering childrenComp = compare(lhsChildren[i], rhsChildren[i], options); if (childrenComp != Ordering::equal) { diff --git a/src/fintamath/expressions/ExpressionUtils.cpp b/src/fintamath/expressions/ExpressionUtils.cpp index 99ad079c3..7b3992eb2 100644 --- a/src/fintamath/expressions/ExpressionUtils.cpp +++ b/src/fintamath/expressions/ExpressionUtils.cpp @@ -90,7 +90,7 @@ bool containsIf(const ArgumentPtr &arg, const std::functiongetChildren(); - return stdr::any_of(children, [&comp](const auto &child) { + return std::ranges::any_of(children, [&comp](const auto &child) { bool res = false; if (containsIf(child, comp)) { @@ -226,12 +226,9 @@ std::pair splitRational(const ArgumentPtr &arg) { ArgumentPtr negate(const ArgumentPtr &arg) { if (const auto expr = cast(arg)) { if (is(expr->getFunction())) { - auto negChildrenView = - expr->getChildren() | - stdv::transform([](const ArgumentPtr &child) { - return negate(child); - }); - return makePolynom(Add{}, ArgumentPtrVector(negChildrenView.begin(), negChildrenView.end())); // TODO: use C++23 stdv::to + ArgumentPtrVector negChildren = expr->getChildren(); + std::ranges::transform(negChildren, negChildren.begin(), &negate); + return makePolynom(Add{}, std::move(negChildren)); } if (is(expr->getFunction())) { @@ -281,7 +278,7 @@ ArgumentPtrVector getPolynomChildren(const IFunction &func, const ArgumentPtr &a std::vector argumentVectorToStringVector(const ArgumentPtrVector &args) { std::vector argStrings(args.size()); - for (const auto i : stdv::iota(0U, argStrings.size())) { + for (size_t i = 0; i < argStrings.size(); i++) { argStrings[i] = args[i].get()->toString(); } diff --git a/src/fintamath/expressions/IExpression.cpp b/src/fintamath/expressions/IExpression.cpp index 5a358a5d4..6be1d77ea 100644 --- a/src/fintamath/expressions/IExpression.cpp +++ b/src/fintamath/expressions/IExpression.cpp @@ -41,8 +41,8 @@ std::vector IExpression::getVariables() const { } } - stdr::sort(vars, std::less{}, &Variable::toString); - auto unique = stdr::unique(vars); + std::ranges::sort(vars, std::less{}, &Variable::toString); + auto unique = std::ranges::unique(vars); vars.erase(unique.begin(), unique.end()); return vars; diff --git a/src/fintamath/expressions/binary/CompExpr.cpp b/src/fintamath/expressions/binary/CompExpr.cpp index e7f16fc07..89c539209 100644 --- a/src/fintamath/expressions/binary/CompExpr.cpp +++ b/src/fintamath/expressions/binary/CompExpr.cpp @@ -216,7 +216,7 @@ ArgumentPtr CompExpr::rateSimplify(const IFunction &func, const ArgumentPtr &lhs return {}; } - for (auto &child : stdv::drop(children, 1)) { + for (auto &child : children | std::views::drop(1)) { child = divExpr(child, rate); } diff --git a/src/fintamath/expressions/binary/DerivativeExpr.cpp b/src/fintamath/expressions/binary/DerivativeExpr.cpp index 340e86e08..2921c1580 100644 --- a/src/fintamath/expressions/binary/DerivativeExpr.cpp +++ b/src/fintamath/expressions/binary/DerivativeExpr.cpp @@ -139,7 +139,7 @@ ArgumentPtr DerivativeExpr::exprSimplify(const std::shared_ptr &var) { ArgumentPtrVector newChildren = children; - stdr::transform(newChildren, newChildren.begin(), [&var](const ArgumentPtr &child) { + std::ranges::transform(newChildren, newChildren.begin(), [&var](const ArgumentPtr &child) { return derivativeExpr(child, var); }); diff --git a/src/fintamath/expressions/binary/DivExpr.cpp b/src/fintamath/expressions/binary/DivExpr.cpp index 0c92896ff..a269809a6 100644 --- a/src/fintamath/expressions/binary/DivExpr.cpp +++ b/src/fintamath/expressions/binary/DivExpr.cpp @@ -188,11 +188,9 @@ ArgumentPtr DivExpr::mulSimplify(const SimplifyFunctionVector &simplFuncs, const size_t rhsChildrenSizeInitial = rhsChildren.size(); // TODO: use more efficient algorithm - for (const auto i : stdv::iota(0U, lhsChildren.size())) { - auto &lhsChild = lhsChildren[i]; - - for (const auto j : stdv::iota(0U, rhsChildren.size())) { - const auto &rhsChild = rhsChildren[j]; + for (auto &lhsChild : lhsChildren) { + for (size_t i = 0; i < rhsChildren.size(); i++) { + const auto &rhsChild = rhsChildren[i]; ArgumentPtr res = useSimplifyFunctions(simplFuncs, func, @@ -201,7 +199,7 @@ ArgumentPtr DivExpr::mulSimplify(const SimplifyFunctionVector &simplFuncs, if (res && !is(res) && *res != *makeExpr(func, lhsChild, rhsChild)) { lhsChild = std::move(res); - rhsChildren.erase(rhsChildren.begin() + j); + rhsChildren.erase(rhsChildren.begin() + static_cast(i)); break; } } @@ -394,7 +392,7 @@ std::pair DivExpr::mulSumSimplify(const ArgumentPtr &l ArgumentPtrVector multiplicators; - for (const auto &rhsChild : rhsChildren | stdv::drop(1)) { + for (const auto &rhsChild : rhsChildren | std::views::drop(1)) { multiplicators.emplace_back(mulExpr(rhsChild, result)); } diff --git a/src/fintamath/expressions/binary/PowExpr.cpp b/src/fintamath/expressions/binary/PowExpr.cpp index 647f8bbe7..de3616aba 100644 --- a/src/fintamath/expressions/binary/PowExpr.cpp +++ b/src/fintamath/expressions/binary/PowExpr.cpp @@ -137,10 +137,10 @@ PowExpr::SimplifyFunctionVector PowExpr::getFunctionsForPostSimplify() const { Integer PowExpr::generateFirstNum(const Integer &countOfOne) { Integer n = 0; - for ([[maybe_unused]] const auto _ : stdv::iota(0U, countOfOne)) { + repeat(countOfOne, [&]() { n <<= 1; n |= 1; - } + }); return n; } @@ -193,21 +193,21 @@ ArgumentPtr PowExpr::sumPolynomSimplify(const ArgumentPtr &expr, const Integer & ArgumentPtrVector newChildren; - for ([[maybe_unused]] const auto _ : stdv::iota(0U, combins)) { + repeat(combins, [&]() { std::vector vectOfPows = getPartition(bitNumber, Integer(variableCount)); bitNumber = generateNextNumber(bitNumber); ArgumentPtrVector mulExprChildren; mulExprChildren.emplace_back(multinomialCoefficient(power, vectOfPows).clone()); - for (const auto i : stdv::iota(0U, variableCount)) { + for (size_t i = 0; i < variableCount; i++) { ArgumentPtr powExprChild = powExpr(polynom[i], vectOfPows[i].clone()); mulExprChildren.emplace_back(std::move(powExprChild)); } ArgumentPtr mulExprChild = mulExpr(std::move(mulExprChildren)); newChildren.emplace_back(std::move(mulExprChild)); - } + }); return addExpr(std::move(newChildren)); } diff --git a/src/fintamath/expressions/interfaces/IPolynomExpression.cpp b/src/fintamath/expressions/interfaces/IPolynomExpression.cpp index 74509803d..bba48aab8 100644 --- a/src/fintamath/expressions/interfaces/IPolynomExpression.cpp +++ b/src/fintamath/expressions/interfaces/IPolynomExpression.cpp @@ -48,7 +48,7 @@ std::string IPolynomExpression::toString() const { result += childToString(*oper, children.front(), {}); - for (const auto i : stdv::iota(1U, children.size())) { + for (size_t i = 1; i < children.size(); i++) { const std::string childStr = childToString(*oper, children[i], children[i - 1]); if (childStr.size() > 2 && childStr[0] == ' ' && std::isdigit(childStr[1]) && std::isdigit(result.back())) { @@ -225,7 +225,7 @@ void IPolynomExpression::setChildren(const ArgumentPtrVector &childVect) { } void IPolynomExpression::sort() { - stdr::stable_sort(children, [this](const ArgumentPtr &lhs, const ArgumentPtr &rhs) { + std::ranges::stable_sort(children, [this](const ArgumentPtr &lhs, const ArgumentPtr &rhs) { return compare(lhs, rhs) == std::strong_ordering::greater; }); } diff --git a/src/fintamath/expressions/polynomial/AddExpr.cpp b/src/fintamath/expressions/polynomial/AddExpr.cpp index af2354d9c..62ab8e2a8 100644 --- a/src/fintamath/expressions/polynomial/AddExpr.cpp +++ b/src/fintamath/expressions/polynomial/AddExpr.cpp @@ -236,7 +236,7 @@ ArgumentPtr AddExpr::mulLogSimplify(const IFunction & /*func*/, const ArgumentPt std::vector AddExpr::findLogarithms(const ArgumentPtrVector &children) { std::vector indices; - for (const auto i : stdv::iota(0U, children.size())) { + for (size_t i = 0; i < children.size(); i++) { if (const auto childExpr = cast(children[i]); childExpr && is(childExpr->getFunction())) { diff --git a/src/fintamath/expressions/polynomial/OrExpr.cpp b/src/fintamath/expressions/polynomial/OrExpr.cpp index 3fd2bbe34..5fd63d6e1 100644 --- a/src/fintamath/expressions/polynomial/OrExpr.cpp +++ b/src/fintamath/expressions/polynomial/OrExpr.cpp @@ -145,7 +145,7 @@ ArgumentPtr OrExpr::andSimplify(const IFunction & /*func*/, const ArgumentPtr &l size_t resolutionIndex = lhsChildren.size(); - for (const auto i : stdv::iota(0U, lhsChildren.size())) { + for (size_t i = 0; i < lhsChildren.size(); i++) { ArgumentPtr lhsSubChild = lhsChildren[i]; ArgumentPtr rhsSubChild = rhsChildren[i]; diff --git a/src/fintamath/functions/IFunction.cpp b/src/fintamath/functions/IFunction.cpp index a85518b1b..eee5bd9ed 100644 --- a/src/fintamath/functions/IFunction.cpp +++ b/src/fintamath/functions/IFunction.cpp @@ -17,7 +17,7 @@ void IFunction::validateArgsSize(const ArgumentRefVector &argVect) const { std::vector argNameVect(argVect.size()); - for (const auto i : stdv::iota(0U, argNameVect.size())) { + for (size_t i = 0; i < argNameVect.size(); i++) { argNameVect[i] = argVect[i].get().toString(); } diff --git a/src/fintamath/functions/calculus/Max.cpp b/src/fintamath/functions/calculus/Max.cpp index 34c83fe7c..8c2e58574 100644 --- a/src/fintamath/functions/calculus/Max.cpp +++ b/src/fintamath/functions/calculus/Max.cpp @@ -15,7 +15,7 @@ namespace fintamath { std::unique_ptr Max::call(const ArgumentRefVector &argVect) const { std::reference_wrapper res = cast(argVect.front().get()); - for (const auto &arg : argVect | stdv::drop(1)) { + for (const auto &arg : argVect | std::views::drop(1)) { if (is(arg)) { return {}; } diff --git a/src/fintamath/functions/calculus/Min.cpp b/src/fintamath/functions/calculus/Min.cpp index 6010020b4..a3bd2338a 100644 --- a/src/fintamath/functions/calculus/Min.cpp +++ b/src/fintamath/functions/calculus/Min.cpp @@ -15,7 +15,7 @@ namespace fintamath { std::unique_ptr Min::call(const ArgumentRefVector &argVect) const { std::reference_wrapper res = cast(argVect.front().get()); - for (const auto &arg : argVect | stdv::drop(1)) { + for (const auto &arg : argVect | std::views::drop(1)) { if (is(arg)) { return {}; } diff --git a/src/fintamath/numbers/Rational.cpp b/src/fintamath/numbers/Rational.cpp index f25159cfc..7d513a96a 100644 --- a/src/fintamath/numbers/Rational.cpp +++ b/src/fintamath/numbers/Rational.cpp @@ -27,7 +27,7 @@ Rational::Rational(const std::string &str) { } int64_t firstDigitNum = 0; - const int64_t firstDotNum = stdr::distance(str.begin(), stdr::find(str, '.')); + const int64_t firstDotNum = std::ranges::distance(str.begin(), std::ranges::find(str, '.')); bool isNegative = false; if (str.front() == '-') { diff --git a/tests/src/core/CoreUtilsTests.cpp b/tests/src/core/CoreUtilsTests.cpp index 32abc5e91..0f709b553 100644 --- a/tests/src/core/CoreUtilsTests.cpp +++ b/tests/src/core/CoreUtilsTests.cpp @@ -2,4 +2,6 @@ #include "fintamath/core/CoreUtils.hpp" -// Nothing to test yet +TEST(CoreUtilsTests, repeatTest) { + // TODO: implement +} diff --git a/tests/src/core/HashTests.cpp b/tests/src/core/HashTests.cpp index 23b67de50..5e17f9b8e 100644 --- a/tests/src/core/HashTests.cpp +++ b/tests/src/core/HashTests.cpp @@ -60,38 +60,38 @@ TEST(HashTests, hashCombineTest) { TEST(HashTests, hashTupleTest) { { - std::tuple tuple1 = {123, 1.23, "abc"}; - std::tuple tuple2 = {123, 1.23, "abc"}; + std::tuple tuple1 = {123, 1.23, std::string("abc")}; + std::tuple tuple2 = {123, 1.23, std::string("abc")}; EXPECT_EQ(Hash{}(tuple1), Hash{}(tuple2)); } { - std::tuple tuple1 = {124, 1.23, "abc"}; - std::tuple tuple2 = {123, 1.23, "abc"}; + std::tuple tuple1 = {124, 1.23, std::string("abc")}; + std::tuple tuple2 = {123, 1.23, std::string("abc")}; EXPECT_NE(Hash{}(tuple1), Hash{}(tuple2)); } { - std::tuple tuple1 = {123, 1.23, "abc"}; - std::tuple tuple2 = {123, 2.23, "abc"}; + std::tuple tuple1 = {123, 1.23, std::string("abc")}; + std::tuple tuple2 = {123, 2.23, std::string("abc")}; EXPECT_NE(Hash{}(tuple1), Hash{}(tuple2)); } { std::tuple tuple1 = {123, 1.23, "abd"}; - std::tuple tuple2 = {123, 1.23, "abc"}; + std::tuple tuple2 = {123, 1.23, std::string("abc")}; EXPECT_NE(Hash{}(tuple1), Hash{}(tuple2)); } { std::tuple tuple1 = {123, "abd", 1.23}; - std::tuple tuple2 = {123, 1.23, "abc"}; + std::tuple tuple2 = {123, 1.23, std::string("abc")}; EXPECT_NE(Hash{}(tuple1), Hash{}(tuple2)); } { std::tuple tuple1 = {123, 1.23, "abd"}; - std::tuple tuple2 = {1.23, "abc", 123}; + std::tuple tuple2 = {1.23, std::string("abc"), 123}; EXPECT_NE(Hash{}(tuple1), Hash{}(tuple2)); }