Eigen  3.2.93
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  HasDirectAccess = internal::has_direct_access<Derived>::ret,
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  AlignmentMatch = (int(traits<PlainObjectType>::Alignment)==int(Unaligned)) || (int(evaluator<Derived>::Alignment) >= int(Alignment)), // FIXME the first condition is not very clear, it should be replaced by the required alignment
39  ScalarTypeMatch = internal::is_same<typename PlainObjectType::Scalar, typename Derived::Scalar>::value,
40  MatchAtCompileTime = HasDirectAccess && StorageOrderMatch && InnerStrideMatch && OuterStrideMatch && AlignmentMatch && ScalarTypeMatch
41  };
42  typedef typename internal::conditional<MatchAtCompileTime,internal::true_type,internal::false_type>::type type;
43  };
44 
45 };
46 
47 template<typename Derived>
48 struct traits<RefBase<Derived> > : public traits<Derived> {};
49 
50 }
51 
52 template<typename Derived> class RefBase
53  : public MapBase<Derived>
54 {
55  typedef typename internal::traits<Derived>::PlainObjectType PlainObjectType;
56  typedef typename internal::traits<Derived>::StrideType StrideType;
57 
58 public:
59 
60  typedef MapBase<Derived> Base;
61  EIGEN_DENSE_PUBLIC_INTERFACE(RefBase)
62 
63  EIGEN_DEVICE_FUNC inline Index innerStride() const
64  {
65  return StrideType::InnerStrideAtCompileTime != 0 ? m_stride.inner() : 1;
66  }
67 
68  EIGEN_DEVICE_FUNC inline Index outerStride() const
69  {
70  return StrideType::OuterStrideAtCompileTime != 0 ? m_stride.outer()
71  : IsVectorAtCompileTime ? this->size()
72  : int(Flags)&RowMajorBit ? this->cols()
73  : this->rows();
74  }
75 
76  EIGEN_DEVICE_FUNC RefBase()
77  : Base(0,RowsAtCompileTime==Dynamic?0:RowsAtCompileTime,ColsAtCompileTime==Dynamic?0:ColsAtCompileTime),
78  // Stride<> does not allow default ctor for Dynamic strides, so let' initialize it with dummy values:
79  m_stride(StrideType::OuterStrideAtCompileTime==Dynamic?0:StrideType::OuterStrideAtCompileTime,
80  StrideType::InnerStrideAtCompileTime==Dynamic?0:StrideType::InnerStrideAtCompileTime)
81  {}
82 
83  EIGEN_INHERIT_ASSIGNMENT_OPERATORS(RefBase)
84 
85 protected:
86 
87  typedef Stride<StrideType::OuterStrideAtCompileTime,StrideType::InnerStrideAtCompileTime> StrideBase;
88 
89  template<typename Expression>
90  EIGEN_DEVICE_FUNC void construct(Expression& expr)
91  {
92  if(PlainObjectType::RowsAtCompileTime==1)
93  {
94  eigen_assert(expr.rows()==1 || expr.cols()==1);
95  ::new (static_cast<Base*>(this)) Base(expr.data(), 1, expr.size());
96  }
97  else if(PlainObjectType::ColsAtCompileTime==1)
98  {
99  eigen_assert(expr.rows()==1 || expr.cols()==1);
100  ::new (static_cast<Base*>(this)) Base(expr.data(), expr.size(), 1);
101  }
102  else
103  ::new (static_cast<Base*>(this)) Base(expr.data(), expr.rows(), expr.cols());
104 
105  if(Expression::IsVectorAtCompileTime && (!PlainObjectType::IsVectorAtCompileTime) && ((Expression::Flags&RowMajorBit)!=(PlainObjectType::Flags&RowMajorBit)))
106  ::new (&m_stride) StrideBase(expr.innerStride(), StrideType::InnerStrideAtCompileTime==0?0:1);
107  else
108  ::new (&m_stride) StrideBase(StrideType::OuterStrideAtCompileTime==0?0:expr.outerStride(),
109  StrideType::InnerStrideAtCompileTime==0?0:expr.innerStride());
110  }
111 
112  StrideBase m_stride;
113 };
114 
184 template<typename PlainObjectType, int Options, typename StrideType> class Ref
185  : public RefBase<Ref<PlainObjectType, Options, StrideType> >
186 {
187  private:
188  typedef internal::traits<Ref> Traits;
189  template<typename Derived>
190  EIGEN_DEVICE_FUNC inline Ref(const PlainObjectBase<Derived>& expr,
191  typename internal::enable_if<bool(Traits::template match<Derived>::MatchAtCompileTime),Derived>::type* = 0);
192  public:
193 
194  typedef RefBase<Ref> Base;
195  EIGEN_DENSE_PUBLIC_INTERFACE(Ref)
196 
197 
198  #ifndef EIGEN_PARSED_BY_DOXYGEN
199  template<typename Derived>
200  EIGEN_DEVICE_FUNC inline Ref(PlainObjectBase<Derived>& expr,
201  typename internal::enable_if<bool(Traits::template match<Derived>::MatchAtCompileTime),Derived>::type* = 0)
202  {
203  EIGEN_STATIC_ASSERT(bool(Traits::template match<Derived>::MatchAtCompileTime), STORAGE_LAYOUT_DOES_NOT_MATCH);
204  Base::construct(expr.derived());
205  }
206  template<typename Derived>
207  EIGEN_DEVICE_FUNC inline Ref(const DenseBase<Derived>& expr,
208  typename internal::enable_if<bool(Traits::template match<Derived>::MatchAtCompileTime),Derived>::type* = 0)
209  #else
210 
211  template<typename Derived>
212  inline Ref(DenseBase<Derived>& expr)
213  #endif
214  {
215  EIGEN_STATIC_ASSERT(bool(internal::is_lvalue<Derived>::value), THIS_EXPRESSION_IS_NOT_A_LVALUE__IT_IS_READ_ONLY);
216  EIGEN_STATIC_ASSERT(bool(Traits::template match<Derived>::MatchAtCompileTime), STORAGE_LAYOUT_DOES_NOT_MATCH);
217  EIGEN_STATIC_ASSERT(!Derived::IsPlainObjectBase,THIS_EXPRESSION_IS_NOT_A_LVALUE__IT_IS_READ_ONLY);
218  Base::construct(expr.const_cast_derived());
219  }
220 
221  EIGEN_INHERIT_ASSIGNMENT_OPERATORS(Ref)
222 
223 };
224 
225 // this is the const ref version
226 template<typename TPlainObjectType, int Options, typename StrideType> class Ref<const TPlainObjectType, Options, StrideType>
227  : public RefBase<Ref<const TPlainObjectType, Options, StrideType> >
228 {
229  typedef internal::traits<Ref> Traits;
230  public:
231 
232  typedef RefBase<Ref> Base;
233  EIGEN_DENSE_PUBLIC_INTERFACE(Ref)
234 
235  template<typename Derived>
236  EIGEN_DEVICE_FUNC inline Ref(const DenseBase<Derived>& expr,
237  typename internal::enable_if<bool(Traits::template match<Derived>::ScalarTypeMatch),Derived>::type* = 0)
238  {
239 // std::cout << match_helper<Derived>::HasDirectAccess << "," << match_helper<Derived>::OuterStrideMatch << "," << match_helper<Derived>::InnerStrideMatch << "\n";
240 // std::cout << int(StrideType::OuterStrideAtCompileTime) << " - " << int(Derived::OuterStrideAtCompileTime) << "\n";
241 // std::cout << int(StrideType::InnerStrideAtCompileTime) << " - " << int(Derived::InnerStrideAtCompileTime) << "\n";
242  construct(expr.derived(), typename Traits::template match<Derived>::type());
243  }
244 
245  EIGEN_DEVICE_FUNC inline Ref(const Ref& other) : Base(other) {
246  // copy constructor shall not copy the m_object, to avoid unnecessary malloc and copy
247  }
248 
249  template<typename OtherRef>
250  EIGEN_DEVICE_FUNC inline Ref(const RefBase<OtherRef>& other) {
251  construct(other.derived(), typename Traits::template match<OtherRef>::type());
252  }
253 
254  protected:
255 
256  template<typename Expression>
257  EIGEN_DEVICE_FUNC void construct(const Expression& expr,internal::true_type)
258  {
259  Base::construct(expr);
260  }
261 
262  template<typename Expression>
263  EIGEN_DEVICE_FUNC void construct(const Expression& expr, internal::false_type)
264  {
265  internal::call_assignment_no_alias(m_object,expr,internal::assign_op<Scalar,Scalar>());
266  Base::construct(m_object);
267  }
268 
269  protected:
270  TPlainObjectType m_object;
271 };
272 
273 } // end namespace Eigen
274 
275 #endif // EIGEN_REF_H
Namespace containing all symbols from the Eigen library.
Definition: Core:271
Derived & derived()
Definition: EigenBase.h:44
Definition: Constants.h:228
const unsigned int RowMajorBit
Definition: Constants.h:61
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: XprHelper.h:35
Dense storage base class for matrices and arrays.
Definition: PlainObjectBase.h:89
Ref(DenseBase< Derived > &expr)
Definition: Ref.h:212
A matrix or vector expression mapping an existing expression.
Definition: Ref.h:184
Definition: Eigen_Colamd.h:50
const int Dynamic
Definition: Constants.h:21