Eigen-Contrib  5.0.1-dev+master
 
Loading...
Searching...
No Matches
TensorFixedSize.h
1// This file is part of Eigen, a lightweight C++ template library
2// for linear algebra.
3//
4// Copyright (C) 2014 Benoit Steiner <benoit.steiner.goog@gmail.com>
5//
6// This Source Code Form is subject to the terms of the Mozilla
7// Public License v. 2.0. If a copy of the MPL was not distributed
8// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9// SPDX-License-Identifier: MPL-2.0
10
11#ifndef EIGEN_TENSOR_TENSOR_FIXED_SIZE_H
12#define EIGEN_TENSOR_TENSOR_FIXED_SIZE_H
13
14// IWYU pragma: private
15#include "./InternalHeaderCheck.h"
16
17namespace Eigen {
18
29template <typename Scalar_, typename Dimensions_, int Options_, typename IndexType>
30class TensorFixedSize : public TensorBase<TensorFixedSize<Scalar_, Dimensions_, Options_, IndexType> > {
31 public:
32 using Self = TensorFixedSize<Scalar_, Dimensions_, Options_, IndexType>;
34 using Nested = typename Eigen::internal::ref_selector<Self>::type;
35 using StorageKind = typename internal::traits<Self>::StorageKind;
36 using Index = typename internal::traits<Self>::Index;
37 using Scalar = Scalar_;
38 using RealScalar = typename NumTraits<Scalar>::Real;
39 using CoeffReturnType = typename Base::CoeffReturnType;
40
41 static constexpr int Options = Options_;
42 static constexpr int Layout = Options_ & RowMajor ? RowMajor : ColMajor;
43
44 enum {
45 IsAligned = bool(EIGEN_MAX_STATIC_ALIGN_BYTES >= Aligned),
46 PacketAccess = (internal::packet_traits<Scalar>::size > 1),
47 BlockAccess = false,
48 PreferBlockAccess = false,
49 CoordAccess = true,
50 RawAccess = true
51 };
52
53 //===- Tensor block evaluation strategy (see TensorBlock.h) -------------===//
54 using TensorBlock = internal::TensorBlockNotImplemented;
55 //===--------------------------------------------------------------------===//
56
57 using Dimensions = Dimensions_;
58 static constexpr std::size_t NumIndices = Dimensions::count;
59
60 protected:
61 TensorStorage<Scalar, Dimensions, Options> m_storage;
62
63 public:
64 constexpr EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index rank() const { return NumIndices; }
65 template <Index N>
66 static constexpr EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index dimension() {
67 EIGEN_STATIC_ASSERT(0 <= N && N < NumIndices, INVALID_DIMENSION);
68 return Dimensions{}[N];
69 }
70 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index dimension(Index n) const {
71 eigen_assert(0 <= n && n < static_cast<Index>(NumIndices));
72 return m_storage.dimensions()[n];
73 }
74 constexpr EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Dimensions dimensions() const { return m_storage.dimensions(); }
75 constexpr EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index size() const { return m_storage.size(); }
76 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar* data() { return m_storage.data(); }
77 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Scalar* data() const { return m_storage.data(); }
78
79 // This makes EIGEN_INITIALIZE_COEFFS_IF_THAT_OPTION_IS_ENABLED
80 // work, because that uses base().coeffRef() - and we don't yet
81 // implement a similar class hierarchy
82 inline Self& base() { return *this; }
83 inline const Self& base() const { return *this; }
84
85 template <typename... IndexTypes>
86 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Scalar& coeff(Index firstIndex, IndexTypes... otherIndices) const {
87 // The number of indices used to access a tensor coefficient must be equal to the rank of the tensor.
88 EIGEN_STATIC_ASSERT(sizeof...(otherIndices) + 1 == NumIndices, YOU_MADE_A_PROGRAMMING_MISTAKE)
89 eigen_assert(internal::indices_fit<Index>(otherIndices...));
90 return coeff(array<Index, NumIndices>{{firstIndex, static_cast<Index>(otherIndices)...}});
91 }
92
93 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Scalar& coeff(const array<Index, NumIndices>& indices) const {
94 eigen_internal_assert(checkIndexRange(indices));
95 return m_storage.data()[linearizedIndex(indices)];
96 }
97
98 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Scalar& coeff(Index index) const {
99 eigen_internal_assert(index >= 0 && index < size());
100 return m_storage.data()[index];
101 }
102
103 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Scalar& coeff() const {
104 EIGEN_STATIC_ASSERT(NumIndices == 0, YOU_MADE_A_PROGRAMMING_MISTAKE);
105 return m_storage.data()[0];
106 }
107
108 template <typename... IndexTypes>
109 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar& coeffRef(Index firstIndex, IndexTypes... otherIndices) {
110 // The number of indices used to access a tensor coefficient must be equal to the rank of the tensor.
111 EIGEN_STATIC_ASSERT(sizeof...(otherIndices) + 1 == NumIndices, YOU_MADE_A_PROGRAMMING_MISTAKE)
112 eigen_assert(internal::indices_fit<Index>(otherIndices...));
113 return coeffRef(array<Index, NumIndices>{{firstIndex, static_cast<Index>(otherIndices)...}});
114 }
115
116 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar& coeffRef(const array<Index, NumIndices>& indices) {
117 eigen_internal_assert(checkIndexRange(indices));
118 return m_storage.data()[linearizedIndex(indices)];
119 }
120
121 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar& coeffRef(Index index) {
122 eigen_internal_assert(index >= 0 && index < size());
123 return m_storage.data()[index];
124 }
125
126 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar& coeffRef() {
127 EIGEN_STATIC_ASSERT(NumIndices == 0, YOU_MADE_A_PROGRAMMING_MISTAKE);
128 return m_storage.data()[0];
129 }
130
131 template <typename... IndexTypes>
132 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Scalar& operator()(Index firstIndex, IndexTypes... otherIndices) const {
133 // The number of indices used to access a tensor coefficient must be equal to the rank of the tensor.
134 EIGEN_STATIC_ASSERT(sizeof...(otherIndices) + 1 == NumIndices, YOU_MADE_A_PROGRAMMING_MISTAKE)
135 eigen_assert(internal::indices_fit<Index>(otherIndices...));
136 return this->operator()(array<Index, NumIndices>{{firstIndex, static_cast<Index>(otherIndices)...}});
137 }
138
139 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Scalar& operator()(const array<Index, NumIndices>& indices) const {
140 eigen_assert(checkIndexRange(indices));
141 return coeff(indices);
142 }
143
144 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Scalar& operator()(Index index) const {
145 eigen_internal_assert(index >= 0 && index < size());
146 return coeff(index);
147 }
148
149 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Scalar& operator()() const {
150 EIGEN_STATIC_ASSERT(NumIndices == 0, YOU_MADE_A_PROGRAMMING_MISTAKE);
151 return coeff();
152 }
153
154 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Scalar& operator[](Index index) const {
155 // The bracket operator is only for vectors, use the parenthesis operator instead.
156 EIGEN_STATIC_ASSERT(NumIndices == 1, YOU_MADE_A_PROGRAMMING_MISTAKE);
157 return coeff(index);
158 }
159
160 template <typename... IndexTypes>
161 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar& operator()(Index firstIndex, IndexTypes... otherIndices) {
162 // The number of indices used to access a tensor coefficient must be equal to the rank of the tensor.
163 EIGEN_STATIC_ASSERT(sizeof...(otherIndices) + 1 == NumIndices, YOU_MADE_A_PROGRAMMING_MISTAKE)
164 eigen_assert(internal::indices_fit<Index>(otherIndices...));
165 return operator()(array<Index, NumIndices>{{firstIndex, static_cast<Index>(otherIndices)...}});
166 }
167
168 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar& operator()(const array<Index, NumIndices>& indices) {
169 eigen_assert(checkIndexRange(indices));
170 return coeffRef(indices);
171 }
172
173 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar& operator()(Index index) {
174 eigen_assert(index >= 0 && index < size());
175 return coeffRef(index);
176 }
177
178 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar& operator()() {
179 EIGEN_STATIC_ASSERT(NumIndices == 0, YOU_MADE_A_PROGRAMMING_MISTAKE);
180 return coeffRef();
181 }
182
183 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar& operator[](Index index) {
184 // The bracket operator is only for vectors, use the parenthesis operator instead.
185 EIGEN_STATIC_ASSERT(NumIndices == 1, YOU_MADE_A_PROGRAMMING_MISTAKE)
186 return coeffRef(index);
187 }
188
189 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorFixedSize() : m_storage() {}
190
191 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorFixedSize(Self&& other) : m_storage(other.m_storage) {}
192
193 template <typename OtherDerived>
194 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorFixedSize(const TensorBase<OtherDerived, ReadOnlyAccessors>& other) {
196 Assign assign(*this, other.derived());
197 internal::TensorExecutor<const Assign, DefaultDevice>::run(assign, DefaultDevice());
198 }
199 template <typename OtherDerived>
200 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorFixedSize(const TensorBase<OtherDerived, WriteAccessors>& other) {
202 Assign assign(*this, other.derived());
203 internal::TensorExecutor<const Assign, DefaultDevice>::run(assign, DefaultDevice());
204 }
205
206 // FIXME: check that the dimensions of other match the dimensions of *this.
207 // Unfortunately this isn't possible yet when the rhs is an expression.
208 EIGEN_INHERIT_ASSIGNMENT_OPERATORS(TensorFixedSize)
209
210 protected:
211 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE bool checkIndexRange(const array<Index, NumIndices>& /*indices*/) const {
212 return true;
213 }
214
215 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index linearizedIndex(const array<Index, NumIndices>& indices) const {
216 EIGEN_IF_CONSTEXPR (Options & RowMajor) {
217 return internal::convert_index<Index>(m_storage.dimensions().IndexOfRowMajor(indices));
218 } else {
219 return internal::convert_index<Index>(m_storage.dimensions().IndexOfColMajor(indices));
220 }
221 }
222};
223
224#if EIGEN_COMP_CXXVER < 17
225template <typename Scalar_, typename Dimensions_, int Options_, typename IndexType>
226constexpr std::size_t TensorFixedSize<Scalar_, Dimensions_, Options_, IndexType>::NumIndices;
227template <typename Scalar_, typename Dimensions_, int Options_, typename IndexType>
228constexpr int TensorFixedSize<Scalar_, Dimensions_, Options_, IndexType>::Layout;
229template <typename Scalar_, typename Dimensions_, int Options_, typename IndexType>
230constexpr int TensorFixedSize<Scalar_, Dimensions_, Options_, IndexType>::Options;
231#endif
232
233} // end namespace Eigen
234
235#endif // EIGEN_TENSOR_TENSOR_FIXED_SIZE_H
Definition TensorAssign.h:47
The tensor base class.
Definition TensorForwardDeclarations.h:69
Namespace containing all symbols from the Eigen library.