cMHN 1.2
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 requires IsTensorView<V>
15 class Chip
16 : public Conditional<IsAssignable<V>,
17 Assignable<T, N, Chip<T, N, D, V>>, View<T, N, Chip<T, N, D, V>>>
18 {
19 private:
22
23 public:
24 template<class X>
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<IsConvertible<Index>... Is>
42 requires(sizeof...(Is) == Base::Dimension)
43 constexpr decltype(auto) operator()(Is const... indices)
44 {
45 return this->call(indices...);
46 }
47
48 template<IsConvertible<Index>... Is>
49 requires(sizeof...(Is) == Base::Dimension)
50 constexpr decltype(auto) operator()(Is const... indices) const
51 {
52 return this->call(indices...);
53 }
54
55 constexpr decltype(auto) operator()(
56 typename Base::Subscripts const &subscripts)
57 {
58 return operator()(subscripts, makeRange<Index, 0, D>(),
60 }
61
62 constexpr decltype(auto) operator()(
63 typename Base::Subscripts const &subscripts) const
64 {
65 return operator()(subscripts, makeRange<Index, 0, D>(),
67 }
68
69 constexpr decltype(auto) operator[](Index const index)
70 requires IsSubscriptable<V> && (D == 0 || D == Base::Dimension)
71 {
72 if constexpr(D == 0)
73 {
74 return mA[V::size(0) * index + mIndex];
75 }
76 else
77 {
78 return mA[Base::size() * mIndex + index];
79 }
80 }
81
82 constexpr decltype(auto) operator[](Index const index) const
83 requires IsSubscriptable<V> && (D == 0 || D == Base::Dimension)
84 {
85 if constexpr(D == 0)
86 {
87 return mA[V::size(0) * index + mIndex];
88 }
89 else
90 {
91 return mA[Base::size() * mIndex + index];
92 }
93 }
94
95 private:
96 template<Index... Ss, Index... Es>
97 constexpr decltype(auto) operator()(
98 typename Base::Subscripts const &subscripts,
99 Sequence<Index, Ss...> const, Sequence<Index, Es...> const)
100 {
101 return mA(subscripts[Ss]..., mIndex, subscripts[Es]...);
102 }
103
104 template<Index... Ss, Index... Es>
105 constexpr decltype(auto) operator()(
106 typename Base::Subscripts const &subscripts,
107 Sequence<Index, Ss...> const, Sequence<Index, Es...> const) const
108 {
109 return mA(subscripts[Ss]..., mIndex, subscripts[Es]...);
110 }
111
112 private:
113 V mA;
114 Index const mIndex;
115 };
116}
117#endif // pRC_CORE_TENSOR_VIEWS_CHIP_H
pRC::Size const D
Definition CalculatePThetaTests.cpp:9
Definition gtest_unittest.cc:5120
Definition sequence.hpp:29
Definition assignable.hpp:21
Definition chip.hpp:18
Chip(X &&a, Index const index)
Definition chip.hpp:26
constexpr decltype(auto) operator()(Is const ... indices)
Definition chip.hpp:43
Definition declarations.hpp:20
Definition concepts.hpp:28
Definition subscript.hpp:21
static void error(Xs &&...args)
Definition log.hpp:14
Definition declarations.hpp:18
Size Index
Definition basics.hpp:32
static constexpr auto makeRange()
Definition sequence.hpp:421
constexpr auto cDebugLevel
Definition config.hpp:48
std::conditional_t< B, T, F > Conditional
Definition basics.hpp:56