cMHN 1.1
C++ library for learning MHNs with pRC
Loading...
Searching...
No Matches
chip.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: BSD-2-Clause
2
3#ifndef pRC_CORE_TENSOR_VIEWS_CHIP_H
4#define pRC_CORE_TENSOR_VIEWS_CHIP_H
5
10
11namespace pRC::TensorViews
12{
13 template<class T, class N, Index D, class V>
14 class Chip
15 : public Conditional<IsAssignable<V>,
16 Assignable<T, N, Chip<T, N, D, V>>, View<T, N, Chip<T, N, D, V>>>
17 {
18 static_assert(IsTensorView<V>());
19
20 private:
23
24 public:
25 template<class X, If<IsSame<V, RemoveReference<X>>> = 0>
26 Chip(X &&a, Index const index)
27 : mA(forward<X>(a))
28 , mIndex(index)
29 {
30 if constexpr(cDebugLevel >= DebugLevel::Low)
31 {
32 if(!(mIndex < V::size(D)))
33 {
34 Logging::error("Chip index out of range.");
35 }
36 }
37 }
38
39 using Base::operator=;
40
41 template<class... Is, If<All<IsConvertible<Is, Index>...>> = 0,
42 If<IsSatisfied<(sizeof...(Is) == typename Base::Dimension())>> = 0>
43 constexpr decltype(auto) operator()(Is const... indices)
44 {
45 return operator()(Indices<sizeof...(Is)>(indices...),
47 makeRange<Index, D, typename Base::Dimension{}>());
48 }
49
50 template<class... Is, If<All<IsConvertible<Is, Index>...>> = 0,
51 If<IsSatisfied<(sizeof...(Is) == typename Base::Dimension())>> = 0>
52 constexpr decltype(auto) operator()(Is const... indices) const
53 {
54 return operator()(Indices<sizeof...(Is)>(indices...),
56 makeRange<Index, D, typename Base::Dimension{}>());
57 }
58
59 constexpr decltype(auto) operator()(
60 typename Base::Subscripts const &subscripts)
61 {
62 return this->call(subscripts);
63 }
64
65 constexpr decltype(auto) operator()(
66 typename Base::Subscripts const &subscripts) const
67 {
68 return this->call(subscripts);
69 }
70
71 template<class E = IsSubscriptable<V>,
72 class L1 = IsSatisfied<(D == typename Base::Dimension())>,
73 class L2 = IsSatisfied<(D == 0)>, If<All<E, Any<L1, L2>>> = 0>
74 constexpr decltype(auto) operator[](Index const index)
75 {
76 if constexpr(D == 0)
77 {
78 return mA[V::size(0) * index + mIndex];
79 }
80 else
81 {
82 return mA[Base::size() * mIndex + index];
83 }
84 }
85
86 template<class E = IsSubscriptable<V>,
87 class L1 = IsSatisfied<(D == typename Base::Dimension())>,
88 class L2 = IsSatisfied<(D == 0)>, If<All<E, Any<L1, L2>>> = 0>
89 constexpr decltype(auto) operator[](Index const index) const
90 {
91 if constexpr(D == 0)
92 {
93 return mA[V::size(0) * index + mIndex];
94 }
95 else
96 {
97 return mA[Base::size() * mIndex + index];
98 }
99 }
100
101 private:
102 template<Index... Ss, Index... Es>
103 constexpr decltype(auto) operator()(
104 Indices<typename Base::Dimension{}> const &indices,
105 Sequence<Index, Ss...> const, Sequence<Index, Es...> const)
106 {
107 return mA(indices[Ss]..., mIndex, indices[Es]...);
108 }
109
110 template<Index... Ss, Index... Es>
111 constexpr decltype(auto) operator()(
112 Indices<typename Base::Dimension{}> const &indices,
113 Sequence<Index, Ss...> const, Sequence<Index, Es...> const) const
114 {
115 return mA(indices[Ss]..., mIndex, indices[Es]...);
116 }
117
118 private:
119 V mA;
120 Index const mIndex;
121 };
122}
123#endif // pRC_CORE_TENSOR_VIEWS_CHIP_H
pRC::Size const D
Definition CalculatePThetaTests.cpp:9
Definition indices.hpp:15
Definition sequence.hpp:34
Definition assignable.hpp:22
Definition chip.hpp:17
constexpr decltype(auto) operator()(Is const ... indices)
Definition chip.hpp:43
constexpr decltype(auto) operator()(Is const ... indices) const
Definition chip.hpp:52
Chip(X &&a, Index const index)
Definition chip.hpp:26
Definition type_traits.hpp:32
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
Indices(Is const ...) -> Indices< sizeof...(Is)>
Constant< Bool, B > IsSatisfied
Definition type_traits.hpp:71
static constexpr auto makeRange()
Definition sequence.hpp:379
constexpr auto cDebugLevel
Definition config.hpp:46
Sequence(std::integer_sequence< T, Seq... > const) -> Sequence< T, Seq... >
std::conditional_t< B{}, T, F > Conditional
Definition type_traits.hpp:131