Medial Code Documentation
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
85namespace internal {
86
87template<typename _PlainObjectType, int _Options, typename _StrideType>
89 : public traits<Map<_PlainObjectType, _Options, _StrideType> >
90{
92 typedef _StrideType StrideType;
93 enum {
94 Options = _Options,
95 Flags = traits<Map<_PlainObjectType, _Options, _StrideType> >::Flags | NestByRefBit,
97 };
98
99 template<typename Derived> struct match {
100 enum {
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
110 MatchAtCompileTime = HasDirectAccess && StorageOrderMatch && InnerStrideMatch && OuterStrideMatch && AlignmentMatch && ScalarTypeMatch
111 };
113 };
114
115};
116
117template<typename Derived>
118struct traits<RefBase<Derived> > : public traits<Derived> {};
119
120}
121
122template<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
128public:
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
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
155protected:
156
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
186template<typename PlainObjectType, int Options, typename StrideType> class Ref
187 : public RefBase<Ref<PlainObjectType, Options, StrideType> >
188{
189 private:
191 template<typename Derived>
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>
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
227template<typename TPlainObjectType, int Options, typename StrideType> class Ref<const TPlainObjectType, Options, StrideType>
228 : public RefBase<Ref<const TPlainObjectType, Options, StrideType> >
229{
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
Base class for Map and Block expression with direct access.
Definition ForwardDeclarations.h:117
Definition Ref.h:124
A matrix or vector expression mapping an existing expression.
Definition Ref.h:188
Pseudo expression representing a solving operation.
Definition Solve.h:63
EIGEN_DEVICE_FUNC Index inner() const
Definition Stride.h:80
EIGEN_DEVICE_FUNC Index outer() const
Definition Stride.h:77
@ Unaligned
Data pointer has no specific alignment.
Definition Constants.h:228
const unsigned int RowMajorBit
for a matrix, this means that the storage order is row-major.
Definition Constants.h:61
Definition AssignmentFunctors.h:21
Definition Meta.h:131
Definition CoreEvaluators.h:82
Definition Meta.h:31
Definition ForwardDeclarations.h:26
Definition XprHelper.h:628
Definition Meta.h:39
Definition ForwardDeclarations.h:17
Definition Meta.h:30