10#ifndef EIGEN_VISITOR_H
11#define EIGEN_VISITOR_H
17template<
typename Visitor,
typename Derived,
int UnrollCount>
21 col = (UnrollCount-1) / Derived::RowsAtCompileTime,
22 row = (UnrollCount-1) % Derived::RowsAtCompileTime
26 static inline void run(
const Derived &mat, Visitor& visitor)
28 visitor_impl<Visitor, Derived, UnrollCount-1>::run(mat, visitor);
29 visitor(mat.coeff(row, col), row, col);
33template<
typename Visitor,
typename Derived>
34struct visitor_impl<Visitor, Derived, 1>
37 static inline void run(
const Derived &mat, Visitor& visitor)
39 return visitor.init(mat.coeff(0, 0), 0, 0);
44template<
typename Visitor,
typename Derived>
45struct visitor_impl<Visitor, Derived, 0> {
47 static inline void run(
const Derived &, Visitor& )
51template<
typename Visitor,
typename Derived>
52struct visitor_impl<Visitor, Derived,
Dynamic>
55 static inline void run(
const Derived& mat, Visitor& visitor)
57 visitor.init(mat.coeff(0,0), 0, 0);
58 for(
Index i = 1; i < mat.rows(); ++i)
59 visitor(mat.coeff(i, 0), i, 0);
60 for(
Index j = 1; j < mat.cols(); ++j)
61 for(
Index i = 0; i < mat.rows(); ++i)
62 visitor(mat.coeff(i, j), i, j);
67template<
typename XprType>
68class visitor_evaluator
72 explicit visitor_evaluator(
const XprType &xpr) : m_evaluator(xpr), m_xpr(xpr) {}
74 typedef typename XprType::Scalar Scalar;
75 typedef typename XprType::CoeffReturnType CoeffReturnType;
78 RowsAtCompileTime = XprType::RowsAtCompileTime,
79 CoeffReadCost = internal::evaluator<XprType>::CoeffReadCost
82 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR
Index rows() const EIGEN_NOEXCEPT {
return m_xpr.rows(); }
83 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR
Index cols() const EIGEN_NOEXCEPT {
return m_xpr.cols(); }
84 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR
Index size() const EIGEN_NOEXCEPT {
return m_xpr.size(); }
86 EIGEN_DEVICE_FUNC CoeffReturnType coeff(
Index row,
Index col)
const
87 {
return m_evaluator.coeff(row, col); }
90 internal::evaluator<XprType> m_evaluator;
114template<
typename Derived>
115template<
typename Visitor>
122 typedef typename internal::visitor_evaluator<Derived> ThisEvaluator;
123 ThisEvaluator thisEval(derived());
126 unroll = SizeAtCompileTime !=
Dynamic
127 && SizeAtCompileTime * int(ThisEvaluator::CoeffReadCost) + (SizeAtCompileTime-1) *
int(internal::functor_traits<Visitor>::Cost) <= EIGEN_UNROLLING_LIMIT
129 return internal::visitor_impl<Visitor, ThisEvaluator, unroll ? int(SizeAtCompileTime) :
Dynamic>::run(thisEval, visitor);
137template <
typename Derived>
142 coeff_visitor() : row(-1), col(-1), res(0) {}
143 typedef typename Derived::Scalar Scalar;
147 inline void init(
const Scalar& value,
Index i,
Index j)
160template <
typename Derived,
int NaNPropagation>
161struct min_coeff_visitor : coeff_visitor<Derived>
163 typedef typename Derived::Scalar Scalar;
165 void operator() (
const Scalar& value,
Index i,
Index j)
167 if(value < this->res)
176template <
typename Derived>
179 typedef typename Derived::Scalar Scalar;
181 void operator() (
const Scalar& value,
Index i,
Index j)
183 if((numext::isnan)(this->res) || (!(numext::isnan)(value) && value < this->res))
192template <
typename Derived>
193struct min_coeff_visitor<Derived,
PropagateNaN> : coeff_visitor<Derived>
195 typedef typename Derived::Scalar Scalar;
197 void operator() (
const Scalar& value,
Index i,
Index j)
199 if((numext::isnan)(value) || value < this->res)
208template<
typename Scalar,
int NaNPropagation>
209 struct functor_traits<min_coeff_visitor<Scalar, NaNPropagation> > {
211 Cost = NumTraits<Scalar>::AddCost
220template <
typename Derived,
int NaNPropagation>
221struct max_coeff_visitor : coeff_visitor<Derived>
223 typedef typename Derived::Scalar Scalar;
225 void operator() (
const Scalar& value,
Index i,
Index j)
227 if(value > this->res)
236template <
typename Derived>
239 typedef typename Derived::Scalar Scalar;
241 void operator() (
const Scalar& value,
Index i,
Index j)
243 if((numext::isnan)(this->res) || (!(numext::isnan)(value) && value > this->res))
252template <
typename Derived>
253struct max_coeff_visitor<Derived,
PropagateNaN> : coeff_visitor<Derived>
255 typedef typename Derived::Scalar Scalar;
257 void operator() (
const Scalar& value,
Index i,
Index j)
259 if((numext::isnan)(value) || value > this->res)
268template<
typename Scalar,
int NaNPropagation>
269struct functor_traits<max_coeff_visitor<Scalar, NaNPropagation> > {
271 Cost = NumTraits<Scalar>::AddCost
288template<
typename Derived>
289template<
int NaNPropagation,
typename IndexType>
291typename internal::traits<Derived>::Scalar
294 eigen_assert(this->rows()>0 && this->cols()>0 &&
"you are using an empty matrix");
296 internal::min_coeff_visitor<Derived, NaNPropagation> minVisitor;
297 this->visit(minVisitor);
298 *rowId = minVisitor.row;
299 if (colId) *colId = minVisitor.col;
300 return minVisitor.res;
313template<
typename Derived>
314template<
int NaNPropagation,
typename IndexType>
316typename internal::traits<Derived>::Scalar
319 eigen_assert(this->rows()>0 && this->cols()>0 &&
"you are using an empty matrix");
321 EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
322 internal::min_coeff_visitor<Derived, NaNPropagation> minVisitor;
323 this->visit(minVisitor);
324 *index = IndexType((RowsAtCompileTime==1) ? minVisitor.col : minVisitor.row);
325 return minVisitor.res;
339template<
typename Derived>
340template<
int NaNPropagation,
typename IndexType>
342typename internal::traits<Derived>::Scalar
345 eigen_assert(this->rows()>0 && this->cols()>0 &&
"you are using an empty matrix");
347 internal::max_coeff_visitor<Derived, NaNPropagation> maxVisitor;
348 this->visit(maxVisitor);
349 *rowPtr = maxVisitor.row;
350 if (colPtr) *colPtr = maxVisitor.col;
351 return maxVisitor.res;
364template<
typename Derived>
365template<
int NaNPropagation,
typename IndexType>
367typename internal::traits<Derived>::Scalar
370 eigen_assert(this->rows()>0 && this->cols()>0 &&
"you are using an empty matrix");
372 EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
373 internal::max_coeff_visitor<Derived, NaNPropagation> maxVisitor;
374 this->visit(maxVisitor);
375 *index = (RowsAtCompileTime==1) ? maxVisitor.col : maxVisitor.row;
376 return maxVisitor.res;
Base class for all dense matrices, vectors, and arrays.
Definition: DenseBase.h:47
@ PropagateNaN
Definition: Constants.h:343
@ PropagateNumbers
Definition: Constants.h:345
Namespace containing all symbols from the Eigen library.
Definition: Core:141
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:74
const int Dynamic
Definition: Constants.h:22