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
152 virtual std::vector<std::shared_ptr<ExprBase>> children_expr_base() const
153 {
154 return {};
155 }
156
157 protected:
158
160 };
161
176 template<typename... X>
178 {
179 public:
180
190 OperationExprBase(std::shared_ptr<X>... x)
191 : _x(std::make_tuple((x)...))
192 { }
193
205 : _x(e._x)
206 {
207 std::apply(
208 [](auto &&... x)
209 {
210 ((x = __copy(x)), ...);
211 }, _x);
212 }
213
223 void replace_arg(const ExprID& old_arg_id, const std::shared_ptr<ExprBase>& new_expr)
224 {
225 std::apply(
226 [old_arg_id,new_expr](auto &&... x)
227 {
228 (__replace_arg(x,old_arg_id,new_expr), ...);
229 }, _x);
230 }
231
240 std::vector<std::shared_ptr<ExprBase>> children_expr_base() const
241 {
242 std::vector<std::shared_ptr<ExprBase>> children;
243 children.reserve(sizeof...(X));
244 std::apply(
245 [&children](const auto&... x)
246 {
247 (children.push_back(std::static_pointer_cast<ExprBase>(x)), ...);
248 }, _x);
249 return children;
250 }
251
252 protected:
253
263 template<typename X_>
264 static std::shared_ptr<X_> __copy(const std::shared_ptr<X_>& x)
265 {
266 return std::dynamic_pointer_cast<X_>(x->copy());
267 }
268
280 template<typename D>
281 static void __replace_arg(std::shared_ptr<D>& x, const ExprID& old_arg_id, const std::shared_ptr<ExprBase>& new_expr)
282 {
283 if(x->unique_id() == old_arg_id)
284 {
285 assert(std::dynamic_pointer_cast<VarBase>(x) && "this subexpr should be some variable");
286 x = std::dynamic_pointer_cast<D>(new_expr);
287 }
288 else
289 x->replace_arg(old_arg_id, new_expr);
290 }
291
292 std::tuple<std::shared_ptr<X>...> _x;
293 };
294}
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 std::vector< std::shared_ptr< ExprBase > > children_expr_base() const
Definition codac2_ExprBase.h:152
virtual ~ExprBase()=default
Virtual destructor.
const ExprID _unique_id
unique identifier for this expression
Definition codac2_ExprBase.h:159
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:204
std::vector< std::shared_ptr< ExprBase > > children_expr_base() const
Definition codac2_ExprBase.h:240
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:223
OperationExprBase(std::shared_ptr< X >... x)
Constructs an OperationExprBase with operand expressions.
Definition codac2_ExprBase.h:190
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:281
std::tuple< std::shared_ptr< X >... > _x
tuple storing the operand expressions
Definition codac2_ExprBase.h:292
static std::shared_ptr< X_ > __copy(const std::shared_ptr< X_ > &x)
Helper function to copy a single operand expression.
Definition codac2_ExprBase.h:264
Abstract base class for representing variables in analytic or set functions.
Definition codac2_VarBase.h:24
Definition codac2_OctaSym.h:21