1 // This file is part of Eigen, a lightweight C++ template library 2 // for linear algebra. 3 // 4 // Copyright (C) 2008 Gael Guennebaud <gael.guennebaud@inria.fr> 5 // Copyright (C) 2006-2010 Benoit Jacob <jacob.benoit.1@gmail.com> 6 // 7 // This Source Code Form is subject to the terms of the Mozilla 8 // Public License v. 2.0. If a copy of the MPL was not distributed 9 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/. 10 11 #ifndef EIGEN_BLOCK_H 12 #define EIGEN_BLOCK_H 13 14 namespace Eigen { 15 16 /** \class Block 17 * \ingroup Core_Module 18 * 19 * \brief Expression of a fixed-size or dynamic-size block 20 * 21 * \param XprType the type of the expression in which we are taking a block 22 * \param BlockRows the number of rows of the block we are taking at compile time (optional) 23 * \param BlockCols the number of columns of the block we are taking at compile time (optional) 24 * 25 * This class represents an expression of either a fixed-size or dynamic-size block. It is the return 26 * type of DenseBase::block(Index,Index,Index,Index) and DenseBase::block<int,int>(Index,Index) and 27 * most of the time this is the only way it is used. 28 * 29 * However, if you want to directly maniputate block expressions, 30 * for instance if you want to write a function returning such an expression, you 31 * will need to use this class. 32 * 33 * Here is an example illustrating the dynamic case: 34 * \include class_Block.cpp 35 * Output: \verbinclude class_Block.out 36 * 37 * \note Even though this expression has dynamic size, in the case where \a XprType 38 * has fixed size, this expression inherits a fixed maximal size which means that evaluating 39 * it does not cause a dynamic memory allocation. 40 * 41 * Here is an example illustrating the fixed-size case: 42 * \include class_FixedBlock.cpp 43 * Output: \verbinclude class_FixedBlock.out 44 * 45 * \sa DenseBase::block(Index,Index,Index,Index), DenseBase::block(Index,Index), class VectorBlock 46 */ 47 48 namespace internal { 49 template<typename XprType, int BlockRows, int BlockCols, bool InnerPanel> 50 struct traits<Block<XprType, BlockRows, BlockCols, InnerPanel> > : traits<XprType> 51 { 52 typedef typename traits<XprType>::Scalar Scalar; 53 typedef typename traits<XprType>::StorageKind StorageKind; 54 typedef typename traits<XprType>::XprKind XprKind; 55 typedef typename nested<XprType>::type XprTypeNested; 56 typedef typename remove_reference<XprTypeNested>::type _XprTypeNested; 57 enum{ 58 MatrixRows = traits<XprType>::RowsAtCompileTime, 59 MatrixCols = traits<XprType>::ColsAtCompileTime, 60 RowsAtCompileTime = MatrixRows == 0 ? 0 : BlockRows, 61 ColsAtCompileTime = MatrixCols == 0 ? 0 : BlockCols, 62 MaxRowsAtCompileTime = BlockRows==0 ? 0 63 : RowsAtCompileTime != Dynamic ? int(RowsAtCompileTime) 64 : int(traits<XprType>::MaxRowsAtCompileTime), 65 MaxColsAtCompileTime = BlockCols==0 ? 0 66 : ColsAtCompileTime != Dynamic ? int(ColsAtCompileTime) 67 : int(traits<XprType>::MaxColsAtCompileTime), 68 XprTypeIsRowMajor = (int(traits<XprType>::Flags)&RowMajorBit) != 0, 69 IsDense = is_same<StorageKind,Dense>::value, 70 IsRowMajor = (IsDense&&MaxRowsAtCompileTime==1&&MaxColsAtCompileTime!=1) ? 1 71 : (IsDense&&MaxColsAtCompileTime==1&&MaxRowsAtCompileTime!=1) ? 0 72 : XprTypeIsRowMajor, 73 HasSameStorageOrderAsXprType = (IsRowMajor == XprTypeIsRowMajor), 74 InnerSize = IsRowMajor ? int(ColsAtCompileTime) : int(RowsAtCompileTime), 75 InnerStrideAtCompileTime = HasSameStorageOrderAsXprType 76 ? int(inner_stride_at_compile_time<XprType>::ret) 77 : int(outer_stride_at_compile_time<XprType>::ret), 78 OuterStrideAtCompileTime = HasSameStorageOrderAsXprType 79 ? int(outer_stride_at_compile_time<XprType>::ret) 80 : int(inner_stride_at_compile_time<XprType>::ret), 81 MaskPacketAccessBit = (InnerSize == Dynamic || (InnerSize % packet_traits<Scalar>::size) == 0) 82 && (InnerStrideAtCompileTime == 1) 83 ? PacketAccessBit : 0, 84 MaskAlignedBit = (InnerPanel && (OuterStrideAtCompileTime!=Dynamic) && (((OuterStrideAtCompileTime * int(sizeof(Scalar))) % 16) == 0)) ? AlignedBit : 0, 85 FlagsLinearAccessBit = (RowsAtCompileTime == 1 || ColsAtCompileTime == 1 || (InnerPanel && (traits<XprType>::Flags&LinearAccessBit))) ? LinearAccessBit : 0, 86 FlagsLvalueBit = is_lvalue<XprType>::value ? LvalueBit : 0, 87 FlagsRowMajorBit = IsRowMajor ? RowMajorBit : 0, 88 Flags0 = traits<XprType>::Flags & ( (HereditaryBits & ~RowMajorBit) | 89 DirectAccessBit | 90 MaskPacketAccessBit | 91 MaskAlignedBit), 92 Flags = Flags0 | FlagsLinearAccessBit | FlagsLvalueBit | FlagsRowMajorBit 93 }; 94 }; 95 96 template<typename XprType, int BlockRows=Dynamic, int BlockCols=Dynamic, bool InnerPanel = false, 97 bool HasDirectAccess = internal::has_direct_access<XprType>::ret> class BlockImpl_dense; 98 99 } // end namespace internal 100 101 template<typename XprType, int BlockRows, int BlockCols, bool InnerPanel, typename StorageKind> class BlockImpl; 102 103 template<typename XprType, int BlockRows, int BlockCols, bool InnerPanel> class Block 104 : public BlockImpl<XprType, BlockRows, BlockCols, InnerPanel, typename internal::traits<XprType>::StorageKind> 105 { 106 typedef BlockImpl<XprType, BlockRows, BlockCols, InnerPanel, typename internal::traits<XprType>::StorageKind> Impl; 107 public: 108 //typedef typename Impl::Base Base; 109 typedef Impl Base; 110 EIGEN_GENERIC_PUBLIC_INTERFACE(Block) 111 EIGEN_INHERIT_ASSIGNMENT_OPERATORS(Block) 112 113 /** Column or Row constructor 114 */ 115 inline Block(XprType& xpr, Index i) : Impl(xpr,i) 116 { 117 eigen_assert( (i>=0) && ( 118 ((BlockRows==1) && (BlockCols==XprType::ColsAtCompileTime) && i<xpr.rows()) 119 ||((BlockRows==XprType::RowsAtCompileTime) && (BlockCols==1) && i<xpr.cols()))); 120 } 121 122 /** Fixed-size constructor 123 */ 124 inline Block(XprType& xpr, Index a_startRow, Index a_startCol) 125 : Impl(xpr, a_startRow, a_startCol) 126 { 127 EIGEN_STATIC_ASSERT(RowsAtCompileTime!=Dynamic && ColsAtCompileTime!=Dynamic,THIS_METHOD_IS_ONLY_FOR_FIXED_SIZE) 128 eigen_assert(a_startRow >= 0 && BlockRows >= 1 && a_startRow + BlockRows <= xpr.rows() 129 && a_startCol >= 0 && BlockCols >= 1 && a_startCol + BlockCols <= xpr.cols()); 130 } 131 132 /** Dynamic-size constructor 133 */ 134 inline Block(XprType& xpr, 135 Index a_startRow, Index a_startCol, 136 Index blockRows, Index blockCols) 137 : Impl(xpr, a_startRow, a_startCol, blockRows, blockCols) 138 { 139 eigen_assert((RowsAtCompileTime==Dynamic || RowsAtCompileTime==blockRows) 140 && (ColsAtCompileTime==Dynamic || ColsAtCompileTime==blockCols)); 141 eigen_assert(a_startRow >= 0 && blockRows >= 0 && a_startRow <= xpr.rows() - blockRows 142 && a_startCol >= 0 && blockCols >= 0 && a_startCol <= xpr.cols() - blockCols); 143 } 144 }; 145 146 // The generic default implementation for dense block simplu forward to the internal::BlockImpl_dense 147 // that must be specialized for direct and non-direct access... 148 template<typename XprType, int BlockRows, int BlockCols, bool InnerPanel> 149 class BlockImpl<XprType, BlockRows, BlockCols, InnerPanel, Dense> 150 : public internal::BlockImpl_dense<XprType, BlockRows, BlockCols, InnerPanel> 151 { 152 typedef internal::BlockImpl_dense<XprType, BlockRows, BlockCols, InnerPanel> Impl; 153 typedef typename XprType::Index Index; 154 public: 155 typedef Impl Base; 156 EIGEN_INHERIT_ASSIGNMENT_OPERATORS(BlockImpl) 157 inline BlockImpl(XprType& xpr, Index i) : Impl(xpr,i) {} 158 inline BlockImpl(XprType& xpr, Index a_startRow, Index a_startCol) : Impl(xpr, a_startRow, a_startCol) {} 159 inline BlockImpl(XprType& xpr, Index a_startRow, Index a_startCol, Index blockRows, Index blockCols) 160 : Impl(xpr, a_startRow, a_startCol, blockRows, blockCols) {} 161 }; 162 163 namespace internal { 164 165 /** \internal Internal implementation of dense Blocks in the general case. */ 166 template<typename XprType, int BlockRows, int BlockCols, bool InnerPanel, bool HasDirectAccess> class BlockImpl_dense 167 : public internal::dense_xpr_base<Block<XprType, BlockRows, BlockCols, InnerPanel> >::type 168 { 169 typedef Block<XprType, BlockRows, BlockCols, InnerPanel> BlockType; 170 public: 171 172 typedef typename internal::dense_xpr_base<BlockType>::type Base; 173 EIGEN_DENSE_PUBLIC_INTERFACE(BlockType) 174 EIGEN_INHERIT_ASSIGNMENT_OPERATORS(BlockImpl_dense) 175 176 class InnerIterator; 177 178 /** Column or Row constructor 179 */ 180 inline BlockImpl_dense(XprType& xpr, Index i) 181 : m_xpr(xpr), 182 // It is a row if and only if BlockRows==1 and BlockCols==XprType::ColsAtCompileTime, 183 // and it is a column if and only if BlockRows==XprType::RowsAtCompileTime and BlockCols==1, 184 // all other cases are invalid. 185 // The case a 1x1 matrix seems ambiguous, but the result is the same anyway. 186 m_startRow( (BlockRows==1) && (BlockCols==XprType::ColsAtCompileTime) ? i : 0), 187 m_startCol( (BlockRows==XprType::RowsAtCompileTime) && (BlockCols==1) ? i : 0), 188 m_blockRows(BlockRows==1 ? 1 : xpr.rows()), 189 m_blockCols(BlockCols==1 ? 1 : xpr.cols()) 190 {} 191 192 /** Fixed-size constructor 193 */ 194 inline BlockImpl_dense(XprType& xpr, Index a_startRow, Index a_startCol) 195 : m_xpr(xpr), m_startRow(a_startRow), m_startCol(a_startCol), 196 m_blockRows(BlockRows), m_blockCols(BlockCols) 197 {} 198 199 /** Dynamic-size constructor 200 */ 201 inline BlockImpl_dense(XprType& xpr, 202 Index a_startRow, Index a_startCol, 203 Index blockRows, Index blockCols) 204 : m_xpr(xpr), m_startRow(a_startRow), m_startCol(a_startCol), 205 m_blockRows(blockRows), m_blockCols(blockCols) 206 {} 207 208 inline Index rows() const { return m_blockRows.value(); } 209 inline Index cols() const { return m_blockCols.value(); } 210 211 inline Scalar& coeffRef(Index rowId, Index colId) 212 { 213 EIGEN_STATIC_ASSERT_LVALUE(XprType) 214 return m_xpr.const_cast_derived() 215 .coeffRef(rowId + m_startRow.value(), colId + m_startCol.value()); 216 } 217 218 inline const Scalar& coeffRef(Index rowId, Index colId) const 219 { 220 return m_xpr.derived() 221 .coeffRef(rowId + m_startRow.value(), colId + m_startCol.value()); 222 } 223 224 EIGEN_STRONG_INLINE const CoeffReturnType coeff(Index rowId, Index colId) const 225 { 226 return m_xpr.coeff(rowId + m_startRow.value(), colId + m_startCol.value()); 227 } 228 229 inline Scalar& coeffRef(Index index) 230 { 231 EIGEN_STATIC_ASSERT_LVALUE(XprType) 232 return m_xpr.const_cast_derived() 233 .coeffRef(m_startRow.value() + (RowsAtCompileTime == 1 ? 0 : index), 234 m_startCol.value() + (RowsAtCompileTime == 1 ? index : 0)); 235 } 236 237 inline const Scalar& coeffRef(Index index) const 238 { 239 return m_xpr.const_cast_derived() 240 .coeffRef(m_startRow.value() + (RowsAtCompileTime == 1 ? 0 : index), 241 m_startCol.value() + (RowsAtCompileTime == 1 ? index : 0)); 242 } 243 244 inline const CoeffReturnType coeff(Index index) const 245 { 246 return m_xpr 247 .coeff(m_startRow.value() + (RowsAtCompileTime == 1 ? 0 : index), 248 m_startCol.value() + (RowsAtCompileTime == 1 ? index : 0)); 249 } 250 251 template<int LoadMode> 252 inline PacketScalar packet(Index rowId, Index colId) const 253 { 254 return m_xpr.template packet<Unaligned> 255 (rowId + m_startRow.value(), colId + m_startCol.value()); 256 } 257 258 template<int LoadMode> 259 inline void writePacket(Index rowId, Index colId, const PacketScalar& val) 260 { 261 m_xpr.const_cast_derived().template writePacket<Unaligned> 262 (rowId + m_startRow.value(), colId + m_startCol.value(), val); 263 } 264 265 template<int LoadMode> 266 inline PacketScalar packet(Index index) const 267 { 268 return m_xpr.template packet<Unaligned> 269 (m_startRow.value() + (RowsAtCompileTime == 1 ? 0 : index), 270 m_startCol.value() + (RowsAtCompileTime == 1 ? index : 0)); 271 } 272 273 template<int LoadMode> 274 inline void writePacket(Index index, const PacketScalar& val) 275 { 276 m_xpr.const_cast_derived().template writePacket<Unaligned> 277 (m_startRow.value() + (RowsAtCompileTime == 1 ? 0 : index), 278 m_startCol.value() + (RowsAtCompileTime == 1 ? index : 0), val); 279 } 280 281 #ifdef EIGEN_PARSED_BY_DOXYGEN 282 /** \sa MapBase::data() */ 283 inline const Scalar* data() const; 284 inline Index innerStride() const; 285 inline Index outerStride() const; 286 #endif 287 288 const typename internal::remove_all<typename XprType::Nested>::type& nestedExpression() const 289 { 290 return m_xpr; 291 } 292 293 Index startRow() const 294 { 295 return m_startRow.value(); 296 } 297 298 Index startCol() const 299 { 300 return m_startCol.value(); 301 } 302 303 protected: 304 305 const typename XprType::Nested m_xpr; 306 const internal::variable_if_dynamic<Index, XprType::RowsAtCompileTime == 1 ? 0 : Dynamic> m_startRow; 307 const internal::variable_if_dynamic<Index, XprType::ColsAtCompileTime == 1 ? 0 : Dynamic> m_startCol; 308 const internal::variable_if_dynamic<Index, RowsAtCompileTime> m_blockRows; 309 const internal::variable_if_dynamic<Index, ColsAtCompileTime> m_blockCols; 310 }; 311 312 /** \internal Internal implementation of dense Blocks in the direct access case.*/ 313 template<typename XprType, int BlockRows, int BlockCols, bool InnerPanel> 314 class BlockImpl_dense<XprType,BlockRows,BlockCols, InnerPanel,true> 315 : public MapBase<Block<XprType, BlockRows, BlockCols, InnerPanel> > 316 { 317 typedef Block<XprType, BlockRows, BlockCols, InnerPanel> BlockType; 318 public: 319 320 typedef MapBase<BlockType> Base; 321 EIGEN_DENSE_PUBLIC_INTERFACE(BlockType) 322 EIGEN_INHERIT_ASSIGNMENT_OPERATORS(BlockImpl_dense) 323 324 /** Column or Row constructor 325 */ 326 inline BlockImpl_dense(XprType& xpr, Index i) 327 : Base(internal::const_cast_ptr(&xpr.coeffRef( 328 (BlockRows==1) && (BlockCols==XprType::ColsAtCompileTime) ? i : 0, 329 (BlockRows==XprType::RowsAtCompileTime) && (BlockCols==1) ? i : 0)), 330 BlockRows==1 ? 1 : xpr.rows(), 331 BlockCols==1 ? 1 : xpr.cols()), 332 m_xpr(xpr) 333 { 334 init(); 335 } 336 337 /** Fixed-size constructor 338 */ 339 inline BlockImpl_dense(XprType& xpr, Index startRow, Index startCol) 340 : Base(internal::const_cast_ptr(&xpr.coeffRef(startRow,startCol))), m_xpr(xpr) 341 { 342 init(); 343 } 344 345 /** Dynamic-size constructor 346 */ 347 inline BlockImpl_dense(XprType& xpr, 348 Index startRow, Index startCol, 349 Index blockRows, Index blockCols) 350 : Base(internal::const_cast_ptr(&xpr.coeffRef(startRow,startCol)), blockRows, blockCols), 351 m_xpr(xpr) 352 { 353 init(); 354 } 355 356 const typename internal::remove_all<typename XprType::Nested>::type& nestedExpression() const 357 { 358 return m_xpr; 359 } 360 361 /** \sa MapBase::innerStride() */ 362 inline Index innerStride() const 363 { 364 return internal::traits<BlockType>::HasSameStorageOrderAsXprType 365 ? m_xpr.innerStride() 366 : m_xpr.outerStride(); 367 } 368 369 /** \sa MapBase::outerStride() */ 370 inline Index outerStride() const 371 { 372 return m_outerStride; 373 } 374 375 #ifndef __SUNPRO_CC 376 // FIXME sunstudio is not friendly with the above friend... 377 // META-FIXME there is no 'friend' keyword around here. Is this obsolete? 378 protected: 379 #endif 380 381 #ifndef EIGEN_PARSED_BY_DOXYGEN 382 /** \internal used by allowAligned() */ 383 inline BlockImpl_dense(XprType& xpr, const Scalar* data, Index blockRows, Index blockCols) 384 : Base(data, blockRows, blockCols), m_xpr(xpr) 385 { 386 init(); 387 } 388 #endif 389 390 protected: 391 void init() 392 { 393 m_outerStride = internal::traits<BlockType>::HasSameStorageOrderAsXprType 394 ? m_xpr.outerStride() 395 : m_xpr.innerStride(); 396 } 397 398 typename XprType::Nested m_xpr; 399 Index m_outerStride; 400 }; 401 402 } // end namespace internal 403 404 } // end namespace Eigen 405 406 #endif // EIGEN_BLOCK_H 407