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 {
32  StorageOrderMatch = PlainObjectType::IsVectorAtCompileTime || Derived::IsVectorAtCompileTime || ((PlainObjectType::Flags&RowMajorBit)==(Derived::Flags&RowMajorBit)),
33  InnerStrideMatch = int(StrideType::InnerStrideAtCompileTime)==int(Dynamic)
34  || int(StrideType::InnerStrideAtCompileTime)==int(Derived::InnerStrideAtCompileTime)
35  || (int(StrideType::InnerStrideAtCompileTime)==0 && int(Derived::InnerStrideAtCompileTime)==1),
36  OuterStrideMatch = Derived::IsVectorAtCompileTime
37  || int(StrideType::OuterStrideAtCompileTime)==int(Dynamic) || int(StrideType::OuterStrideAtCompileTime)==int(Derived::OuterStrideAtCompileTime),
38  // NOTE, this indirection of evaluator<Derived>::Alignment is needed
39  // to workaround a very strange bug in MSVC related to the instantiation
40  // of has_*ary_operator in evaluator<CwiseNullaryOp>.
41  // This line is surprisingly very sensitive. For instance, simply adding parenthesis
42  // as "DerivedAlignment = (int(evaluator<Derived>::Alignment))," will make MSVC fail...
43  DerivedAlignment = int(evaluator<Derived>::Alignment),
44  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  MatchAtCompileTime = HasDirectAccess && StorageOrderMatch && InnerStrideMatch && OuterStrideMatch && AlignmentMatch && ScalarTypeMatch
47  };
49  };
50 
51 };
52 
53 template<typename Derived>
54 struct traits<RefBase<Derived> > : public traits<Derived> {};
55 
56 }
57 
58 template<typename Derived> class RefBase
59  : public MapBase<Derived>
60 {
63 
64 public:
65 
68 
69  EIGEN_DEVICE_FUNC inline Index innerStride() const
70  {
71  return StrideType::InnerStrideAtCompileTime != 0 ? m_stride.inner() : 1;
72  }
73 
74  EIGEN_DEVICE_FUNC inline Index outerStride() const
75  {
76  return StrideType::OuterStrideAtCompileTime != 0 ? m_stride.outer()
77  : IsVectorAtCompileTime ? this->size()
78  : int(Flags)&RowMajorBit ? this->cols()
79  : this->rows();
80  }
81 
82  EIGEN_DEVICE_FUNC RefBase()
83  : Base(0,RowsAtCompileTime==Dynamic?0:RowsAtCompileTime,ColsAtCompileTime==Dynamic?0:ColsAtCompileTime),
84  // Stride<> does not allow default ctor for Dynamic strides, so let' initialize it with dummy values:
85  m_stride(StrideType::OuterStrideAtCompileTime==Dynamic?0:StrideType::OuterStrideAtCompileTime,
86  StrideType::InnerStrideAtCompileTime==Dynamic?0:StrideType::InnerStrideAtCompileTime)
87  {}
88 
90 
91 protected:
92 
94 
95  template<typename Expression>
96  EIGEN_DEVICE_FUNC void construct(Expression& expr)
97  {
98  if(PlainObjectType::RowsAtCompileTime==1)
99  {
100  eigen_assert(expr.rows()==1 || expr.cols()==1);
101  ::new (static_cast<Base*>(this)) Base(expr.data(), 1, expr.size());
102  }
103  else if(PlainObjectType::ColsAtCompileTime==1)
104  {
105  eigen_assert(expr.rows()==1 || expr.cols()==1);
106  ::new (static_cast<Base*>(this)) Base(expr.data(), expr.size(), 1);
107  }
108  else
109  ::new (static_cast<Base*>(this)) Base(expr.data(), expr.rows(), expr.cols());
110 
111  if(Expression::IsVectorAtCompileTime && (!PlainObjectType::IsVectorAtCompileTime) && ((Expression::Flags&RowMajorBit)!=(PlainObjectType::Flags&RowMajorBit)))
112  ::new (&m_stride) StrideBase(expr.innerStride(), StrideType::InnerStrideAtCompileTime==0?0:1);
113  else
114  ::new (&m_stride) StrideBase(StrideType::OuterStrideAtCompileTime==0?0:expr.outerStride(),
115  StrideType::InnerStrideAtCompileTime==0?0:expr.innerStride());
116  }
117 
118  StrideBase m_stride;
119 };
120 
190 template<typename PlainObjectType, int Options, typename StrideType> class Ref
191  : public RefBase<Ref<PlainObjectType, Options, StrideType> >
192 {
193  private:
195  template<typename Derived>
196  EIGEN_DEVICE_FUNC inline Ref(const PlainObjectBase<Derived>& expr,
197  typename internal::enable_if<bool(Traits::template match<Derived>::MatchAtCompileTime),Derived>::type* = 0);
198  public:
199 
202 
203 
204  #ifndef EIGEN_PARSED_BY_DOXYGEN
205  template<typename Derived>
206  EIGEN_DEVICE_FUNC inline Ref(PlainObjectBase<Derived>& expr,
207  typename internal::enable_if<bool(Traits::template match<Derived>::MatchAtCompileTime),Derived>::type* = 0)
208  {
209  EIGEN_STATIC_ASSERT(bool(Traits::template match<Derived>::MatchAtCompileTime), STORAGE_LAYOUT_DOES_NOT_MATCH);
210  Base::construct(expr.derived());
211  }
212  template<typename Derived>
213  EIGEN_DEVICE_FUNC inline Ref(const DenseBase<Derived>& expr,
214  typename internal::enable_if<bool(Traits::template match<Derived>::MatchAtCompileTime),Derived>::type* = 0)
215  #else
216 
217  template<typename Derived>
218  inline Ref(DenseBase<Derived>& expr)
219  #endif
220  {
221  EIGEN_STATIC_ASSERT(bool(internal::is_lvalue<Derived>::value), THIS_EXPRESSION_IS_NOT_A_LVALUE__IT_IS_READ_ONLY);
222  EIGEN_STATIC_ASSERT(bool(Traits::template match<Derived>::MatchAtCompileTime), STORAGE_LAYOUT_DOES_NOT_MATCH);
223  EIGEN_STATIC_ASSERT(!Derived::IsPlainObjectBase,THIS_EXPRESSION_IS_NOT_A_LVALUE__IT_IS_READ_ONLY);
224  Base::construct(expr.const_cast_derived());
225  }
226 
228 
229 };
230 
231 // this is the const ref version
232 template<typename TPlainObjectType, int Options, typename StrideType> class Ref<const TPlainObjectType, Options, StrideType>
233  : public RefBase<Ref<const TPlainObjectType, Options, StrideType> >
234 {
236  public:
237 
240 
241  template<typename Derived>
242  EIGEN_DEVICE_FUNC inline Ref(const DenseBase<Derived>& expr,
243  typename internal::enable_if<bool(Traits::template match<Derived>::ScalarTypeMatch),Derived>::type* = 0)
244  {
245 // std::cout << match_helper<Derived>::HasDirectAccess << "," << match_helper<Derived>::OuterStrideMatch << "," << match_helper<Derived>::InnerStrideMatch << "\n";
246 // std::cout << int(StrideType::OuterStrideAtCompileTime) << " - " << int(Derived::OuterStrideAtCompileTime) << "\n";
247 // std::cout << int(StrideType::InnerStrideAtCompileTime) << " - " << int(Derived::InnerStrideAtCompileTime) << "\n";
248  construct(expr.derived(), typename Traits::template match<Derived>::type());
249  }
250 
251  EIGEN_DEVICE_FUNC inline Ref(const Ref& other) : Base(other) {
252  // copy constructor shall not copy the m_object, to avoid unnecessary malloc and copy
253  }
254 
255  template<typename OtherRef>
256  EIGEN_DEVICE_FUNC inline Ref(const RefBase<OtherRef>& other) {
257  construct(other.derived(), typename Traits::template match<OtherRef>::type());
258  }
259 
260  protected:
261 
262  template<typename Expression>
263  EIGEN_DEVICE_FUNC void construct(const Expression& expr,internal::true_type)
264  {
265  Base::construct(expr);
266  }
267 
268  template<typename Expression>
269  EIGEN_DEVICE_FUNC void construct(const Expression& expr, internal::false_type)
270  {
272  Base::construct(m_object);
273  }
274 
275  protected:
276  TPlainObjectType m_object;
277 };
278 
279 } // end namespace Eigen
280 
281 #endif // EIGEN_REF_H
EIGEN_DEVICE_FUNC Index outerStride() const
Definition: Ref.h:74
EIGEN_DEVICE_FUNC Ref(const DenseBase< Derived > &expr, typename internal::enable_if< bool(Traits::template match< Derived >::MatchAtCompileTime), Derived >::type *=0)
Definition: Ref.h:213
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void call_assignment_no_alias(Dst &dst, const Src &src, const Func &func)
Stride< StrideType::OuterStrideAtCompileTime, StrideType::InnerStrideAtCompileTime > StrideBase
Definition: Ref.h:93
MapBase< Derived > Base
Definition: Ref.h:66
EIGEN_DEVICE_FUNC RefBase()
Definition: Ref.h:82
internal::traits< Ref > Traits
Definition: Ref.h:194
internal::traits< Derived >::PlainObjectType PlainObjectType
Definition: Ref.h:61
Definition: LDLT.h:16
static constexpr size_t size(Tuple< Args... > &)
Provides access to the number of elements in a tuple as a compile-time constant expression.
#define EIGEN_STATIC_ASSERT(CONDITION, MSG)
Definition: StaticAssert.h:122
EIGEN_DEVICE_FUNC Ref(const RefBase< OtherRef > &other)
Definition: Ref.h:256
const unsigned int RowMajorBit
Definition: Constants.h:61
Base class for all dense matrices, vectors, and arrays.
Definition: DenseBase.h:41
EIGEN_DEVICE_FUNC void construct(Expression &expr)
Definition: Ref.h:96
internal::conditional< MatchAtCompileTime, internal::true_type, internal::false_type >::type type
Definition: Ref.h:48
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:33
#define eigen_assert(x)
Definition: Macros.h:577
EIGEN_DEVICE_FUNC Ref(const Ref &other)
Definition: Ref.h:251
#define EIGEN_INHERIT_ASSIGNMENT_OPERATORS(Derived)
Definition: Macros.h:839
StrideBase m_stride
Definition: Ref.h:118
A matrix or vector expression mapping an existing expression.
Definition: Ref.h:190
const unsigned int NestByRefBit
Definition: Constants.h:164
EIGEN_DEVICE_FUNC Ref(PlainObjectBase< Derived > &expr, typename internal::enable_if< bool(Traits::template match< Derived >::MatchAtCompileTime), Derived >::type *=0)
Definition: Ref.h:206
RefBase< Ref > Base
Definition: Ref.h:200
#define EIGEN_DENSE_PUBLIC_INTERFACE(Derived)
Definition: Macros.h:867
const int Dynamic
Definition: Constants.h:21
EIGEN_DEVICE_FUNC void construct(const Expression &expr, internal::true_type)
Definition: Ref.h:263
EIGEN_DEVICE_FUNC void construct(const Expression &expr, internal::false_type)
Definition: Ref.h:269
internal::traits< Derived >::StrideType StrideType
Definition: Ref.h:62


hebiros
Author(s): Xavier Artache , Matthew Tesch
autogenerated on Thu Sep 3 2020 04:08:43