Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #230

Merged
merged 4 commits into from
Apr 5, 2024
Merged

Fixes #230

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions include/fintamath/core/CoreUtils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,27 @@

#include <concepts>
#include <functional>
#include <ranges>

namespace fintamath {

namespace stdr = std::ranges;
namespace stdv = std::views;

template <typename From, typename To>
concept ConvertibleToAndNotSameAs = std::convertible_to<From, To> && !std::same_as<From, To>;

template <class T>
template <typename T>
concept TupleLike = requires {
std::tuple_size<T>::value;
};

template <class T>
template <typename T>
concept Hashable = requires(const T &v) {
std::hash<T>{}(v);
};

template <typename Number, typename Function>
void repeat(const Number &num, Function &&func) {
for (Number i = 0; i < num; ++i) {
func();
}
}

}
4 changes: 2 additions & 2 deletions include/fintamath/core/Hash.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@

namespace fintamath::detail {

template <class T>
template <typename T>
struct Hash;

template <class T>
template <typename T>
void hashCombine(size_t &seed, const T &v) noexcept {
Hash<T> hasher;
seed ^= hasher(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
Expand Down
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> {
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;
}
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 @@ -101,7 +101,7 @@ class IFunctionCRTP_ : public IFunction {
bool doAnyArgsMatch(const ArgumentRefVector &argVect) const {
using AnyArgsType = typename std::tuple_element_t<0, std::tuple<Args...>>;

return stdr::all_of(argVect, [](const auto &arg) {
return std::ranges::all_of(argVect, [](const auto &arg) {
return is<AnyArgsType>(arg);
});
}
Expand Down
2 changes: 1 addition & 1 deletion src/fintamath/core/Tokenizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
10 changes: 5 additions & 5 deletions src/fintamath/expressions/Expression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}

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

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

Expand Down Expand Up @@ -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];
}
Expand Down Expand Up @@ -503,7 +503,7 @@ bool Expression::doesArgMatch(const MathObjectClass &expectedType, const Argumen
namespace detail {

std::unique_ptr<IMathObject> 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());
Expand Down
4 changes: 2 additions & 2 deletions src/fintamath/expressions/ExpressionComparator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ Ordering reverse(Ordering ordering);

template <typename T>
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<T>(child); })) {
return i;
}
Expand Down Expand Up @@ -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) {
Expand Down
13 changes: 5 additions & 8 deletions src/fintamath/expressions/ExpressionUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ bool containsIf(const ArgumentPtr &arg, const std::function<bool(const ArgumentP

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

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)) {
Expand Down Expand Up @@ -226,12 +226,9 @@ std::pair<ArgumentPtr, ArgumentPtr> splitRational(const ArgumentPtr &arg) {
ArgumentPtr negate(const ArgumentPtr &arg) {
if (const auto expr = cast<IExpression>(arg)) {
if (is<Add>(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<Mul>(expr->getFunction())) {
Expand Down Expand Up @@ -281,7 +278,7 @@ ArgumentPtrVector getPolynomChildren(const IFunction &func, const ArgumentPtr &a
std::vector<std::string> argumentVectorToStringVector(const ArgumentPtrVector &args) {
std::vector<std::string> 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();
}

Expand Down
4 changes: 2 additions & 2 deletions src/fintamath/expressions/IExpression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ std::vector<Variable> 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;
Expand Down
2 changes: 1 addition & 1 deletion src/fintamath/expressions/binary/CompExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
2 changes: 1 addition & 1 deletion src/fintamath/expressions/binary/DerivativeExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ ArgumentPtr DerivativeExpr::exprSimplify(const std::shared_ptr<const IExpression
ArgumentPtr DerivativeExpr::addSimplify(const ArgumentPtrVector &children, const std::shared_ptr<const Variable> &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);
});

Expand Down
12 changes: 5 additions & 7 deletions src/fintamath/expressions/binary/DivExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -201,7 +199,7 @@ ArgumentPtr DivExpr::mulSimplify(const SimplifyFunctionVector &simplFuncs,

if (res && !is<Rational>(res) && *res != *makeExpr(func, lhsChild, rhsChild)) {
lhsChild = std::move(res);
rhsChildren.erase(rhsChildren.begin() + j);
rhsChildren.erase(rhsChildren.begin() + static_cast<ptrdiff_t>(i));
break;
}
}
Expand Down Expand Up @@ -394,7 +392,7 @@ std::pair<ArgumentPtr, ArgumentPtr> 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));
}

Expand Down
10 changes: 5 additions & 5 deletions src/fintamath/expressions/binary/PowExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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<Integer> 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));
}
Expand Down
4 changes: 2 additions & 2 deletions src/fintamath/expressions/interfaces/IPolynomExpression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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())) {
Expand Down Expand Up @@ -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;
});
}
Expand Down
2 changes: 1 addition & 1 deletion src/fintamath/expressions/polynomial/AddExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ ArgumentPtr AddExpr::mulLogSimplify(const IFunction & /*func*/, const ArgumentPt
std::vector<size_t> AddExpr::findLogarithms(const ArgumentPtrVector &children) {
std::vector<size_t> indices;

for (const auto i : stdv::iota(0U, children.size())) {
for (size_t i = 0; i < children.size(); i++) {
if (const auto childExpr = cast<const IExpression>(children[i]);
childExpr && is<Log>(childExpr->getFunction())) {

Expand Down
2 changes: 1 addition & 1 deletion src/fintamath/expressions/polynomial/OrExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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];

Expand Down
2 changes: 1 addition & 1 deletion src/fintamath/functions/IFunction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ void IFunction::validateArgsSize(const ArgumentRefVector &argVect) const {

std::vector<std::string> 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();
}

Expand Down
2 changes: 1 addition & 1 deletion src/fintamath/functions/calculus/Max.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace fintamath {
std::unique_ptr<IMathObject> Max::call(const ArgumentRefVector &argVect) const {
std::reference_wrapper res = cast<IComparable>(argVect.front().get());

for (const auto &arg : argVect | stdv::drop(1)) {
for (const auto &arg : argVect | std::views::drop(1)) {
if (is<Complex>(arg)) {
return {};
}
Expand Down
2 changes: 1 addition & 1 deletion src/fintamath/functions/calculus/Min.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace fintamath {
std::unique_ptr<IMathObject> Min::call(const ArgumentRefVector &argVect) const {
std::reference_wrapper res = cast<IComparable>(argVect.front().get());

for (const auto &arg : argVect | stdv::drop(1)) {
for (const auto &arg : argVect | std::views::drop(1)) {
if (is<Complex>(arg)) {
return {};
}
Expand Down
2 changes: 1 addition & 1 deletion src/fintamath/numbers/Rational.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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() == '-') {
Expand Down
4 changes: 3 additions & 1 deletion tests/src/core/CoreUtilsTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@

#include "fintamath/core/CoreUtils.hpp"

// Nothing to test yet
TEST(CoreUtilsTests, repeatTest) {
// TODO: implement
}
18 changes: 9 additions & 9 deletions tests/src/core/HashTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<decltype(tuple1)>{}(tuple1), Hash<decltype(tuple2)>{}(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<decltype(tuple1)>{}(tuple1), Hash<decltype(tuple2)>{}(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<decltype(tuple1)>{}(tuple1), Hash<decltype(tuple2)>{}(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<decltype(tuple1)>{}(tuple1), Hash<decltype(tuple2)>{}(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<decltype(tuple1)>{}(tuple1), Hash<decltype(tuple2)>{}(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<decltype(tuple1)>{}(tuple1), Hash<decltype(tuple2)>{}(tuple2));
}
Expand Down