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

Split MathObjectTypes into 2 files and make MathObjectType constructor consteval #225

Merged
merged 1 commit into from
Feb 19, 2024
Merged
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
10 changes: 5 additions & 5 deletions cmake/Coverage.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@
if(CMAKE_CXX_COMPILER_ID MATCHES ".*Clang")
option(${PROJECT_NAME}_enable_coverage "Enable coverage reporting for gcc/clang" OFF)
if(${PROJECT_NAME}_enable_coverage)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fprofile-instr-generate -fcoverage-mapping -O0")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fprofile-instr-generate -fcoverage-mapping -O0 -g")

add_custom_target(
${PROJECT_NAME}_coverage
COMMAND ./bin/fintamath_tests
COMMAND llvm-profdata merge -o merged.profdata *.profraw
COMMAND llvm-cov show --show-branches=count --ignore-filename-regex='tests|build|thirdparty' --instr-profile
merged.profdata bin/fintamath_tests > coverage.txt
COMMAND llvm-cov export --ignore-filename-regex='tests|build|thirdparty' --instr-profile merged.profdata
bin/fintamath_tests -format lcov > lcov.info
COMMAND llvm-cov show --show-branches=count --ignore-filename-regex='tests|build|thirdparty|MathObjectType.hpp'
--show-instantiations --instr-profile merged.profdata bin/fintamath_tests > coverage.txt
COMMAND llvm-cov export --ignore-filename-regex='tests|build|thirdparty|MathObjectType.hpp' --instr-profile
merged.profdata bin/fintamath_tests -format lcov > lcov.info
COMMAND genhtml --branch-coverage lcov.info -o coverage
DEPENDS ${PROJECT_NAME}_tests
WORKING_DIRECTORY ${CMAKE_BINARY_DIR})
Expand Down
2 changes: 1 addition & 1 deletion include/fintamath/core/CoreUtils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include <ranges>
#include <type_traits>

#include "fintamath/core/MathObjectTypes.hpp"
#include "fintamath/core/MathObjectBoundTypes.hpp"

