MapBase.h
Go to the documentation of this file.
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;
56  typedef std::conditional_t<
57  bool(internal::is_lvalue<Derived>::value),
58  Scalar *,
59  const Scalar *>
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 
93  inline Index rows() const EIGEN_NOEXCEPT { return m_rows.value(); }
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 
108  inline const Scalar& coeff(Index rowId, Index colId) const
109  {
110  return m_data[colId * colStride() + rowId * rowStride()];
111  }
112 
115  inline const Scalar& coeff(Index index) const
116  {
118  return m_data[index * innerStride()];
119  }
120 
123  inline const Scalar& coeffRef(Index rowId, Index colId) const
124  {
125  return this->m_data[colId * colStride() + rowId * rowStride()];
126  }
127 
130  inline const Scalar& coeffRef(Index index) const
131  {
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  {
149  return internal::ploadt<PacketScalar, LoadMode>(m_data + index * innerStride());
150  }
151 
154  explicit inline MapBase(PointerType dataPtr) : m_data(dataPtr), m_rows(RowsAtCompileTime), m_cols(ColsAtCompileTime)
155  {
157  checkSanity<Derived>();
158  }
159 
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  {
168  eigen_assert(vecSize >= 0);
169  eigen_assert(dataPtr == 0 || SizeAtCompileTime == Dynamic || SizeAtCompileTime == vecSize);
170  checkSanity<Derived>();
171  }
172 
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:
191 
192  template<typename T>
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(( ((std::uintptr_t(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>
207  void checkSanity(std::enable_if_t<internal::traits<T>::Alignment==0,void*> = 0) const
208  {}
209 
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
255 
257  inline const Scalar* data() const { return this->m_data; }
259  inline ScalarWithConstIfNotLvalue* data() { return this->m_data; } // no const-cast here so non-const-correct code will give a compile error
260 
263  {
264  return this->m_data[col * colStride() + row * rowStride()];
265  }
266 
269  {
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  {
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 
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:
306 };
307 
308 #undef EIGEN_STATIC_ASSERT_INDEX_BASED_ACCESS
309 
310 } // end namespace Eigen
311 
312 #endif // EIGEN_MAPBASE_H
RowXpr row(Index i)
This is the const version of row(). *‍/.
ColXpr col(Index i)
This is the const version of col().
#define EIGEN_DEFAULT_COPY_CONSTRUCTOR(CLASS)
Definition: Macros.h:1113
#define EIGEN_DEFAULT_EMPTY_CONSTRUCTOR_AND_DESTRUCTOR(Derived)
Definition: Macros.h:1133
#define EIGEN_NOEXCEPT
Definition: Macros.h:1260
#define EIGEN_CONSTEXPR
Definition: Macros.h:747
#define EIGEN_DEVICE_FUNC
Definition: Macros.h:883
#define EIGEN_ONLY_USED_FOR_DEBUG(x)
Definition: Macros.h:914
#define eigen_assert(x)
Definition: Macros.h:902
#define EIGEN_STATIC_ASSERT_INDEX_BASED_ACCESS(Derived)
Definition: MapBase.h:14
#define EIGEN_STATIC_ASSERT_FIXED_SIZE(TYPE)
Definition: StaticAssert.h:41
#define EIGEN_STATIC_ASSERT_VECTOR_ONLY(TYPE)
Definition: StaticAssert.h:36
Base class for dense Map and Block expression with direct access.
Definition: MapBase.h:41
PacketScalar packet(Index rowId, Index colId) const
Definition: MapBase.h:138
internal::dense_xpr_base< Derived >::type Base
Definition: MapBase.h:44
internal::packet_traits< Scalar >::type PacketScalar
Definition: MapBase.h:54
Base::CoeffReturnType CoeffReturnType
Definition: MapBase.h:89
const Scalar & coeffRef(Index index) const
Definition: MapBase.h:130
const Scalar & coeff(Index rowId, Index colId) const
Definition: MapBase.h:108
PacketScalar packet(Index index) const
Definition: MapBase.h:146
const internal::variable_if_dynamic< Index, ColsAtCompileTime > m_cols
Definition: MapBase.h:212
MapBase(PointerType dataPtr, Index vecSize)
Definition: MapBase.h:162
void checkSanity(std::enable_if_t<(internal::traits< T >::Alignment >0), void * >=0) const
Definition: MapBase.h:194
EIGEN_CONSTEXPR Index rows() const EIGEN_NOEXCEPT
Definition: MapBase.h:93
internal::traits< Derived >::Scalar Scalar
Definition: MapBase.h:53
const internal::variable_if_dynamic< Index, RowsAtCompileTime > m_rows
Definition: MapBase.h:211
MapBase(PointerType dataPtr, Index rows, Index cols)
Definition: MapBase.h:175
const Scalar & coeffRef(Index rowId, Index colId) const
Definition: MapBase.h:123
void checkSanity(std::enable_if_t< internal::traits< T >::Alignment==0, void * >=0) const
Definition: MapBase.h:207
NumTraits< Scalar >::Real RealScalar
Definition: MapBase.h:55
EIGEN_CONSTEXPR Index cols() const EIGEN_NOEXCEPT
Definition: MapBase.h:96
const Scalar & coeff(Index index) const
Definition: MapBase.h:115
std::conditional_t< bool(internal::is_lvalue< Derived >::value), Scalar *, const Scalar * > PointerType
Definition: MapBase.h:60
internal::traits< Derived >::StorageKind StorageKind
Definition: MapBase.h:52
ScalarWithConstIfNotLvalue & coeffRef(Index row, Index col)
Definition: MapBase.h:262
MapBase< Derived, ReadOnlyAccessors > Base
Definition: MapBase.h:231
void writePacket(Index row, Index col, const PacketScalar &val)
Definition: MapBase.h:275
MapBase< Derived, ReadOnlyAccessors > ReadOnlyMapBase
Definition: MapBase.h:228
void writePacket(Index index, const PacketScalar &val)
Definition: MapBase.h:282
ScalarWithConstIfNotLvalue * data()
Definition: MapBase.h:259
MapBase(PointerType dataPtr, Index rows, Index cols)
Definition: MapBase.h:291
ScalarWithConstIfNotLvalue & coeffRef(Index index)
Definition: MapBase.h:268
Derived & operator=(const MapBase &other)
Definition: MapBase.h:294
std::conditional_t< internal::is_lvalue< Derived >::value, Scalar, const Scalar > ScalarWithConstIfNotLvalue
Definition: MapBase.h:254
MapBase(PointerType dataPtr, Index vecSize)
Definition: MapBase.h:290
@ ReadOnlyAccessors
Definition: Constants.h:378
@ WriteAccessors
Definition: Constants.h:380
: InteropHeaders
Definition: Core:139
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:82
const int Dynamic
Definition: Constants.h:24
Holds information about the various numeric (i.e. scalar) types allowed by Eigen.
Definition: NumTraits.h:231