Eigen  3.4.90 (git rev 67eeba6e720c5745abc77ae6c92ce0a44aa7b7ae)
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 
14 #include "./InternalHeaderCheck.h"
15 
16 namespace Eigen {
17 
29 template<typename XprType>
31 {
32  typedef typename XprType::Scalar Scalar;
33 
34  EIGEN_DEVICE_FUNC
35  inline CommaInitializer(XprType& xpr, const Scalar& s)
36  : m_xpr(xpr), m_row(0), m_col(1), m_currentBlockRows(1)
37  {
38  eigen_assert(m_xpr.rows() > 0 && m_xpr.cols() > 0
39  && "Cannot comma-initialize a 0x0 matrix (operator<<)");
40  m_xpr.coeffRef(0,0) = s;
41  }
42 
43  template<typename OtherDerived>
44  EIGEN_DEVICE_FUNC
45  inline CommaInitializer(XprType& xpr, const DenseBase<OtherDerived>& other)
46  : m_xpr(xpr), m_row(0), m_col(other.cols()), m_currentBlockRows(other.rows())
47  {
48  eigen_assert(m_xpr.rows() >= other.rows() && m_xpr.cols() >= other.cols()
49  && "Cannot comma-initialize a 0x0 matrix (operator<<)");
50  m_xpr.template block<OtherDerived::RowsAtCompileTime, OtherDerived::ColsAtCompileTime>(0, 0, other.rows(), other.cols()) = other;
51  }
52 
53  /* Copy/Move constructor which transfers ownership. This is crucial in
54  * absence of return value optimization to avoid assertions during destruction. */
55  // FIXME in C++11 mode this could be replaced by a proper RValue constructor
56  EIGEN_DEVICE_FUNC
57  inline CommaInitializer(const CommaInitializer& o)
58  : m_xpr(o.m_xpr), m_row(o.m_row), m_col(o.m_col), m_currentBlockRows(o.m_currentBlockRows) {
59  // Mark original object as finished. In absence of R-value references we need to const_cast:
60  const_cast<CommaInitializer&>(o).m_row = m_xpr.rows();
61  const_cast<CommaInitializer&>(o).m_col = m_xpr.cols();
62  const_cast<CommaInitializer&>(o).m_currentBlockRows = 0;
63  }
64 
65  /* inserts a scalar value in the target matrix */
66  EIGEN_DEVICE_FUNC
67  CommaInitializer& operator,(const Scalar& s)
68  {
69  if (m_col==m_xpr.cols())
70  {
71  m_row+=m_currentBlockRows;
72  m_col = 0;
73  m_currentBlockRows = 1;
74  eigen_assert(m_row<m_xpr.rows()
75  && "Too many rows passed to comma initializer (operator<<)");
76  }
77  eigen_assert(m_col<m_xpr.cols()
78  && "Too many coefficients passed to comma initializer (operator<<)");
79  eigen_assert(m_currentBlockRows==1);
80  m_xpr.coeffRef(m_row, m_col++) = s;
81  return *this;
82  }
83 
84  /* inserts a matrix expression in the target matrix */
85  template<typename OtherDerived>
86  EIGEN_DEVICE_FUNC
87  CommaInitializer& operator,(const DenseBase<OtherDerived>& other)
88  {
89  if (m_col==m_xpr.cols() && (other.cols()!=0 || other.rows()!=m_currentBlockRows))
90  {
91  m_row+=m_currentBlockRows;
92  m_col = 0;
93  m_currentBlockRows = other.rows();
94  eigen_assert(m_row+m_currentBlockRows<=m_xpr.rows()
95  && "Too many rows passed to comma initializer (operator<<)");
96  }
97  eigen_assert((m_col + other.cols() <= m_xpr.cols())
98  && "Too many coefficients passed to comma initializer (operator<<)");
99  eigen_assert(m_currentBlockRows==other.rows());
100  m_xpr.template block<OtherDerived::RowsAtCompileTime, OtherDerived::ColsAtCompileTime>
101  (m_row, m_col, other.rows(), other.cols()) = other;
102  m_col += other.cols();
103  return *this;
104  }
105 
106  EIGEN_DEVICE_FUNC
107  inline ~CommaInitializer()
108 #if defined VERIFY_RAISES_ASSERT && (!defined EIGEN_NO_ASSERTION_CHECKING) && defined EIGEN_EXCEPTIONS
109  EIGEN_EXCEPTION_SPEC(Eigen::eigen_assert_exception)
110 #endif
111  {
112  finished();
113  }
114 
122  EIGEN_DEVICE_FUNC
123  inline XprType& finished() {
124  eigen_assert(((m_row+m_currentBlockRows) == m_xpr.rows() || m_xpr.cols() == 0)
125  && m_col == m_xpr.cols()
126  && "Too few coefficients passed to comma initializer (operator<<)");
127  return m_xpr;
128  }
129 
130  XprType& m_xpr; // target expression
131  Index m_row; // current row id
132  Index m_col; // current col id
133  Index m_currentBlockRows; // current block height
134 };
135 
149 template<typename Derived>
150 EIGEN_DEVICE_FUNC inline CommaInitializer<Derived> DenseBase<Derived>::operator<< (const Scalar& s)
151 {
152  return CommaInitializer<Derived>(*static_cast<Derived*>(this), s);
153 }
154 
156 template<typename Derived>
157 template<typename OtherDerived>
158 EIGEN_DEVICE_FUNC inline CommaInitializer<Derived>
159 DenseBase<Derived>::operator<<(const DenseBase<OtherDerived>& other)
160 {
161  return CommaInitializer<Derived>(*static_cast<Derived *>(this), other);
162 }
163 
164 } // end namespace Eigen
165 
166 #endif // EIGEN_COMMAINITIALIZER_H
Base class for all dense matrices, vectors, and arrays.
Definition: DenseBase.h:42
CommaInitializer< Derived > operator<<(const Scalar &s)
Definition: CommaInitializer.h:150
EIGEN_CONSTEXPR Index cols() const EIGEN_NOEXCEPT
Definition: EigenBase.h:65
EIGEN_CONSTEXPR Index rows() const EIGEN_NOEXCEPT
Definition: EigenBase.h:62
Namespace containing all symbols from the Eigen library.
Definition: Core:139
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:59
Helper class used by the comma initializer operator.
Definition: CommaInitializer.h:31
XprType & finished()
Definition: CommaInitializer.h:123