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 #include "./InternalHeaderCheck.h"
15 
16 namespace Eigen {
17 
29 template<typename XprType>
31 {
32  typedef typename XprType::Scalar Scalar;
33 
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>
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
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 */
68  {
69  if (m_col==m_xpr.cols())
70  {
72  m_col = 0;
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<<)");
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>
88  {
89  if (m_col==m_xpr.cols() && (other.cols()!=0 || other.rows()!=m_currentBlockRows))
90  {
92  m_col = 0;
93  m_currentBlockRows = other.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<<)");
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 
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 
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>
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
#define EIGEN_DEVICE_FUNC
Definition: Macros.h:883
#define EIGEN_EXCEPTION_SPEC(X)
Definition: Macros.h:1263
#define eigen_assert(x)
Definition: Macros.h:902
Base class for all dense matrices, vectors, and arrays.
Definition: DenseBase.h:42
CommaInitializer< Derived > operator<<(const Scalar &s)
EIGEN_CONSTEXPR Index cols() const EIGEN_NOEXCEPT
Definition: EigenBase.h:65
EIGEN_CONSTEXPR Index rows() const EIGEN_NOEXCEPT
Definition: EigenBase.h:62
: InteropHeaders
Definition: Core:139
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:82
Helper class used by the comma initializer operator.
CommaInitializer & operator,(const DenseBase< OtherDerived > &other)
CommaInitializer(const CommaInitializer &o)
CommaInitializer(XprType &xpr, const Scalar &s)
CommaInitializer(XprType &xpr, const DenseBase< OtherDerived > &other)
CommaInitializer & operator,(const Scalar &s)