Please, help us to better know about our user community by answering the following short survey: https://forms.gle/wpyrxWi18ox9Z5ae9
Eigen  3.4.0
Ref.h
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2012 Gael Guennebaud <gael.guennebaud@inria.fr>
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 
10 #ifndef EIGEN_REF_H
11 #define EIGEN_REF_H
12 
13 namespace Eigen {
14 
15 namespace internal {
16 
17 template<typename _PlainObjectType, int _Options, typename _StrideType>
18 struct traits<Ref<_PlainObjectType, _Options, _StrideType> >
19  : public traits<Map<_PlainObjectType, _Options, _StrideType> >
20 {
21  typedef _PlainObjectType PlainObjectType;
22  typedef _StrideType StrideType;
23  enum {
24  Options = _Options,
25  Flags = traits<Map<_PlainObjectType, _Options, _StrideType> >::Flags | NestByRefBit,
26  Alignment = traits<Map<_PlainObjectType, _Options, _StrideType> >::Alignment
27  };
28 
29  template<typename Derived> struct match {
30  enum {
31  IsVectorAtCompileTime = PlainObjectType::IsVectorAtCompileTime || Derived::IsVectorAtCompileTime,
32  HasDirectAccess = internal::has_direct_access<Derived>::ret,
33  StorageOrderMatch = IsVectorAtCompileTime || ((PlainObjectType::Flags&RowMajorBit)==(Derived::Flags&RowMajorBit)),
34  InnerStrideMatch = int(StrideType::InnerStrideAtCompileTime)==int(Dynamic)
35  || int(StrideType::InnerStrideAtCompileTime)==int(Derived::InnerStrideAtCompileTime)
36  || (int(StrideType::InnerStrideAtCompileTime)==0 && int(Derived::InnerStrideAtCompileTime)==1),
37  OuterStrideMatch = IsVectorAtCompileTime
38  || int(StrideType::OuterStrideAtCompileTime)==int(Dynamic) || int(StrideType::OuterStrideAtCompileTime)==int(Derived::OuterStrideAtCompileTime),
39  // NOTE, this indirection of evaluator<Derived>::Alignment is needed
40  // to workaround a very strange bug in MSVC related to the instantiation
41  // of has_*ary_operator in evaluator<CwiseNullaryOp>.
42  // This line is surprisingly very sensitive. For instance, simply adding parenthesis
43  // as "DerivedAlignment = (int(evaluator<Derived>::Alignment))," will make MSVC fail...
44  DerivedAlignment = int(evaluator<Derived>::Alignment),
45  AlignmentMatch = (int(traits<PlainObjectType>::Alignment)==int(Unaligned)) || (DerivedAlignment >= int(Alignment)), // FIXME the first condition is not very clear, it should be replaced by the required alignment
46  ScalarTypeMatch = internal::is_same<typename PlainObjectType::Scalar, typename Derived::Scalar>::value,
47  MatchAtCompileTime = HasDirectAccess && StorageOrderMatch && InnerStrideMatch && OuterStrideMatch && AlignmentMatch && ScalarTypeMatch
48  };
49  typedef typename internal::conditional<MatchAtCompileTime,internal::true_type,internal::false_type>::type type;
50  };
51 
52 };
53 
54 template<typename Derived>
55 struct traits<RefBase<Derived> > : public traits<Derived> {};
56 
57 }
58 
59 template<typename Derived> class RefBase
60  : public MapBase<Derived>
61 {
62  typedef typename internal::traits<Derived>::PlainObjectType PlainObjectType;
63  typedef typename internal::traits<Derived>::StrideType StrideType;
64 
65 public:
66 
67  typedef MapBase<Derived> Base;
68  EIGEN_DENSE_PUBLIC_INTERFACE(RefBase)
69 
70  EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR inline Index innerStride() const
71  {
72  return StrideType::InnerStrideAtCompileTime != 0 ? m_stride.inner() : 1;
73  }
74 
75  EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR inline Index outerStride() const
76  {
77  return StrideType::OuterStrideAtCompileTime != 0 ? m_stride.outer()
78  : IsVectorAtCompileTime ? this->size()
79  : int(Flags)&RowMajorBit ? this->cols()
80  : this->rows();
81  }
82 
83  EIGEN_DEVICE_FUNC RefBase()
84  : Base(0,RowsAtCompileTime==Dynamic?0:RowsAtCompileTime,ColsAtCompileTime==Dynamic?0:ColsAtCompileTime),
85  // Stride<> does not allow default ctor for Dynamic strides, so let' initialize it with dummy values:
86  m_stride(StrideType::OuterStrideAtCompileTime==Dynamic?0:StrideType::OuterStrideAtCompileTime,
87  StrideType::InnerStrideAtCompileTime==Dynamic?0:StrideType::InnerStrideAtCompileTime)
88  {}
89 
90  EIGEN_INHERIT_ASSIGNMENT_OPERATORS(RefBase)
91 
92 protected:
93 
94  typedef Stride<StrideType::OuterStrideAtCompileTime,StrideType::InnerStrideAtCompileTime> StrideBase;
95 
96  // Resolves inner stride if default 0.
97  static EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR Index resolveInnerStride(Index inner) {
98  return inner == 0 ? 1 : inner;
99  }
100 
101  // Resolves outer stride if default 0.
102  static EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR Index resolveOuterStride(Index inner, Index outer, Index rows, Index cols, bool isVectorAtCompileTime, bool isRowMajor) {
103  return outer == 0 ? isVectorAtCompileTime ? inner * rows * cols : isRowMajor ? inner * cols : inner * rows : outer;
104  }
105 
106  // Returns true if construction is valid, false if there is a stride mismatch,
107  // and fails if there is a size mismatch.
108  template<typename Expression>
109  EIGEN_DEVICE_FUNC bool construct(Expression& expr)
110  {
111  // Check matrix sizes. If this is a compile-time vector, we do allow
112  // implicitly transposing.
113  EIGEN_STATIC_ASSERT(
114  EIGEN_PREDICATE_SAME_MATRIX_SIZE(PlainObjectType, Expression)
115  // If it is a vector, the transpose sizes might match.
116  || ( PlainObjectType::IsVectorAtCompileTime
117  && ((int(PlainObjectType::RowsAtCompileTime)==Eigen::Dynamic
118  || int(Expression::ColsAtCompileTime)==Eigen::Dynamic
119  || int(PlainObjectType::RowsAtCompileTime)==int(Expression::ColsAtCompileTime))
120  && (int(PlainObjectType::ColsAtCompileTime)==Eigen::Dynamic
121  || int(Expression::RowsAtCompileTime)==Eigen::Dynamic
122  || int(PlainObjectType::ColsAtCompileTime)==int(Expression::RowsAtCompileTime)))),
123  YOU_MIXED_MATRICES_OF_DIFFERENT_SIZES
124  )
125 
126  // Determine runtime rows and columns.
127  Index rows = expr.rows();
128  Index cols = expr.cols();
129  if(PlainObjectType::RowsAtCompileTime==1)
130  {
131  eigen_assert(expr.rows()==1 || expr.cols()==1);
132  rows = 1;
133  cols = expr.size();
134  }
135  else if(PlainObjectType::ColsAtCompileTime==1)
136  {
137  eigen_assert(expr.rows()==1 || expr.cols()==1);
138  rows = expr.size();
139  cols = 1;
140  }
141  // Verify that the sizes are valid.
142  eigen_assert(
143  (PlainObjectType::RowsAtCompileTime == Dynamic) || (PlainObjectType::RowsAtCompileTime == rows));
144  eigen_assert(
145  (PlainObjectType::ColsAtCompileTime == Dynamic) || (PlainObjectType::ColsAtCompileTime == cols));
146 
147 
148  // If this is a vector, we might be transposing, which means that stride should swap.
149  const bool transpose = PlainObjectType::IsVectorAtCompileTime && (rows != expr.rows());
150  // If the storage format differs, we also need to swap the stride.
151  const bool row_major = ((PlainObjectType::Flags)&RowMajorBit) != 0;
152  const bool expr_row_major = (Expression::Flags&RowMajorBit) != 0;
153  const bool storage_differs = (row_major != expr_row_major);
154 
155  const bool swap_stride = (transpose != storage_differs);
156 
157  // Determine expr's actual strides, resolving any defaults if zero.
158  const Index expr_inner_actual = resolveInnerStride(expr.innerStride());
159  const Index expr_outer_actual = resolveOuterStride(expr_inner_actual,
160  expr.outerStride(),
161  expr.rows(),
162  expr.cols(),
163  Expression::IsVectorAtCompileTime != 0,
164  expr_row_major);
165 
166  // If this is a column-major row vector or row-major column vector, the inner-stride
167  // is arbitrary, so set it to either the compile-time inner stride or 1.
168  const bool row_vector = (rows == 1);
169  const bool col_vector = (cols == 1);
170  const Index inner_stride =
171  ( (!row_major && row_vector) || (row_major && col_vector) ) ?
172  ( StrideType::InnerStrideAtCompileTime > 0 ? Index(StrideType::InnerStrideAtCompileTime) : 1)
173  : swap_stride ? expr_outer_actual : expr_inner_actual;
174 
175  // If this is a column-major column vector or row-major row vector, the outer-stride
176  // is arbitrary, so set it to either the compile-time outer stride or vector size.
177  const Index outer_stride =
178  ( (!row_major && col_vector) || (row_major && row_vector) ) ?
179  ( StrideType::OuterStrideAtCompileTime > 0 ? Index(StrideType::OuterStrideAtCompileTime) : rows * cols * inner_stride)
180  : swap_stride ? expr_inner_actual : expr_outer_actual;
181 
182  // Check if given inner/outer strides are compatible with compile-time strides.
183  const bool inner_valid = (StrideType::InnerStrideAtCompileTime == Dynamic)
184  || (resolveInnerStride(Index(StrideType::InnerStrideAtCompileTime)) == inner_stride);
185  if (!inner_valid) {
186  return false;
187  }
188 
189  const bool outer_valid = (StrideType::OuterStrideAtCompileTime == Dynamic)
190  || (resolveOuterStride(
191  inner_stride,
192  Index(StrideType::OuterStrideAtCompileTime),
193  rows, cols, PlainObjectType::IsVectorAtCompileTime != 0,
194  row_major)
195  == outer_stride);
196  if (!outer_valid) {
197  return false;
198  }
199 
200  ::new (static_cast<Base*>(this)) Base(expr.data(), rows, cols);
201  ::new (&m_stride) StrideBase(
202  (StrideType::OuterStrideAtCompileTime == 0) ? 0 : outer_stride,
203  (StrideType::InnerStrideAtCompileTime == 0) ? 0 : inner_stride );
204  return true;
205  }
206 
207  StrideBase m_stride;
208 };
209 
281 template<typename PlainObjectType, int Options, typename StrideType> class Ref
282  : public RefBase<Ref<PlainObjectType, Options, StrideType> >
283 {
284  private:
285  typedef internal::traits<Ref> Traits;
286  template<typename Derived>
287  EIGEN_DEVICE_FUNC inline Ref(const PlainObjectBase<Derived>& expr,
288  typename internal::enable_if<bool(Traits::template match<Derived>::MatchAtCompileTime),Derived>::type* = 0);
289  public:
290 
291  typedef RefBase<Ref> Base;
292  EIGEN_DENSE_PUBLIC_INTERFACE(Ref)
293 
294 
295  #ifndef EIGEN_PARSED_BY_DOXYGEN
296  template<typename Derived>
297  EIGEN_DEVICE_FUNC inline Ref(PlainObjectBase<Derived>& expr,
298  typename internal::enable_if<bool(Traits::template match<Derived>::MatchAtCompileTime),Derived>::type* = 0)
299  {
300  EIGEN_STATIC_ASSERT(bool(Traits::template match<Derived>::MatchAtCompileTime), STORAGE_LAYOUT_DOES_NOT_MATCH);
301  // Construction must pass since we will not create temprary storage in the non-const case.
302  const bool success = Base::construct(expr.derived());
303  EIGEN_UNUSED_VARIABLE(success)
304  eigen_assert(success);
305  }
306  template<typename Derived>
307  EIGEN_DEVICE_FUNC inline Ref(const DenseBase<Derived>& expr,
308  typename internal::enable_if<bool(Traits::template match<Derived>::MatchAtCompileTime),Derived>::type* = 0)
309  #else
310 
311  template<typename Derived>
312  inline Ref(DenseBase<Derived>& expr)
313  #endif
314  {
315  EIGEN_STATIC_ASSERT(bool(internal::is_lvalue<Derived>::value), THIS_EXPRESSION_IS_NOT_A_LVALUE__IT_IS_READ_ONLY);
316  EIGEN_STATIC_ASSERT(bool(Traits::template match<Derived>::MatchAtCompileTime), STORAGE_LAYOUT_DOES_NOT_MATCH);
317  EIGEN_STATIC_ASSERT(!Derived::IsPlainObjectBase,THIS_EXPRESSION_IS_NOT_A_LVALUE__IT_IS_READ_ONLY);
318  // Construction must pass since we will not create temporary storage in the non-const case.
319  const bool success = Base::construct(expr.const_cast_derived());
320  EIGEN_UNUSED_VARIABLE(success)
321  eigen_assert(success);
322  }
323 
324  EIGEN_INHERIT_ASSIGNMENT_OPERATORS(Ref)
325 
326 };
327 
328 // this is the const ref version
329 template<typename TPlainObjectType, int Options, typename StrideType> class Ref<const TPlainObjectType, Options, StrideType>
330  : public RefBase<Ref<const TPlainObjectType, Options, StrideType> >
331 {
332  typedef internal::traits<Ref> Traits;
333  public:
334 
335  typedef RefBase<Ref> Base;
336  EIGEN_DENSE_PUBLIC_INTERFACE(Ref)
337 
338  template<typename Derived>
339  EIGEN_DEVICE_FUNC inline Ref(const DenseBase<Derived>& expr,
340  typename internal::enable_if<bool(Traits::template match<Derived>::ScalarTypeMatch),Derived>::type* = 0)
341  {
342 // std::cout << match_helper<Derived>::HasDirectAccess << "," << match_helper<Derived>::OuterStrideMatch << "," << match_helper<Derived>::InnerStrideMatch << "\n";
343 // std::cout << int(StrideType::OuterStrideAtCompileTime) << " - " << int(Derived::OuterStrideAtCompileTime) << "\n";
344 // std::cout << int(StrideType::InnerStrideAtCompileTime) << " - " << int(Derived::InnerStrideAtCompileTime) << "\n";
345  construct(expr.derived(), typename Traits::template match<Derived>::type());
346  }
347 
348  EIGEN_DEVICE_FUNC inline Ref(const Ref& other) : Base(other) {
349  // copy constructor shall not copy the m_object, to avoid unnecessary malloc and copy
350  }
351 
352  template<typename OtherRef>
353  EIGEN_DEVICE_FUNC inline Ref(const RefBase<OtherRef>& other) {
354  construct(other.derived(), typename Traits::template match<OtherRef>::type());
355  }
356 
357  protected:
358 
359  template<typename Expression>
360  EIGEN_DEVICE_FUNC void construct(const Expression& expr,internal::true_type)
361  {
362  // Check if we can use the underlying expr's storage directly, otherwise call the copy version.
363  if (!Base::construct(expr)) {
364  construct(expr, internal::false_type());
365  }
366  }
367 
368  template<typename Expression>
369  EIGEN_DEVICE_FUNC void construct(const Expression& expr, internal::false_type)
370  {
371  internal::call_assignment_no_alias(m_object,expr,internal::assign_op<Scalar,Scalar>());
372  Base::construct(m_object);
373  }
374 
375  protected:
376  TPlainObjectType m_object;
377 };
378 
379 } // end namespace Eigen
380 
381 #endif // EIGEN_REF_H
Namespace containing all symbols from the Eigen library.
Definition: Core:141
Derived & derived()
Definition: EigenBase.h:46
Definition: Constants.h:233
const unsigned int RowMajorBit
Definition: Constants.h:66
Base class for all dense matrices, vectors, and arrays.
Definition: DenseBase.h:41
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:74
Dense storage base class for matrices and arrays.
Definition: PlainObjectBase.h:95
Ref(DenseBase< Derived > &expr)
Definition: Ref.h:312
A matrix or vector expression mapping an existing expression.
Definition: Ref.h:281
Definition: Eigen_Colamd.h:50
const int Dynamic
Definition: Constants.h:22