Eigen  3.4.90 (git rev 67eeba6e720c5745abc77ae6c92ce0a44aa7b7ae)
MapBase.h
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2007-2010 Benoit Jacob <jacob.benoit.1@gmail.com>
5 // Copyright (C) 2008 Gael Guennebaud <gael.guennebaud@inria.fr>
6 //
7 // This Source Code Form is subject to the terms of the Mozilla
8 // Public License v. 2.0. If a copy of the MPL was not distributed
9 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
10 
11 #ifndef EIGEN_MAPBASE_H
12 #define EIGEN_MAPBASE_H
13 
14 #define EIGEN_STATIC_ASSERT_INDEX_BASED_ACCESS(Derived) \
15  EIGEN_STATIC_ASSERT((int(internal::evaluator<Derived>::Flags) & LinearAccessBit) || Derived::IsVectorAtCompileTime, \
16  YOU_ARE_TRYING_TO_USE_AN_INDEX_BASED_ACCESSOR_ON_AN_EXPRESSION_THAT_DOES_NOT_SUPPORT_THAT)
17 
18 #include "./InternalHeaderCheck.h"
19 
20 namespace Eigen {
21 
39 template<typename Derived> class MapBase<Derived, ReadOnlyAccessors>
40  : public internal::dense_xpr_base<Derived>::type
41 {
42  public:
43 
44  typedef typename internal::dense_xpr_base<Derived>::type Base;
45  enum {
46  RowsAtCompileTime = internal::traits<Derived>::RowsAtCompileTime,
47  ColsAtCompileTime = internal::traits<Derived>::ColsAtCompileTime,
48  InnerStrideAtCompileTime = internal::traits<Derived>::InnerStrideAtCompileTime,
49  SizeAtCompileTime = Base::SizeAtCompileTime
50  };
51 
52  typedef typename internal::traits<Derived>::StorageKind StorageKind;
53  typedef typename internal::traits<Derived>::Scalar Scalar;
54  typedef typename internal::packet_traits<Scalar>::type PacketScalar;
55  typedef typename NumTraits<Scalar>::Real RealScalar;
56  typedef std::conditional_t<
57  bool(internal::is_lvalue<Derived>::value),
58  Scalar *,
59  const Scalar *>
60  PointerType;
61 
62  using Base::derived;
63 // using Base::RowsAtCompileTime;
64 // using Base::ColsAtCompileTime;
65 // using Base::SizeAtCompileTime;
66  using Base::MaxRowsAtCompileTime;
67  using Base::MaxColsAtCompileTime;
68  using Base::MaxSizeAtCompileTime;
69  using Base::IsVectorAtCompileTime;
70  using Base::Flags;
71  using Base::IsRowMajor;
72 
73  using Base::rows;
74  using Base::cols;
75  using Base::size;
76  using Base::coeff;
77  using Base::coeffRef;
78  using Base::lazyAssign;
79  using Base::eval;
80 
81  using Base::innerStride;
82  using Base::outerStride;
83  using Base::rowStride;
84  using Base::colStride;
85 
86  // bug 217 - compile error on ICC 11.1
87  using Base::operator=;
88 
89  typedef typename Base::CoeffReturnType CoeffReturnType;
90 
92  EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR
93  inline Index rows() const EIGEN_NOEXCEPT { return m_rows.value(); }
95  EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR
96  inline Index cols() const EIGEN_NOEXCEPT { return m_cols.value(); }
97 
104  EIGEN_DEVICE_FUNC inline const Scalar* data() const { return m_data; }
105 
107  EIGEN_DEVICE_FUNC
108  inline const Scalar& coeff(Index rowId, Index colId) const
109  {
110  return m_data[colId * colStride() + rowId * rowStride()];
111  }
112 
114  EIGEN_DEVICE_FUNC
115  inline const Scalar& coeff(Index index) const
116  {
117  EIGEN_STATIC_ASSERT_INDEX_BASED_ACCESS(Derived)
118  return m_data[index * innerStride()];
119  }
120 
122  EIGEN_DEVICE_FUNC
123  inline const Scalar& coeffRef(Index rowId, Index colId) const
124  {
125  return this->m_data[colId * colStride() + rowId * rowStride()];
126  }
127 
129  EIGEN_DEVICE_FUNC
130  inline const Scalar& coeffRef(Index index) const
131  {
132  EIGEN_STATIC_ASSERT_INDEX_BASED_ACCESS(Derived)
133  return this->m_data[index * innerStride()];
134  }
135 
137  template<int LoadMode>
138  inline PacketScalar packet(Index rowId, Index colId) const
139  {
140  return internal::ploadt<PacketScalar, LoadMode>
141  (m_data + (colId * colStride() + rowId * rowStride()));
142  }
143 
145  template<int LoadMode>
146  inline PacketScalar packet(Index index) const
147  {
148  EIGEN_STATIC_ASSERT_INDEX_BASED_ACCESS(Derived)
149  return internal::ploadt<PacketScalar, LoadMode>(m_data + index * innerStride());
150  }
151 
153  EIGEN_DEVICE_FUNC
154  explicit inline MapBase(PointerType dataPtr) : m_data(dataPtr), m_rows(RowsAtCompileTime), m_cols(ColsAtCompileTime)
155  {
156  EIGEN_STATIC_ASSERT_FIXED_SIZE(Derived)
157  checkSanity<Derived>();
158  }
159 
161  EIGEN_DEVICE_FUNC
162  inline MapBase(PointerType dataPtr, Index vecSize)
163  : m_data(dataPtr),
164  m_rows(RowsAtCompileTime == Dynamic ? vecSize : Index(RowsAtCompileTime)),
165  m_cols(ColsAtCompileTime == Dynamic ? vecSize : Index(ColsAtCompileTime))
166  {
167  EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
168  eigen_assert(vecSize >= 0);
169  eigen_assert(dataPtr == 0 || SizeAtCompileTime == Dynamic || SizeAtCompileTime == vecSize);
170  checkSanity<Derived>();
171  }
172 
174  EIGEN_DEVICE_FUNC
175  inline MapBase(PointerType dataPtr, Index rows, Index cols)
176  : m_data(dataPtr), m_rows(rows), m_cols(cols)
177  {
178  eigen_assert( (dataPtr == 0)
179  || ( rows >= 0 && (RowsAtCompileTime == Dynamic || RowsAtCompileTime == rows)
180  && cols >= 0 && (ColsAtCompileTime == Dynamic || ColsAtCompileTime == cols)));
181  checkSanity<Derived>();
182  }
183 
184  #ifdef EIGEN_MAPBASE_PLUGIN
185  #include EIGEN_MAPBASE_PLUGIN
186  #endif
187 
188  protected:
189  EIGEN_DEFAULT_COPY_CONSTRUCTOR(MapBase)
190  EIGEN_DEFAULT_EMPTY_CONSTRUCTOR_AND_DESTRUCTOR(MapBase)
191 
192  template<typename T>
193  EIGEN_DEVICE_FUNC
194  void checkSanity(std::enable_if_t<(internal::traits<T>::Alignment>0),void*> = 0) const
195  {
196 #if EIGEN_MAX_ALIGN_BYTES>0
197  // innerStride() is not set yet when this function is called, so we optimistically assume the lowest plausible value:
198  const Index minInnerStride = InnerStrideAtCompileTime == Dynamic ? 1 : Index(InnerStrideAtCompileTime);
199  EIGEN_ONLY_USED_FOR_DEBUG(minInnerStride);
200  eigen_assert(( ((internal::UIntPtr(m_data) % internal::traits<Derived>::Alignment) == 0)
201  || (cols() * rows() * minInnerStride * sizeof(Scalar)) < internal::traits<Derived>::Alignment ) && "data is not aligned");
202 #endif
203  }
204 
205  template<typename T>
206  EIGEN_DEVICE_FUNC
207  void checkSanity(std::enable_if_t<internal::traits<T>::Alignment==0,void*> = 0) const
208  {}
209 
210  PointerType m_data;
211  const internal::variable_if_dynamic<Index, RowsAtCompileTime> m_rows;
212  const internal::variable_if_dynamic<Index, ColsAtCompileTime> m_cols;
213 };
214 
225 template<typename Derived> class MapBase<Derived, WriteAccessors>
226  : public MapBase<Derived, ReadOnlyAccessors>
227 {
229  public:
230 
232 
233  typedef typename Base::Scalar Scalar;
234  typedef typename Base::PacketScalar PacketScalar;
235  typedef typename Base::StorageIndex StorageIndex;
236  typedef typename Base::PointerType PointerType;
237 
238  using Base::derived;
239  using Base::rows;
240  using Base::cols;
241  using Base::size;
242  using Base::coeff;
243  using Base::coeffRef;
244 
245  using Base::innerStride;
246  using Base::outerStride;
247  using Base::rowStride;
248  using Base::colStride;
249 
250  typedef std::conditional_t<
251  internal::is_lvalue<Derived>::value,
252  Scalar,
253  const Scalar
254  > ScalarWithConstIfNotLvalue;
255 
256  EIGEN_DEVICE_FUNC
257  inline const Scalar* data() const { return this->m_data; }
258  EIGEN_DEVICE_FUNC
259  inline ScalarWithConstIfNotLvalue* data() { return this->m_data; } // no const-cast here so non-const-correct code will give a compile error
260 
261  EIGEN_DEVICE_FUNC
262  inline ScalarWithConstIfNotLvalue& coeffRef(Index row, Index col)
263  {
264  return this->m_data[col * colStride() + row * rowStride()];
265  }
266 
267  EIGEN_DEVICE_FUNC
268  inline ScalarWithConstIfNotLvalue& coeffRef(Index index)
269  {
270  EIGEN_STATIC_ASSERT_INDEX_BASED_ACCESS(Derived)
271  return this->m_data[index * innerStride()];
272  }
273 
274  template<int StoreMode>
275  inline void writePacket(Index row, Index col, const PacketScalar& val)
276  {
277  internal::pstoret<Scalar, PacketScalar, StoreMode>
278  (this->m_data + (col * colStride() + row * rowStride()), val);
279  }
280 
281  template<int StoreMode>
282  inline void writePacket(Index index, const PacketScalar& val)
283  {
284  EIGEN_STATIC_ASSERT_INDEX_BASED_ACCESS(Derived)
285  internal::pstoret<Scalar, PacketScalar, StoreMode>
286  (this->m_data + index * innerStride(), val);
287  }
288 
289  EIGEN_DEVICE_FUNC explicit inline MapBase(PointerType dataPtr) : Base(dataPtr) {}
290  EIGEN_DEVICE_FUNC inline MapBase(PointerType dataPtr, Index vecSize) : Base(dataPtr, vecSize) {}
291  EIGEN_DEVICE_FUNC inline MapBase(PointerType dataPtr, Index rows, Index cols) : Base(dataPtr, rows, cols) {}
292 
293  EIGEN_DEVICE_FUNC
294  Derived& operator=(const MapBase& other)
295  {
296  ReadOnlyMapBase::Base::operator=(other);
297  return derived();
298  }
299 
300  // In theory we could simply refer to Base:Base::operator=, but MSVC does not like Base::Base,
301  // see bugs 821 and 920.
302  using ReadOnlyMapBase::Base::operator=;
303  protected:
304  EIGEN_DEFAULT_COPY_CONSTRUCTOR(MapBase)
305  EIGEN_DEFAULT_EMPTY_CONSTRUCTOR_AND_DESTRUCTOR(MapBase)
306 };
307 
308 #undef EIGEN_STATIC_ASSERT_INDEX_BASED_ACCESS
309 
310 } // end namespace Eigen
311 
312 #endif // EIGEN_MAPBASE_H
Base class for dense Map and Block expression with direct access.
Definition: MapBase.h:41
const Scalar * data() const
Definition: MapBase.h:104
const Scalar & coeffRef(Index index) const
Definition: MapBase.h:130
const Scalar & coeff(Index rowId, Index colId) const
Definition: MapBase.h:108
EIGEN_CONSTEXPR Index rows() const EIGEN_NOEXCEPT
Definition: MapBase.h:93
const Scalar & coeffRef(Index rowId, Index colId) const
Definition: MapBase.h:123
EIGEN_CONSTEXPR Index cols() const EIGEN_NOEXCEPT
Definition: MapBase.h:96
const Scalar & coeff(Index index) const
Definition: MapBase.h:115
@ ReadOnlyAccessors
Definition: Constants.h:378
@ WriteAccessors
Definition: Constants.h:380
Namespace containing all symbols from the Eigen library.
Definition: Core:139
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:59
const int Dynamic
Definition: Constants.h:24
Holds information about the various numeric (i.e. scalar) types allowed by Eigen.
Definition: NumTraits.h:231