10 #ifndef EIGEN_CXX11_TENSOR_TENSOR_MAP_H
11 #define EIGEN_CXX11_TENSOR_TENSOR_MAP_H
29 template<
typename PlainObjectType,
int Options_,
template <
class>
class MakePointer_>
class TensorMap :
public TensorBase<TensorMap<PlainObjectType, Options_, MakePointer_> >
33 typedef typename PlainObjectType::Base Base;
34 typedef typename Eigen::internal::nested<Self>::type Nested;
35 typedef typename internal::traits<PlainObjectType>::StorageKind StorageKind;
36 typedef typename internal::traits<PlainObjectType>::Index Index;
37 typedef typename internal::traits<PlainObjectType>::Scalar Scalar;
39 typedef typename Base::CoeffReturnType CoeffReturnType;
46 typedef typename MakePointer_<Scalar>::Type PointerType;
47 typedef PointerType PointerArgType;
49 static const int Options = Options_;
51 static const Index NumIndices = PlainObjectType::NumIndices;
52 typedef typename PlainObjectType::Dimensions Dimensions;
56 Layout = PlainObjectType::Layout,
62 EIGEN_STRONG_INLINE
TensorMap(PointerArgType dataPtr) : m_data(dataPtr), m_dimensions() {
64 EIGEN_STATIC_ASSERT((0 == NumIndices || NumIndices ==
Dynamic), YOU_MADE_A_PROGRAMMING_MISTAKE)
67 #if EIGEN_HAS_VARIADIC_TEMPLATES
68 template<
typename... IndexTypes> EIGEN_DEVICE_FUNC
69 EIGEN_STRONG_INLINE
TensorMap(PointerArgType dataPtr, Index firstDimension, IndexTypes... otherDimensions) : m_data(dataPtr), m_dimensions(firstDimension, otherDimensions...) {
71 EIGEN_STATIC_ASSERT((
sizeof...(otherDimensions) + 1 == NumIndices || NumIndices ==
Dynamic), YOU_MADE_A_PROGRAMMING_MISTAKE)
75 EIGEN_STRONG_INLINE
TensorMap(PointerArgType dataPtr, Index firstDimension) : m_data(dataPtr), m_dimensions(firstDimension) {
77 EIGEN_STATIC_ASSERT((1 == NumIndices || NumIndices ==
Dynamic), YOU_MADE_A_PROGRAMMING_MISTAKE)
80 EIGEN_STRONG_INLINE
TensorMap(PointerArgType dataPtr, Index dim1, Index dim2) : m_data(dataPtr), m_dimensions(dim1, dim2) {
81 EIGEN_STATIC_ASSERT(2 == NumIndices || NumIndices ==
Dynamic, YOU_MADE_A_PROGRAMMING_MISTAKE)
84 EIGEN_STRONG_INLINE
TensorMap(PointerArgType dataPtr, Index dim1, Index dim2, Index dim3) : m_data(dataPtr), m_dimensions(dim1, dim2, dim3) {
85 EIGEN_STATIC_ASSERT(3 == NumIndices || NumIndices ==
Dynamic, YOU_MADE_A_PROGRAMMING_MISTAKE)
88 EIGEN_STRONG_INLINE
TensorMap(PointerArgType dataPtr, Index dim1, Index dim2, Index dim3, Index dim4) : m_data(dataPtr), m_dimensions(dim1, dim2, dim3, dim4) {
89 EIGEN_STATIC_ASSERT(4 == NumIndices || NumIndices ==
Dynamic, YOU_MADE_A_PROGRAMMING_MISTAKE)
92 EIGEN_STRONG_INLINE
TensorMap(PointerArgType dataPtr, Index dim1, Index dim2, Index dim3, Index dim4, Index dim5) : m_data(dataPtr), m_dimensions(dim1, dim2, dim3, dim4, dim5) {
93 EIGEN_STATIC_ASSERT(5 == NumIndices || NumIndices ==
Dynamic, YOU_MADE_A_PROGRAMMING_MISTAKE)
97 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
TensorMap(PointerArgType dataPtr,
const array<Index, NumIndices>& dimensions)
98 : m_data(dataPtr), m_dimensions(dimensions)
101 template <
typename Dimensions>
102 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
TensorMap(PointerArgType dataPtr,
const Dimensions& dimensions)
103 : m_data(dataPtr), m_dimensions(dimensions)
106 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
TensorMap(PlainObjectType& tensor)
107 : m_data(tensor.data()), m_dimensions(tensor.dimensions())
111 EIGEN_STRONG_INLINE Index rank()
const {
return m_dimensions.rank(); }
113 EIGEN_STRONG_INLINE Index dimension(Index n)
const {
return m_dimensions[n]; }
115 EIGEN_STRONG_INLINE
const Dimensions& dimensions()
const {
return m_dimensions; }
117 EIGEN_STRONG_INLINE Index size()
const {
return m_dimensions.TotalSize(); }
119 EIGEN_STRONG_INLINE PointerType data() {
return m_data; }
121 EIGEN_STRONG_INLINE
const PointerType data()
const {
return m_data; }
124 EIGEN_STRONG_INLINE
const Scalar& operator()(
const array<Index, NumIndices>& indices)
const
127 if (PlainObjectType::Options&
RowMajor) {
128 const Index index = m_dimensions.IndexOfRowMajor(indices);
129 return m_data[index];
131 const Index index = m_dimensions.IndexOfColMajor(indices);
132 return m_data[index];
137 EIGEN_STRONG_INLINE
const Scalar& operator()()
const
139 EIGEN_STATIC_ASSERT(NumIndices == 0, YOU_MADE_A_PROGRAMMING_MISTAKE)
144 EIGEN_STRONG_INLINE
const Scalar& operator()(Index index)
const
146 eigen_internal_assert(index >= 0 && index < size());
147 return m_data[index];
150 #if EIGEN_HAS_VARIADIC_TEMPLATES
151 template<
typename... IndexTypes> EIGEN_DEVICE_FUNC
152 EIGEN_STRONG_INLINE
const Scalar& operator()(Index firstIndex, Index secondIndex, IndexTypes... otherIndices)
const
154 EIGEN_STATIC_ASSERT(
sizeof...(otherIndices) + 2 == NumIndices, YOU_MADE_A_PROGRAMMING_MISTAKE)
155 if (PlainObjectType::Options&
RowMajor) {
156 const Index index = m_dimensions.IndexOfRowMajor(array<Index, NumIndices>{{firstIndex, secondIndex, otherIndices...}});
157 return m_data[index];
159 const Index index = m_dimensions.IndexOfColMajor(array<Index, NumIndices>{{firstIndex, secondIndex, otherIndices...}});
160 return m_data[index];
165 EIGEN_STRONG_INLINE
const Scalar& operator()(Index i0, Index i1)
const
167 if (PlainObjectType::Options&
RowMajor) {
168 const Index index = i1 + i0 * m_dimensions[1];
169 return m_data[index];
171 const Index index = i0 + i1 * m_dimensions[0];
172 return m_data[index];
176 EIGEN_STRONG_INLINE
const Scalar& operator()(Index i0, Index i1, Index i2)
const
178 if (PlainObjectType::Options&
RowMajor) {
179 const Index index = i2 + m_dimensions[2] * (i1 + m_dimensions[1] * i0);
180 return m_data[index];
182 const Index index = i0 + m_dimensions[0] * (i1 + m_dimensions[1] * i2);
183 return m_data[index];
187 EIGEN_STRONG_INLINE
const Scalar& operator()(Index i0, Index i1, Index i2, Index i3)
const
189 if (PlainObjectType::Options&
RowMajor) {
190 const Index index = i3 + m_dimensions[3] * (i2 + m_dimensions[2] * (i1 + m_dimensions[1] * i0));
191 return m_data[index];
193 const Index index = i0 + m_dimensions[0] * (i1 + m_dimensions[1] * (i2 + m_dimensions[2] * i3));
194 return m_data[index];
198 EIGEN_STRONG_INLINE
const Scalar& operator()(Index i0, Index i1, Index i2, Index i3, Index i4)
const
200 if (PlainObjectType::Options&
RowMajor) {
201 const Index index = i4 + m_dimensions[4] * (i3 + m_dimensions[3] * (i2 + m_dimensions[2] * (i1 + m_dimensions[1] * i0)));
202 return m_data[index];
204 const Index index = i0 + m_dimensions[0] * (i1 + m_dimensions[1] * (i2 + m_dimensions[2] * (i3 + m_dimensions[3] * i4)));
205 return m_data[index];
211 EIGEN_STRONG_INLINE Scalar& operator()(
const array<Index, NumIndices>& indices)
214 if (PlainObjectType::Options&
RowMajor) {
215 const Index index = m_dimensions.IndexOfRowMajor(indices);
216 return m_data[index];
218 const Index index = m_dimensions.IndexOfColMajor(indices);
219 return m_data[index];
224 EIGEN_STRONG_INLINE Scalar& operator()()
226 EIGEN_STATIC_ASSERT(NumIndices == 0, YOU_MADE_A_PROGRAMMING_MISTAKE)
231 EIGEN_STRONG_INLINE Scalar& operator()(Index index)
233 eigen_internal_assert(index >= 0 && index < size());
234 return m_data[index];
237 #if EIGEN_HAS_VARIADIC_TEMPLATES
238 template<
typename... IndexTypes> EIGEN_DEVICE_FUNC
239 EIGEN_STRONG_INLINE Scalar& operator()(Index firstIndex, Index secondIndex, IndexTypes... otherIndices)
241 static_assert(
sizeof...(otherIndices) + 2 == NumIndices || NumIndices ==
Dynamic,
"Number of indices used to access a tensor coefficient must be equal to the rank of the tensor.");
242 const std::size_t NumDims =
sizeof...(otherIndices) + 2;
243 if (PlainObjectType::Options&
RowMajor) {
244 const Index index = m_dimensions.IndexOfRowMajor(array<Index, NumDims>{{firstIndex, secondIndex, otherIndices...}});
245 return m_data[index];
247 const Index index = m_dimensions.IndexOfColMajor(array<Index, NumDims>{{firstIndex, secondIndex, otherIndices...}});
248 return m_data[index];
253 EIGEN_STRONG_INLINE Scalar& operator()(Index i0, Index i1)
255 if (PlainObjectType::Options&
RowMajor) {
256 const Index index = i1 + i0 * m_dimensions[1];
257 return m_data[index];
259 const Index index = i0 + i1 * m_dimensions[0];
260 return m_data[index];
264 EIGEN_STRONG_INLINE Scalar& operator()(Index i0, Index i1, Index i2)
266 if (PlainObjectType::Options&
RowMajor) {
267 const Index index = i2 + m_dimensions[2] * (i1 + m_dimensions[1] * i0);
268 return m_data[index];
270 const Index index = i0 + m_dimensions[0] * (i1 + m_dimensions[1] * i2);
271 return m_data[index];
275 EIGEN_STRONG_INLINE Scalar& operator()(Index i0, Index i1, Index i2, Index i3)
277 if (PlainObjectType::Options&
RowMajor) {
278 const Index index = i3 + m_dimensions[3] * (i2 + m_dimensions[2] * (i1 + m_dimensions[1] * i0));
279 return m_data[index];
281 const Index index = i0 + m_dimensions[0] * (i1 + m_dimensions[1] * (i2 + m_dimensions[2] * i3));
282 return m_data[index];
286 EIGEN_STRONG_INLINE Scalar& operator()(Index i0, Index i1, Index i2, Index i3, Index i4)
288 if (PlainObjectType::Options&
RowMajor) {
289 const Index index = i4 + m_dimensions[4] * (i3 + m_dimensions[3] * (i2 + m_dimensions[2] * (i1 + m_dimensions[1] * i0)));
290 return m_data[index];
292 const Index index = i0 + m_dimensions[0] * (i1 + m_dimensions[1] * (i2 + m_dimensions[2] * (i3 + m_dimensions[3] * i4)));
293 return m_data[index];
298 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
Self& operator=(
const Self& other)
300 typedef TensorAssignOp<Self, const Self> Assign;
301 Assign assign(*
this, other);
302 internal::TensorExecutor<const Assign, DefaultDevice>::run(assign, DefaultDevice());
306 template<
typename OtherDerived>
307 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
308 Self& operator=(
const OtherDerived& other)
310 typedef TensorAssignOp<Self, const OtherDerived> Assign;
311 Assign assign(*
this, other);
312 internal::TensorExecutor<const Assign, DefaultDevice>::run(assign, DefaultDevice());
317 typename MakePointer_<Scalar>::Type m_data;
318 Dimensions m_dimensions;
The tensor base class.
Definition: TensorForwardDeclarations.h:29
A tensor expression mapping an existing array of data.
Definition: TensorMap.h:30
Namespace containing all symbols from the Eigen library.