codac 2.0.5
Loading...
Searching...
No Matches
codac2_CtcUnion.h
Go to the documentation of this file.
1
9
10#pragma once
11
12#include <tuple>
13#include <type_traits>
14#include "codac2_CtcWrapper.h"
15#include "codac2_Collection.h"
17
18namespace codac2
19{
20 template<typename... X>
21 class CtcUnion : public Ctc<CtcUnion<X...>,X...>
22 {
23 public:
24
25 explicit CtcUnion(Index n)
26 : Ctc<CtcUnion<X...>,X...>(n)
27 {
28 if constexpr(std::is_same_v<X...,Interval>)
29 assert_release(n == 1);
30 }
31
32 template<typename C>
33 requires (IsCtcBaseOrPtr<C,X...> && !std::is_same_v<CtcUnion<X...>,C>)
34 CtcUnion(const C& c)
35 : Ctc<CtcUnion<X...>,X...>(size_of(c)), _ctcs(c)
36 { }
37
38 template<typename... C>
39 requires (IsCtcBaseOrPtr<C,X...> && ...)
40 CtcUnion(const C&... c)
41 : Ctc<CtcUnion<X...>,X...>(size_first_item(c...)), _ctcs(c...)
42 {
43 assert_release(all_same_size(c...));
44 }
45
46 CtcUnion(const Collection<CtcBase<X...>>& ctcs)
47 : Ctc<CtcUnion<X...>,X...>(ctcs.front()->size()), _ctcs(ctcs)
48 {
49 for(const auto& ci : _ctcs)
50 {
51 assert_release(ci->size() == this->size());
52 }
53 }
54
55 template<typename C>
56 requires IsCtcBaseOrPtr<C,X...>
57 CtcUnion(std::initializer_list<C> ctcs)
58 : CtcUnion(Collection<CtcBase<X...>>(ctcs))
59 { }
60
61 size_t nb() const
62 {
63 return _ctcs.size();
64 }
65
66 void contract(X&... x) const
67 {
68 auto result = std::tuple<X...>(x...);
69 std::apply([](auto&... xi)
70 {
71 (xi.set_empty(), ...);
72 }, result);
73
74 auto accumulate_union = [&]<std::size_t... I>(const std::tuple<X...>& y, std::index_sequence<I...>)
75 {
76 ((std::get<I>(result) |= std::get<I>(y)), ...);
77 };
78
79 for(const auto& ci : _ctcs)
80 {
81 auto saved = std::tuple<X...>(x...);
82
83 std::apply([&](auto&... xi)
84 {
85 ci->contract(xi...);
86 }, saved);
87
88 accumulate_union(saved, std::index_sequence_for<X...>{});
89 }
90
91 std::tie(x...) = result;
92 }
93
94 template<typename C>
95 requires IsCtcBaseOrPtr<C,X...>
96 CtcUnion<X...>& operator|=(const C& c)
97 {
98 assert_release(size_of(c) == this->size());
99 _ctcs.push_back(c);
100 return *this;
101 }
102
103 protected:
104
105 Collection<CtcBase<X...>> _ctcs;
106 };
107
108 template <>
109 class CtcUnion<> : public CtcUnion<IntervalVector>
110 { };
111
112 template<typename Tuple>
113 struct CtcUnionType;
114
115 template<typename... T>
116 struct CtcUnionType<std::tuple<T...>> {
117 using Ctc = CtcUnion<T...>;
118 };
119
120 template<typename C1,typename C2>
121 typename CtcUnionType<typename C1::ContractedTypes>::Ctc operator|(const C1& c1, const C2& c2)
122 {
123 return { c1, c2 };
124 }
125
126 template<typename C1,typename C2>
127 typename CtcUnionType<typename C1::ContractedTypes>::Ctc operator|(const std::shared_ptr<C1>& c1, const std::shared_ptr<C2>& c2)
128 {
129 return { c1, c2 };
130 }
131
132 template<typename C1,typename C2>
133 typename CtcUnionType<typename C1::ContractedTypes>::Ctc operator|(const std::shared_ptr<C1>& c1, const C2& c2)
134 {
135 return { c1, c2 };
136 }
137
138 template<typename C1,typename C2>
139 typename CtcUnionType<typename C1::ContractedTypes>::Ctc operator|(const C1& c1, const std::shared_ptr<C2>& c2)
140 {
141 return { c1, c2 };
142 }
143
144 template<typename C2>
145 requires std::is_base_of_v<CtcBase<IntervalVector>,C2>
146 inline CtcUnion<IntervalVector> operator|(const IntervalVector& c1, const C2& c2)
147 {
148 assert_release(c1.size() == c2.size());
149 return CtcUnion<IntervalVector>(CtcWrapper(c1),c2);
150 }
151
152 template<typename C1>
153 requires std::is_base_of_v<CtcBase<IntervalVector>,C1>
154 inline CtcUnion<IntervalVector> operator|(const C1& c1, const IntervalVector& c2)
155 {
156 assert_release(c1.size() == c2.size());
157 return CtcUnion<IntervalVector>(c1,CtcWrapper(c2));
158 }
159
160 // Template deduction guides
161 CtcUnion(Index) -> CtcUnion<IntervalVector>;
162
163 template<typename... C>
164 requires (IsCtcBaseOrPtr<C,IntervalVector> && ...)
165 CtcUnion(const C&...) -> CtcUnion<IntervalVector>;
166
167 template<typename C>
168 CtcUnion(std::initializer_list<C>) -> CtcUnion<IntervalVector>;
169}
Definition codac2_OctaSym.h:21
Eigen::Matrix< Interval,-1, 1 > IntervalVector
Alias for a dynamic-size column vector of intervals.
Definition codac2_IntervalVector.h:25