From 6a265063417db175eb436e6bcc0e3e74780a1b2f Mon Sep 17 00:00:00 2001 From: Gael Guennebaud Date: Wed, 4 Mar 2009 13:00:00 +0000 Subject: [PATCH] add ReturnByValue pseudo expression for in-place evaluation with a return-by-value API style (will soon use it for the transform products) --- Eigen/Core | 1 + Eigen/src/Core/Matrix.h | 10 +++- Eigen/src/Core/MatrixBase.h | 3 ++ Eigen/src/Core/ReturnByValue.h | 63 +++++++++++++++++++++++ Eigen/src/Core/util/ForwardDeclarations.h | 1 + 5 files changed, 77 insertions(+), 1 deletion(-) create mode 100644 Eigen/src/Core/ReturnByValue.h diff --git a/Eigen/Core b/Eigen/Core index 1e38ecf69..e75b90931 100644 --- a/Eigen/Core +++ b/Eigen/Core @@ -121,6 +121,7 @@ namespace Eigen { #include "src/Core/MatrixStorage.h" #include "src/Core/NestByValue.h" +#include "src/Core/ReturnByValue.h" #include "src/Core/Flagged.h" #include "src/Core/Matrix.h" #include "src/Core/Cwise.h" diff --git a/Eigen/src/Core/Matrix.h b/Eigen/src/Core/Matrix.h index 0b172e97c..fbd55e526 100644 --- a/Eigen/src/Core/Matrix.h +++ b/Eigen/src/Core/Matrix.h @@ -289,9 +289,13 @@ class Matrix */ EIGEN_STRONG_INLINE Matrix& operator=(const Matrix& other) { - return _set(other); + return _set(other); } + template + EIGEN_STRONG_INLINE Matrix& operator=(const ReturnByValue& func) + { return Base::operator=(func); } + EIGEN_INHERIT_ASSIGNMENT_OPERATOR(Matrix, +=) EIGEN_INHERIT_ASSIGNMENT_OPERATOR(Matrix, -=) EIGEN_INHERIT_SCALAR_ASSIGNMENT_OPERATOR(Matrix, *=) @@ -412,6 +416,10 @@ class Matrix _check_template_params(); _set_noalias(other); } + /** Copy constructor with in-place evaluation */ + template + EIGEN_STRONG_INLINE Matrix(const ReturnByValue& other) + { other.evalTo(*this); } /** Destructor */ inline ~Matrix() {} diff --git a/Eigen/src/Core/MatrixBase.h b/Eigen/src/Core/MatrixBase.h index 654296489..3a7f1a3e0 100644 --- a/Eigen/src/Core/MatrixBase.h +++ b/Eigen/src/Core/MatrixBase.h @@ -630,6 +630,9 @@ template class MatrixBase template Derived& lazyAssign(const SparseProduct& product); + template + Derived& operator=(const ReturnByValue& func); + #ifdef EIGEN_MATRIXBASE_PLUGIN #include EIGEN_MATRIXBASE_PLUGIN #endif diff --git a/Eigen/src/Core/ReturnByValue.h b/Eigen/src/Core/ReturnByValue.h new file mode 100644 index 000000000..e6d2b0d01 --- /dev/null +++ b/Eigen/src/Core/ReturnByValue.h @@ -0,0 +1,63 @@ +// This file is part of Eigen, a lightweight C++ template library +// for linear algebra. Eigen itself is part of the KDE project. +// +// Copyright (C) 2009 Gael Guennebaud +// +// Eigen is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 3 of the License, or (at your option) any later version. +// +// Alternatively, you can redistribute it and/or +// modify it under the terms of the GNU General Public License as +// published by the Free Software Foundation; either version 2 of +// the License, or (at your option) any later version. +// +// Eigen is distributed in the hope that it will be useful, but WITHOUT ANY +// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +// FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License and a copy of the GNU General Public License along with +// Eigen. If not, see . + +#ifndef EIGEN_RETURNBYVALUE_H +#define EIGEN_RETURNBYVALUE_H + +/** \class ReturnByValue + * + */ +template +struct ei_traits > : public ei_traits +{ + enum { + Flags = ei_traits::Flags | EvalBeforeNestingBit + }; +}; + +template +struct ei_nested, n, EvalType> +{ + typedef EvalType type; +}; + +template class ReturnByValue + : public MatrixBase > +{ + public: + EIGEN_GENERIC_PUBLIC_INTERFACE(ReturnByValue) + template + inline void evalTo(Dest& dst) const + { static_cast(this)->evalTo(dst); } +}; + +template +template +Derived& MatrixBase::operator=(const ReturnByValue& other) +{ + other.evalTo(derived()); + return derived(); +} + +#endif // EIGEN_RETURNBYVALUE_H diff --git a/Eigen/src/Core/util/ForwardDeclarations.h b/Eigen/src/Core/util/ForwardDeclarations.h index 46faf1e48..3dc6762f8 100644 --- a/Eigen/src/Core/util/ForwardDeclarations.h +++ b/Eigen/src/Core/util/ForwardDeclarations.h @@ -56,6 +56,7 @@ template class Extract; template class Cwise; template class WithFormat; template struct CommaInitializer; +template class ReturnByValue; template struct ei_product_mode;