Eigen  3.2.91
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 namespace Eigen {
19 
27 template<typename Derived> class MapBase<Derived, ReadOnlyAccessors>
28  : public internal::dense_xpr_base<Derived>::type
29 {
30  public:
31 
32  typedef typename internal::dense_xpr_base<Derived>::type Base;
33  enum {
34  RowsAtCompileTime = internal::traits<Derived>::RowsAtCompileTime,
35  ColsAtCompileTime = internal::traits<Derived>::ColsAtCompileTime,
36  SizeAtCompileTime = Base::SizeAtCompileTime
37  };
38 
39  typedef typename internal::traits<Derived>::StorageKind StorageKind;
40  typedef typename internal::traits<Derived>::Scalar Scalar;
41  typedef typename internal::packet_traits<Scalar>::type PacketScalar;
42  typedef typename NumTraits<Scalar>::Real RealScalar;
43  typedef typename internal::conditional<
44  bool(internal::is_lvalue<Derived>::value),
45  Scalar *,
46  const Scalar *>::type
47  PointerType;
48 
49  using Base::derived;
50 // using Base::RowsAtCompileTime;
51 // using Base::ColsAtCompileTime;
52 // using Base::SizeAtCompileTime;
53  using Base::MaxRowsAtCompileTime;
54  using Base::MaxColsAtCompileTime;
55  using Base::MaxSizeAtCompileTime;
56  using Base::IsVectorAtCompileTime;
57  using Base::Flags;
58  using Base::IsRowMajor;
59 
60  using Base::rows;
61  using Base::cols;
62  using Base::size;
63  using Base::coeff;
64  using Base::coeffRef;
65  using Base::lazyAssign;
66  using Base::eval;
67 
68  using Base::innerStride;
69  using Base::outerStride;
70  using Base::rowStride;
71  using Base::colStride;
72 
73  // bug 217 - compile error on ICC 11.1
74  using Base::operator=;
75 
76  typedef typename Base::CoeffReturnType CoeffReturnType;
77 
78  EIGEN_DEVICE_FUNC inline Index rows() const { return m_rows.value(); }
79  EIGEN_DEVICE_FUNC inline Index cols() const { return m_cols.value(); }
80 
87  EIGEN_DEVICE_FUNC inline const Scalar* data() const { return m_data; }
88 
89  EIGEN_DEVICE_FUNC
90  inline const Scalar& coeff(Index rowId, Index colId) const
91  {
92  return m_data[colId * colStride() + rowId * rowStride()];
93  }
94 
95  EIGEN_DEVICE_FUNC
96  inline const Scalar& coeff(Index index) const
97  {
98  EIGEN_STATIC_ASSERT_INDEX_BASED_ACCESS(Derived)
99  return m_data[index * innerStride()];
100  }
101 
102  EIGEN_DEVICE_FUNC
103  inline const Scalar& coeffRef(Index rowId, Index colId) const
104  {
105  return this->m_data[colId * colStride() + rowId * rowStride()];
106  }
107 
108  EIGEN_DEVICE_FUNC
109  inline const Scalar& coeffRef(Index index) const
110  {
111  EIGEN_STATIC_ASSERT_INDEX_BASED_ACCESS(Derived)
112  return this->m_data[index * innerStride()];
113  }
114 
115  template<int LoadMode>
116  inline PacketScalar packet(Index rowId, Index colId) const
117  {
118  return internal::ploadt<PacketScalar, LoadMode>
119  (m_data + (colId * colStride() + rowId * rowStride()));
120  }
121 
122  template<int LoadMode>
123  inline PacketScalar packet(Index index) const
124  {
125  EIGEN_STATIC_ASSERT_INDEX_BASED_ACCESS(Derived)
126  return internal::ploadt<PacketScalar, LoadMode>(m_data + index * innerStride());
127  }
128 
129  EIGEN_DEVICE_FUNC
130  explicit inline MapBase(PointerType dataPtr) : m_data(dataPtr), m_rows(RowsAtCompileTime), m_cols(ColsAtCompileTime)
131  {
132  EIGEN_STATIC_ASSERT_FIXED_SIZE(Derived)
133  checkSanity();
134  }
135 
136  EIGEN_DEVICE_FUNC
137  inline MapBase(PointerType dataPtr, Index vecSize)
138  : m_data(dataPtr),
139  m_rows(RowsAtCompileTime == Dynamic ? vecSize : Index(RowsAtCompileTime)),
140  m_cols(ColsAtCompileTime == Dynamic ? vecSize : Index(ColsAtCompileTime))
141  {
142  EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
143  eigen_assert(vecSize >= 0);
144  eigen_assert(dataPtr == 0 || SizeAtCompileTime == Dynamic || SizeAtCompileTime == vecSize);
145  checkSanity();
146  }
147 
148  EIGEN_DEVICE_FUNC
149  inline MapBase(PointerType dataPtr, Index rows, Index cols)
150  : m_data(dataPtr), m_rows(rows), m_cols(cols)
151  {
152  eigen_assert( (dataPtr == 0)
153  || ( rows >= 0 && (RowsAtCompileTime == Dynamic || RowsAtCompileTime == rows)
154  && cols >= 0 && (ColsAtCompileTime == Dynamic || ColsAtCompileTime == cols)));
155  checkSanity();
156  }
157 
158  protected:
159 
160  EIGEN_DEVICE_FUNC
161  void checkSanity() const
162  {
163 #if EIGEN_MAX_ALIGN_BYTES>0
164  eigen_assert(((size_t(m_data) % EIGEN_PLAIN_ENUM_MAX(1,internal::traits<Derived>::Alignment)) == 0) && "data is not aligned");
165 #endif
166  }
167 
168  PointerType m_data;
169  const internal::variable_if_dynamic<Index, RowsAtCompileTime> m_rows;
170  const internal::variable_if_dynamic<Index, ColsAtCompileTime> m_cols;
171 };
172 
173 template<typename Derived> class MapBase<Derived, WriteAccessors>
174  : public MapBase<Derived, ReadOnlyAccessors>
175 {
176  typedef MapBase<Derived, ReadOnlyAccessors> ReadOnlyMapBase;
177  public:
178 
179  typedef MapBase<Derived, ReadOnlyAccessors> Base;
180 
181  typedef typename Base::Scalar Scalar;
182  typedef typename Base::PacketScalar PacketScalar;
183  typedef typename Base::StorageIndex StorageIndex;
184  typedef typename Base::PointerType PointerType;
185 
186  using Base::derived;
187  using Base::rows;
188  using Base::cols;
189  using Base::size;
190  using Base::coeff;
191  using Base::coeffRef;
192 
193  using Base::innerStride;
194  using Base::outerStride;
195  using Base::rowStride;
196  using Base::colStride;
197 
198  typedef typename internal::conditional<
199  internal::is_lvalue<Derived>::value,
200  Scalar,
201  const Scalar
202  >::type ScalarWithConstIfNotLvalue;
203 
204  EIGEN_DEVICE_FUNC
205  inline const Scalar* data() const { return this->m_data; }
206  EIGEN_DEVICE_FUNC
207  inline ScalarWithConstIfNotLvalue* data() { return this->m_data; } // no const-cast here so non-const-correct code will give a compile error
208 
209  EIGEN_DEVICE_FUNC
210  inline ScalarWithConstIfNotLvalue& coeffRef(Index row, Index col)
211  {
212  return this->m_data[col * colStride() + row * rowStride()];
213  }
214 
215  EIGEN_DEVICE_FUNC
216  inline ScalarWithConstIfNotLvalue& coeffRef(Index index)
217  {
218  EIGEN_STATIC_ASSERT_INDEX_BASED_ACCESS(Derived)
219  return this->m_data[index * innerStride()];
220  }
221 
222  template<int StoreMode>
223  inline void writePacket(Index row, Index col, const PacketScalar& val)
224  {
225  internal::pstoret<Scalar, PacketScalar, StoreMode>
226  (this->m_data + (col * colStride() + row * rowStride()), val);
227  }
228 
229  template<int StoreMode>
230  inline void writePacket(Index index, const PacketScalar& val)
231  {
232  EIGEN_STATIC_ASSERT_INDEX_BASED_ACCESS(Derived)
233  internal::pstoret<Scalar, PacketScalar, StoreMode>
234  (this->m_data + index * innerStride(), val);
235  }
236 
237  EIGEN_DEVICE_FUNC explicit inline MapBase(PointerType dataPtr) : Base(dataPtr) {}
238  EIGEN_DEVICE_FUNC inline MapBase(PointerType dataPtr, Index vecSize) : Base(dataPtr, vecSize) {}
239  EIGEN_DEVICE_FUNC inline MapBase(PointerType dataPtr, Index rows, Index cols) : Base(dataPtr, rows, cols) {}
240 
241  EIGEN_DEVICE_FUNC
242  Derived& operator=(const MapBase& other)
243  {
244  ReadOnlyMapBase::Base::operator=(other);
245  return derived();
246  }
247 
248  // In theory we could simply refer to Base:Base::operator=, but MSVC does not like Base::Base,
249  // see bugs 821 and 920.
250  using ReadOnlyMapBase::Base::operator=;
251 };
252 
253 #undef EIGEN_STATIC_ASSERT_INDEX_BASED_ACCESS
254 
255 } // end namespace Eigen
256 
257 #endif // EIGEN_MAPBASE_H
Definition: Constants.h:360
Definition: LDLT.h:16
Definition: Constants.h:358
Definition: Eigen_Colamd.h:54