OASIS
Open Algebra Software
Loading...
Searching...
No Matches
UnaryExpression.hpp
Go to the documentation of this file.
1//
2// Created by Matthew McCall on 3/29/24.
3//
4
5#ifndef UNARYEXPRESSION_HPP
6#define UNARYEXPRESSION_HPP
7
8#include "Expression.hpp"
9#include "Serialization.hpp"
10
11namespace Oasis {
12
13template <template <IExpression> class DerivedT, IExpression OperandT>
15
16 using DerivedSpecialized = DerivedT<OperandT>;
17 using DerivedGeneralized = DerivedT<Expression>;
18
19public:
20 UnaryExpression() = default;
21
23 : Expression(other)
24 {
25 if (other.HasOperand()) {
26 SetOperand(other.GetOperand());
27 }
28 }
29
30 explicit UnaryExpression(const OperandT& operand)
31 {
32 SetOperand(operand);
33 }
34
35 [[nodiscard]] auto Copy() const -> std::unique_ptr<Expression> final
36 {
37 return std::make_unique<DerivedSpecialized>(*static_cast<const DerivedSpecialized*>(this));
38 }
39
40 [[nodiscard]] auto Equals(const Expression& other) const -> bool final
41 {
42 if (!other.Is<DerivedSpecialized>()) {
43 return false;
44 }
45
46 // generalize
47 const auto otherGeneralized = other.Generalize();
48 const auto& otherUnaryGeneralized = dynamic_cast<const DerivedGeneralized&>(*otherGeneralized);
49
50 return op->Equals(otherUnaryGeneralized.GetOperand());
51 }
52
53 [[nodiscard]] auto Generalize() const -> std::unique_ptr<Expression> final
54 {
55 DerivedGeneralized generalized;
56 if (this->op) {
57 generalized.SetOperand(*this->op->Copy());
58 }
59 return std::make_unique<DerivedGeneralized>(generalized);
60 }
61
62 auto GetOperand() const -> const OperandT&
63 {
64 return *op;
65 }
66
67 auto HasOperand() const -> bool
68 {
69 return op != nullptr;
70 }
71
72 [[nodiscard]] auto StructurallyEquivalent(const Expression& other) const -> bool final
73 {
74 return this->GetType() == other.GetType();
75 }
76
77 auto SetOperand(const OperandT& operand) -> void
78 {
80 this->op = operand.Copy();
81 } else {
82 this->op = std::make_unique<OperandT>(operand);
83 }
84 }
85
86 auto Substitute(const Expression& var, const Expression& val) -> std::unique_ptr<Expression> override
87 {
88 std::unique_ptr<Expression> right = ((GetOperand().Copy())->Substitute(var, val));
89 DerivedT<Expression> comb = DerivedT<Expression> { *right };
90 auto ret = comb.Simplify();
91 return ret;
92 }
93
94 void Serialize(SerializationVisitor& visitor) const override
95 {
96 const auto generalized = Generalize();
97 const auto& derivedGeneralized = dynamic_cast<const DerivedGeneralized&>(*generalized);
98 visitor.Serialize(derivedGeneralized);
99 }
100
101protected:
103};
104
105} // Oasis
106
107#endif // UNARYEXPRESSION_HPP
An expression.
Definition Expression.hpp:56
virtual auto GetType() const -> ExpressionType
Gets the type of this expression.
Definition Expression.cpp:220
Definition Serialization.hpp:50
virtual void Serialize(const Real &real)=0
Definition UnaryExpression.hpp:14
auto Equals(const Expression &other) const -> bool final
Compares this expression to another expression for equality.
Definition UnaryExpression.hpp:40
auto Generalize() const -> std::unique_ptr< Expression > final
Converts this expression to a more general expression.
Definition UnaryExpression.hpp:53
auto StructurallyEquivalent(const Expression &other) const -> bool final
Checks whether this expression is structurally equivalent to another expression.
Definition UnaryExpression.hpp:72
void Serialize(SerializationVisitor &visitor) const override
This function serializes the expression object.
Definition UnaryExpression.hpp:94
auto Copy() const -> std::unique_ptr< Expression > final
Copies this expression.
Definition UnaryExpression.hpp:35
UnaryExpression(const UnaryExpression &other)
Definition UnaryExpression.hpp:22
UnaryExpression(const OperandT &operand)
Definition UnaryExpression.hpp:30
auto HasOperand() const -> bool
Definition UnaryExpression.hpp:67
auto Substitute(const Expression &var, const Expression &val) -> std::unique_ptr< Expression > override
Definition UnaryExpression.hpp:86
auto GetOperand() const -> const OperandT &
Definition UnaryExpression.hpp:62
auto SetOperand(const OperandT &operand) -> void
Definition UnaryExpression.hpp:77
std::unique_ptr< OperandT > op
Definition UnaryExpression.hpp:102
T is_same_v
Definition Add.hpp:11