Block.h
Go to the documentation of this file.
00001 // This file is part of Eigen, a lightweight C++ template library
00002 // for linear algebra.
00003 //
00004 // Copyright (C) 2008 Gael Guennebaud <gael.guennebaud@inria.fr>
00005 // Copyright (C) 2006-2010 Benoit Jacob <jacob.benoit.1@gmail.com>
00006 //
00007 // This Source Code Form is subject to the terms of the Mozilla
00008 // Public License v. 2.0. If a copy of the MPL was not distributed
00009 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
00010 
00011 #ifndef EIGEN_BLOCK_H
00012 #define EIGEN_BLOCK_H
00013 
00014 namespace Eigen { 
00015 
00049 namespace internal {
00050 template<typename XprType, int BlockRows, int BlockCols, bool InnerPanel, bool HasDirectAccess>
00051 struct traits<Block<XprType, BlockRows, BlockCols, InnerPanel, HasDirectAccess> > : traits<XprType>
00052 {
00053   typedef typename traits<XprType>::Scalar Scalar;
00054   typedef typename traits<XprType>::StorageKind StorageKind;
00055   typedef typename traits<XprType>::XprKind XprKind;
00056   typedef typename nested<XprType>::type XprTypeNested;
00057   typedef typename remove_reference<XprTypeNested>::type _XprTypeNested;
00058   enum{
00059     MatrixRows = traits<XprType>::RowsAtCompileTime,
00060     MatrixCols = traits<XprType>::ColsAtCompileTime,
00061     RowsAtCompileTime = MatrixRows == 0 ? 0 : BlockRows,
00062     ColsAtCompileTime = MatrixCols == 0 ? 0 : BlockCols,
00063     MaxRowsAtCompileTime = BlockRows==0 ? 0
00064                          : RowsAtCompileTime != Dynamic ? int(RowsAtCompileTime)
00065                          : int(traits<XprType>::MaxRowsAtCompileTime),
00066     MaxColsAtCompileTime = BlockCols==0 ? 0
00067                          : ColsAtCompileTime != Dynamic ? int(ColsAtCompileTime)
00068                          : int(traits<XprType>::MaxColsAtCompileTime),
00069     XprTypeIsRowMajor = (int(traits<XprType>::Flags)&RowMajorBit) != 0,
00070     IsRowMajor = (MaxRowsAtCompileTime==1&&MaxColsAtCompileTime!=1) ? 1
00071                : (MaxColsAtCompileTime==1&&MaxRowsAtCompileTime!=1) ? 0
00072                : XprTypeIsRowMajor,
00073     HasSameStorageOrderAsXprType = (IsRowMajor == XprTypeIsRowMajor),
00074     InnerSize = IsRowMajor ? int(ColsAtCompileTime) : int(RowsAtCompileTime),
00075     InnerStrideAtCompileTime = HasSameStorageOrderAsXprType
00076                              ? int(inner_stride_at_compile_time<XprType>::ret)
00077                              : int(outer_stride_at_compile_time<XprType>::ret),
00078     OuterStrideAtCompileTime = HasSameStorageOrderAsXprType
00079                              ? int(outer_stride_at_compile_time<XprType>::ret)
00080                              : int(inner_stride_at_compile_time<XprType>::ret),
00081     MaskPacketAccessBit = (InnerSize == Dynamic || (InnerSize % packet_traits<Scalar>::size) == 0)
00082                        && (InnerStrideAtCompileTime == 1)
00083                         ? PacketAccessBit : 0,
00084     MaskAlignedBit = (InnerPanel && (OuterStrideAtCompileTime!=Dynamic) && (((OuterStrideAtCompileTime * int(sizeof(Scalar))) % 16) == 0)) ? AlignedBit : 0,
00085     FlagsLinearAccessBit = (RowsAtCompileTime == 1 || ColsAtCompileTime == 1) ? LinearAccessBit : 0,
00086     FlagsLvalueBit = is_lvalue<XprType>::value ? LvalueBit : 0,
00087     FlagsRowMajorBit = IsRowMajor ? RowMajorBit : 0,
00088     Flags0 = traits<XprType>::Flags & ( (HereditaryBits & ~RowMajorBit) |
00089                                         DirectAccessBit |
00090                                         MaskPacketAccessBit |
00091                                         MaskAlignedBit),
00092     Flags = Flags0 | FlagsLinearAccessBit | FlagsLvalueBit | FlagsRowMajorBit
00093   };
00094 };
00095 }
00096 
00097 template<typename XprType, int BlockRows, int BlockCols, bool InnerPanel, bool HasDirectAccess> class Block
00098   : public internal::dense_xpr_base<Block<XprType, BlockRows, BlockCols, InnerPanel, HasDirectAccess> >::type
00099 {
00100   public:
00101 
00102     typedef typename internal::dense_xpr_base<Block>::type Base;
00103     EIGEN_DENSE_PUBLIC_INTERFACE(Block)
00104 
00105     class InnerIterator;
00106 
00109     inline Block(XprType& xpr, Index i)
00110       : m_xpr(xpr),
00111         // It is a row if and only if BlockRows==1 and BlockCols==XprType::ColsAtCompileTime,
00112         // and it is a column if and only if BlockRows==XprType::RowsAtCompileTime and BlockCols==1,
00113         // all other cases are invalid.
00114         // The case a 1x1 matrix seems ambiguous, but the result is the same anyway.
00115         m_startRow( (BlockRows==1) && (BlockCols==XprType::ColsAtCompileTime) ? i : 0),
00116         m_startCol( (BlockRows==XprType::RowsAtCompileTime) && (BlockCols==1) ? i : 0),
00117         m_blockRows(BlockRows==1 ? 1 : xpr.rows()),
00118         m_blockCols(BlockCols==1 ? 1 : xpr.cols())
00119     {
00120       eigen_assert( (i>=0) && (
00121           ((BlockRows==1) && (BlockCols==XprType::ColsAtCompileTime) && i<xpr.rows())
00122         ||((BlockRows==XprType::RowsAtCompileTime) && (BlockCols==1) && i<xpr.cols())));
00123     }
00124 
00127     inline Block(XprType& xpr, Index startRow, Index startCol)
00128       : m_xpr(xpr), m_startRow(startRow), m_startCol(startCol),
00129         m_blockRows(BlockRows), m_blockCols(BlockCols)
00130     {
00131       EIGEN_STATIC_ASSERT(RowsAtCompileTime!=Dynamic && ColsAtCompileTime!=Dynamic,THIS_METHOD_IS_ONLY_FOR_FIXED_SIZE)
00132       eigen_assert(startRow >= 0 && BlockRows >= 1 && startRow + BlockRows <= xpr.rows()
00133              && startCol >= 0 && BlockCols >= 1 && startCol + BlockCols <= xpr.cols());
00134     }
00135 
00138     inline Block(XprType& xpr,
00139           Index startRow, Index startCol,
00140           Index blockRows, Index blockCols)
00141       : m_xpr(xpr), m_startRow(startRow), m_startCol(startCol),
00142                           m_blockRows(blockRows), m_blockCols(blockCols)
00143     {
00144       eigen_assert((RowsAtCompileTime==Dynamic || RowsAtCompileTime==blockRows)
00145           && (ColsAtCompileTime==Dynamic || ColsAtCompileTime==blockCols));
00146       eigen_assert(startRow >= 0 && blockRows >= 0 && startRow + blockRows <= xpr.rows()
00147           && startCol >= 0 && blockCols >= 0 && startCol + blockCols <= xpr.cols());
00148     }
00149 
00150     EIGEN_INHERIT_ASSIGNMENT_OPERATORS(Block)
00151 
00152     inline Index rows() const { return m_blockRows.value(); }
00153     inline Index cols() const { return m_blockCols.value(); }
00154 
00155     inline Scalar& coeffRef(Index row, Index col)
00156     {
00157       EIGEN_STATIC_ASSERT_LVALUE(XprType)
00158       return m_xpr.const_cast_derived()
00159                .coeffRef(row + m_startRow.value(), col + m_startCol.value());
00160     }
00161 
00162     inline const Scalar& coeffRef(Index row, Index col) const
00163     {
00164       return m_xpr.derived()
00165                .coeffRef(row + m_startRow.value(), col + m_startCol.value());
00166     }
00167 
00168     EIGEN_STRONG_INLINE const CoeffReturnType coeff(Index row, Index col) const
00169     {
00170       return m_xpr.coeff(row + m_startRow.value(), col + m_startCol.value());
00171     }
00172 
00173     inline Scalar& coeffRef(Index index)
00174     {
00175       EIGEN_STATIC_ASSERT_LVALUE(XprType)
00176       return m_xpr.const_cast_derived()
00177              .coeffRef(m_startRow.value() + (RowsAtCompileTime == 1 ? 0 : index),
00178                        m_startCol.value() + (RowsAtCompileTime == 1 ? index : 0));
00179     }
00180 
00181     inline const Scalar& coeffRef(Index index) const
00182     {
00183       return m_xpr.const_cast_derived()
00184              .coeffRef(m_startRow.value() + (RowsAtCompileTime == 1 ? 0 : index),
00185                        m_startCol.value() + (RowsAtCompileTime == 1 ? index : 0));
00186     }
00187 
00188     inline const CoeffReturnType coeff(Index index) const
00189     {
00190       return m_xpr
00191              .coeff(m_startRow.value() + (RowsAtCompileTime == 1 ? 0 : index),
00192                     m_startCol.value() + (RowsAtCompileTime == 1 ? index : 0));
00193     }
00194 
00195     template<int LoadMode>
00196     inline PacketScalar packet(Index row, Index col) const
00197     {
00198       return m_xpr.template packet<Unaligned>
00199               (row + m_startRow.value(), col + m_startCol.value());
00200     }
00201 
00202     template<int LoadMode>
00203     inline void writePacket(Index row, Index col, const PacketScalar& x)
00204     {
00205       m_xpr.const_cast_derived().template writePacket<Unaligned>
00206               (row + m_startRow.value(), col + m_startCol.value(), x);
00207     }
00208 
00209     template<int LoadMode>
00210     inline PacketScalar packet(Index index) const
00211     {
00212       return m_xpr.template packet<Unaligned>
00213               (m_startRow.value() + (RowsAtCompileTime == 1 ? 0 : index),
00214                m_startCol.value() + (RowsAtCompileTime == 1 ? index : 0));
00215     }
00216 
00217     template<int LoadMode>
00218     inline void writePacket(Index index, const PacketScalar& x)
00219     {
00220       m_xpr.const_cast_derived().template writePacket<Unaligned>
00221          (m_startRow.value() + (RowsAtCompileTime == 1 ? 0 : index),
00222           m_startCol.value() + (RowsAtCompileTime == 1 ? index : 0), x);
00223     }
00224 
00225     #ifdef EIGEN_PARSED_BY_DOXYGEN
00226 
00227     inline const Scalar* data() const;
00228     inline Index innerStride() const;
00229     inline Index outerStride() const;
00230     #endif
00231 
00232     const typename internal::remove_all<typename XprType::Nested>::type& nestedExpression() const 
00233     { 
00234       return m_xpr; 
00235     }
00236       
00237     Index startRow() const 
00238     { 
00239       return m_startRow.value(); 
00240     }
00241       
00242     Index startCol() const 
00243     { 
00244       return m_startCol.value(); 
00245     }
00246 
00247   protected:
00248 
00249     const typename XprType::Nested m_xpr;
00250     const internal::variable_if_dynamic<Index, XprType::RowsAtCompileTime == 1 ? 0 : Dynamic> m_startRow;
00251     const internal::variable_if_dynamic<Index, XprType::ColsAtCompileTime == 1 ? 0 : Dynamic> m_startCol;
00252     const internal::variable_if_dynamic<Index, RowsAtCompileTime> m_blockRows;
00253     const internal::variable_if_dynamic<Index, ColsAtCompileTime> m_blockCols;
00254 };
00255 
00257 template<typename XprType, int BlockRows, int BlockCols, bool InnerPanel>
00258 class Block<XprType,BlockRows,BlockCols, InnerPanel,true>
00259   : public MapBase<Block<XprType, BlockRows, BlockCols, InnerPanel, true> >
00260 {
00261   public:
00262 
00263     typedef MapBase<Block> Base;
00264     EIGEN_DENSE_PUBLIC_INTERFACE(Block)
00265 
00266     EIGEN_INHERIT_ASSIGNMENT_OPERATORS(Block)
00267 
00270     inline Block(XprType& xpr, Index i)
00271       : Base(internal::const_cast_ptr(&xpr.coeffRef(
00272               (BlockRows==1) && (BlockCols==XprType::ColsAtCompileTime) ? i : 0,
00273               (BlockRows==XprType::RowsAtCompileTime) && (BlockCols==1) ? i : 0)),
00274              BlockRows==1 ? 1 : xpr.rows(),
00275              BlockCols==1 ? 1 : xpr.cols()),
00276         m_xpr(xpr)
00277     {
00278       eigen_assert( (i>=0) && (
00279           ((BlockRows==1) && (BlockCols==XprType::ColsAtCompileTime) && i<xpr.rows())
00280         ||((BlockRows==XprType::RowsAtCompileTime) && (BlockCols==1) && i<xpr.cols())));
00281       init();
00282     }
00283 
00286     inline Block(XprType& xpr, Index startRow, Index startCol)
00287       : Base(internal::const_cast_ptr(&xpr.coeffRef(startRow,startCol))), m_xpr(xpr)
00288     {
00289       eigen_assert(startRow >= 0 && BlockRows >= 1 && startRow + BlockRows <= xpr.rows()
00290              && startCol >= 0 && BlockCols >= 1 && startCol + BlockCols <= xpr.cols());
00291       init();
00292     }
00293 
00296     inline Block(XprType& xpr,
00297           Index startRow, Index startCol,
00298           Index blockRows, Index blockCols)
00299       : Base(internal::const_cast_ptr(&xpr.coeffRef(startRow,startCol)), blockRows, blockCols),
00300         m_xpr(xpr)
00301     {
00302       eigen_assert((RowsAtCompileTime==Dynamic || RowsAtCompileTime==blockRows)
00303              && (ColsAtCompileTime==Dynamic || ColsAtCompileTime==blockCols));
00304       eigen_assert(startRow >= 0 && blockRows >= 0 && startRow + blockRows <= xpr.rows()
00305              && startCol >= 0 && blockCols >= 0 && startCol + blockCols <= xpr.cols());
00306       init();
00307     }
00308 
00309     const typename internal::remove_all<typename XprType::Nested>::type& nestedExpression() const 
00310     { 
00311       return m_xpr; 
00312     }
00313       
00315     inline Index innerStride() const
00316     {
00317       return internal::traits<Block>::HasSameStorageOrderAsXprType
00318              ? m_xpr.innerStride()
00319              : m_xpr.outerStride();
00320     }
00321 
00323     inline Index outerStride() const
00324     {
00325       return m_outerStride;
00326     }
00327 
00328   #ifndef __SUNPRO_CC
00329   // FIXME sunstudio is not friendly with the above friend...
00330   // META-FIXME there is no 'friend' keyword around here. Is this obsolete?
00331   protected:
00332   #endif
00333 
00334     #ifndef EIGEN_PARSED_BY_DOXYGEN
00335 
00336     inline Block(XprType& xpr, const Scalar* data, Index blockRows, Index blockCols)
00337       : Base(data, blockRows, blockCols), m_xpr(xpr)
00338     {
00339       init();
00340     }
00341     #endif
00342 
00343   protected:
00344     void init()
00345     {
00346       m_outerStride = internal::traits<Block>::HasSameStorageOrderAsXprType
00347                     ? m_xpr.outerStride()
00348                     : m_xpr.innerStride();
00349     }
00350 
00351     typename XprType::Nested m_xpr;
00352     Index m_outerStride;
00353 };
00354 
00355 } // end namespace Eigen
00356 
00357 #endif // EIGEN_BLOCK_H


win_eigen
Author(s): Daniel Stonier
autogenerated on Mon Oct 6 2014 12:24:12