cMHN 1.1
C++ library for learning MHNs with pRC
Loading...
Searching...
No Matches
loop.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: BSD-2-Clause
2
3#ifndef pRC_CORE_TENSOR_VIEWS_LOOP_H
4#define pRC_CORE_TENSOR_VIEWS_LOOP_H
5
9
10namespace pRC::TensorViews
11{
12 template<class T, class N, class F, class... Vs>
13 class Loop
14 : public Conditional<IsAssignable<ResultOf<F,
15 ResultOf<Vs, typename Vs::Subscripts>...>>,
16 Assignable<T, N, Loop<T, N, F, Vs...>>,
17 View<T, N, Loop<T, N, F, Vs...>>>
18 {
19 static_assert(All<IsTensorView<Vs>...>());
20
21 private:
22 using Base = Conditional<
25
26 public:
27 template<class... Xs, If<All<IsSame<Vs, RemoveReference<Xs>>...>> = 0>
28 Loop(F f, Xs &&...args)
29 : mF(forward<F>(f))
30 , mArgs(forward<Xs>(args)...)
31 {
32 }
33
34 using Base::operator=;
35
36 template<class... Is, If<All<IsConvertible<Is, Index>...>> = 0,
37 If<IsSatisfied<(sizeof...(Is) == typename Base::Dimension())>> = 0>
38 constexpr decltype(auto) operator()(Is const... indices)
39 {
40 return operator()(typename Base::Subscripts(indices...));
41 }
42
43 template<class... Is, If<All<IsConvertible<Is, Index>...>> = 0,
44 If<IsSatisfied<(sizeof...(Is) == typename Base::Dimension())>> = 0>
45 constexpr decltype(auto) operator()(Is const... indices) const
46 {
47 return operator()(typename Base::Subscripts(indices...));
48 }
49
50 constexpr decltype(auto) operator()(
51 typename Base::Subscripts const &subscripts)
52 {
53 check(subscripts);
55 [this, &subscripts](auto const... ops) -> decltype(auto)
56 {
57 return mF(get<ops>(mArgs)(subscripts)...);
58 });
59 }
60
61 constexpr decltype(auto) operator()(
62 typename Base::Subscripts const &subscripts) const
63 {
64 check(subscripts);
66 [this, &subscripts](auto const... ops) -> decltype(auto)
67 {
68 return mF(get<ops>(mArgs)(subscripts)...);
69 });
70 }
71
72 template<class E = All<IsSubscriptable<Vs>...>, If<E> = 0>
73 constexpr decltype(auto) operator[](Index const index)
74 {
75 check(index);
77 [this, &index](auto const... ops) -> decltype(auto)
78 {
79 return mF(get<ops>(mArgs)[index]...);
80 });
81 }
82
83 template<class E = All<IsSubscriptable<Vs>...>, If<E> = 0>
84 constexpr decltype(auto) operator[](Index const index) const
85 {
86 check(index);
88 [this, &index](auto const... ops) -> decltype(auto)
89 {
90 return mF(get<ops>(mArgs)[index]...);
91 });
92 }
93
94 private:
95 static constexpr auto check(
96 [[maybe_unused]] typename Base::Subscripts const &subscripts)
97 {
98 if constexpr(sizeof...(Vs) == 0 && cDebugLevel >= DebugLevel::Mid)
99 {
100 if(subscripts.isOutOfRange())
101 {
102 Logging::error("Tensor View Loop subscripts out of range.");
103 }
104 }
105 }
106
107 static constexpr auto check([[maybe_unused]] Index const index)
108 {
109 if constexpr(sizeof...(Vs) == 0 && cDebugLevel >= DebugLevel::Mid)
110 {
111 if(!(index < Base::size()))
112 {
113 Logging::error("Tensor View Loop index out of range.");
114 }
115 }
116 }
117
118 private:
119 F mF;
120 tuple<Vs...> mArgs;
121 };
122}
123#endif // pRC_CORE_TENSOR_VIEWS_LOOP_H
Definition assignable.hpp:22
Definition loop.hpp:18
constexpr decltype(auto) operator()(Is const ... indices)
Definition loop.hpp:38
constexpr decltype(auto) operator()(Is const ... indices) const
Definition loop.hpp:45
Loop(F f, Xs &&...args)
Definition loop.hpp:28
Definition type_traits.hpp:32
pRC::Float<> T
Definition externs_nonTT.hpp:1
static void error(Xs &&...args)
Definition log.hpp:14
Definition diagonal.hpp:11
static constexpr auto makeConstantSequence()
Definition sequence.hpp:402
Size Index
Definition type_traits.hpp:21
std::enable_if_t< B{}, int > If
Definition type_traits.hpp:68
Constant< Bool, B > IsSatisfied
Definition type_traits.hpp:71
std::is_assignable< T, U > IsAssignable
Definition type_traits.hpp:146
constexpr auto cDebugLevel
Definition config.hpp:46
std::conditional_t< B{}, T, F > Conditional
Definition type_traits.hpp:131
std::conjunction< Bs... > All
Definition type_traits.hpp:77
static constexpr decltype(auto) expand(Sequence< T, Seq... > const, F &&f, Xs &&...args)
Definition sequence.hpp:344