Skip to content

Commit

Permalink
Use std::views
Browse files Browse the repository at this point in the history
  • Loading branch information
fintarin committed Sep 23, 2023
1 parent 66a339e commit 6b30d99
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 33 deletions.
2 changes: 1 addition & 1 deletion include/fintamath/expressions/IExpressionCRTP.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class IExpressionCRTP_ : public IExpressionBaseCRTP<Derived, isMultiFunction> {
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;
}
Expand Down
2 changes: 1 addition & 1 deletion include/fintamath/functions/IFunctionCRTP.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ class IFunctionCRTP_ : public IFunction {
void throwInvalidInputFunctionException(const ArgumentRefVector &argsVect) const {
std::vector<std::string> 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();
}

Expand Down
9 changes: 5 additions & 4 deletions include/fintamath/parser/Parser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <functional>
#include <map>
#include <memory>
#include <ranges>
#include <string>
#include <vector>

Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand Down
40 changes: 28 additions & 12 deletions src/fintamath/expressions/Expression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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<size_t>::max()) {
Expand Down Expand Up @@ -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);
}
Expand All @@ -202,6 +205,8 @@ ArgumentPtrVector Expression::parseFunctionArgs(const TermVector &terms, size_t

return funcArgs;
}

i++;
}

funcArgs.emplace_back(parseExpr(terms, start, end));
Expand Down Expand Up @@ -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);
}
Expand All @@ -300,14 +305,17 @@ 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])) {

auto term = std::make_shared<Term>(mul->toString(), mul);
terms.insert(terms.begin() + ptrdiff_t(i), term);
i++;
}

i++;
}
}

Expand All @@ -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];

Expand All @@ -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];

Expand All @@ -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];

Expand All @@ -381,6 +391,8 @@ void Expression::collapseFactorials(TermVector &terms) {
terms.erase(terms.begin() + ptrdiff_t(i) + 1);
i--;
}

i++;
}
}

Expand All @@ -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 == "(") {
Expand Down Expand Up @@ -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]});
}
}
Expand All @@ -502,7 +518,7 @@ void Expression::validateFunctionArgs(const std::shared_ptr<IFunction> &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];

Expand Down
2 changes: 1 addition & 1 deletion src/fintamath/expressions/ExpressionUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ bool isNegated(const ArgumentPtr &arg) {
std::vector<std::string> argumentVectorToStringVector(const ArgumentPtrVector &args) {
std::vector<std::string> 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();
}

Expand Down
15 changes: 9 additions & 6 deletions src/fintamath/expressions/binary/DivExpression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<Rational>(res)) {
Expand Down Expand Up @@ -360,7 +363,7 @@ std::pair<ArgumentPtr, ArgumentPtr> 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));
}

Expand Down
18 changes: 10 additions & 8 deletions src/fintamath/expressions/binary/PowExpression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Integer> vectOfPows = getPartition(bitNumber, variableCount);
for ([[maybe_unused]] auto _ : std::views::iota(0U, combins)) {
std::vector<Integer> 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);
}
Expand Down

0 comments on commit 6b30d99

Please sign in to comment.