add writeable IndexedView

This commit is contained in:
Gael Guennebaud 2017-01-10 17:10:35 +01:00
parent c9d5e5c6da
commit d072fc4b14
3 changed files with 81 additions and 7 deletions

View File

@ -558,27 +558,27 @@ template<typename Derived> class DenseBase
EIGEN_DEVICE_FUNC void reverseInPlace();
template<typename RowIndices, typename ColIndices>
struct IndexedViewType {
struct ConstIndexedViewType {
typedef IndexedView<const Derived,typename internal::MakeIndexing<RowIndices>::type,typename internal::MakeIndexing<ColIndices>::type> type;
};
template<typename RowIndices, typename ColIndices>
typename internal::enable_if<
! (internal::traits<typename IndexedViewType<RowIndices,ColIndices>::type>::IsBlockAlike
! (internal::traits<typename ConstIndexedViewType<RowIndices,ColIndices>::type>::IsBlockAlike
|| (internal::is_integral<RowIndices>::value && internal::is_integral<ColIndices>::value)),
typename IndexedViewType<RowIndices,ColIndices>::type >::type
typename ConstIndexedViewType<RowIndices,ColIndices>::type >::type
operator()(const RowIndices& rowIndices, const ColIndices& colIndices) const {
return typename IndexedViewType<RowIndices,ColIndices>::type(
return typename ConstIndexedViewType<RowIndices,ColIndices>::type(
derived(), internal::make_indexing(rowIndices,derived().rows()), internal::make_indexing(colIndices,derived().cols()));
}
template<typename RowIndices, typename ColIndices>
typename internal::enable_if<
internal::traits<typename IndexedViewType<RowIndices,ColIndices>::type>::IsBlockAlike
internal::traits<typename ConstIndexedViewType<RowIndices,ColIndices>::type>::IsBlockAlike
&& !(internal::is_integral<RowIndices>::value && internal::is_integral<ColIndices>::value),
typename internal::traits<typename IndexedViewType<RowIndices,ColIndices>::type>::BlockType>::type
typename internal::traits<typename ConstIndexedViewType<RowIndices,ColIndices>::type>::BlockType>::type
operator()(const RowIndices& rowIndices, const ColIndices& colIndices) const {
typedef typename internal::traits<typename IndexedViewType<RowIndices,ColIndices>::type>::BlockType BlockType;
typedef typename internal::traits<typename ConstIndexedViewType<RowIndices,ColIndices>::type>::BlockType BlockType;
typename internal::MakeIndexing<RowIndices>::type actualRowIndices = internal::make_indexing(rowIndices,derived().rows());
typename internal::MakeIndexing<ColIndices>::type actualColIndices = internal::make_indexing(colIndices,derived().cols());
return BlockType(derived(),
@ -609,6 +609,58 @@ template<typename Derived> class DenseBase
derived(), rowIndices, colIndices);
}
template<typename RowIndices, typename ColIndices>
struct IndexedViewType {
typedef IndexedView<Derived,typename internal::MakeIndexing<RowIndices>::type,typename internal::MakeIndexing<ColIndices>::type> type;
};
template<typename RowIndices, typename ColIndices>
typename internal::enable_if<
! (internal::traits<typename IndexedViewType<RowIndices,ColIndices>::type>::IsBlockAlike
|| (internal::is_integral<RowIndices>::value && internal::is_integral<ColIndices>::value)),
typename IndexedViewType<RowIndices,ColIndices>::type >::type
operator()(const RowIndices& rowIndices, const ColIndices& colIndices) {
return typename IndexedViewType<RowIndices,ColIndices>::type(
derived(), internal::make_indexing(rowIndices,derived().rows()), internal::make_indexing(colIndices,derived().cols()));
}
template<typename RowIndices, typename ColIndices>
typename internal::enable_if<
internal::traits<typename IndexedViewType<RowIndices,ColIndices>::type>::IsBlockAlike
&& !(internal::is_integral<RowIndices>::value && internal::is_integral<ColIndices>::value),
typename internal::traits<typename IndexedViewType<RowIndices,ColIndices>::type>::BlockType>::type
operator()(const RowIndices& rowIndices, const ColIndices& colIndices) {
typedef typename internal::traits<typename IndexedViewType<RowIndices,ColIndices>::type>::BlockType BlockType;
typename internal::MakeIndexing<RowIndices>::type actualRowIndices = internal::make_indexing(rowIndices,derived().rows());
typename internal::MakeIndexing<ColIndices>::type actualColIndices = internal::make_indexing(colIndices,derived().cols());
return BlockType(derived(),
internal::first(actualRowIndices),
internal::first(actualColIndices),
internal::size(actualRowIndices),
internal::size(actualColIndices));
}
template<typename RowIndicesT, std::size_t RowIndicesN, typename ColIndices>
IndexedView<Derived,const RowIndicesT (&)[RowIndicesN],typename internal::MakeIndexing<ColIndices>::type>
operator()(const RowIndicesT (&rowIndices)[RowIndicesN], const ColIndices& colIndices) {
return IndexedView<Derived,const RowIndicesT (&)[RowIndicesN],typename internal::MakeIndexing<ColIndices>::type>(
derived(), rowIndices, internal::make_indexing(colIndices,derived().cols()));
}
template<typename RowIndices, typename ColIndicesT, std::size_t ColIndicesN>
IndexedView<Derived,typename internal::MakeIndexing<RowIndices>::type, const ColIndicesT (&)[ColIndicesN]>
operator()(const RowIndices& rowIndices, const ColIndicesT (&colIndices)[ColIndicesN]) {
return IndexedView<Derived,typename internal::MakeIndexing<RowIndices>::type,const ColIndicesT (&)[ColIndicesN]>(
derived(), internal::make_indexing(rowIndices,derived().rows()), colIndices);
}
template<typename RowIndicesT, std::size_t RowIndicesN, typename ColIndicesT, std::size_t ColIndicesN>
IndexedView<Derived,const RowIndicesT (&)[RowIndicesN], const ColIndicesT (&)[ColIndicesN]>
operator()(const RowIndicesT (&rowIndices)[RowIndicesN], const ColIndicesT (&colIndices)[ColIndicesN]) {
return IndexedView<Derived,const RowIndicesT (&)[RowIndicesN],const ColIndicesT (&)[ColIndicesN]>(
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)

View File

@ -104,6 +104,7 @@ class IndexedView : public IndexedViewImpl<XprType, RowIndices, ColIndices, type
public:
typedef typename IndexedViewImpl<XprType, RowIndices, ColIndices, typename internal::traits<XprType>::StorageKind>::Base Base;
EIGEN_GENERIC_PUBLIC_INTERFACE(IndexedView)
EIGEN_INHERIT_ASSIGNMENT_OPERATORS(IndexedView)
typedef typename internal::ref_selector<XprType>::non_const_type MatrixTypeNested;
typedef typename internal::remove_all<XprType>::type NestedExpression;
@ -180,6 +181,12 @@ struct unary_evaluator<IndexedView<ArgType, RowIndices, ColIndices>, IndexBased>
return m_argImpl.coeff(m_xpr.rowIndices()[row], m_xpr.colIndices()[col]);
}
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
Scalar& coeffRef(Index row, Index col)
{
return m_argImpl.coeffRef(m_xpr.rowIndices()[row], m_xpr.colIndices()[col]);
}
protected:
evaluator<ArgType> m_argImpl;

View File

@ -168,6 +168,12 @@ void check_indexed_view()
// Check fall-back to Block
{
VERIFY( is_same_type(A.col(0), A(all,0)) );
VERIFY( is_same_type(A.row(0), A(0,all)) );
VERIFY( is_same_type(A.block(0,0,2,2), A(seqN(0,2),seq(0,1))) );
VERIFY( is_same_type(A.middleRows(2,4), A(seqN(2,4),all)) );
VERIFY( is_same_type(A.middleCols(2,4), A(all,seqN(2,4))) );
const ArrayXXi& cA(A);
VERIFY( is_same_type(cA.col(0), cA(all,0)) );
VERIFY( is_same_type(cA.row(0), cA(0,all)) );
@ -176,6 +182,15 @@ void check_indexed_view()
VERIFY( is_same_type(cA.middleCols(2,4), cA(all,seqN(2,4))) );
}
ArrayXXi A1=A, A2 = ArrayXXi::Random(4,4);
ArrayXi range25(4); range25 << 3,2,4,5;
A1(seqN(3,4),seq(2,5)) = A2;
VERIFY_IS_APPROX( A1.block(3,2,4,4), A2 );
A1 = A;
A2.setOnes();
A1(seq(6,3,-1),range25) = A2;
VERIFY_IS_APPROX( A1.block(3,2,4,4), A2 );
#if EIGEN_HAS_CXX11
VERIFY( (A(all, std::array<int,4>{{1,3,2,4}})).ColsAtCompileTime == 4);