Eigen  3.3.3
CommaInitializer.h
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-2008 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_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 (m_col==m_xpr.cols() && (other.cols()!=0 || other.rows()!=m_currentBlockRows))
00084     {
00085       m_row+=m_currentBlockRows;
00086       m_col = 0;
00087       m_currentBlockRows = other.rows();
00088       eigen_assert(m_row+m_currentBlockRows<=m_xpr.rows()
00089         && "Too many rows passed to comma initializer (operator<<)");
00090     }
00091     eigen_assert((m_col + other.cols() <= m_xpr.cols())
00092       && "Too many coefficients passed to comma initializer (operator<<)");
00093     eigen_assert(m_currentBlockRows==other.rows());
00094     m_xpr.template block<OtherDerived::RowsAtCompileTime, OtherDerived::ColsAtCompileTime>
00095                     (m_row, m_col, other.rows(), other.cols()) = other;
00096     m_col += other.cols();
00097     return *this;
00098   }
00099 
00100   EIGEN_DEVICE_FUNC
00101   inline ~CommaInitializer()
00102 #if defined VERIFY_RAISES_ASSERT && (!defined EIGEN_NO_ASSERTION_CHECKING) && defined EIGEN_EXCEPTIONS
00103   EIGEN_EXCEPTION_SPEC(Eigen::eigen_assert_exception)
00104 #endif
00105   {
00106       finished();
00107   }
00108 
00116   EIGEN_DEVICE_FUNC
00117   inline XprType& finished() {
00118       eigen_assert(((m_row+m_currentBlockRows) == m_xpr.rows() || m_xpr.cols() == 0)
00119            && m_col == m_xpr.cols()
00120            && "Too few coefficients passed to comma initializer (operator<<)");
00121       return m_xpr;
00122   }
00123 
00124   XprType& m_xpr;           // target expression
00125   Index m_row;              // current row id
00126   Index m_col;              // current col id
00127   Index m_currentBlockRows; // current block height
00128 };
00129 
00143 template<typename Derived>
00144 inline CommaInitializer<Derived> DenseBase<Derived>::operator<< (const Scalar& s)
00145 {
00146   return CommaInitializer<Derived>(*static_cast<Derived*>(this), s);
00147 }
00148 
00150 template<typename Derived>
00151 template<typename OtherDerived>
00152 inline CommaInitializer<Derived>
00153 DenseBase<Derived>::operator<<(const DenseBase<OtherDerived>& other)
00154 {
00155   return CommaInitializer<Derived>(*static_cast<Derived *>(this), other);
00156 }
00157 
00158 } // end namespace Eigen
00159 
00160 #endif // EIGEN_COMMAINITIALIZER_H
 All Classes Functions Variables Typedefs Enumerations Enumerator Friends