Eigen  3.3.9
 
Loading...
Searching...
No Matches
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
13namespace Eigen {
14
15namespace internal {
16
17template<typename _PlainObjectType, int _Options, typename _StrideType>
18struct 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 IsVectorAtCompileTime = PlainObjectType::IsVectorAtCompileTime || Derived::IsVectorAtCompileTime,
32 HasDirectAccess = internal::has_direct_access<Derived>::ret,
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
46 ScalarTypeMatch = internal::is_same<typename PlainObjectType::Scalar, typename Derived::Scalar>::value,
47 MatchAtCompileTime = HasDirectAccess && StorageOrderMatch && InnerStrideMatch && OuterStrideMatch && AlignmentMatch && ScalarTypeMatch
48 };
49 typedef typename internal::conditional<MatchAtCompileTime,internal::true_type,internal::false_type>::type type;
50 };
51
52};
53
54template<typename Derived>
55struct traits<RefBase<Derived> > : public traits<Derived> {};
56
57}
58
59template<typename Derived> class RefBase
60 : public MapBase<Derived>
61{
62 typedef typename internal::traits<Derived>::PlainObjectType PlainObjectType;
63 typedef typename internal::traits<Derived>::StrideType StrideType;
64
65public:
66
67 typedef MapBase<Derived> Base;
68 EIGEN_DENSE_PUBLIC_INTERFACE(RefBase)
69
70 EIGEN_DEVICE_FUNC inline Index innerStride() const
71 {
72 return StrideType::InnerStrideAtCompileTime != 0 ? m_stride.inner() : 1;
73 }
74
75 EIGEN_DEVICE_FUNC inline Index outerStride() const
76 {
77 return StrideType::OuterStrideAtCompileTime != 0 ? m_stride.outer()
78 : IsVectorAtCompileTime ? this->size()
79 : int(Flags)&RowMajorBit ? this->cols()
80 : this->rows();
81 }
82
83 EIGEN_DEVICE_FUNC RefBase()
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
90 EIGEN_INHERIT_ASSIGNMENT_OPERATORS(RefBase)
91
92protected:
93
94 typedef Stride<StrideType::OuterStrideAtCompileTime,StrideType::InnerStrideAtCompileTime> StrideBase;
95
96 template<typename Expression>
97 EIGEN_DEVICE_FUNC void construct(Expression& expr)
98 {
99 EIGEN_STATIC_ASSERT_SAME_MATRIX_SIZE(PlainObjectType,Expression);
100
101 if(PlainObjectType::RowsAtCompileTime==1)
102 {
103 eigen_assert(expr.rows()==1 || expr.cols()==1);
104 ::new (static_cast<Base*>(this)) Base(expr.data(), 1, expr.size());
105 }
106 else if(PlainObjectType::ColsAtCompileTime==1)
107 {
108 eigen_assert(expr.rows()==1 || expr.cols()==1);
109 ::new (static_cast<Base*>(this)) Base(expr.data(), expr.size(), 1);
110 }
111 else
112 ::new (static_cast<Base*>(this)) Base(expr.data(), expr.rows(), expr.cols());
113
114 if(Expression::IsVectorAtCompileTime && (!PlainObjectType::IsVectorAtCompileTime) && ((Expression::Flags&RowMajorBit)!=(PlainObjectType::Flags&RowMajorBit)))
115 ::new (&m_stride) StrideBase(expr.innerStride(), StrideType::InnerStrideAtCompileTime==0?0:1);
116 else
117 ::new (&m_stride) StrideBase(StrideType::OuterStrideAtCompileTime==0?0:expr.outerStride(),
118 StrideType::InnerStrideAtCompileTime==0?0:expr.innerStride());
119 }
120
121 StrideBase m_stride;
122};
123
193template<typename PlainObjectType, int Options, typename StrideType> class Ref
194 : public RefBase<Ref<PlainObjectType, Options, StrideType> >
195{
196 private:
197 typedef internal::traits<Ref> Traits;
198 template<typename Derived>
199 EIGEN_DEVICE_FUNC inline Ref(const PlainObjectBase<Derived>& expr,
200 typename internal::enable_if<bool(Traits::template match<Derived>::MatchAtCompileTime),Derived>::type* = 0);
201 public:
202
203 typedef RefBase<Ref> Base;
204 EIGEN_DENSE_PUBLIC_INTERFACE(Ref)
205
206
207 #ifndef EIGEN_PARSED_BY_DOXYGEN
208 template<typename Derived>
209 EIGEN_DEVICE_FUNC inline Ref(PlainObjectBase<Derived>& expr,
210 typename internal::enable_if<bool(Traits::template match<Derived>::MatchAtCompileTime),Derived>::type* = 0)
211 {
212 EIGEN_STATIC_ASSERT(bool(Traits::template match<Derived>::MatchAtCompileTime), STORAGE_LAYOUT_DOES_NOT_MATCH);
213 Base::construct(expr.derived());
214 }
215 template<typename Derived>
216 EIGEN_DEVICE_FUNC inline Ref(const DenseBase<Derived>& expr,
217 typename internal::enable_if<bool(Traits::template match<Derived>::MatchAtCompileTime),Derived>::type* = 0)
218 #else
220 template<typename Derived>
222 #endif
223 {
224 EIGEN_STATIC_ASSERT(bool(internal::is_lvalue<Derived>::value), THIS_EXPRESSION_IS_NOT_A_LVALUE__IT_IS_READ_ONLY);
225 EIGEN_STATIC_ASSERT(bool(Traits::template match<Derived>::MatchAtCompileTime), STORAGE_LAYOUT_DOES_NOT_MATCH);
226 EIGEN_STATIC_ASSERT(!Derived::IsPlainObjectBase,THIS_EXPRESSION_IS_NOT_A_LVALUE__IT_IS_READ_ONLY);
227 Base::construct(expr.const_cast_derived());
228 }
229
230 EIGEN_INHERIT_ASSIGNMENT_OPERATORS(Ref)
231
232};
233
234// this is the const ref version
235template<typename TPlainObjectType, int Options, typename StrideType> class Ref<const TPlainObjectType, Options, StrideType>
236 : public RefBase<Ref<const TPlainObjectType, Options, StrideType> >
237{
238 typedef internal::traits<Ref> Traits;
239 public:
240
241 typedef RefBase<Ref> Base;
242 EIGEN_DENSE_PUBLIC_INTERFACE(Ref)
243
244 template<typename Derived>
245 EIGEN_DEVICE_FUNC inline Ref(const DenseBase<Derived>& expr,
246 typename internal::enable_if<bool(Traits::template match<Derived>::ScalarTypeMatch),Derived>::type* = 0)
247 {
248// std::cout << match_helper<Derived>::HasDirectAccess << "," << match_helper<Derived>::OuterStrideMatch << "," << match_helper<Derived>::InnerStrideMatch << "\n";
249// std::cout << int(StrideType::OuterStrideAtCompileTime) << " - " << int(Derived::OuterStrideAtCompileTime) << "\n";
250// std::cout << int(StrideType::InnerStrideAtCompileTime) << " - " << int(Derived::InnerStrideAtCompileTime) << "\n";
251 construct(expr.derived(), typename Traits::template match<Derived>::type());
252 }
253
254 EIGEN_DEVICE_FUNC inline Ref(const Ref& other) : Base(other) {
255 // copy constructor shall not copy the m_object, to avoid unnecessary malloc and copy
256 }
257
258 template<typename OtherRef>
259 EIGEN_DEVICE_FUNC inline Ref(const RefBase<OtherRef>& other) {
260 construct(other.derived(), typename Traits::template match<OtherRef>::type());
261 }
262
263 protected:
264
265 template<typename Expression>
266 EIGEN_DEVICE_FUNC void construct(const Expression& expr,internal::true_type)
267 {
268 Base::construct(expr);
269 }
270
271 template<typename Expression>
272 EIGEN_DEVICE_FUNC void construct(const Expression& expr, internal::false_type)
273 {
274 internal::call_assignment_no_alias(m_object,expr,internal::assign_op<Scalar,Scalar>());
275 Base::construct(m_object);
276 }
277
278 protected:
279 TPlainObjectType m_object;
280};
281
282} // end namespace Eigen
283
284#endif // EIGEN_REF_H
Base class for all dense matrices, vectors, and arrays.
Definition DenseBase.h:47
Dense storage base class for matrices and arrays.
Definition PlainObjectBase.h:100
A matrix or vector expression mapping an existing expression.
Definition Ref.h:195
Ref(DenseBase< Derived > &expr)
Definition Ref.h:221
const unsigned int RowMajorBit
Definition Constants.h:61
Namespace containing all symbols from the Eigen library.
Definition A05_PortingFrom2To3.dox:1
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition Meta.h:65
const int Dynamic
Definition Constants.h:21