Skip to content

Commit

Permalink
Add name to MathObjectType
Browse files Browse the repository at this point in the history
  • Loading branch information
fintarin committed Feb 18, 2024
1 parent 11756c7 commit f7e1697
Show file tree
Hide file tree
Showing 236 changed files with 407 additions and 400 deletions.
2 changes: 1 addition & 1 deletion include/fintamath/core/IArithmetic.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class IArithmetic : public IMathObject {
}

static constexpr MathObjectType getTypeStatic() {
return MathObjectType::IArithmetic;
return {MathObjectType::IArithmetic, "IArithmetic"};
}

protected:
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 @@ -30,7 +30,7 @@ class IComparable : public IArithmetic {
}

static constexpr MathObjectType getTypeStatic() {
return MathObjectType::IComparable;
return {MathObjectType::IComparable, "IComparable"};
}

protected:
Expand Down
5 changes: 2 additions & 3 deletions include/fintamath/core/IMathObject.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class IMathObject {
virtual std::unique_ptr<IMathObject> clone() && = 0;

virtual std::string toString() const {
return typeid(*this).name();
return std::string(getTypeStatic().getName());
}

virtual std::unique_ptr<IMathObject> toMinimalObject() const {
Expand All @@ -46,7 +46,7 @@ class IMathObject {
}

static constexpr MathObjectType getTypeStatic() {
return MathObjectType::IMathObject;
return {MathObjectType::IMathObject, "IMathObject"};
}

protected:
Expand All @@ -60,7 +60,6 @@ template <typename Derived>
class IMathObjectCRTP : public IMathObject {
#define I_MATH_OBJECT_CRTP IMathObjectCRTP
#include "fintamath/core/IMathObjectCRTP.hpp"

#undef I_MATH_OBJECT_CRTP
};

Expand Down
95 changes: 49 additions & 46 deletions include/fintamath/core/MathObjectTypes.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@
#include <compare>
#include <cstddef>
#include <limits>
#include <string_view>
#include <unordered_map>

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

#include "fintamath/config/Config.hpp"

namespace fintamath {

class MathObjectType final {
public:
struct MathObjectType final {
enum class Id : size_t {
IMathObject = 0,

Expand Down Expand Up @@ -160,90 +161,92 @@ class MathObjectType final {
using enum Id;

public:
constexpr MathObjectType(Id rhs) : id(static_cast<size_t>(rhs)) {
}

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

constexpr bool operator==(const MathObjectType &rhs) const = default;

constexpr bool operator==(Id rhs) const {
return id == static_cast<size_t>(rhs);
constexpr MathObjectType(const Id inId, const std::string_view inName)
: MathObjectType(static_cast<size_t>(inId), inName) {
}

constexpr bool operator==(const size_t rhs) const {
return id == rhs;
constexpr bool operator==(const MathObjectType &rhs) const {
return id == rhs.id && name == rhs.name;
}

constexpr std::strong_ordering operator<=>(const MathObjectType &rhs) const = default;

constexpr std::strong_ordering operator<=>(Id rhs) const {
return id <=> static_cast<size_t>(rhs);
constexpr std::strong_ordering operator<=>(const MathObjectType &rhs) const {
return id <=> rhs.id;
}

constexpr std::strong_ordering operator<=>(const size_t rhs) const {
return id <=> rhs;
constexpr size_t getId() const {
return id;
}

constexpr operator size_t() const {
return id;
constexpr std::string_view getName() const {
return name;
}

private:
std::string_view name;
size_t id;

private:
[[maybe_unused]] inline static const detail::Config config;
};

inline size_t hash_value(const MathObjectType &rhs) noexcept {
return boost::hash<size_t>{}(rhs);
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 TypeToBoundTypeMap = std::unordered_map<MathObjectType, MathObjectType, boost::hash<MathObjectType>>;

static TypeToBoundTypeMap &getMapMutable() {
static TypeToBoundTypeMap typeToBoundTypeMap{
{IMathObject, None},
{IArithmetic, ILiteral},
{IExpression, IComparable},
{IUnaryExpression, IBinaryExpression},
{IBinaryExpression, IPolynomExpression},
{IPolynomExpression, IComparable},
{IComparable, ILiteral},
{INumber, ILiteral},
{IInteger, ILiteral},
{ILiteral, IFunction},
{IConstant, IFunction},
{IFunction, None},
{IOperator, None},
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 typeToBoundTypeMap;
return typeIdToBoundTypeIdMap;
}

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

static void bindTypes(const MathObjectType &type, const MathObjectType &boundType) {
getMapMutable().emplace(type, boundType);
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 &typeToTypeMap = detail::MathObjectBoundTypes::getMap();
const auto &map = detail::MathObjectBoundTypes::getMap();

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

return toType == fromType;
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 @@ -96,7 +96,7 @@ class Expression final : public IExpressionCRTP<Expression> {
static void registerFunctionExpressionMaker(ExpressionMaker maker);

static constexpr MathObjectType getTypeStatic() {
return MathObjectType::Expression;
return {MathObjectType::Expression, "Expression"};
}

protected:
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 @@ -46,7 +46,7 @@ class IExpression : public IArithmetic {
}

static constexpr MathObjectType getTypeStatic() {
return MathObjectType::IExpression;
return {MathObjectType::IExpression, "IExpression"};
}

protected:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class IBinaryExpression : public IExpression {
void setChildren(const ArgumentPtrVector &childVect) final;

static constexpr MathObjectType getTypeStatic() {
return MathObjectType::IBinaryExpression;
return {MathObjectType::IBinaryExpression, "IBinaryExpression"};
}

protected:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class IPolynomExpression : public IExpression {
void setChildren(const ArgumentPtrVector &childVect) final;

static constexpr MathObjectType getTypeStatic() {
return MathObjectType::IPolynomExpression;
return {MathObjectType::IPolynomExpression, "IPolynomExpression"};
}

protected:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class IUnaryExpression : public IExpression {
void setChildren(const ArgumentPtrVector &childVect) override;

static constexpr MathObjectType getTypeStatic() {
return MathObjectType::IUnaryExpression;
return {MathObjectType::IUnaryExpression, "IUnaryExpression"};
}

protected:
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 @@ -62,7 +62,7 @@ class IFunction : public IMathObject {
}

static constexpr MathObjectType getTypeStatic() {
return MathObjectType::IFunction;
return {MathObjectType::IFunction, "IFunction"};
}

protected:
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 @@ -61,7 +61,7 @@ class IOperator : public IFunction {
}

static constexpr MathObjectType getTypeStatic() {
return MathObjectType::IOperator;
return {MathObjectType::IOperator, "IOperator"};
}

private:
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 @@ -19,7 +19,7 @@ class Abs final : public IFunctionCRTP<INumber, Abs, INumber> {
}

static constexpr MathObjectType getTypeStatic() {
return MathObjectType::Abs;
return {MathObjectType::Abs, "Abs"};
}

protected:
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 @@ -27,7 +27,7 @@ class Add final : public IOperatorCRTP<IArithmetic, Add, IArithmetic, IArithmeti
}

static constexpr MathObjectType getTypeStatic() {
return MathObjectType::Add;
return {MathObjectType::Add, "Add"};
}

protected:
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 @@ -26,7 +26,7 @@ class Div final : public IOperatorCRTP<IArithmetic, Div, IArithmetic, IArithmeti
}

static constexpr MathObjectType getTypeStatic() {
return MathObjectType::Div;
return {MathObjectType::Div, "Div"};
}

protected:
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 @@ -18,7 +18,7 @@ class Frac final : public IFunctionCRTP<IArithmetic, Frac, IArithmetic, IArithme
}

static constexpr MathObjectType getTypeStatic() {
return MathObjectType::Frac;
return {MathObjectType::Frac, "Frac"};
}

protected:
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 @@ -18,7 +18,7 @@ class FracMixed final : public IFunctionCRTP<IArithmetic, FracMixed, IArithmetic
}

static constexpr MathObjectType getTypeStatic() {
return MathObjectType::FracMixed;
return {MathObjectType::FracMixed, "FracMixed"};
}

protected:
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 @@ -27,7 +27,7 @@ class Mul final : public IOperatorCRTP<IArithmetic, Mul, IArithmetic, IArithmeti
}

static constexpr MathObjectType getTypeStatic() {
return MathObjectType::Mul;
return {MathObjectType::Mul, "Mul"};
}

protected:
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 @@ -23,7 +23,7 @@ class Neg final : public IOperatorCRTP<IArithmetic, Neg, IArithmetic> {
}

static constexpr MathObjectType getTypeStatic() {
return MathObjectType::Neg;
return {MathObjectType::Neg, "Neg"};
}

protected:
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 @@ -19,7 +19,7 @@ class Sign final : public IFunctionCRTP<INumber, Sign, INumber> {
}

static constexpr MathObjectType getTypeStatic() {
return MathObjectType::Sign;
return {MathObjectType::Sign, "Sign"};
}

protected:
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 @@ -23,7 +23,7 @@ class Sub final : public IOperatorCRTP<IArithmetic, Sub, IArithmetic, IArithmeti
}

static constexpr MathObjectType getTypeStatic() {
return MathObjectType::Sub;
return {MathObjectType::Sub, "Sub"};
}

protected:
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 @@ -22,7 +22,7 @@ class UnaryPlus final : public IOperatorCRTP<IArithmetic, UnaryPlus, IArithmetic
}

static constexpr MathObjectType getTypeStatic() {
return MathObjectType::UnaryPlus;
return {MathObjectType::UnaryPlus, "UnaryPlus"};
}

protected:
Expand Down
2 changes: 1 addition & 1 deletion include/fintamath/functions/calculus/Derivative.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class Derivative final : public IFunctionCRTP<IComparable, Derivative, IComparab
}

static constexpr MathObjectType getTypeStatic() {
return MathObjectType::Derivative;
return {MathObjectType::Derivative, "Derivative"};
}

protected:
Expand Down
2 changes: 1 addition & 1 deletion include/fintamath/functions/calculus/Integral.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class Integral final : public IFunctionCRTP<IComparable, Integral, IComparable,
}

static constexpr MathObjectType getTypeStatic() {
return MathObjectType::Integral;
return {MathObjectType::Integral, "Integral"};
}

protected:
Expand Down
2 changes: 1 addition & 1 deletion include/fintamath/functions/calculus/Max.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class Max final : public IFunctionCRTP<IComparable, Max, IComparable> {
}

static constexpr MathObjectType getTypeStatic() {
return MathObjectType::Max;
return {MathObjectType::Max, "Max"};
}

protected:
Expand Down
2 changes: 1 addition & 1 deletion include/fintamath/functions/calculus/Min.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class Min final : public IFunctionCRTP<IComparable, Min, IComparable> {
}

static constexpr MathObjectType getTypeStatic() {
return MathObjectType::Min;
return {MathObjectType::Min, "Min"};
}

protected:
Expand Down
2 changes: 1 addition & 1 deletion include/fintamath/functions/comparison/Eqv.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class Eqv final : public IOperatorCRTP<Boolean, Eqv, IComparable, IComparable> {
}

static constexpr MathObjectType getTypeStatic() {
return MathObjectType::Eqv;
return {MathObjectType::Eqv, "Eqv"};
}

protected:
Expand Down
Loading

0 comments on commit f7e1697

Please sign in to comment.