namespace fintamath {

Expand Down
2 changes: 1 addition & 1 deletion include/fintamath/core/IArithmetic.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

#include "fintamath/core/CoreUtils.hpp"
#include "fintamath/core/IMathObject.hpp"
#include "fintamath/core/MathObjectTypes.hpp"
#include "fintamath/core/MathObjectType.hpp"
#include "fintamath/core/Parser.hpp"

namespace fintamath {
Expand Down
2 changes: 1 addition & 1 deletion include/fintamath/core/IComparable.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

#include "fintamath/core/CoreUtils.hpp"
#include "fintamath/core/IArithmetic.hpp"
#include "fintamath/core/MathObjectTypes.hpp"
#include "fintamath/core/MathObjectType.hpp"
#include "fintamath/core/Parser.hpp"

namespace fintamath {
Expand Down
2 changes: 1 addition & 1 deletion include/fintamath/core/IMathObject.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

#include "fintamath/core/Converter.hpp"
#include "fintamath/core/CoreUtils.hpp"
#include "fintamath/core/MathObjectTypes.hpp"
#include "fintamath/core/MathObjectType.hpp"
#include "fintamath/core/Parser.hpp"

namespace fintamath {
Expand Down
63 changes: 63 additions & 0 deletions include/fintamath/core/MathObjectBoundTypes.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#pragma once

#include <unordered_map>

#include "fintamath/core/MathObjectType.hpp"

namespace fintamath {

bool isBaseOf(const MathObjectType &toType, const MathObjectType &fromType);

namespace detail {

class MathObjectBoundTypes final {
using enum MathObjectType::Id;

using TypeIdToBoundTypeIdMap = std::unordered_map<size_t, size_t>;

static TypeIdToBoundTypeIdMap &getMap() {
static TypeIdToBoundTypeIdMap typeIdToBoundTypeIdMap{
makeTypeIdPair(IMathObject, None),
makeTypeIdPair(IArithmetic, ILiteral),
makeTypeIdPair(IArithmetic, ILiteral),
makeTypeIdPair(IExpression, IComparable),
makeTypeIdPair(IUnaryExpression, IBinaryExpression),
makeTypeIdPair(IBinaryExpression, IPolynomExpression),
makeTypeIdPair(IPolynomExpression, IComparable),
makeTypeIdPair(IComparable, ILiteral),
makeTypeIdPair(INumber, ILiteral),
makeTypeIdPair(IInteger, ILiteral),
makeTypeIdPair(ILiteral, IFunction),
makeTypeIdPair(IConstant, IFunction),
makeTypeIdPair(IFunction, None),
makeTypeIdPair(IOperator, None),
};
return typeIdToBoundTypeIdMap;
}

public:
static void bindTypes(const MathObjectType &type, const MathObjectType &boundType) {
getMap().emplace(type.getId(), boundType.getId());
}

friend bool fintamath::isBaseOf(const MathObjectType &toType, const MathObjectType &fromType);

private:
static std::pair<size_t, size_t> makeTypeIdPair(const MathObjectType::Id lhs, const MathObjectType::Id rhs) {
return {static_cast<size_t>(lhs), static_cast<size_t>(rhs)};
}
};

}

inline bool isBaseOf(const MathObjectType &toType, const MathObjectType &fromType) {
const auto &map = detail::MathObjectBoundTypes::getMap();

if (const auto boundaries = map.find(toType.getId()); boundaries != map.end()) {
return fromType.getId() >= boundaries->first && fromType.getId() < boundaries->second;
}

return toType == fromType;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@
#include <cstddef>
#include <limits>
#include <string_view>
#include <unordered_map>

#include <boost/container_hash/hash.hpp>
#include <utility>

#include "fintamath/config/Config.hpp"

Expand Down Expand Up @@ -161,12 +157,12 @@ struct MathObjectType final {
using enum Id;

public:
constexpr MathObjectType(const size_t rhs, const std::string_view inName)
consteval MathObjectType(const size_t inId, const std::string_view inName)
: name(inName),
id(rhs) {
id(inId) {
}

constexpr MathObjectType(const Id inId, const std::string_view inName)
consteval MathObjectType(const Id inId, const std::string_view inName)
: MathObjectType(static_cast<size_t>(inId), inName) {
}

Expand Down Expand Up @@ -194,62 +190,8 @@ struct MathObjectType final {
[[maybe_unused]] inline static const detail::Config config;
};

inline size_t hash_value(const MathObjectType &rhs) noexcept {
return boost::hash<size_t>{}(rhs.getId());
}

bool isBaseOf(const MathObjectType &toType, const MathObjectType &fromType);

namespace detail {

class MathObjectBoundTypes final {
using enum MathObjectType::Id;

using TypeIdToBoundTypeIdMap = std::unordered_map<size_t, size_t>;

static TypeIdToBoundTypeIdMap &getMap() {
static TypeIdToBoundTypeIdMap typeIdToBoundTypeIdMap{
makeTypeIdPair(IMathObject, None),
makeTypeIdPair(IArithmetic, ILiteral),
makeTypeIdPair(IArithmetic, ILiteral),
makeTypeIdPair(IExpression, IComparable),
makeTypeIdPair(IUnaryExpression, IBinaryExpression),
makeTypeIdPair(IBinaryExpression, IPolynomExpression),
makeTypeIdPair(IPolynomExpression, IComparable),
makeTypeIdPair(IComparable, ILiteral),
makeTypeIdPair(INumber, ILiteral),
makeTypeIdPair(IInteger, ILiteral),
makeTypeIdPair(ILiteral, IFunction),
makeTypeIdPair(IConstant, IFunction),
makeTypeIdPair(IFunction, None),
makeTypeIdPair(IOperator, None),
};
return typeIdToBoundTypeIdMap;
}

public:
static void bindTypes(const MathObjectType &type, const MathObjectType &boundType) {
getMap().emplace(type.getId(), boundType.getId());
}

friend bool fintamath::isBaseOf(const MathObjectType &toType, const MathObjectType &fromType);

private:
static std::pair<size_t, size_t> makeTypeIdPair(const MathObjectType::Id lhs, const MathObjectType::Id rhs) {
return {static_cast<size_t>(lhs), static_cast<size_t>(rhs)};
}
};

}

inline bool isBaseOf(const MathObjectType &toType, const MathObjectType &fromType) {
const auto &map = detail::MathObjectBoundTypes::getMap();

if (const auto boundaries = map.find(toType.getId()); boundaries != map.end()) {
return fromType.getId() >= boundaries->first && fromType.getId() < boundaries->second;
}

return toType == fromType;
constexpr size_t hash_value(const MathObjectType &rhs) noexcept {
return rhs.getId();
}

}
2 changes: 1 addition & 1 deletion include/fintamath/core/MultiMethod.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include <boost/container_hash/hash.hpp>

#include "fintamath/core/CoreUtils.hpp"
#include "fintamath/core/MathObjectTypes.hpp"
#include "fintamath/core/MathObjectType.hpp"

namespace fintamath::detail {

Expand Down
2 changes: 1 addition & 1 deletion include/fintamath/expressions/Expression.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#include <vector>

#include "fintamath/core/IMathObject.hpp"
#include "fintamath/core/MathObjectTypes.hpp"
#include "fintamath/core/MathObjectType.hpp"
#include "fintamath/core/Parser.hpp"
#include "fintamath/core/Tokenizer.hpp"
#include "fintamath/expressions/IExpression.hpp"
Expand Down
2 changes: 1 addition & 1 deletion include/fintamath/expressions/ExpressionUtils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include <utility>
#include <vector>

#include "fintamath/core/MathObjectTypes.hpp"
#include "fintamath/core/MathObjectType.hpp"
#include "fintamath/functions/FunctionArguments.hpp"
#include "fintamath/functions/IFunction.hpp"
#include "fintamath/functions/IOperator.hpp"
Expand Down
2 changes: 1 addition & 1 deletion include/fintamath/expressions/IExpression.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

#include "fintamath/core/IArithmetic.hpp"
#include "fintamath/core/IMathObject.hpp"
#include "fintamath/core/MathObjectTypes.hpp"
#include "fintamath/core/MathObjectType.hpp"
#include "fintamath/core/Parser.hpp"
#include "fintamath/functions/FunctionArguments.hpp"
#include "fintamath/functions/IFunction.hpp"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include <utility>
#include <vector>

#include "fintamath/core/MathObjectTypes.hpp"
#include "fintamath/core/MathObjectType.hpp"
#include "fintamath/expressions/IExpression.hpp"
#include "fintamath/functions/FunctionArguments.hpp"
#include "fintamath/functions/IFunction.hpp"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include <utility>
#include <vector>

#include "fintamath/core/MathObjectTypes.hpp"
#include "fintamath/core/MathObjectType.hpp"
#include "fintamath/expressions/IExpression.hpp"
#include "fintamath/functions/FunctionArguments.hpp"
#include "fintamath/functions/IFunction.hpp"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include <utility>
#include <vector>

#include "fintamath/core/MathObjectTypes.hpp"
#include "fintamath/core/MathObjectType.hpp"
#include "fintamath/expressions/IExpression.hpp"
#include "fintamath/functions/FunctionArguments.hpp"
#include "fintamath/functions/IFunction.hpp"
Expand Down
2 changes: 1 addition & 1 deletion include/fintamath/functions/FunctionArguments.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include <memory>
#include <vector>

#include "fintamath/core/MathObjectTypes.hpp"
#include "fintamath/core/MathObjectType.hpp"

namespace fintamath {

Expand Down
2 changes: 1 addition & 1 deletion include/fintamath/functions/IFunction.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include <unordered_map>

#include "fintamath/core/IMathObject.hpp"
#include "fintamath/core/MathObjectTypes.hpp"
#include "fintamath/core/MathObjectType.hpp"
#include "fintamath/core/Parser.hpp"
#include "fintamath/exceptions/UndefinedException.hpp"
#include "fintamath/functions/FunctionArguments.hpp"
Expand Down
2 changes: 1 addition & 1 deletion include/fintamath/functions/IOperator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include <memory>
#include <string>

#include "fintamath/core/MathObjectTypes.hpp"
#include "fintamath/core/MathObjectType.hpp"
#include "fintamath/core/Parser.hpp"
#include "fintamath/functions/IFunction.hpp"

Expand Down
2 changes: 1 addition & 1 deletion include/fintamath/functions/arithmetic/Abs.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include <string>

#include "fintamath/core/IMathObject.hpp"
#include "fintamath/core/MathObjectTypes.hpp"
#include "fintamath/core/MathObjectType.hpp"
#include "fintamath/functions/FunctionArguments.hpp"
#include "fintamath/functions/FunctionUtils.hpp"
#include "fintamath/functions/IFunction.hpp"
Expand Down
2 changes: 1 addition & 1 deletion include/fintamath/functions/arithmetic/Add.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

#include "fintamath/core/IArithmetic.hpp"
#include "fintamath/core/IMathObject.hpp"
#include "fintamath/core/MathObjectTypes.hpp"
#include "fintamath/core/MathObjectType.hpp"
#include "fintamath/functions/FunctionArguments.hpp"
#include "fintamath/functions/FunctionUtils.hpp"
#include "fintamath/functions/IOperator.hpp"
Expand Down
2 changes: 1 addition & 1 deletion include/fintamath/functions/arithmetic/Div.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

#include "fintamath/core/IArithmetic.hpp"
#include "fintamath/core/IMathObject.hpp"
#include "fintamath/core/MathObjectTypes.hpp"
#include "fintamath/core/MathObjectType.hpp"
#include "fintamath/functions/FunctionArguments.hpp"
#include "fintamath/functions/FunctionUtils.hpp"
#include "fintamath/functions/IOperator.hpp"
Expand Down
2 changes: 1 addition & 1 deletion include/fintamath/functions/arithmetic/Frac.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

#include "fintamath/core/IArithmetic.hpp"
#include "fintamath/core/IMathObject.hpp"
#include "fintamath/core/MathObjectTypes.hpp"
#include "fintamath/core/MathObjectType.hpp"
#include "fintamath/functions/FunctionArguments.hpp"
#include "fintamath/functions/IFunction.hpp"

Expand Down
2 changes: 1 addition & 1 deletion include/fintamath/functions/arithmetic/FracMixed.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

#include "fintamath/core/IArithmetic.hpp"
#include "fintamath/core/IMathObject.hpp"
#include "fintamath/core/MathObjectTypes.hpp"
#include "fintamath/core/MathObjectType.hpp"
#include "fintamath/functions/FunctionArguments.hpp"
#include "fintamath/functions/IFunction.hpp"

Expand Down
2 changes: 1 addition & 1 deletion include/fintamath/functions/arithmetic/Mul.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

#include "fintamath/core/IArithmetic.hpp"
#include "fintamath/core/IMathObject.hpp"
#include "fintamath/core/MathObjectTypes.hpp"
#include "fintamath/core/MathObjectType.hpp"
#include "fintamath/functions/FunctionArguments.hpp"
#include "fintamath/functions/FunctionUtils.hpp"
#include "fintamath/functions/IOperator.hpp"
Expand Down
2 changes: 1 addition & 1 deletion include/fintamath/functions/arithmetic/Neg.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

#include "fintamath/core/IArithmetic.hpp"
#include "fintamath/core/IMathObject.hpp"
#include "fintamath/core/MathObjectTypes.hpp"
#include "fintamath/core/MathObjectType.hpp"
#include "fintamath/functions/FunctionArguments.hpp"
#include "fintamath/functions/FunctionUtils.hpp"
#include "fintamath/functions/IOperator.hpp"
Expand Down
2 changes: 1 addition & 1 deletion include/fintamath/functions/arithmetic/Sign.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include <string>

#include "fintamath/core/IMathObject.hpp"
#include "fintamath/core/MathObjectTypes.hpp"
#include "fintamath/core/MathObjectType.hpp"
#include "fintamath/functions/FunctionArguments.hpp"
#include "fintamath/functions/FunctionUtils.hpp"
#include "fintamath/functions/IFunction.hpp"
Expand Down
2 changes: 1 addition & 1 deletion include/fintamath/functions/arithmetic/Sub.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

#include "fintamath/core/IArithmetic.hpp"
#include "fintamath/core/IMathObject.hpp"
#include "fintamath/core/MathObjectTypes.hpp"
#include "fintamath/core/MathObjectType.hpp"
#include "fintamath/functions/FunctionArguments.hpp"
#include "fintamath/functions/FunctionUtils.hpp"
#include "fintamath/functions/IOperator.hpp"
Expand Down
2 changes: 1 addition & 1 deletion include/fintamath/functions/arithmetic/UnaryPlus.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

#include "fintamath/core/IArithmetic.hpp"
#include "fintamath/core/IMathObject.hpp"
#include "fintamath/core/MathObjectTypes.hpp"
#include "fintamath/core/MathObjectType.hpp"
#include "fintamath/functions/FunctionArguments.hpp"
#include "fintamath/functions/IOperator.hpp"

Expand Down
Loading
Loading