From 17eac60446650dc31bde7156a4febe504ab347b7 Mon Sep 17 00:00:00 2001 From: Gael Guennebaud Date: Tue, 10 Jan 2017 21:45:55 +0100 Subject: [PATCH] Factorize const and non-const version of the generic operator() method. --- Eigen/src/Core/DenseBase.h | 105 +------------------------ Eigen/src/Core/IndexedView.h | 4 +- Eigen/src/plugins/IndexedViewMethods.h | 81 +++++++++++++++++++ 3 files changed, 84 insertions(+), 106 deletions(-) create mode 100644 Eigen/src/plugins/IndexedViewMethods.h diff --git a/Eigen/src/Core/DenseBase.h b/Eigen/src/Core/DenseBase.h index 909fa0f12..a8229cf03 100644 --- a/Eigen/src/Core/DenseBase.h +++ b/Eigen/src/Core/DenseBase.h @@ -557,116 +557,13 @@ template class DenseBase } EIGEN_DEVICE_FUNC void reverseInPlace(); - template - struct ConstIndexedViewType { - typedef IndexedView::type,typename internal::MakeIndexing::type> type; - }; - - template - typename internal::enable_if< - ! (internal::traits::type>::IsBlockAlike - || (internal::is_integral::value && internal::is_integral::value)), - typename ConstIndexedViewType::type >::type - operator()(const RowIndices& rowIndices, const ColIndices& colIndices) const { - return typename ConstIndexedViewType::type( - derived(), internal::make_indexing(rowIndices,derived().rows()), internal::make_indexing(colIndices,derived().cols())); - } - - template - typename internal::enable_if< - internal::traits::type>::IsBlockAlike - && !(internal::is_integral::value && internal::is_integral::value), - typename internal::traits::type>::BlockType>::type - operator()(const RowIndices& rowIndices, const ColIndices& colIndices) const { - typedef typename internal::traits::type>::BlockType BlockType; - typename internal::MakeIndexing::type actualRowIndices = internal::make_indexing(rowIndices,derived().rows()); - typename internal::MakeIndexing::type actualColIndices = internal::make_indexing(colIndices,derived().cols()); - return BlockType(derived(), - internal::first(actualRowIndices), - internal::first(actualColIndices), - internal::size(actualRowIndices), - internal::size(actualColIndices)); - } - - template - IndexedView::type> - operator()(const RowIndicesT (&rowIndices)[RowIndicesN], const ColIndices& colIndices) const { - return IndexedView::type>( - derived(), rowIndices, internal::make_indexing(colIndices,derived().cols())); - } - - template - IndexedView::type, const ColIndicesT (&)[ColIndicesN]> - operator()(const RowIndices& rowIndices, const ColIndicesT (&colIndices)[ColIndicesN]) const { - return IndexedView::type,const ColIndicesT (&)[ColIndicesN]>( - derived(), internal::make_indexing(rowIndices,derived().rows()), colIndices); - } - - template - IndexedView - operator()(const RowIndicesT (&rowIndices)[RowIndicesN], const ColIndicesT (&colIndices)[ColIndicesN]) const { - return IndexedView( - derived(), rowIndices, colIndices); - } - - template - struct IndexedViewType { - typedef IndexedView::type,typename internal::MakeIndexing::type> type; - }; - - template - typename internal::enable_if< - ! (internal::traits::type>::IsBlockAlike - || (internal::is_integral::value && internal::is_integral::value)), - typename IndexedViewType::type >::type - operator()(const RowIndices& rowIndices, const ColIndices& colIndices) { - return typename IndexedViewType::type( - derived(), internal::make_indexing(rowIndices,derived().rows()), internal::make_indexing(colIndices,derived().cols())); - } - - template - typename internal::enable_if< - internal::traits::type>::IsBlockAlike - && !(internal::is_integral::value && internal::is_integral::value), - typename internal::traits::type>::BlockType>::type - operator()(const RowIndices& rowIndices, const ColIndices& colIndices) { - typedef typename internal::traits::type>::BlockType BlockType; - typename internal::MakeIndexing::type actualRowIndices = internal::make_indexing(rowIndices,derived().rows()); - typename internal::MakeIndexing::type actualColIndices = internal::make_indexing(colIndices,derived().cols()); - return BlockType(derived(), - internal::first(actualRowIndices), - internal::first(actualColIndices), - internal::size(actualRowIndices), - internal::size(actualColIndices)); - } - - template - IndexedView::type> - operator()(const RowIndicesT (&rowIndices)[RowIndicesN], const ColIndices& colIndices) { - return IndexedView::type>( - derived(), rowIndices, internal::make_indexing(colIndices,derived().cols())); - } - - template - IndexedView::type, const ColIndicesT (&)[ColIndicesN]> - operator()(const RowIndices& rowIndices, const ColIndicesT (&colIndices)[ColIndicesN]) { - return IndexedView::type,const ColIndicesT (&)[ColIndicesN]>( - derived(), internal::make_indexing(rowIndices,derived().rows()), colIndices); - } - - template - IndexedView - operator()(const RowIndicesT (&rowIndices)[RowIndicesN], const ColIndicesT (&colIndices)[ColIndicesN]) { - return IndexedView( - derived(), rowIndices, colIndices); - } - #define EIGEN_CURRENT_STORAGE_BASE_CLASS Eigen::DenseBase #define EIGEN_DOC_BLOCK_ADDONS_NOT_INNER_PANEL #define EIGEN_DOC_BLOCK_ADDONS_INNER_PANEL_IF(COND) #define EIGEN_DOC_UNARY_ADDONS(X,Y) # include "../plugins/CommonCwiseUnaryOps.h" # include "../plugins/BlockMethods.h" +# include "../plugins/IndexedViewMethods.h" # ifdef EIGEN_DENSEBASE_PLUGIN # include EIGEN_DENSEBASE_PLUGIN # endif diff --git a/Eigen/src/Core/IndexedView.h b/Eigen/src/Core/IndexedView.h index 81ff53758..269882e78 100644 --- a/Eigen/src/Core/IndexedView.h +++ b/Eigen/src/Core/IndexedView.h @@ -71,8 +71,8 @@ class IndexedViewImpl; * \tparam ColIndices the type of the object defining the sequence of column indices * * This class represents an expression of a sub-matrix (or sub-vector) defined as the intersection - * of sub-sets of rows and columns, that are themself defined by generic sequences of row indices \f${r_0,r_1,..r_{m-1}\f$ - * and column indices \f${c_0,c_1,..c_{n-1}\f$. Let \f$ A \f$ be the nested matrix, then the resulting matrix \f$ B \f$ has \c m + * of sub-sets of rows and columns, that are themself defined by generic sequences of row indices \f$ \{r_0,r_1,..r_{m-1}\} \f$ + * and column indices \f$ \{c_0,c_1,..c_{n-1} \}\f$. Let \f$ A \f$ be the nested matrix, then the resulting matrix \f$ B \f$ has \c m * rows and \c n columns, and its entries are given by: \f$ B(i,j) = A(r_i,c_j) \f$. * * The \c RowIndices and \c ColIndices types must be compatible with the following API: diff --git a/Eigen/src/plugins/IndexedViewMethods.h b/Eigen/src/plugins/IndexedViewMethods.h new file mode 100644 index 000000000..53eff093c --- /dev/null +++ b/Eigen/src/plugins/IndexedViewMethods.h @@ -0,0 +1,81 @@ +// This file is part of Eigen, a lightweight C++ template library +// for linear algebra. +// +// Copyright (C) 2017 Gael Guennebaud +// +// This Source Code Form is subject to the terms of the Mozilla +// Public License v. 2.0. If a copy of the MPL was not distributed +// with this file, You can obtain one at http://mozilla.org/MPL/2.0/. + + +// This file is automatically included twice to generate const and non-const versions + +#ifndef EIGEN_INDEXED_VIEW_METHOD_2ND_PASS +#define EIGEN_INDEXED_VIEW_METHOD_CONST const +#define EIGEN_INDEXED_VIEW_METHOD_TYPE ConstIndexedViewType +#else +#define EIGEN_INDEXED_VIEW_METHOD_CONST +#define EIGEN_INDEXED_VIEW_METHOD_TYPE IndexedViewType +#endif + +template +struct EIGEN_INDEXED_VIEW_METHOD_TYPE { + typedef IndexedView::type,typename internal::MakeIndexing::type> type; +}; + +template +typename internal::enable_if< + ! (internal::traits::type>::IsBlockAlike + || (internal::is_integral::value && internal::is_integral::value)), + typename EIGEN_INDEXED_VIEW_METHOD_TYPE::type >::type +operator()(const RowIndices& rowIndices, const ColIndices& colIndices) EIGEN_INDEXED_VIEW_METHOD_CONST { + return typename EIGEN_INDEXED_VIEW_METHOD_TYPE::type( + derived(), internal::make_indexing(rowIndices,derived().rows()), internal::make_indexing(colIndices,derived().cols())); +} + +template +typename internal::enable_if< + internal::traits::type>::IsBlockAlike + && !(internal::is_integral::value && internal::is_integral::value), + typename internal::traits::type>::BlockType>::type +operator()(const RowIndices& rowIndices, const ColIndices& colIndices) EIGEN_INDEXED_VIEW_METHOD_CONST { + typedef typename internal::traits::type>::BlockType BlockType; + typename internal::MakeIndexing::type actualRowIndices = internal::make_indexing(rowIndices,derived().rows()); + typename internal::MakeIndexing::type actualColIndices = internal::make_indexing(colIndices,derived().cols()); + return BlockType(derived(), + internal::first(actualRowIndices), + internal::first(actualColIndices), + internal::size(actualRowIndices), + internal::size(actualColIndices)); +} + +template +IndexedView::type> +operator()(const RowIndicesT (&rowIndices)[RowIndicesN], const ColIndices& colIndices) EIGEN_INDEXED_VIEW_METHOD_CONST { + return IndexedView::type>( + derived(), rowIndices, internal::make_indexing(colIndices,derived().cols())); +} + +template +IndexedView::type, const ColIndicesT (&)[ColIndicesN]> +operator()(const RowIndices& rowIndices, const ColIndicesT (&colIndices)[ColIndicesN]) EIGEN_INDEXED_VIEW_METHOD_CONST { + return IndexedView::type,const ColIndicesT (&)[ColIndicesN]>( + derived(), internal::make_indexing(rowIndices,derived().rows()), colIndices); +} + +template +IndexedView +operator()(const RowIndicesT (&rowIndices)[RowIndicesN], const ColIndicesT (&colIndices)[ColIndicesN]) EIGEN_INDEXED_VIEW_METHOD_CONST { + return IndexedView( + derived(), rowIndices, colIndices); +} + +#undef EIGEN_INDEXED_VIEW_METHOD_CONST +#undef EIGEN_INDEXED_VIEW_METHOD_TYPE + +#ifndef EIGEN_INDEXED_VIEW_METHOD_2ND_PASS +#define EIGEN_INDEXED_VIEW_METHOD_2ND_PASS +#include "IndexedViewMethods.h" +#undef EIGEN_INDEXED_VIEW_METHOD_2ND_PASS +#endif +