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 // 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 // IWYU pragma: private
15 #include "./InternalHeaderCheck.h"
16 
17 namespace Eigen {
18 
30 template <typename XprType>
32  typedef typename XprType::Scalar Scalar;
33 
35  : m_xpr(xpr), m_row(0), m_col(1), m_currentBlockRows(1) {
36  eigen_assert(m_xpr.rows() > 0 && m_xpr.cols() > 0 && "Cannot comma-initialize a 0x0 matrix (operator<<)");
37  m_xpr.coeffRef(0, 0) = s;
38  }
39 
40  template <typename OtherDerived>
42  : m_xpr(xpr), m_row(0), m_col(other.cols()), m_currentBlockRows(other.rows()) {
43  eigen_assert(m_xpr.rows() >= other.rows() && m_xpr.cols() >= other.cols() &&
44  "Cannot comma-initialize a 0x0 matrix (operator<<)");
45  m_xpr.template block<OtherDerived::RowsAtCompileTime, OtherDerived::ColsAtCompileTime>(0, 0, other.rows(),
46  other.cols()) = other;
47  }
48 
49  /* Copy/Move constructor which transfers ownership. This is crucial in
50  * absence of return value optimization to avoid assertions during destruction. */
51  // FIXME in C++11 mode this could be replaced by a proper RValue constructor
54  // Mark original object as finished. In absence of R-value references we need to const_cast:
55  const_cast<CommaInitializer&>(o).m_row = m_xpr.rows();
56  const_cast<CommaInitializer&>(o).m_col = m_xpr.cols();
57  const_cast<CommaInitializer&>(o).m_currentBlockRows = 0;
58  }
59 
60  /* inserts a scalar value in the target matrix */
62  if (m_col == m_xpr.cols()) {
64  m_col = 0;
66  eigen_assert(m_row < m_xpr.rows() && "Too many rows passed to comma initializer (operator<<)");
67  }
68  eigen_assert(m_col < m_xpr.cols() && "Too many coefficients passed to comma initializer (operator<<)");
70  m_xpr.coeffRef(m_row, m_col++) = s;
71  return *this;
72  }
73 
74  /* inserts a matrix expression in the target matrix */
75  template <typename OtherDerived>
77  if (m_col == m_xpr.cols() && (other.cols() != 0 || other.rows() != m_currentBlockRows)) {
79  m_col = 0;
80  m_currentBlockRows = other.rows();
82  "Too many rows passed to comma initializer (operator<<)");
83  }
84  eigen_assert((m_col + other.cols() <= m_xpr.cols()) &&
85  "Too many coefficients passed to comma initializer (operator<<)");
86  eigen_assert(m_currentBlockRows == other.rows());
87  m_xpr.template block<OtherDerived::RowsAtCompileTime, OtherDerived::ColsAtCompileTime>(m_row, m_col, other.rows(),
88  other.cols()) = other;
89  m_col += other.cols();
90  return *this;
91  }
92 
94 #if defined VERIFY_RAISES_ASSERT && (!defined EIGEN_NO_ASSERTION_CHECKING) && defined EIGEN_EXCEPTIONS
96 #endif
97  {
98  finished();
99  }
100 
109  eigen_assert(((m_row + m_currentBlockRows) == m_xpr.rows() || m_xpr.cols() == 0) && m_col == m_xpr.cols() &&
110  "Too few coefficients passed to comma initializer (operator<<)");
111  return m_xpr;
112  }
113 
114  XprType& m_xpr; // target expression
115  Index m_row; // current row id
116  Index m_col; // current col id
117  Index m_currentBlockRows; // current block height
118 };
119 
134 template <typename Derived>
136  return CommaInitializer<Derived>(*static_cast<Derived*>(this), s);
137 }
138 
140 template <typename Derived>
141 template <typename OtherDerived>
143  const DenseBase<OtherDerived>& other) {
144  return CommaInitializer<Derived>(*static_cast<Derived*>(this), other);
145 }
146 
147 } // end namespace Eigen
148 
149 #endif // EIGEN_COMMAINITIALIZER_H
#define EIGEN_DEVICE_FUNC
Definition: Macros.h:892
#define EIGEN_EXCEPTION_SPEC(X)
Definition: Macros.h:1270
#define eigen_assert(x)
Definition: Macros.h:910
int rows
Definition: Tutorial_commainit_02.cpp:1
int cols
Definition: Tutorial_commainit_02.cpp:1
SCALAR Scalar
Definition: bench_gemm.cpp:45
Base class for all dense matrices, vectors, and arrays.
Definition: DenseBase.h:44
internal::traits< Derived >::Scalar Scalar
Definition: DenseBase.h:62
EIGEN_DEVICE_FUNC CommaInitializer< Derived > operator<<(const Scalar &s)
Definition: CommaInitializer.h:135
RealScalar s
Definition: level1_cplx_impl.h:130
Namespace containing all symbols from the Eigen library.
Definition: bench_norm.cpp:70
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:83
CwiseBinaryOp< internal::scalar_sum_op< double, double >, const CpyMatrixXd, const CpyMatrixXd > XprType
Definition: nestbyvalue.cpp:15
Helper class used by the comma initializer operator.
Definition: CommaInitializer.h:31
EIGEN_DEVICE_FUNC CommaInitializer(const CommaInitializer &o)
Definition: CommaInitializer.h:52
XprType & m_xpr
Definition: CommaInitializer.h:114
XprType::Scalar Scalar
Definition: CommaInitializer.h:32
EIGEN_DEVICE_FUNC XprType & finished()
Definition: CommaInitializer.h:108
EIGEN_DEVICE_FUNC CommaInitializer(XprType &xpr, const DenseBase< OtherDerived > &other)
Definition: CommaInitializer.h:41
EIGEN_DEVICE_FUNC CommaInitializer & operator,(const DenseBase< OtherDerived > &other)
Definition: CommaInitializer.h:76
Index m_currentBlockRows
Definition: CommaInitializer.h:117
EIGEN_DEVICE_FUNC CommaInitializer & operator,(const Scalar &s)
Definition: CommaInitializer.h:61
Index m_col
Definition: CommaInitializer.h:116
EIGEN_DEVICE_FUNC ~CommaInitializer()
Definition: CommaInitializer.h:93
EIGEN_DEVICE_FUNC CommaInitializer(XprType &xpr, const Scalar &s)
Definition: CommaInitializer.h:34
Index m_row
Definition: CommaInitializer.h:115
Definition: main.h:246