codac 1.5.6
Loading...
Searching...
No Matches
codac2_SetFunction.h
Go to the documentation of this file.
1
9
10#pragma once
11
12#include <map>
13#include "codac2_SetExpr.h"
14#include "codac2_Domain.h"
16#include "codac2_FunctionBase.h"
17#include "codac2_SepWrapper.h"
18
19namespace codac2
20{
21 class SetFunction : public FunctionBase<SetExpr>
22 {
23 public:
24
25 SetFunction(const FunctionArgsList& args, const std::shared_ptr<SetExpr>& y)
26 : FunctionBase<SetExpr>(args, y)
27 {
28 assert_release(y->belongs_to_args_list(this->args()) &&
29 "Invalid arguments: variable not present in input arguments");
30 }
31
32 SetFunction(const SetFunction& f)
33 : FunctionBase<SetExpr>(f)
34 { }
35
36 template<typename... Args>
37 auto create_ctc(const Args&... x)
38 {
39 std::vector<std::shared_ptr<CtcBase<IntervalVector>>> ref_x(sizeof...(Args));
40 Index i = 0;
41 ((ref_x[i++] = create_arg_ctc_copy(x)), ...);
42 assert_release(args().size() == ref_x.size() &&
43 "Invalid arguments: wrong number of input arguments");
44 return this->expr()->create_ctc(args(), ref_x);
45 }
46
47 template<typename... Args>
48 auto create_sep(const Args&... x)
49 {
50 std::vector<std::shared_ptr<SepBase>> ref_x(sizeof...(Args));
51 Index i = 0;
52 ((ref_x[i++] = create_arg_sep_copy(x)), ...);
53 assert_release(args().size() == ref_x.size() &&
54 "Invalid arguments: wrong number of input arguments");
55 return this->expr()->create_sep(args(), ref_x);
56 }
57
58 template<typename... X>
59 std::shared_ptr<SetExpr> operator()(const X&... x) const
60 {
61 auto expr_copy = expr()->copy();
62 Index i = 0;
63 (expr_copy->replace_expr(_args[i++]->unique_id(), this->__get_copy(x)), ...);
64 assert_release(i == this->args().size() &&
65 "Invalid arguments: wrong number of input arguments");
66 return std::dynamic_pointer_cast<SetExpr>(expr_copy);
67 }
68
69 friend std::ostream& operator<<(std::ostream& os, const SetFunction& f)
70 {
71 os << "set function";
72 return os;
73 }
74
75
76 protected:
77
78 template<typename A>
79 std::shared_ptr<CtcBase<IntervalVector>> create_arg_ctc_copy(const A& x)
80 {
81 if constexpr(std::is_base_of_v<Domain,A>)
82 {
83 CtcWrapper<A> sx(x);
84 return sx.copy();
85 }
86
87 else if constexpr(std::is_base_of_v<CtcBase<IntervalVector>,A>)
88 return x.copy();
89
90 else
91 {
92 assert_release(false && "Invalid argument: unknown input type");
93 return nullptr;
94 }
95 }
96
97 template<typename A>
98 std::shared_ptr<SepBase> create_arg_sep_copy(const A& x)
99 {
100 if constexpr(std::is_base_of_v<Domain,A>)
101 {
102 SepWrapper<A> sx(x);
103 return sx.copy();
104 }
105
106 else if constexpr(std::is_base_of_v<SepBase,A>)
107 return x.copy();
108
109 else
110 {
111 assert_release(false && "Invalid argument: unknown input type");
112 return nullptr;
113 }
114 }
115
116 std::shared_ptr<SetExpr> __get_copy(std::shared_ptr<SetExpr> x) const
117 {
118 return std::dynamic_pointer_cast<SetExpr>(x->copy());
119 }
120
121 template<typename A>
122 std::shared_ptr<SetExpr> __get_copy(const A& x) const
123 {
124 if constexpr(std::is_base_of_v<VarBase,A>)
125 return std::dynamic_pointer_cast<A>(x.copy());
126 else
127 return const_set(x);
128 }
129 };
130}