ArrayWrapper.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) 2009-2010 Gael Guennebaud <gael.guennebaud@inria.fr>
5 //
6 // This Source Code Form is subject to the terms of the Mozilla
7 // Public License v. 2.0. If a copy of the MPL was not distributed
8 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 
10 #ifndef EIGEN_ARRAYWRAPPER_H
11 #define EIGEN_ARRAYWRAPPER_H
12 
13 #include "./InternalHeaderCheck.h"
14 
15 namespace Eigen {
16 
28 namespace internal {
29 template<typename ExpressionType>
30 struct traits<ArrayWrapper<ExpressionType> >
31  : public traits<remove_all_t<typename ExpressionType::Nested> >
32 {
33  typedef ArrayXpr XprKind;
34  // Let's remove NestByRefBit
35  enum {
36  Flags0 = traits<remove_all_t<typename ExpressionType::Nested> >::Flags,
37  LvalueBitFlag = is_lvalue<ExpressionType>::value ? LvalueBit : 0,
38  Flags = (Flags0 & ~(NestByRefBit | LvalueBit)) | LvalueBitFlag
39  };
40 };
41 }
42 
43 template<typename ExpressionType>
44 class ArrayWrapper : public ArrayBase<ArrayWrapper<ExpressionType> >
45 {
46  public:
50  typedef internal::remove_all_t<ExpressionType> NestedExpression;
51 
52  typedef std::conditional_t<
53  internal::is_lvalue<ExpressionType>::value,
54  Scalar,
55  const Scalar
57 
58  typedef typename internal::ref_selector<ExpressionType>::non_const_type NestedExpressionType;
59 
60  using Base::coeffRef;
61 
63  explicit EIGEN_STRONG_INLINE ArrayWrapper(ExpressionType& matrix) : m_expression(matrix) {}
64 
66  inline Index rows() const EIGEN_NOEXCEPT { return m_expression.rows(); }
68  inline Index cols() const EIGEN_NOEXCEPT { return m_expression.cols(); }
70  inline Index outerStride() const EIGEN_NOEXCEPT { return m_expression.outerStride(); }
72  inline Index innerStride() const EIGEN_NOEXCEPT { return m_expression.innerStride(); }
73 
75  inline ScalarWithConstIfNotLvalue* data() { return m_expression.data(); }
77  inline const Scalar* data() const { return m_expression.data(); }
78 
80  inline const Scalar& coeffRef(Index rowId, Index colId) const
81  {
82  return m_expression.coeffRef(rowId, colId);
83  }
84 
86  inline const Scalar& coeffRef(Index index) const
87  {
88  return m_expression.coeffRef(index);
89  }
90 
91  template<typename Dest>
93  inline void evalTo(Dest& dst) const { dst = m_expression; }
94 
98  {
99  return m_expression;
100  }
101 
105  void resize(Index newSize) { m_expression.resize(newSize); }
110 
111  protected:
113 };
114 
126 namespace internal {
127 template<typename ExpressionType>
128 struct traits<MatrixWrapper<ExpressionType> >
129  : public traits<remove_all_t<typename ExpressionType::Nested> >
130 {
131  typedef MatrixXpr XprKind;
132  // Let's remove NestByRefBit
133  enum {
134  Flags0 = traits<remove_all_t<typename ExpressionType::Nested> >::Flags,
135  LvalueBitFlag = is_lvalue<ExpressionType>::value ? LvalueBit : 0,
136  Flags = (Flags0 & ~(NestByRefBit | LvalueBit)) | LvalueBitFlag
137  };
138 };
139 }
140 
141 template<typename ExpressionType>
142 class MatrixWrapper : public MatrixBase<MatrixWrapper<ExpressionType> >
143 {
144  public:
148  typedef internal::remove_all_t<ExpressionType> NestedExpression;
149 
150  typedef std::conditional_t<
151  internal::is_lvalue<ExpressionType>::value,
152  Scalar,
153  const Scalar
155 
156  typedef typename internal::ref_selector<ExpressionType>::non_const_type NestedExpressionType;
157 
158  using Base::coeffRef;
159 
161  explicit inline MatrixWrapper(ExpressionType& matrix) : m_expression(matrix) {}
162 
164  inline Index rows() const EIGEN_NOEXCEPT { return m_expression.rows(); }
166  inline Index cols() const EIGEN_NOEXCEPT { return m_expression.cols(); }
168  inline Index outerStride() const EIGEN_NOEXCEPT { return m_expression.outerStride(); }
170  inline Index innerStride() const EIGEN_NOEXCEPT { return m_expression.innerStride(); }
171 
173  inline ScalarWithConstIfNotLvalue* data() { return m_expression.data(); }
175  inline const Scalar* data() const { return m_expression.data(); }
176 
178  inline const Scalar& coeffRef(Index rowId, Index colId) const
179  {
180  return m_expression.derived().coeffRef(rowId, colId);
181  }
182 
184  inline const Scalar& coeffRef(Index index) const
185  {
186  return m_expression.coeffRef(index);
187  }
188 
192  {
193  return m_expression;
194  }
195 
199  void resize(Index newSize) { m_expression.resize(newSize); }
204 
205  protected:
207 };
208 
209 } // end namespace Eigen
210 
211 #endif // EIGEN_ARRAYWRAPPER_H
#define EIGEN_NOEXCEPT
Definition: Macros.h:1260
#define EIGEN_CONSTEXPR
Definition: Macros.h:747
#define EIGEN_DEVICE_FUNC
Definition: Macros.h:883
#define EIGEN_DENSE_PUBLIC_INTERFACE(Derived)
Definition: Macros.h:1168
#define EIGEN_INHERIT_ASSIGNMENT_OPERATORS(Derived)
Definition: Macros.h:1122
Base class for all 1D and 2D array, and related expressions.
Definition: ArrayBase.h:43
MatrixWrapper< ArrayWrapper< ExpressionType > > matrix()
Definition: ArrayBase.h:150
Expression of a mathematical vector or matrix as an array object.
Definition: ArrayWrapper.h:45
NestedExpressionType m_expression
Definition: ArrayWrapper.h:112
EIGEN_CONSTEXPR Index outerStride() const EIGEN_NOEXCEPT
Definition: ArrayWrapper.h:70
ScalarWithConstIfNotLvalue * data()
Definition: ArrayWrapper.h:75
void resize(Index rows, Index cols)
Definition: ArrayWrapper.h:109
void resize(Index newSize)
Definition: ArrayWrapper.h:105
const internal::remove_all_t< NestedExpressionType > & nestedExpression() const
Definition: ArrayWrapper.h:97
EIGEN_CONSTEXPR Index cols() const EIGEN_NOEXCEPT
Definition: ArrayWrapper.h:68
const Scalar * data() const
Definition: ArrayWrapper.h:77
EIGEN_CONSTEXPR Index rows() const EIGEN_NOEXCEPT
Definition: ArrayWrapper.h:66
const Scalar & coeffRef(Index rowId, Index colId) const
Definition: ArrayWrapper.h:80
EIGEN_CONSTEXPR Index innerStride() const EIGEN_NOEXCEPT
Definition: ArrayWrapper.h:72
internal::ref_selector< ExpressionType >::non_const_type NestedExpressionType
Definition: ArrayWrapper.h:58
ArrayBase< ArrayWrapper > Base
Definition: ArrayWrapper.h:47
void evalTo(Dest &dst) const
Definition: ArrayWrapper.h:93
std::conditional_t< internal::is_lvalue< ExpressionType >::value, Scalar, const Scalar > ScalarWithConstIfNotLvalue
Definition: ArrayWrapper.h:56
const Scalar & coeffRef(Index index) const
Definition: ArrayWrapper.h:86
internal::remove_all_t< ExpressionType > NestedExpression
Definition: ArrayWrapper.h:50
internal::traits< Derived >::Scalar Scalar
Definition: DenseBase.h:61
CoeffReturnType value() const
Definition: DenseBase.h:524
Base class for all dense matrices, vectors, and expressions.
Definition: MatrixBase.h:52
MatrixBase< MatrixWrapper< ExpressionType > > & matrix()
Definition: MatrixBase.h:319
Expression of an array as a mathematical vector or matrix.
Definition: ArrayWrapper.h:143
ScalarWithConstIfNotLvalue * data()
Definition: ArrayWrapper.h:173
const Scalar * data() const
Definition: ArrayWrapper.h:175
EIGEN_CONSTEXPR Index cols() const EIGEN_NOEXCEPT
Definition: ArrayWrapper.h:166
MatrixBase< MatrixWrapper< ExpressionType > > Base
Definition: ArrayWrapper.h:145
void resize(Index newSize)
Definition: ArrayWrapper.h:199
const internal::remove_all_t< NestedExpressionType > & nestedExpression() const
Definition: ArrayWrapper.h:191
const Scalar & coeffRef(Index index) const
Definition: ArrayWrapper.h:184
void resize(Index rows, Index cols)
Definition: ArrayWrapper.h:203
EIGEN_CONSTEXPR Index rows() const EIGEN_NOEXCEPT
Definition: ArrayWrapper.h:164
internal::remove_all_t< ExpressionType > NestedExpression
Definition: ArrayWrapper.h:148
const Scalar & coeffRef(Index rowId, Index colId) const
Definition: ArrayWrapper.h:178
EIGEN_CONSTEXPR Index outerStride() const EIGEN_NOEXCEPT
Definition: ArrayWrapper.h:168
NestedExpressionType m_expression
Definition: ArrayWrapper.h:206
EIGEN_CONSTEXPR Index innerStride() const EIGEN_NOEXCEPT
Definition: ArrayWrapper.h:170
internal::ref_selector< ExpressionType >::non_const_type NestedExpressionType
Definition: ArrayWrapper.h:156
std::conditional_t< internal::is_lvalue< ExpressionType >::value, Scalar, const Scalar > ScalarWithConstIfNotLvalue
Definition: ArrayWrapper.h:154
const unsigned int LvalueBit
Definition: Constants.h:146
typename remove_all< T >::type remove_all_t
Definition: Meta.h:119
: InteropHeaders
Definition: Core:139
const unsigned int NestByRefBit
Definition: Constants.h:171
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:82
Definition: BFloat16.h:222
Eigen::Index Index
The interface type of indices.
Definition: EigenBase.h:41