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 | // 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 |
45 | ScalarTypeMatch = internal::is_same<typename PlainObjectType::Scalar, typename Derived::Scalar>::value, |
46 | MatchAtCompileTime = HasDirectAccess && StorageOrderMatch && InnerStrideMatch && OuterStrideMatch && AlignmentMatch && ScalarTypeMatch |
47 | }; |
48 | typedef typename internal::conditional<MatchAtCompileTime,internal::true_type,internal::false_type>::type type; |
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 | { |
61 | typedef typename internal::traits<Derived>::PlainObjectType PlainObjectType; |
62 | typedef typename internal::traits<Derived>::StrideType StrideType; |
63 | |
64 | public: |
65 | |
66 | typedef MapBase<Derived> Base; |
67 | EIGEN_DENSE_PUBLIC_INTERFACE(RefBase) |
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 | |
89 | EIGEN_INHERIT_ASSIGNMENT_OPERATORS(RefBase) |
90 | |
91 | protected: |
92 | |
93 | typedef Stride<StrideType::OuterStrideAtCompileTime,StrideType::InnerStrideAtCompileTime> StrideBase; |
94 | |
95 | template<typename Expression> |
96 | EIGEN_DEVICE_FUNC void construct(Expression& expr) |
97 | { |
98 | EIGEN_STATIC_ASSERT_SAME_MATRIX_SIZE(PlainObjectType,Expression); |
99 | |
100 | if(PlainObjectType::RowsAtCompileTime==1) |
101 | { |
102 | eigen_assert(expr.rows()==1 || expr.cols()==1); |
103 | ::new (static_cast<Base*>(this)) Base(expr.data(), 1, expr.size()); |
104 | } |
105 | else if(PlainObjectType::ColsAtCompileTime==1) |
106 | { |
107 | eigen_assert(expr.rows()==1 || expr.cols()==1); |
108 | ::new (static_cast<Base*>(this)) Base(expr.data(), expr.size(), 1); |
109 | } |
110 | else |
111 | ::new (static_cast<Base*>(this)) Base(expr.data(), expr.rows(), expr.cols()); |
112 | |
113 | if(Expression::IsVectorAtCompileTime && (!PlainObjectType::IsVectorAtCompileTime) && ((Expression::Flags&RowMajorBit)!=(PlainObjectType::Flags&RowMajorBit))) |
114 | ::new (&m_stride) StrideBase(expr.innerStride(), StrideType::InnerStrideAtCompileTime==0?0:1); |
115 | else |
116 | ::new (&m_stride) StrideBase(StrideType::OuterStrideAtCompileTime==0?0:expr.outerStride(), |
117 | StrideType::InnerStrideAtCompileTime==0?0:expr.innerStride()); |
118 | } |
119 | |
120 | StrideBase m_stride; |
121 | }; |
122 | |
123 | /** \class Ref |
124 | * \ingroup Core_Module |
125 | * |
126 | * \brief A matrix or vector expression mapping an existing expression |
127 | * |
128 | * \tparam PlainObjectType the equivalent matrix type of the mapped data |
129 | * \tparam Options specifies the pointer alignment in bytes. It can be: \c #Aligned128, , \c #Aligned64, \c #Aligned32, \c #Aligned16, \c #Aligned8 or \c #Unaligned. |
130 | * The default is \c #Unaligned. |
131 | * \tparam StrideType optionally specifies strides. By default, Ref implies a contiguous storage along the inner dimension (inner stride==1), |
132 | * but accepts a variable outer stride (leading dimension). |
133 | * This can be overridden by specifying strides. |
134 | * The type passed here must be a specialization of the Stride template, see examples below. |
135 | * |
136 | * This class provides a way to write non-template functions taking Eigen objects as parameters while limiting the number of copies. |
137 | * A Ref<> object can represent either a const expression or a l-value: |
138 | * \code |
139 | * // in-out argument: |
140 | * void foo1(Ref<VectorXf> x); |
141 | * |
142 | * // read-only const argument: |
143 | * void foo2(const Ref<const VectorXf>& x); |
144 | * \endcode |
145 | * |
146 | * In the in-out case, the input argument must satisfy the constraints of the actual Ref<> type, otherwise a compilation issue will be triggered. |
147 | * By default, a Ref<VectorXf> can reference any dense vector expression of float having a contiguous memory layout. |
148 | * Likewise, a Ref<MatrixXf> can reference any column-major dense matrix expression of float whose column's elements are contiguously stored with |
149 | * the possibility to have a constant space in-between each column, i.e. the inner stride must be equal to 1, but the outer stride (or leading dimension) |
150 | * can be greater than the number of rows. |
151 | * |
152 | * In the const case, if the input expression does not match the above requirement, then it is evaluated into a temporary before being passed to the function. |
153 | * Here are some examples: |
154 | * \code |
155 | * MatrixXf A; |
156 | * VectorXf a; |
157 | * foo1(a.head()); // OK |
158 | * foo1(A.col()); // OK |
159 | * foo1(A.row()); // Compilation error because here innerstride!=1 |
160 | * foo2(A.row()); // Compilation error because A.row() is a 1xN object while foo2 is expecting a Nx1 object |
161 | * foo2(A.row().transpose()); // The row is copied into a contiguous temporary |
162 | * foo2(2*a); // The expression is evaluated into a temporary |
163 | * foo2(A.col().segment(2,4)); // No temporary |
164 | * \endcode |
165 | * |
166 | * The range of inputs that can be referenced without temporary can be enlarged using the last two template parameters. |
167 | * Here is an example accepting an innerstride!=1: |
168 | * \code |
169 | * // in-out argument: |
170 | * void foo3(Ref<VectorXf,0,InnerStride<> > x); |
171 | * foo3(A.row()); // OK |
172 | * \endcode |
173 | * The downside here is that the function foo3 might be significantly slower than foo1 because it won't be able to exploit vectorization, and will involve more |
174 | * expensive address computations even if the input is contiguously stored in memory. To overcome this issue, one might propose to overload internally calling a |
175 | * template function, e.g.: |
176 | * \code |
177 | * // in the .h: |
178 | * void foo(const Ref<MatrixXf>& A); |
179 | * void foo(const Ref<MatrixXf,0,Stride<> >& A); |
180 | * |
181 | * // in the .cpp: |
182 | * template<typename TypeOfA> void foo_impl(const TypeOfA& A) { |
183 | * ... // crazy code goes here |
184 | * } |
185 | * void foo(const Ref<MatrixXf>& A) { foo_impl(A); } |
186 | * void foo(const Ref<MatrixXf,0,Stride<> >& A) { foo_impl(A); } |
187 | * \endcode |
188 | * |
189 | * |
190 | * \sa PlainObjectBase::Map(), \ref TopicStorageOrders |
191 | */ |
192 | template<typename PlainObjectType, int Options, typename StrideType> class Ref |
193 | : public RefBase<Ref<PlainObjectType, Options, StrideType> > |
194 | { |
195 | private: |
196 | typedef internal::traits<Ref> Traits; |
197 | template<typename Derived> |
198 | EIGEN_DEVICE_FUNC inline Ref(const PlainObjectBase<Derived>& expr, |
199 | typename internal::enable_if<bool(Traits::template match<Derived>::MatchAtCompileTime),Derived>::type* = 0); |
200 | public: |
201 | |
202 | typedef RefBase<Ref> Base; |
203 | EIGEN_DENSE_PUBLIC_INTERFACE(Ref) |
204 | |
205 | |
206 | #ifndef EIGEN_PARSED_BY_DOXYGEN |
207 | template<typename Derived> |
208 | EIGEN_DEVICE_FUNC inline Ref(PlainObjectBase<Derived>& expr, |
209 | typename internal::enable_if<bool(Traits::template match<Derived>::MatchAtCompileTime),Derived>::type* = 0) |
210 | { |
211 | EIGEN_STATIC_ASSERT(bool(Traits::template match<Derived>::MatchAtCompileTime), STORAGE_LAYOUT_DOES_NOT_MATCH); |
212 | Base::construct(expr.derived()); |
213 | } |
214 | template<typename Derived> |
215 | EIGEN_DEVICE_FUNC inline Ref(const DenseBase<Derived>& expr, |
216 | typename internal::enable_if<bool(Traits::template match<Derived>::MatchAtCompileTime),Derived>::type* = 0) |
217 | #else |
218 | /** Implicit constructor from any dense expression */ |
219 | template<typename Derived> |
220 | inline Ref(DenseBase<Derived>& expr) |
221 | #endif |
222 | { |
223 | EIGEN_STATIC_ASSERT(bool(internal::is_lvalue<Derived>::value), THIS_EXPRESSION_IS_NOT_A_LVALUE__IT_IS_READ_ONLY); |
224 | EIGEN_STATIC_ASSERT(bool(Traits::template match<Derived>::MatchAtCompileTime), STORAGE_LAYOUT_DOES_NOT_MATCH); |
225 | EIGEN_STATIC_ASSERT(!Derived::IsPlainObjectBase,THIS_EXPRESSION_IS_NOT_A_LVALUE__IT_IS_READ_ONLY); |
226 | Base::construct(expr.const_cast_derived()); |
227 | } |
228 | |
229 | EIGEN_INHERIT_ASSIGNMENT_OPERATORS(Ref) |
230 | |
231 | }; |
232 | |
233 | // this is the const ref version |
234 | template<typename TPlainObjectType, int Options, typename StrideType> class Ref<const TPlainObjectType, Options, StrideType> |
235 | : public RefBase<Ref<const TPlainObjectType, Options, StrideType> > |
236 | { |
237 | typedef internal::traits<Ref> Traits; |
238 | public: |
239 | |
240 | typedef RefBase<Ref> Base; |
241 | EIGEN_DENSE_PUBLIC_INTERFACE(Ref) |
242 | |
243 | template<typename Derived> |
244 | EIGEN_DEVICE_FUNC inline Ref(const DenseBase<Derived>& expr, |
245 | typename internal::enable_if<bool(Traits::template match<Derived>::ScalarTypeMatch),Derived>::type* = 0) |
246 | { |
247 | // std::cout << match_helper<Derived>::HasDirectAccess << "," << match_helper<Derived>::OuterStrideMatch << "," << match_helper<Derived>::InnerStrideMatch << "\n"; |
248 | // std::cout << int(StrideType::OuterStrideAtCompileTime) << " - " << int(Derived::OuterStrideAtCompileTime) << "\n"; |
249 | // std::cout << int(StrideType::InnerStrideAtCompileTime) << " - " << int(Derived::InnerStrideAtCompileTime) << "\n"; |
250 | construct(expr.derived(), typename Traits::template match<Derived>::type()); |
251 | } |
252 | |
253 | EIGEN_DEVICE_FUNC inline Ref(const Ref& other) : Base(other) { |
254 | // copy constructor shall not copy the m_object, to avoid unnecessary malloc and copy |
255 | } |
256 | |
257 | template<typename OtherRef> |
258 | EIGEN_DEVICE_FUNC inline Ref(const RefBase<OtherRef>& other) { |
259 | construct(other.derived(), typename Traits::template match<OtherRef>::type()); |
260 | } |
261 | |
262 | protected: |
263 | |
264 | template<typename Expression> |
265 | EIGEN_DEVICE_FUNC void construct(const Expression& expr,internal::true_type) |
266 | { |
267 | Base::construct(expr); |
268 | } |
269 | |
270 | template<typename Expression> |
271 | EIGEN_DEVICE_FUNC void construct(const Expression& expr, internal::false_type) |
272 | { |
273 | internal::call_assignment_no_alias(m_object,expr,internal::assign_op<Scalar,Scalar>()); |
274 | Base::construct(m_object); |
275 | } |
276 | |
277 | protected: |
278 | TPlainObjectType m_object; |
279 | }; |
280 | |
281 | } // end namespace Eigen |
282 | |
283 | #endif // EIGEN_REF_H |
284 | |