cMHN 1.2
C++ library for learning MHNs with pRC
Loading...
Searching...
No Matches
assignable.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: BSD-2-Clause
2
3#ifndef pRC_CORE_TENSOR_VIEWS_ASSIGNABLE_H
4#define pRC_CORE_TENSOR_VIEWS_ASSIGNABLE_H
5
16
17namespace pRC::TensorViews
18{
19 template<class T, class N, class F>
20 class Assignable : public View<T, N, F>
21 {
22 private:
23 using Base = View<T, N, F>;
24
25 public:
26 constexpr auto &operator=(Zero<> const)
27 {
28 return *this = zero<F>();
29 }
30
31 constexpr auto &operator=(Unit<> const)
32 {
33 return *this = unit<F>();
34 }
35
36 constexpr auto &operator=(Identity<> const)
37 {
38 return *this = identity<F>();
39 }
40
41 template<class X>
42 requires IsAssignable<T &, X> && (Base::Dimension == 0)
43 constexpr auto &operator=(X &&value)
44 {
45 (*this)() = forward<X>(value);
46 return this->self();
47 }
48
49 template<class X, IsTensorish R = RemoveReference<X>>
52 constexpr auto &operator=(X &&rhs)
53 {
55 {
57 [this, &rhs](auto const i)
58 {
59 (*this)[i] = forward<X>(rhs)[i];
60 });
61 }
62 else
63 {
65 [this, &rhs](auto const... indices)
66 {
67 (*this)(indices...) = forward<X>(rhs)(indices...);
68 });
69 }
70 return this->self();
71 }
72
73 template<class X>
75 constexpr auto operator+=(X &&rhs)
76 {
77 this->self() = this->self() + forward<X>(rhs);
78 return this->self();
79 }
80
81 template<class X>
83 constexpr auto operator-=(X &&rhs)
84 {
85 this->self() = this->self() - forward<X>(rhs);
86 return this->self();
87 }
88
89 template<class X>
91 constexpr auto applyOnTheLeft(X &&lhs)
92 {
93 using U = RemoveReference<X>;
94
95 if constexpr(!IsTensorish<U>)
96 {
97 this->self() = forward<X>(lhs) * this->self();
98 return this->self();
99 }
100
101 if constexpr(IsTensorish<U>)
102 {
103 if constexpr(U::Dimension == 0)
104 {
105 this->self() = eval(forward<X>(lhs)) * this->self();
106 return this->self();
107 }
108 }
109
110 this->self() = eval(forward<X>(lhs) * this->self());
111 return this->self();
112 }
113
114 template<class X>
116 constexpr auto applyOnTheRight(X &&rhs)
117 {
118 using U = RemoveReference<X>;
119
120 if constexpr(!IsTensorish<U>)
121 {
122 this->self() = this->self() * forward<X>(rhs);
123 return this->self();
124 }
125
126 if constexpr(IsTensorish<U>)
127 {
128 if constexpr(U::Dimension == 0)
129 {
130 this->self() = this->self() * eval(forward<X>(rhs));
131 return this->self();
132 }
133 }
134
135 this->self() = eval(this->self() * forward<X>(rhs));
136 return this->self();
137 }
138
139 template<class X>
141 constexpr auto operator*=(X &&rhs)
142 {
143 return applyOnTheRight(forward<X>(rhs));
144 }
145
146 template<class X>
148 constexpr auto operator/=(X &&rhs)
149 {
150 this->self() = this->self() / forward<X>(rhs);
151 return this->self();
152 }
153
154 protected:
155 ~Assignable() = default;
156 constexpr Assignable(Assignable const &) = default;
157 constexpr Assignable(Assignable &&) = default;
158 constexpr Assignable &operator=(Assignable const &) = delete;
159 constexpr Assignable &operator=(Assignable &&) = delete;
160 constexpr Assignable() = default;
161 };
162}
163#endif // pRC_CORE_TENSOR_VIEWS_ASSIGNABLE_H
Definition assignable.hpp:21
constexpr auto operator/=(X &&rhs)
Definition assignable.hpp:148
constexpr auto applyOnTheLeft(X &&lhs)
Definition assignable.hpp:91
constexpr Assignable()=default
constexpr Assignable(Assignable const &)=default
constexpr auto applyOnTheRight(X &&rhs)
Definition assignable.hpp:116
constexpr Assignable(Assignable &&)=default
constexpr auto & operator=(Zero<> const)
Definition assignable.hpp:26
constexpr auto & operator=(Identity<> const)
Definition assignable.hpp:36
constexpr auto operator+=(X &&rhs)
Definition assignable.hpp:75
constexpr Assignable & operator=(Assignable &&)=delete
constexpr auto & operator=(X &&rhs)
Definition assignable.hpp:52
constexpr auto operator*=(X &&rhs)
Definition assignable.hpp:141
constexpr auto & operator=(Unit<> const)
Definition assignable.hpp:31
constexpr auto operator-=(X &&rhs)
Definition assignable.hpp:83
constexpr Assignable & operator=(Assignable const &)=delete
Definition declarations.hpp:20
Definition concepts.hpp:40
Definition concepts.hpp:31
Definition concepts.hpp:28
Definition subscript.hpp:21
Definition declarations.hpp:45
int value
Definition gmock-actions_test.cc:1714
int i
Definition gmock-matchers-comparisons_test.cc:603
Definition declarations.hpp:18
static constexpr auto unit()
Definition unit.hpp:13
std::remove_reference_t< T > RemoveReference
Definition basics.hpp:41
static constexpr auto range(F &&f, Xs &&...args)
Definition range.hpp:18
static constexpr auto identity()
Definition identity.hpp:13
static constexpr auto zero()
Definition zero.hpp:12
static constexpr decltype(auto) eval(X &&a)
Definition eval.hpp:12
Definition identity.hpp:11
Definition unit.hpp:11
Definition zero.hpp:11