codac 1.5.6
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=IntervalVector>
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 {
29 assert(n == 1);
30 }
31 }
32
33 template<typename C>
34 requires (IsCtcBaseOrPtr<C,X> && !std::is_same_v<CtcUnion<X>,C>)
35 CtcUnion(const C& c)
36 : Ctc<CtcUnion<X>,X>(size_of(c)), _ctcs(c)
37 { }
38
39 template<typename... C>
40 requires (IsCtcBaseOrPtr<C,X> && ...)
41 CtcUnion(const C&... c)
42 : Ctc<CtcUnion<X>,X>(size_first_item(c...)), _ctcs(c...)
43 {
44 assert_release(all_same_size(c...));
45 }
46
47 void contract(X& x) const
48 {
49 auto result = x;
50 result.set_empty();
51
52 for(const auto& ci : _ctcs)
53 {
54 auto saved_x = x;
55 ci->contract(saved_x);
56 result |= saved_x;
57 }
58
59 x = result;
60 }
61
62 template<typename C>
63 requires std::is_base_of_v<CtcBase<X>,C>
64 CtcUnion<X>& operator|=(const C& c)
65 {
66 assert_release(c.size() == this->size());
67 _ctcs.add_shared_ptr(std::make_shared<C>(c));
68 return *this;
69 }
70
71 CtcUnion<X>& operator|=(const std::shared_ptr<CtcBase<X>>& c)
72 {
73 assert_release(c->size() == this->size());
74 _ctcs.add_shared_ptr(c);
75 return *this;
76 }
77
78 protected:
79
80 Collection<CtcBase<X>> _ctcs;
81 };
82
83 template<typename C1, typename C2>
84 requires (std::is_same_v<typename C1::ContractedType,typename C2::ContractedType>
85 && std::is_base_of_v<CtcBase<typename C1::ContractedType>,C1>
86 && std::is_base_of_v<CtcBase<typename C1::ContractedType>,C2>)
87 inline CtcUnion<typename C1::ContractedType> operator|(const C1& c1, const C2& c2)
88 {
89 return CtcUnion<typename C1::ContractedType>(c1,c2);
90 }
91
92 template<typename C2>
93 requires std::is_base_of_v<CtcBase<IntervalVector>,C2>
94 inline CtcUnion<IntervalVector> operator|(const IntervalVector& c1, const C2& c2)
95 {
96 assert_release(c1.size() == c2.size());
97 return CtcUnion<IntervalVector>(CtcWrapper(c1),c2);
98 }
99
100 template<typename C1>
101 requires std::is_base_of_v<CtcBase<IntervalVector>,C1>
102 inline CtcUnion<IntervalVector> operator|(const C1& c1, const IntervalVector& c2)
103 {
104 assert_release(c1.size() == c2.size());
105 return CtcUnion<IntervalVector>(c1,CtcWrapper(c2));
106 }
107}