codac 2.0.0
Loading...
Searching...
No Matches
codac2_ExprBase.h
Go to the documentation of this file.
1
9
10#pragma once
11
12#include <map>
13#include <memory>
14#include <cassert>
15#include <utility>
16#include <atomic>
17#include "codac2_Domain.h"
18#include "codac2_Index.h"
19
20namespace codac2
21{
22 class ExprBase;
23 class VarBase;
24
33 class ExprID
34 {
35 public:
36
44
50 Index id() const;
51
58 bool operator==(const ExprID& i) const;
59
69 bool operator<(const ExprID& i) const;
70
71 protected:
72
73 const Index _id;
74 static std::atomic<Index> _id_counter;
75 };
76
85 class ExprBase : public std::enable_shared_from_this<ExprBase>
86 {
87 public:
88
95
104 virtual std::shared_ptr<ExprBase> copy() const = 0;
105
116 virtual void replace_arg(const ExprID& old_arg_id, const std::shared_ptr<ExprBase>& new_expr) = 0;
117
123 const ExprID& unique_id() const;
124
134 bool operator==(const ExprBase& e) const;
135
142 virtual ~ExprBase() = default;
143
144 protected:
145
147 };
148
163 template<typename... X>
165 {
166 public:
167
177 OperationExprBase(std::shared_ptr<X>... x)
178 : _x(std::make_tuple((x)...))
179 { }
180
192 : _x(e._x)
193 {
194 std::apply(
195 [](auto &&... x)
196 {
197 ((x = __copy(x)), ...);
198 }, _x);
199 }
200
210 void replace_arg(const ExprID& old_arg_id, const std::shared_ptr<ExprBase>& new_expr)
211 {
212 std::apply(
213 [old_arg_id,new_expr](auto &&... x)
214 {
215 (__replace_arg(x,old_arg_id,new_expr), ...);
216 }, _x);
217 }
218
219 protected:
220
230 template<typename X_>
231 static std::shared_ptr<X_> __copy(const std::shared_ptr<X_>& x)
232 {
233 return std::dynamic_pointer_cast<X_>(x->copy());
234 }
235
247 template<typename D>
248 static void __replace_arg(std::shared_ptr<D>& x, const ExprID& old_arg_id, const std::shared_ptr<ExprBase>& new_expr)
249 {
250 if(x->unique_id() == old_arg_id)
251 {
252 assert(std::dynamic_pointer_cast<VarBase>(x) && "this subexpr should be some variable");
253 x = std::dynamic_pointer_cast<D>(new_expr);
254 }
255 else
256 x->replace_arg(old_arg_id, new_expr);
257 }
258
259 std::tuple<std::shared_ptr<X>...> _x;
260 };
261}
Abstract base class for representing an expression.
Definition codac2_ExprBase.h:86
const ExprID & unique_id() const
Returns the unique identifier of the expression.
virtual ~ExprBase()=default
Virtual destructor.
const ExprID _unique_id
unique identifier for this expression
Definition codac2_ExprBase.h:146
virtual void replace_arg(const ExprID &old_arg_id, const std::shared_ptr< ExprBase > &new_expr)=0
Replaces a variable by a new expression.
virtual std::shared_ptr< ExprBase > copy() const =0
Creates a copy of the current expression.
ExprBase()
Default constructor.
bool operator==(const ExprBase &e) const
Equality operator for comparing two expressions.
A class representing a unique identifier for expressions.
Definition codac2_ExprBase.h:34
ExprID()
Default constructor.
bool operator<(const ExprID &i) const
Comparison operator.
Index id() const
Retrieves the unique identifier of the expression.
static std::atomic< Index > _id_counter
thread-safe counter used to generate unique IDs
Definition codac2_ExprBase.h:74
const Index _id
unique identifier, cannot be modified after initialization
Definition codac2_ExprBase.h:73
bool operator==(const ExprID &i) const
Equality operator.
OperationExprBase(const OperationExprBase< X... > &e)
Copy constructor.
Definition codac2_ExprBase.h:191
void replace_arg(const ExprID &old_arg_id, const std::shared_ptr< ExprBase > &new_expr)
Replaces a variable by a new expression.
Definition codac2_ExprBase.h:210
OperationExprBase(std::shared_ptr< X >... x)
Constructs an OperationExprBase with operand expressions.
Definition codac2_ExprBase.h:177
static void __replace_arg(std::shared_ptr< D > &x, const ExprID &old_arg_id, const std::shared_ptr< ExprBase > &new_expr)
Helper function to replace a variable by a new expression.
Definition codac2_ExprBase.h:248
std::tuple< std::shared_ptr< X >... > _x
tuple storing the operand expressions
Definition codac2_ExprBase.h:259
static std::shared_ptr< X_ > __copy(const std::shared_ptr< X_ > &x)
Helper function to copy a single operand expression.
Definition codac2_ExprBase.h:231
Abstract base class for representing variables in analytic or set functions.
Definition codac2_VarBase.h:24
Definition codac2_OctaSym.h:21