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 <type_traits>
13#include "codac2_CtcWrapper.h"
14#include "codac2_Collection.h"
16
17namespace codac2
18{
19 template<typename... X>
20 class CtcUnion : public Ctc<CtcUnion<X...>,X...>
21 {
22 public:
23
24 explicit CtcUnion(Index n)
25 : Ctc<CtcUnion<X...>,X...>(n)
26 {
27 if constexpr(std::is_same_v<X...,Interval>)
28 assert_release(n == 1);
29 }
30
31 template<typename C>
32 requires (IsCtcBaseOrPtr<C,X...> && !std::is_same_v<CtcUnion<X...>,C>)
33 CtcUnion(const C& c)
34 : Ctc<CtcUnion<X...>,X...>(size_of(c)), _ctcs(c)
35 { }
36
37 template<typename... C>
38 requires (IsCtcBaseOrPtr<C,X...> && ...)
39 CtcUnion(const C&... c)
40 : Ctc<CtcUnion<X...>,X...>(size_first_item(c...)), _ctcs(c...)
41 {
42 assert_release(all_same_size(c...));
43 }
44
45 size_t nb() const
46 {
47 return _ctcs.size();
48 }
49
50 template<typename X_> // single type
51 void contract_impl(X_& x) const
52 {
53 auto result = x;
54 result.set_empty();
55
56 for(const auto& ci : _ctcs)
57 {
58 auto saved_x = x;
59 ci->contract(saved_x);
60 result |= saved_x;
61 }
62
63 x = result;
64 }
65
66 void contract(X&... x) const
67 {
68 // contract_impl(..) method for multiple types is not yet implemented
69 contract_impl(x...);
70 }
71
72 template<typename C>
73 requires IsCtcBaseOrPtr<C,X...>
74 CtcUnion<X...>& operator|=(const C& c)
75 {
76 assert_release(size_of(c) == this->size());
77 _ctcs.push_back(c);
78 return *this;
79 }
80
81 protected:
82
83 Collection<CtcBase<X...>> _ctcs;
84 };
85
86 template <>
87 class CtcUnion<> : public CtcUnion<IntervalVector>
88 { };
89
90 template<typename Tuple>
91 struct CtcUnionType;
92
93 template<typename... T>
94 struct CtcUnionType<std::tuple<T...>> {
95 using Ctc = CtcUnion<T...>;
96 };
97
98 template<typename C1,typename C2>
99 typename CtcUnionType<typename C1::ContractedTypes>::Ctc operator|(const C1& c1, const C2& c2)
100 {
101 return { c1, c2 };
102 }
103
104 template<typename C1,typename C2>
105 typename CtcUnionType<typename C1::ContractedTypes>::Ctc operator|(const std::shared_ptr<C1>& c1, const std::shared_ptr<C2>& c2)
106 {
107 return { c1, c2 };
108 }
109
110 template<typename C1,typename C2>
111 typename CtcUnionType<typename C1::ContractedTypes>::Ctc operator|(const std::shared_ptr<C1>& c1, const C2& c2)
112 {
113 return { c1, c2 };
114 }
115
116 template<typename C1,typename C2>
117 typename CtcUnionType<typename C1::ContractedTypes>::Ctc operator|(const C1& c1, const std::shared_ptr<C2>& c2)
118 {
119 return { c1, c2 };
120 }
121
122 template<typename C2>
123 requires std::is_base_of_v<CtcBase<IntervalVector>,C2>
124 inline CtcUnion<IntervalVector> operator|(const IntervalVector& c1, const C2& c2)
125 {
126 assert_release(c1.size() == c2.size());
127 return CtcUnion<IntervalVector>(CtcWrapper(c1),c2);
128 }
129
130 template<typename C1>
131 requires std::is_base_of_v<CtcBase<IntervalVector>,C1>
132 inline CtcUnion<IntervalVector> operator|(const C1& c1, const IntervalVector& c2)
133 {
134 assert_release(c1.size() == c2.size());
135 return CtcUnion<IntervalVector>(c1,CtcWrapper(c2));
136 }
137
138 // Template deduction guides
139 CtcUnion(Index) -> CtcUnion<IntervalVector>;
140}
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