CommaInitializer.h
Go to the documentation of this file.
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-2008 Benoit Jacob <jacob.benoit.1@gmail.com>
6 //
7 // Eigen is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU Lesser General Public
9 // License as published by the Free Software Foundation; either
10 // version 3 of the License, or (at your option) any later version.
11 //
12 // Alternatively, you can redistribute it and/or
13 // modify it under the terms of the GNU General Public License as
14 // published by the Free Software Foundation; either version 2 of
15 // the License, or (at your option) any later version.
16 //
17 // Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
18 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
20 // GNU General Public License for more details.
21 //
22 // You should have received a copy of the GNU Lesser General Public
23 // License and a copy of the GNU General Public License along with
24 // Eigen. If not, see <http://www.gnu.org/licenses/>.
25 
26 #ifndef EIGEN_COMMAINITIALIZER_H
27 #define EIGEN_COMMAINITIALIZER_H
28 
29 namespace Eigen {
30 
42 template<typename XprType>
44 {
45  typedef typename XprType::Scalar Scalar;
46  typedef typename XprType::Index Index;
47 
48  inline CommaInitializer(XprType& xpr, const Scalar& s)
49  : m_xpr(xpr), m_row(0), m_col(1), m_currentBlockRows(1)
50  {
51  m_xpr.coeffRef(0,0) = s;
52  }
53 
54  template<typename OtherDerived>
55  inline CommaInitializer(XprType& xpr, const DenseBase<OtherDerived>& other)
56  : m_xpr(xpr), m_row(0), m_col(other.cols()), m_currentBlockRows(other.rows())
57  {
58  m_xpr.block(0, 0, other.rows(), other.cols()) = other;
59  }
60 
61  /* inserts a scalar value in the target matrix */
63  {
64  if (m_col==m_xpr.cols())
65  {
67  m_col = 0;
69  eigen_assert(m_row<m_xpr.rows()
70  && "Too many rows passed to comma initializer (operator<<)");
71  }
72  eigen_assert(m_col<m_xpr.cols()
73  && "Too many coefficients passed to comma initializer (operator<<)");
75  m_xpr.coeffRef(m_row, m_col++) = s;
76  return *this;
77  }
78 
79  /* inserts a matrix expression in the target matrix */
80  template<typename OtherDerived>
82  {
83  if (m_col==m_xpr.cols())
84  {
86  m_col = 0;
87  m_currentBlockRows = other.rows();
89  && "Too many rows passed to comma initializer (operator<<)");
90  }
91  eigen_assert(m_col<m_xpr.cols()
92  && "Too many coefficients passed to comma initializer (operator<<)");
93  eigen_assert(m_currentBlockRows==other.rows());
94  if (OtherDerived::SizeAtCompileTime != Dynamic)
95  m_xpr.template block<OtherDerived::RowsAtCompileTime != Dynamic ? OtherDerived::RowsAtCompileTime : 1,
96  OtherDerived::ColsAtCompileTime != Dynamic ? OtherDerived::ColsAtCompileTime : 1>
97  (m_row, m_col) = other;
98  else
99  m_xpr.block(m_row, m_col, other.rows(), other.cols()) = other;
100  m_col += other.cols();
101  return *this;
102  }
103 
105  {
107  && m_col == m_xpr.cols()
108  && "Too few coefficients passed to comma initializer (operator<<)");
109  }
110 
118  inline XprType& finished() { return m_xpr; }
119 
120  XprType& m_xpr; // target expression
121  Index m_row; // current row id
122  Index m_col; // current col id
123  Index m_currentBlockRows; // current block height
124 };
125 
137 template<typename Derived>
139 {
140  return CommaInitializer<Derived>(*static_cast<Derived*>(this), s);
141 }
142 
144 template<typename Derived>
145 template<typename OtherDerived>
148 {
149  return CommaInitializer<Derived>(*static_cast<Derived *>(this), other);
150 }
151 
152 } // end namespace Eigen
153 
154 #endif // EIGEN_COMMAINITIALIZER_H