Eigen  3.2.92
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 
85 namespace internal {
86 
87 template<typename _PlainObjectType, int _Options, typename _StrideType>
88 struct traits<Ref<_PlainObjectType, _Options, _StrideType> >
89  : public traits<Map<_PlainObjectType, _Options, _StrideType> >
90 {
91  typedef _PlainObjectType PlainObjectType;
92  typedef _StrideType StrideType;
93  enum {
94  Options = _Options,
95  Flags = traits<Map<_PlainObjectType, _Options, _StrideType> >::Flags | NestByRefBit,
96  Alignment = traits<Map<_PlainObjectType, _Options, _StrideType> >::Alignment
97  };
98 
99  template<typename Derived> struct match {
100  enum {
101  HasDirectAccess = internal::has_direct_access<Derived>::ret,
102  StorageOrderMatch = PlainObjectType::IsVectorAtCompileTime || Derived::IsVectorAtCompileTime || ((PlainObjectType::Flags&RowMajorBit)==(Derived::Flags&RowMajorBit)),
103  InnerStrideMatch = int(StrideType::InnerStrideAtCompileTime)==int(Dynamic)
104  || int(StrideType::InnerStrideAtCompileTime)==int(Derived::InnerStrideAtCompileTime)
105  || (int(StrideType::InnerStrideAtCompileTime)==0 && int(Derived::InnerStrideAtCompileTime)==1),
106  OuterStrideMatch = Derived::IsVectorAtCompileTime
107  || int(StrideType::OuterStrideAtCompileTime)==int(Dynamic) || int(StrideType::OuterStrideAtCompileTime)==int(Derived::OuterStrideAtCompileTime),
108  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
109  ScalarTypeMatch = internal::is_same<typename PlainObjectType::Scalar, typename Derived::Scalar>::value,
110  MatchAtCompileTime = HasDirectAccess && StorageOrderMatch && InnerStrideMatch && OuterStrideMatch && AlignmentMatch && ScalarTypeMatch
111  };
112  typedef typename internal::conditional<MatchAtCompileTime,internal::true_type,internal::false_type>::type type;
113  };
114 
115 };
116 
117 template<typename Derived>
118 struct traits<RefBase<Derived> > : public traits<Derived> {};
119 
120 }
121 
122 template<typename Derived> class RefBase
123  : public MapBase<Derived>
124 {
125  typedef typename internal::traits<Derived>::PlainObjectType PlainObjectType;
126  typedef typename internal::traits<Derived>::StrideType StrideType;
127 
128 public:
129 
130  typedef MapBase<Derived> Base;
131  EIGEN_DENSE_PUBLIC_INTERFACE(RefBase)
132 
133  EIGEN_DEVICE_FUNC inline Index innerStride() const
134  {
135  return StrideType::InnerStrideAtCompileTime != 0 ? m_stride.inner() : 1;
136  }
137 
138  EIGEN_DEVICE_FUNC inline Index outerStride() const
139  {
140  return StrideType::OuterStrideAtCompileTime != 0 ? m_stride.outer()
141  : IsVectorAtCompileTime ? this->size()
142  : int(Flags)&RowMajorBit ? this->cols()
143  : this->rows();
144  }
145 
146  EIGEN_DEVICE_FUNC RefBase()
147  : Base(0,RowsAtCompileTime==Dynamic?0:RowsAtCompileTime,ColsAtCompileTime==Dynamic?0:ColsAtCompileTime),
148  // Stride<> does not allow default ctor for Dynamic strides, so let' initialize it with dummy values:
149  m_stride(StrideType::OuterStrideAtCompileTime==Dynamic?0:StrideType::OuterStrideAtCompileTime,
150  StrideType::InnerStrideAtCompileTime==Dynamic?0:StrideType::InnerStrideAtCompileTime)
151  {}
152 
153  EIGEN_INHERIT_ASSIGNMENT_OPERATORS(RefBase)
154 
155 protected:
156 
157  typedef Stride<StrideType::OuterStrideAtCompileTime,StrideType::InnerStrideAtCompileTime> StrideBase;
158 
159  template<typename Expression>
160  EIGEN_DEVICE_FUNC void construct(Expression& expr)
161  {
162  if(PlainObjectType::RowsAtCompileTime==1)
163  {
164  eigen_assert(expr.rows()==1 || expr.cols()==1);
165  ::new (static_cast<Base*>(this)) Base(expr.data(), 1, expr.size());
166  }
167  else if(PlainObjectType::ColsAtCompileTime==1)
168  {
169  eigen_assert(expr.rows()==1 || expr.cols()==1);
170  ::new (static_cast<Base*>(this)) Base(expr.data(), expr.size(), 1);
171  }
172  else
173  ::new (static_cast<Base*>(this)) Base(expr.data(), expr.rows(), expr.cols());
174 
175  if(Expression::IsVectorAtCompileTime && (!PlainObjectType::IsVectorAtCompileTime) && ((Expression::Flags&RowMajorBit)!=(PlainObjectType::Flags&RowMajorBit)))
176  ::new (&m_stride) StrideBase(expr.innerStride(), StrideType::InnerStrideAtCompileTime==0?0:1);
177  else
178  ::new (&m_stride) StrideBase(StrideType::OuterStrideAtCompileTime==0?0:expr.outerStride(),
179  StrideType::InnerStrideAtCompileTime==0?0:expr.innerStride());
180  }
181 
182  StrideBase m_stride;
183 };
184 
185 
186 template<typename PlainObjectType, int Options, typename StrideType> class Ref
187  : public RefBase<Ref<PlainObjectType, Options, StrideType> >
188 {
189  private:
190  typedef internal::traits<Ref> Traits;
191  template<typename Derived>
192  EIGEN_DEVICE_FUNC inline Ref(const PlainObjectBase<Derived>& expr,
193  typename internal::enable_if<bool(Traits::template match<Derived>::MatchAtCompileTime),Derived>::type* = 0);
194  public:
195 
196  typedef RefBase<Ref> Base;
197  EIGEN_DENSE_PUBLIC_INTERFACE(Ref)
198 
199 
200  #ifndef EIGEN_PARSED_BY_DOXYGEN
201  template<typename Derived>
202  EIGEN_DEVICE_FUNC inline Ref(PlainObjectBase<Derived>& expr,
203  typename internal::enable_if<bool(Traits::template match<Derived>::MatchAtCompileTime),Derived>::type* = 0)
204  {
205  EIGEN_STATIC_ASSERT(bool(Traits::template match<Derived>::MatchAtCompileTime), STORAGE_LAYOUT_DOES_NOT_MATCH);
206  Base::construct(expr.derived());
207  }
208  template<typename Derived>
209  EIGEN_DEVICE_FUNC inline Ref(const DenseBase<Derived>& expr,
210  typename internal::enable_if<bool(Traits::template match<Derived>::MatchAtCompileTime),Derived>::type* = 0)
211  #else
212  template<typename Derived>
213  inline Ref(DenseBase<Derived>& expr)
214  #endif
215  {
216  EIGEN_STATIC_ASSERT(bool(internal::is_lvalue<Derived>::value), THIS_EXPRESSION_IS_NOT_A_LVALUE__IT_IS_READ_ONLY);
217  EIGEN_STATIC_ASSERT(bool(Traits::template match<Derived>::MatchAtCompileTime), STORAGE_LAYOUT_DOES_NOT_MATCH);
218  EIGEN_STATIC_ASSERT(!Derived::IsPlainObjectBase,THIS_EXPRESSION_IS_NOT_A_LVALUE__IT_IS_READ_ONLY);
219  Base::construct(expr.const_cast_derived());
220  }
221 
222  EIGEN_INHERIT_ASSIGNMENT_OPERATORS(Ref)
223 
224 };
225 
226 // this is the const ref version
227 template<typename TPlainObjectType, int Options, typename StrideType> class Ref<const TPlainObjectType, Options, StrideType>
228  : public RefBase<Ref<const TPlainObjectType, Options, StrideType> >
229 {
230  typedef internal::traits<Ref> Traits;
231  public:
232 
233  typedef RefBase<Ref> Base;
234  EIGEN_DENSE_PUBLIC_INTERFACE(Ref)
235 
236  template<typename Derived>
237  EIGEN_DEVICE_FUNC inline Ref(const DenseBase<Derived>& expr,
238  typename internal::enable_if<bool(Traits::template match<Derived>::ScalarTypeMatch),Derived>::type* = 0)
239  {
240 // std::cout << match_helper<Derived>::HasDirectAccess << "," << match_helper<Derived>::OuterStrideMatch << "," << match_helper<Derived>::InnerStrideMatch << "\n";
241 // std::cout << int(StrideType::OuterStrideAtCompileTime) << " - " << int(Derived::OuterStrideAtCompileTime) << "\n";
242 // std::cout << int(StrideType::InnerStrideAtCompileTime) << " - " << int(Derived::InnerStrideAtCompileTime) << "\n";
243  construct(expr.derived(), typename Traits::template match<Derived>::type());
244  }
245 
246  EIGEN_DEVICE_FUNC inline Ref(const Ref& other) : Base(other) {
247  // copy constructor shall not copy the m_object, to avoid unnecessary malloc and copy
248  }
249 
250  template<typename OtherRef>
251  EIGEN_DEVICE_FUNC inline Ref(const RefBase<OtherRef>& other) {
252  construct(other.derived(), typename Traits::template match<OtherRef>::type());
253  }
254 
255  protected:
256 
257  template<typename Expression>
258  EIGEN_DEVICE_FUNC void construct(const Expression& expr,internal::true_type)
259  {
260  Base::construct(expr);
261  }
262 
263  template<typename Expression>
264  EIGEN_DEVICE_FUNC void construct(const Expression& expr, internal::false_type)
265  {
266  internal::call_assignment_no_alias(m_object,expr,internal::assign_op<Scalar>());
267  Base::construct(m_object);
268  }
269 
270  protected:
271  TPlainObjectType m_object;
272 };
273 
274 } // end namespace Eigen
275 
276 #endif // EIGEN_REF_H
Definition: LDLT.h:16
const unsigned int RowMajorBit
Definition: Constants.h:61
Base class for all dense matrices, vectors, and arrays.
Definition: DenseBase.h:41
Definition: Constants.h:228
Dense storage base class for matrices and arrays.
Definition: PlainObjectBase.h:88
A matrix or vector expression mapping an existing expression.
Definition: Ref.h:186
Definition: Eigen_Colamd.h:54