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 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,
27  };
28 
29  template<typename Derived> struct match {
30  enum {
31  IsVectorAtCompileTime = PlainObjectType::IsVectorAtCompileTime || Derived::IsVectorAtCompileTime,
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
47  MatchAtCompileTime = HasDirectAccess && StorageOrderMatch && InnerStrideMatch && OuterStrideMatch && AlignmentMatch && ScalarTypeMatch
48  };
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 {
64 
65 public:
66 
69 
70  EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR inline Index innerStride() const
71  {
72  return StrideType::InnerStrideAtCompileTime != 0 ? m_stride.inner() : 1;
73  }
74 
76  {
77  return StrideType::OuterStrideAtCompileTime != 0 ? m_stride.outer()
78  : IsVectorAtCompileTime ? this->size()
79  : int(Flags)&RowMajorBit ? this->cols()
80  : this->rows();
81  }
82 
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 
91 
92 protected:
93 
95 
96  // Resolves inner stride if default 0.
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.
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:
286  template<typename Derived>
288  typename internal::enable_if<bool(Traits::template match<Derived>::MatchAtCompileTime),Derived>::type* = 0);
289  public:
290 
293 
294 
295  #ifndef EIGEN_PARSED_BY_DOXYGEN
296  template<typename Derived>
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>
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 
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 {
333  public:
334 
337 
338  template<typename Derived>
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>
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)) {
365  }
366  }
367 
368  template<typename Expression>
369  EIGEN_DEVICE_FUNC void construct(const Expression& expr, internal::false_type)
370  {
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
EIGEN_DEVICE_FUNC Ref(const DenseBase< Derived > &expr, typename internal::enable_if< bool(Traits::template match< Derived >::MatchAtCompileTime), Derived >::type *=0)
Definition: Ref.h:307
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void call_assignment_no_alias(Dst &dst, const Src &src, const Func &func)
bool match(const T &xpr, std::string ref, std::string str_xpr="")
Stride< StrideType::OuterStrideAtCompileTime, StrideType::InnerStrideAtCompileTime > StrideBase
Definition: Ref.h:94
void construct(...)
Definition: init.h:104
MapBase< Derived > Base
Definition: Ref.h:67
EIGEN_DEVICE_FUNC RefBase()
Definition: Ref.h:83
internal::traits< Ref > Traits
Definition: Ref.h:285
internal::traits< Derived >::PlainObjectType PlainObjectType
Definition: Ref.h:62
Namespace containing all symbols from the Eigen library.
Definition: jet.h:637
EIGEN_DEVICE_FUNC Ref(const DenseBase< Derived > &expr, typename internal::enable_if< bool(Traits::template match< Derived >::ScalarTypeMatch), Derived >::type *=0)
Definition: Ref.h:339
#define EIGEN_STATIC_ASSERT(CONDITION, MSG)
Definition: StaticAssert.h:127
EIGEN_DEVICE_FUNC Ref(const RefBase< OtherRef > &other)
Definition: Ref.h:353
const unsigned int RowMajorBit
Definition: Constants.h:66
Base class for all dense matrices, vectors, and arrays.
Definition: DenseBase.h:41
static EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR Index resolveInnerStride(Index inner)
Definition: Ref.h:97
EIGEN_DEVICE_FUNC bool construct(Expression &expr)
Definition: Ref.h:109
static EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR Index resolveOuterStride(Index inner, Index outer, Index rows, Index cols, bool isVectorAtCompileTime, bool isRowMajor)
Definition: Ref.h:102
internal::conditional< MatchAtCompileTime, internal::true_type, internal::false_type >::type type
Definition: Ref.h:49
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:74
#define eigen_assert(x)
Definition: Macros.h:1037
EIGEN_DEVICE_FUNC Ref(const Ref &other)
Definition: Ref.h:348
#define EIGEN_CONSTEXPR
Definition: Macros.h:787
#define EIGEN_INHERIT_ASSIGNMENT_OPERATORS(Derived)
Definition: Macros.h:1231
StrideBase m_stride
Definition: Ref.h:207
A matrix or vector expression mapping an existing expression.
Definition: Ref.h:281
const unsigned int NestByRefBit
Definition: Constants.h:169
EIGEN_CONSTEXPR Index size(const T &x)
Definition: Meta.h:479
#define EIGEN_DEVICE_FUNC
Definition: Macros.h:976
EIGEN_DEVICE_FUNC Ref(PlainObjectBase< Derived > &expr, typename internal::enable_if< bool(Traits::template match< Derived >::MatchAtCompileTime), Derived >::type *=0)
Definition: Ref.h:297
#define EIGEN_PREDICATE_SAME_MATRIX_SIZE(TYPE0, TYPE1)
Definition: StaticAssert.h:174
RefBase< Ref > Base
Definition: Ref.h:291
#define EIGEN_DENSE_PUBLIC_INTERFACE(Derived)
Definition: Macros.h:1283
const int Dynamic
Definition: Constants.h:22
EIGEN_DEVICE_FUNC void construct(const Expression &expr, internal::true_type)
Definition: Ref.h:360
Generic expression where a coefficient-wise unary operator is applied to an expression.
Definition: CwiseUnaryOp.h:55
EIGEN_DEVICE_FUNC void construct(const Expression &expr, internal::false_type)
Definition: Ref.h:369
EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR Index outerStride() const
Definition: Ref.h:75
#define EIGEN_UNUSED_VARIABLE(var)
Definition: Macros.h:1076
Definition: pytypes.h:1370
internal::traits< Derived >::StrideType StrideType
Definition: Ref.h:63


gtsam
Author(s):
autogenerated on Tue Jul 4 2023 02:35:32