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 |
|
---|
14 | namespace Eigen {
|
---|
15 |
|
---|
16 | /** \class CommaInitializer
|
---|
17 | * \ingroup Core_Module
|
---|
18 | *
|
---|
19 | * \brief Helper class used by the comma initializer operator
|
---|
20 | *
|
---|
21 | * This class is internally used to implement the comma initializer feature. It is
|
---|
22 | * the return type of MatrixBase::operator<<, and most of the time this is the only
|
---|
23 | * way it is used.
|
---|
24 | *
|
---|
25 | * \sa \ref MatrixBaseCommaInitRef "MatrixBase::operator<<", CommaInitializer::finished()
|
---|
26 | */
|
---|
27 | template<typename XprType>
|
---|
28 | struct 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 | /* Copy/Move constructor which transfers ownership. This is crucial in
|
---|
47 | * absence of return value optimization to avoid assertions during destruction. */
|
---|
48 | // FIXME in C++11 mode this could be replaced by a proper RValue constructor
|
---|
49 | inline CommaInitializer(const CommaInitializer& o)
|
---|
50 | : m_xpr(o.m_xpr), m_row(o.m_row), m_col(o.m_col), m_currentBlockRows(o.m_currentBlockRows) {
|
---|
51 | // Mark original object as finished. In absence of R-value references we need to const_cast:
|
---|
52 | const_cast<CommaInitializer&>(o).m_row = m_xpr.rows();
|
---|
53 | const_cast<CommaInitializer&>(o).m_col = m_xpr.cols();
|
---|
54 | const_cast<CommaInitializer&>(o).m_currentBlockRows = 0;
|
---|
55 | }
|
---|
56 |
|
---|
57 | /* inserts a scalar value in the target matrix */
|
---|
58 | CommaInitializer& operator,(const Scalar& s)
|
---|
59 | {
|
---|
60 | if (m_col==m_xpr.cols())
|
---|
61 | {
|
---|
62 | m_row+=m_currentBlockRows;
|
---|
63 | m_col = 0;
|
---|
64 | m_currentBlockRows = 1;
|
---|
65 | eigen_assert(m_row<m_xpr.rows()
|
---|
66 | && "Too many rows passed to comma initializer (operator<<)");
|
---|
67 | }
|
---|
68 | eigen_assert(m_col<m_xpr.cols()
|
---|
69 | && "Too many coefficients passed to comma initializer (operator<<)");
|
---|
70 | eigen_assert(m_currentBlockRows==1);
|
---|
71 | m_xpr.coeffRef(m_row, m_col++) = s;
|
---|
72 | return *this;
|
---|
73 | }
|
---|
74 |
|
---|
75 | /* inserts a matrix expression in the target matrix */
|
---|
76 | template<typename OtherDerived>
|
---|
77 | CommaInitializer& operator,(const DenseBase<OtherDerived>& other)
|
---|
78 | {
|
---|
79 | if(other.rows()==0)
|
---|
80 | {
|
---|
81 | m_col += other.cols();
|
---|
82 | return *this;
|
---|
83 | }
|
---|
84 | if (m_col==m_xpr.cols())
|
---|
85 | {
|
---|
86 | m_row+=m_currentBlockRows;
|
---|
87 | m_col = 0;
|
---|
88 | m_currentBlockRows = other.rows();
|
---|
89 | eigen_assert(m_row+m_currentBlockRows<=m_xpr.rows()
|
---|
90 | && "Too many rows passed to comma initializer (operator<<)");
|
---|
91 | }
|
---|
92 | eigen_assert((m_col<m_xpr.cols() || (m_xpr.cols()==0 && m_col==0))
|
---|
93 | && "Too many coefficients passed to comma initializer (operator<<)");
|
---|
94 | eigen_assert(m_currentBlockRows==other.rows());
|
---|
95 | if (OtherDerived::SizeAtCompileTime != Dynamic)
|
---|
96 | m_xpr.template block<OtherDerived::RowsAtCompileTime != Dynamic ? OtherDerived::RowsAtCompileTime : 1,
|
---|
97 | OtherDerived::ColsAtCompileTime != Dynamic ? OtherDerived::ColsAtCompileTime : 1>
|
---|
98 | (m_row, m_col) = other;
|
---|
99 | else
|
---|
100 | m_xpr.block(m_row, m_col, other.rows(), other.cols()) = other;
|
---|
101 | m_col += other.cols();
|
---|
102 | return *this;
|
---|
103 | }
|
---|
104 |
|
---|
105 | inline ~CommaInitializer()
|
---|
106 | {
|
---|
107 | eigen_assert((m_row+m_currentBlockRows) == m_xpr.rows()
|
---|
108 | && m_col == m_xpr.cols()
|
---|
109 | && "Too few coefficients passed to comma initializer (operator<<)");
|
---|
110 | }
|
---|
111 |
|
---|
112 | /** \returns the built matrix once all its coefficients have been set.
|
---|
113 | * Calling finished is 100% optional. Its purpose is to write expressions
|
---|
114 | * like this:
|
---|
115 | * \code
|
---|
116 | * quaternion.fromRotationMatrix((Matrix3f() << axis0, axis1, axis2).finished());
|
---|
117 | * \endcode
|
---|
118 | */
|
---|
119 | inline XprType& finished() { return m_xpr; }
|
---|
120 |
|
---|
121 | XprType& m_xpr; // target expression
|
---|
122 | Index m_row; // current row id
|
---|
123 | Index m_col; // current col id
|
---|
124 | Index m_currentBlockRows; // current block height
|
---|
125 | };
|
---|
126 |
|
---|
127 | /** \anchor MatrixBaseCommaInitRef
|
---|
128 | * Convenient operator to set the coefficients of a matrix.
|
---|
129 | *
|
---|
130 | * The coefficients must be provided in a row major order and exactly match
|
---|
131 | * the size of the matrix. Otherwise an assertion is raised.
|
---|
132 | *
|
---|
133 | * Example: \include MatrixBase_set.cpp
|
---|
134 | * Output: \verbinclude MatrixBase_set.out
|
---|
135 | *
|
---|
136 | * \note According the c++ standard, the argument expressions of this comma initializer are evaluated in arbitrary order.
|
---|
137 | *
|
---|
138 | * \sa CommaInitializer::finished(), class CommaInitializer
|
---|
139 | */
|
---|
140 | template<typename Derived>
|
---|
141 | inline CommaInitializer<Derived> DenseBase<Derived>::operator<< (const Scalar& s)
|
---|
142 | {
|
---|
143 | return CommaInitializer<Derived>(*static_cast<Derived*>(this), s);
|
---|
144 | }
|
---|
145 |
|
---|
146 | /** \sa operator<<(const Scalar&) */
|
---|
147 | template<typename Derived>
|
---|
148 | template<typename OtherDerived>
|
---|
149 | inline CommaInitializer<Derived>
|
---|
150 | DenseBase<Derived>::operator<<(const DenseBase<OtherDerived>& other)
|
---|
151 | {
|
---|
152 | return CommaInitializer<Derived>(*static_cast<Derived *>(this), other);
|
---|
153 | }
|
---|
154 |
|
---|
155 | } // end namespace Eigen
|
---|
156 |
|
---|
157 | #endif // EIGEN_COMMAINITIALIZER_H
|
---|