CommaInitializer.h
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// 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_COMMAINITIALIZER_H
12#define EIGEN_COMMAINITIALIZER_H
13
14namespace Eigen {
15
27template<typename XprType>
28struct CommaInitializer
29{
30 typedef typename XprType::Scalar Scalar;
31 typedef typename XprType::Index Index;
32
33 inline CommaInitializer(XprType& xpr, const Scalar& s)
34 : m_xpr(xpr), m_row(0), m_col(1), m_currentBlockRows(1)
35 {
36 m_xpr.coeffRef(0,0) = s;
37 }
38
39 template<typename OtherDerived>
40 inline CommaInitializer(XprType& xpr, const DenseBase<OtherDerived>& other)
41 : m_xpr(xpr), m_row(0), m_col(other.cols()), m_currentBlockRows(other.rows())
42 {
43 m_xpr.block(0, 0, other.rows(), other.cols()) = other;
44 }
45
46 /* inserts a scalar value in the target matrix */
47 CommaInitializer& operator,(const Scalar& s)
48 {
49 if (m_col==m_xpr.cols())
50 {
51 m_row+=m_currentBlockRows;
52 m_col = 0;
53 m_currentBlockRows = 1;
54 eigen_assert(m_row<m_xpr.rows()
55 && "Too many rows passed to comma initializer (operator<<)");
56 }
57 eigen_assert(m_col<m_xpr.cols()
58 && "Too many coefficients passed to comma initializer (operator<<)");
59 eigen_assert(m_currentBlockRows==1);
60 m_xpr.coeffRef(m_row, m_col++) = s;
61 return *this;
62 }
63
64 /* inserts a matrix expression in the target matrix */
65 template<typename OtherDerived>
66 CommaInitializer& operator,(const DenseBase<OtherDerived>& other)
67 {
68 if(other.cols()==0 || other.rows()==0)
69 return *this;
70 if (m_col==m_xpr.cols())
71 {
72 m_row+=m_currentBlockRows;
73 m_col = 0;
74 m_currentBlockRows = other.rows();
75 eigen_assert(m_row+m_currentBlockRows<=m_xpr.rows()
76 && "Too many rows passed to comma initializer (operator<<)");
77 }
78 eigen_assert(m_col<m_xpr.cols()
79 && "Too many coefficients passed to comma initializer (operator<<)");
80 eigen_assert(m_currentBlockRows==other.rows());
81 if (OtherDerived::SizeAtCompileTime != Dynamic)
82 m_xpr.template block<OtherDerived::RowsAtCompileTime != Dynamic ? OtherDerived::RowsAtCompileTime : 1,
83 OtherDerived::ColsAtCompileTime != Dynamic ? OtherDerived::ColsAtCompileTime : 1>
84 (m_row, m_col) = other;
85 else
86 m_xpr.block(m_row, m_col, other.rows(), other.cols()) = other;
87 m_col += other.cols();
88 return *this;
89 }
90
91 inline ~CommaInitializer()
92 {
93 eigen_assert((m_row+m_currentBlockRows) == m_xpr.rows()
94 && m_col == m_xpr.cols()
95 && "Too few coefficients passed to comma initializer (operator<<)");
96 }
97
105 inline XprType& finished() { return m_xpr; }
106
107 XprType& m_xpr; // target expression
108 Index m_row; // current row id
109 Index m_col; // current col id
110 Index m_currentBlockRows; // current block height
111};
112
124template<typename Derived>
125inline CommaInitializer<Derived> DenseBase<Derived>::operator<< (const Scalar& s)
126{
127 return CommaInitializer<Derived>(*static_cast<Derived*>(this), s);
128}
129
131template<typename Derived>
132template<typename OtherDerived>
135{
136 return CommaInitializer<Derived>(*static_cast<Derived *>(this), other);
137}
138
139} // end namespace Eigen
140
141#endif // EIGEN_COMMAINITIALIZER_H
Base class for all dense matrices, vectors, and arrays.
Definition DenseBase.h:38
Block< Derived > block(Index startRow, Index startCol, Index blockRows, Index blockCols)
Definition DenseBase.h:55
CommaInitializer< Derived > operator<<(const Scalar &s)
Definition CommaInitializer.h:125
Helper class used by the comma initializer operator.
Definition CommaInitializer.h:29
XprType & finished()
Definition CommaInitializer.h:105