Ref.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) 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 
71 
73  {
74  return StrideType::InnerStrideAtCompileTime != 0 ? m_stride.inner() : 1;
75  }
76 
78  {
79  return StrideType::OuterStrideAtCompileTime != 0 ? m_stride.outer()
80  : IsVectorAtCompileTime ? this->size()
81  : int(Flags)&RowMajorBit ? this->cols()
82  : this->rows();
83  }
84 
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 
93 
94 protected:
95 
97 
98  // Resolves inner stride if default 0.
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.
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);
204  (StrideType::OuterStrideAtCompileTime == 0) ? 0 : outer_stride,
205  (StrideType::InnerStrideAtCompileTime == 0) ? 0 : inner_stride );
206  return true;
207  }
208 
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>
290  std::enable_if_t<bool(Traits::template match<Derived>::MatchAtCompileTime),Derived>* = 0);
291  public:
292 
295 
296 
297  #ifndef EIGEN_PARSED_BY_DOXYGEN
298  template<typename Derived>
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 
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 
336  static constexpr bool may_map_m_object_successfully =
337  (static_cast<int>(StrideType::InnerStrideAtCompileTime) == 0 ||
338  static_cast<int>(StrideType::InnerStrideAtCompileTime) == 1 ||
339  static_cast<int>(StrideType::InnerStrideAtCompileTime) == Dynamic) &&
340  (TPlainObjectType::IsVectorAtCompileTime ||
341  static_cast<int>(StrideType::OuterStrideAtCompileTime) == 0 ||
342  static_cast<int>(StrideType::OuterStrideAtCompileTime) == Dynamic ||
343  static_cast<int>(StrideType::OuterStrideAtCompileTime) == static_cast<int>(TPlainObjectType::InnerSizeAtCompileTime) ||
344  static_cast<int>(TPlainObjectType::InnerSizeAtCompileTime) == Dynamic);
345  public:
346 
349 
350  template<typename Derived>
352  std::enable_if_t<bool(Traits::template match<Derived>::ScalarTypeMatch),Derived>* = 0)
353  {
354 // std::cout << match_helper<Derived>::HasDirectAccess << "," << match_helper<Derived>::OuterStrideMatch << "," << match_helper<Derived>::InnerStrideMatch << "\n";
355 // std::cout << int(StrideType::OuterStrideAtCompileTime) << " - " << int(Derived::OuterStrideAtCompileTime) << "\n";
356 // std::cout << int(StrideType::InnerStrideAtCompileTime) << " - " << int(Derived::InnerStrideAtCompileTime) << "\n";
357  EIGEN_STATIC_ASSERT(Traits::template match<Derived>::type::value || may_map_m_object_successfully,
358  STORAGE_LAYOUT_DOES_NOT_MATCH);
359  construct(expr.derived(), typename Traits::template match<Derived>::type());
360  }
361 
362  EIGEN_DEVICE_FUNC inline Ref(const Ref& other) : Base(other) {
363  // copy constructor shall not copy the m_object, to avoid unnecessary malloc and copy
364  }
365 
366  EIGEN_DEVICE_FUNC inline Ref(Ref&& other) {
367  if (other.data() == other.m_object.data()) {
368  m_object = std::move(other.m_object);
369  Base::construct(m_object);
370  }
371  else
372  Base::construct(other);
373  }
374 
375  template<typename OtherRef>
376  EIGEN_DEVICE_FUNC inline Ref(const RefBase<OtherRef>& other) {
377  EIGEN_STATIC_ASSERT(Traits::template match<OtherRef>::type::value || may_map_m_object_successfully,
378  STORAGE_LAYOUT_DOES_NOT_MATCH);
379  construct(other.derived(), typename Traits::template match<OtherRef>::type());
380  }
381 
382  protected:
383 
384  template<typename Expression>
385  EIGEN_DEVICE_FUNC void construct(const Expression& expr,internal::true_type)
386  {
387  // Check if we can use the underlying expr's storage directly, otherwise call the copy version.
388  if (!Base::construct(expr)) {
389  construct(expr, internal::false_type());
390  }
391  }
392 
393  template<typename Expression>
394  EIGEN_DEVICE_FUNC void construct(const Expression& expr, internal::false_type)
395  {
396  internal::call_assignment_no_alias(m_object,expr,internal::assign_op<Scalar,Scalar>());
397  const bool success = Base::construct(m_object);
399  eigen_assert(success);
400  }
401 
402  protected:
403  TPlainObjectType m_object;
404 };
405 
406 } // end namespace Eigen
407 
408 #endif // EIGEN_REF_H
#define EIGEN_CONSTEXPR
Definition: Macros.h:747
#define EIGEN_UNUSED_VARIABLE(var)
Definition: Macros.h:957
#define EIGEN_DEVICE_FUNC
Definition: Macros.h:883
#define EIGEN_ONLY_USED_FOR_DEBUG(x)
Definition: Macros.h:914
#define EIGEN_DENSE_PUBLIC_INTERFACE(Derived)
Definition: Macros.h:1168
#define eigen_assert(x)
Definition: Macros.h:902
#define EIGEN_INHERIT_ASSIGNMENT_OPERATORS(Derived)
Definition: Macros.h:1122
#define EIGEN_STATIC_ASSERT(X, MSG)
Definition: StaticAssert.h:26
#define EIGEN_PREDICATE_SAME_MATRIX_SIZE(TYPE0, TYPE1)
Definition: StaticAssert.h:68
Base class for all dense matrices, vectors, and arrays.
Definition: DenseBase.h:42
Dense storage base class for matrices and arrays.
internal::traits< Derived >::PlainObjectType PlainObjectType
Definition: Ref.h:64
static EIGEN_CONSTEXPR Index resolveInnerStride(Index inner)
Definition: Ref.h:99
EIGEN_CONSTEXPR Index innerStride() const
Definition: Ref.h:72
static EIGEN_CONSTEXPR Index resolveOuterStride(Index inner, Index outer, Index rows, Index cols, bool isVectorAtCompileTime, bool isRowMajor)
Definition: Ref.h:104
Stride< StrideType::OuterStrideAtCompileTime, StrideType::InnerStrideAtCompileTime > StrideBase
Definition: Ref.h:96
EIGEN_CONSTEXPR Index outerStride() const
Definition: Ref.h:77
internal::traits< Derived >::StrideType StrideType
Definition: Ref.h:65
bool construct(Expression &expr)
Definition: Ref.h:111
RefBase()
Definition: Ref.h:85
StrideBase m_stride
Definition: Ref.h:209
MapBase< Derived > Base
Definition: Ref.h:69
void construct(const Expression &expr, internal::false_type)
Definition: Ref.h:394
Ref(const RefBase< OtherRef > &other)
Definition: Ref.h:376
void construct(const Expression &expr, internal::true_type)
Definition: Ref.h:385
Ref(const DenseBase< Derived > &expr, std::enable_if_t< bool(Traits::template match< Derived >::ScalarTypeMatch), Derived > *=0)
Definition: Ref.h:351
A matrix or vector expression mapping an existing expression.
Definition: Ref.h:285
Ref(DenseBase< Derived > &expr)
Definition: Ref.h:314
internal::traits< Ref > Traits
Definition: Ref.h:287
Ref(const PlainObjectBase< Derived > &expr, std::enable_if_t< bool(Traits::template match< Derived >::MatchAtCompileTime), Derived > *=0)
RefBase< Ref > Base
Definition: Ref.h:293
EIGEN_CONSTEXPR Index inner() const
Definition: Stride.h:91
EIGEN_CONSTEXPR Index outer() const
Definition: Stride.h:88
@ Unaligned
Definition: Constants.h:235
const unsigned int RowMajorBit
Definition: Constants.h:68
EIGEN_CONSTEXPR void call_assignment_no_alias(Dst &dst, const Src &src, const Func &func)
T * construct_at(T *p, Args &&... args)
Definition: Memory.h:1248
: InteropHeaders
Definition: Core:139
const unsigned int NestByRefBit
Definition: Constants.h:171
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
Derived & const_cast_derived() const
Definition: EigenBase.h:54