MOAB  4.9.3pre
CommaInitializer.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 <[email protected]>
00005 // Copyright (C) 2006-2008 Benoit Jacob <[email protected]>
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_COMMAINITIALIZER_H
00012 #define EIGEN_COMMAINITIALIZER_H
00013 
00014 namespace Eigen { 
00015 
00027 template<typename XprType>
00028 struct CommaInitializer
00029 {
00030   typedef typename XprType::Scalar Scalar;
00031 
00032   EIGEN_DEVICE_FUNC
00033   inline CommaInitializer(XprType& xpr, const Scalar& s)
00034     : m_xpr(xpr), m_row(0), m_col(1), m_currentBlockRows(1)
00035   {
00036     m_xpr.coeffRef(0,0) = s;
00037   }
00038 
00039   template<typename OtherDerived>
00040   EIGEN_DEVICE_FUNC
00041   inline CommaInitializer(XprType& xpr, const DenseBase<OtherDerived>& other)
00042     : m_xpr(xpr), m_row(0), m_col(other.cols()), m_currentBlockRows(other.rows())
00043   {
00044     m_xpr.block(0, 0, other.rows(), other.cols()) = other;
00045   }
00046 
00047   /* Copy/Move constructor which transfers ownership. This is crucial in 
00048    * absence of return value optimization to avoid assertions during destruction. */
00049   // FIXME in C++11 mode this could be replaced by a proper RValue constructor
00050   EIGEN_DEVICE_FUNC
00051   inline CommaInitializer(const CommaInitializer& o)
00052   : m_xpr(o.m_xpr), m_row(o.m_row), m_col(o.m_col), m_currentBlockRows(o.m_currentBlockRows) {
00053     // Mark original object as finished. In absence of R-value references we need to const_cast:
00054     const_cast<CommaInitializer&>(o).m_row = m_xpr.rows();
00055     const_cast<CommaInitializer&>(o).m_col = m_xpr.cols();
00056     const_cast<CommaInitializer&>(o).m_currentBlockRows = 0;
00057   }
00058 
00059   /* inserts a scalar value in the target matrix */
00060   EIGEN_DEVICE_FUNC
00061   CommaInitializer& operator,(const Scalar& s)
00062   {
00063     if (m_col==m_xpr.cols())
00064     {
00065       m_row+=m_currentBlockRows;
00066       m_col = 0;
00067       m_currentBlockRows = 1;
00068       eigen_assert(m_row<m_xpr.rows()
00069         && "Too many rows passed to comma initializer (operator<<)");
00070     }
00071     eigen_assert(m_col<m_xpr.cols()
00072       && "Too many coefficients passed to comma initializer (operator<<)");
00073     eigen_assert(m_currentBlockRows==1);
00074     m_xpr.coeffRef(m_row, m_col++) = s;
00075     return *this;
00076   }
00077 
00078   /* inserts a matrix expression in the target matrix */
00079   template<typename OtherDerived>
00080   EIGEN_DEVICE_FUNC
00081   CommaInitializer& operator,(const DenseBase<OtherDerived>& other)
00082   {
00083     if(other.cols()==0 || other.rows()==0)
00084       return *this;
00085     if (m_col==m_xpr.cols())
00086     {
00087       m_row+=m_currentBlockRows;
00088       m_col = 0;
00089       m_currentBlockRows = other.rows();
00090       eigen_assert(m_row+m_currentBlockRows<=m_xpr.rows()
00091         && "Too many rows passed to comma initializer (operator<<)");
00092     }
00093     eigen_assert(m_col<m_xpr.cols()
00094       && "Too many coefficients passed to comma initializer (operator<<)");
00095     eigen_assert(m_currentBlockRows==other.rows());
00096     if (OtherDerived::SizeAtCompileTime != Dynamic)
00097       m_xpr.template block<OtherDerived::RowsAtCompileTime != Dynamic ? OtherDerived::RowsAtCompileTime : 1,
00098                               OtherDerived::ColsAtCompileTime != Dynamic ? OtherDerived::ColsAtCompileTime : 1>
00099                     (m_row, m_col) = other;
00100     else
00101       m_xpr.block(m_row, m_col, other.rows(), other.cols()) = other;
00102     m_col += other.cols();
00103     return *this;
00104   }
00105 
00106   EIGEN_DEVICE_FUNC
00107   inline ~CommaInitializer()
00108 #if defined VERIFY_RAISES_ASSERT && (!defined EIGEN_NO_ASSERTION_CHECKING) && defined EIGEN_EXCEPTIONS
00109   EIGEN_EXCEPTION_SPEC(Eigen::eigen_assert_exception)
00110 #endif
00111   {
00112     eigen_assert((m_row+m_currentBlockRows) == m_xpr.rows()
00113          && m_col == m_xpr.cols()
00114          && "Too few coefficients passed to comma initializer (operator<<)");
00115   }
00116 
00124   EIGEN_DEVICE_FUNC
00125   inline XprType& finished() { return m_xpr; }
00126 
00127   XprType& m_xpr;           // target expression
00128   Index m_row;              // current row id
00129   Index m_col;              // current col id
00130   Index m_currentBlockRows; // current block height
00131 };
00132 
00146 template<typename Derived>
00147 inline CommaInitializer<Derived> DenseBase<Derived>::operator<< (const Scalar& s)
00148 {
00149   return CommaInitializer<Derived>(*static_cast<Derived*>(this), s);
00150 }
00151 
00153 template<typename Derived>
00154 template<typename OtherDerived>
00155 inline CommaInitializer<Derived>
00156 DenseBase<Derived>::operator<<(const DenseBase<OtherDerived>& other)
00157 {
00158   return CommaInitializer<Derived>(*static_cast<Derived *>(this), other);
00159 }
00160 
00161 } // end namespace Eigen
00162 
00163 #endif // EIGEN_COMMAINITIALIZER_H
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines