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 <gael.guennebaud@inria.fr>
00005 // Copyright (C) 2006-2008 Benoit Jacob <jacob.benoit.1@gmail.com>
00006 //
00007 // Eigen is free software; you can redistribute it and/or
00008 // modify it under the terms of the GNU Lesser General Public
00009 // License as published by the Free Software Foundation; either
00010 // version 3 of the License, or (at your option) any later version.
00011 //
00012 // Alternatively, you can redistribute it and/or
00013 // modify it under the terms of the GNU General Public License as
00014 // published by the Free Software Foundation; either version 2 of
00015 // the License, or (at your option) any later version.
00016 //
00017 // Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
00018 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00019 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
00020 // GNU General Public License for more details.
00021 //
00022 // You should have received a copy of the GNU Lesser General Public
00023 // License and a copy of the GNU General Public License along with
00024 // Eigen. If not, see <http://www.gnu.org/licenses/>.
00025 
00026 #ifndef EIGEN_COMMAINITIALIZER_H
00027 #define EIGEN_COMMAINITIALIZER_H
00028 
00029 namespace Eigen { 
00030 
00042 template<typename XprType>
00043 struct CommaInitializer
00044 {
00045   typedef typename XprType::Scalar Scalar;
00046   typedef typename XprType::Index Index;
00047 
00048   inline CommaInitializer(XprType& xpr, const Scalar& s)
00049     : m_xpr(xpr), m_row(0), m_col(1), m_currentBlockRows(1)
00050   {
00051     m_xpr.coeffRef(0,0) = s;
00052   }
00053 
00054   template<typename OtherDerived>
00055   inline CommaInitializer(XprType& xpr, const DenseBase<OtherDerived>& other)
00056     : m_xpr(xpr), m_row(0), m_col(other.cols()), m_currentBlockRows(other.rows())
00057   {
00058     m_xpr.block(0, 0, other.rows(), other.cols()) = other;
00059   }
00060 
00061   /* inserts a scalar value in the target matrix */
00062   CommaInitializer& operator,(const Scalar& s)
00063   {
00064     if (m_col==m_xpr.cols())
00065     {
00066       m_row+=m_currentBlockRows;
00067       m_col = 0;
00068       m_currentBlockRows = 1;
00069       eigen_assert(m_row<m_xpr.rows()
00070         && "Too many rows passed to comma initializer (operator<<)");
00071     }
00072     eigen_assert(m_col<m_xpr.cols()
00073       && "Too many coefficients passed to comma initializer (operator<<)");
00074     eigen_assert(m_currentBlockRows==1);
00075     m_xpr.coeffRef(m_row, m_col++) = s;
00076     return *this;
00077   }
00078 
00079   /* inserts a matrix expression in the target matrix */
00080   template<typename OtherDerived>
00081   CommaInitializer& operator,(const DenseBase<OtherDerived>& other)
00082   {
00083     if (m_col==m_xpr.cols())
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<m_xpr.cols()
00092       && "Too many coefficients passed to comma initializer (operator<<)");
00093     eigen_assert(m_currentBlockRows==other.rows());
00094     if (OtherDerived::SizeAtCompileTime != Dynamic)
00095       m_xpr.template block<OtherDerived::RowsAtCompileTime != Dynamic ? OtherDerived::RowsAtCompileTime : 1,
00096                               OtherDerived::ColsAtCompileTime != Dynamic ? OtherDerived::ColsAtCompileTime : 1>
00097                     (m_row, m_col) = other;
00098     else
00099       m_xpr.block(m_row, m_col, other.rows(), other.cols()) = other;
00100     m_col += other.cols();
00101     return *this;
00102   }
00103 
00104   inline ~CommaInitializer()
00105   {
00106     eigen_assert((m_row+m_currentBlockRows) == m_xpr.rows()
00107          && m_col == m_xpr.cols()
00108          && "Too few coefficients passed to comma initializer (operator<<)");
00109   }
00110 
00118   inline XprType& finished() { return m_xpr; }
00119 
00120   XprType& m_xpr;   // target expression
00121   Index m_row;              // current row id
00122   Index m_col;              // current col id
00123   Index m_currentBlockRows; // current block height
00124 };
00125 
00137 template<typename Derived>
00138 inline CommaInitializer<Derived> DenseBase<Derived>::operator<< (const Scalar& s)
00139 {
00140   return CommaInitializer<Derived>(*static_cast<Derived*>(this), s);
00141 }
00142 
00144 template<typename Derived>
00145 template<typename OtherDerived>
00146 inline CommaInitializer<Derived>
00147 DenseBase<Derived>::operator<<(const DenseBase<OtherDerived>& other)
00148 {
00149   return CommaInitializer<Derived>(*static_cast<Derived *>(this), other);
00150 }
00151 
00152 } // end namespace Eigen
00153 
00154 #endif // EIGEN_COMMAINITIALIZER_H