cMHN 1.2
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 requires(IsTensorView<Vs> && ...)
14 class Loop
16 ResultOf<Vs, typename Vs::Subscripts>...>>,
17 Assignable<T, N, Loop<T, N, F, Vs...>>,
18 View<T, N, Loop<T, N, F, Vs...>>>
19 {
20 private:
21 using Base = Conditional<
24
25 public:
26 template<class... Xs>
27 requires(IsSame<Vs, RemoveReference<Xs>> && ...)
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<IsConvertible<Index>... Is>
37 requires(sizeof...(Is) == Base::Dimension)
38 constexpr decltype(auto) operator()(Is const... indices)
39 {
40 return operator()(typename Base::Subscripts(indices...));
41 }
42
43 template<IsConvertible<Index>... Is>
44 requires(sizeof...(Is) == Base::Dimension)
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 constexpr decltype(auto) operator[](Index const index)
73 requires(IsSubscriptable<Vs> && ...)
74 {
75 check(index);
77 [this, &index](auto const... ops) -> decltype(auto)
78 {
79 return mF(get<ops>(mArgs)[index]...);
80 });
81 }
82
83 constexpr decltype(auto) operator[](Index const index) const
84 requires(IsSubscriptable<Vs> && ...)
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 gtest_unittest.cc:5120
Definition value.hpp:12
Definition subscripts.hpp:21
Definition assignable.hpp:21
Definition loop.hpp:19
Definition declarations.hpp:20
Definition concepts.hpp:40
Definition concepts.hpp:28
Definition subscript.hpp:21
pRC::Float<> T
Definition externs_nonTT.hpp:1
static void error(Xs &&...args)
Definition log.hpp:14
Definition declarations.hpp:18
Size Index
Definition basics.hpp:32
static constexpr auto makeSeriesFor()
Definition sequence.hpp:399
std::tuple< Ts... > Tuple
Definition basics.hpp:23
std::invoke_result_t< F, Args... > ResultOf
Definition basics.hpp:59
constexpr auto cDebugLevel
Definition config.hpp:48
std::conditional_t< B, T, F > Conditional
Definition basics.hpp:56
static constexpr decltype(auto) expand(Sequence< T, Seq... > const, F &&f, Xs &&...args)
Definition sequence.hpp:383