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