codac 2.0.0
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 size_t nb() const
47 {
48 return _ctcs.size();
49 }
50
51 void contract(X&... x) const
52 {
53 auto result = std::tuple<X...>(x...);
54 std::apply([](auto&... xi)
55 {
56 (xi.set_empty(), ...);
57 }, result);
58
59 auto accumulate_union = [&]<std::size_t... I>(const std::tuple<X...>& y, std::index_sequence<I...>)
60 {
61 ((std::get<I>(result) |= std::get<I>(y)), ...);
62 };
63
64 for(const auto& ci : _ctcs)
65 {
66 auto saved = std::tuple<X...>(x...);
67
68 std::apply([&](auto&... xi)
69 {
70 ci->contract(xi...);
71 }, saved);
72
73 accumulate_union(saved, std::index_sequence_for<X...>{});
74 }
75
76 std::tie(x...) = result;
77 }
78
79 template<typename C>
80 requires IsCtcBaseOrPtr<C,X...>
81 CtcUnion<X...>& operator|=(const C& c)
82 {
83 assert_release(size_of(c) == this->size());
84 _ctcs.push_back(c);
85 return *this;
86 }
87
88 protected:
89
90 Collection<CtcBase<X...>> _ctcs;
91 };
92
93 template <>
94 class CtcUnion<> : public CtcUnion<IntervalVector>
95 { };
96
97 template<typename Tuple>
98 struct CtcUnionType;
99
100 template<typename... T>
101 struct CtcUnionType<std::tuple<T...>> {
102 using Ctc = CtcUnion<T...>;
103 };
104
105 template<typename C1,typename C2>
106 typename CtcUnionType<typename C1::ContractedTypes>::Ctc operator|(const C1& c1, const C2& c2)
107 {
108 return { c1, c2 };
109 }
110
111 template<typename C1,typename C2>
112 typename CtcUnionType<typename C1::ContractedTypes>::Ctc operator|(const std::shared_ptr<C1>& c1, const std::shared_ptr<C2>& c2)
113 {
114 return { c1, c2 };
115 }
116
117 template<typename C1,typename C2>
118 typename CtcUnionType<typename C1::ContractedTypes>::Ctc operator|(const std::shared_ptr<C1>& c1, const C2& c2)
119 {
120 return { c1, c2 };
121 }
122
123 template<typename C1,typename C2>
124 typename CtcUnionType<typename C1::ContractedTypes>::Ctc operator|(const C1& c1, const std::shared_ptr<C2>& c2)
125 {
126 return { c1, c2 };
127 }
128
129 template<typename C2>
130 requires std::is_base_of_v<CtcBase<IntervalVector>,C2>
131 inline CtcUnion<IntervalVector> operator|(const IntervalVector& c1, const C2& c2)
132 {
133 assert_release(c1.size() == c2.size());
134 return CtcUnion<IntervalVector>(CtcWrapper(c1),c2);
135 }
136
137 template<typename C1>
138 requires std::is_base_of_v<CtcBase<IntervalVector>,C1>
139 inline CtcUnion<IntervalVector> operator|(const C1& c1, const IntervalVector& c2)
140 {
141 assert_release(c1.size() == c2.size());
142 return CtcUnion<IntervalVector>(c1,CtcWrapper(c2));
143 }
144
145 // Template deduction guides
146 CtcUnion(Index) -> CtcUnion<IntervalVector>;
147}
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