Eigen  3.4.90 (git rev 5a9f66fb35d03a4da9ef8976e67a61b30aa16dcf)
 
Loading...
Searching...
No Matches
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 <[email protected]>
5// Copyright (C) 2006-2008 Benoit Jacob <[email protected]>
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
17namespace Eigen {
18
30template <typename XprType>
32 typedef typename XprType::Scalar Scalar;
33
34 EIGEN_DEVICE_FUNC inline CommaInitializer(XprType& xpr, const Scalar& s)
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>
41 EIGEN_DEVICE_FUNC inline CommaInitializer(XprType& xpr, const DenseBase<OtherDerived>& other)
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
52 EIGEN_DEVICE_FUNC inline CommaInitializer(const CommaInitializer& o)
53 : m_xpr(o.m_xpr), m_row(o.m_row), m_col(o.m_col), m_currentBlockRows(o.m_currentBlockRows) {
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 */
61 EIGEN_DEVICE_FUNC CommaInitializer &operator,(const Scalar& s) {
62 if (m_col == m_xpr.cols()) {
63 m_row += m_currentBlockRows;
64 m_col = 0;
65 m_currentBlockRows = 1;
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<<)");
69 eigen_assert(m_currentBlockRows == 1);
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>
76 EIGEN_DEVICE_FUNC CommaInitializer &operator,(const DenseBase<OtherDerived>& other) {
77 if (m_col == m_xpr.cols() && (other.cols() != 0 || other.rows() != m_currentBlockRows)) {
78 m_row += m_currentBlockRows;
79 m_col = 0;
80 m_currentBlockRows = other.rows();
81 eigen_assert(m_row + m_currentBlockRows <= m_xpr.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
93 EIGEN_DEVICE_FUNC inline ~CommaInitializer()
94#if defined VERIFY_RAISES_ASSERT && (!defined EIGEN_NO_ASSERTION_CHECKING) && defined EIGEN_EXCEPTIONS
95 EIGEN_EXCEPTION_SPEC(Eigen::eigen_assert_exception)
96#endif
97 {
98 finished();
99 }
100
108 EIGEN_DEVICE_FUNC inline XprType& finished() {
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
134template <typename Derived>
135EIGEN_DEVICE_FUNC inline CommaInitializer<Derived> DenseBase<Derived>::operator<<(const Scalar& s) {
136 return CommaInitializer<Derived>(*static_cast<Derived*>(this), s);
137}
138
140template <typename Derived>
141template <typename OtherDerived>
142EIGEN_DEVICE_FUNC inline CommaInitializer<Derived> DenseBase<Derived>::operator<<(
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
Base class for all dense matrices, vectors, and arrays.
Definition DenseBase.h:44
EIGEN_CONSTEXPR Index cols() const EIGEN_NOEXCEPT
Definition EigenBase.h:61
EIGEN_CONSTEXPR Index rows() const EIGEN_NOEXCEPT
Definition EigenBase.h:59
Namespace containing all symbols from the Eigen library.
Definition Core:137
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition Meta.h:83
Helper class used by the comma initializer operator.
Definition CommaInitializer.h:31
XprType & finished()
Definition CommaInitializer.h:108