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 // Eigen is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 3 of the License, or (at your option) any later version.
10 //
11 // Alternatively, you can redistribute it and/or
12 // modify it under the terms of the GNU General Public License as
13 // published by the Free Software Foundation; either version 2 of
14 // the License, or (at your option) any later version.
15 //
16 // Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
17 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
19 // GNU General Public License for more details.
20 //
21 // You should have received a copy of the GNU Lesser General Public
22 // License and a copy of the GNU General Public License along with
23 // Eigen. If not, see <http://www.gnu.org/licenses/>.
24 
25 #ifndef EIGEN_ARRAYWRAPPER_H
26 #define EIGEN_ARRAYWRAPPER_H
27 
28 namespace Eigen {
29 
41 namespace internal {
42 template<typename ExpressionType>
43 struct traits<ArrayWrapper<ExpressionType> >
44  : public traits<typename remove_all<typename ExpressionType::Nested>::type >
45 {
46  typedef ArrayXpr XprKind;
47 };
48 }
49 
50 template<typename ExpressionType>
51 class ArrayWrapper : public ArrayBase<ArrayWrapper<ExpressionType> >
52 {
53  public:
57 
58  typedef typename internal::conditional<
59  internal::is_lvalue<ExpressionType>::value,
60  Scalar,
61  const Scalar
63 
64  typedef typename internal::nested<ExpressionType>::type NestedExpressionType;
65 
66  inline ArrayWrapper(ExpressionType& matrix) : m_expression(matrix) {}
67 
68  inline Index rows() const { return m_expression.rows(); }
69  inline Index cols() const { return m_expression.cols(); }
70  inline Index outerStride() const { return m_expression.outerStride(); }
71  inline Index innerStride() const { return m_expression.innerStride(); }
72 
73  inline ScalarWithConstIfNotLvalue* data() { return m_expression.data(); }
74  inline const Scalar* data() const { return m_expression.data(); }
75 
77  {
78  return m_expression.coeff(row, col);
79  }
80 
82  {
83  return m_expression.const_cast_derived().coeffRef(row, col);
84  }
85 
86  inline const Scalar& coeffRef(Index row, Index col) const
87  {
88  return m_expression.const_cast_derived().coeffRef(row, col);
89  }
90 
91  inline CoeffReturnType coeff(Index index) const
92  {
93  return m_expression.coeff(index);
94  }
95 
96  inline Scalar& coeffRef(Index index)
97  {
98  return m_expression.const_cast_derived().coeffRef(index);
99  }
100 
101  inline const Scalar& coeffRef(Index index) const
102  {
103  return m_expression.const_cast_derived().coeffRef(index);
104  }
105 
106  template<int LoadMode>
107  inline const PacketScalar packet(Index row, Index col) const
108  {
109  return m_expression.template packet<LoadMode>(row, col);
110  }
111 
112  template<int LoadMode>
113  inline void writePacket(Index row, Index col, const PacketScalar& x)
114  {
115  m_expression.const_cast_derived().template writePacket<LoadMode>(row, col, x);
116  }
117 
118  template<int LoadMode>
119  inline const PacketScalar packet(Index index) const
120  {
121  return m_expression.template packet<LoadMode>(index);
122  }
123 
124  template<int LoadMode>
125  inline void writePacket(Index index, const PacketScalar& x)
126  {
127  m_expression.const_cast_derived().template writePacket<LoadMode>(index, x);
128  }
129 
130  template<typename Dest>
131  inline void evalTo(Dest& dst) const { dst = m_expression; }
132 
133  const typename internal::remove_all<NestedExpressionType>::type&
135  {
136  return m_expression;
137  }
138 
139  protected:
141 };
142 
154 namespace internal {
155 template<typename ExpressionType>
156 struct traits<MatrixWrapper<ExpressionType> >
157  : public traits<typename remove_all<typename ExpressionType::Nested>::type >
158 {
159  typedef MatrixXpr XprKind;
160 };
161 }
162 
163 template<typename ExpressionType>
164 class MatrixWrapper : public MatrixBase<MatrixWrapper<ExpressionType> >
165 {
166  public:
170 
171  typedef typename internal::conditional<
172  internal::is_lvalue<ExpressionType>::value,
173  Scalar,
174  const Scalar
176 
177  typedef typename internal::nested<ExpressionType>::type NestedExpressionType;
178 
179  inline MatrixWrapper(ExpressionType& matrix) : m_expression(matrix) {}
180 
181  inline Index rows() const { return m_expression.rows(); }
182  inline Index cols() const { return m_expression.cols(); }
183  inline Index outerStride() const { return m_expression.outerStride(); }
184  inline Index innerStride() const { return m_expression.innerStride(); }
185 
186  inline ScalarWithConstIfNotLvalue* data() { return m_expression.data(); }
187  inline const Scalar* data() const { return m_expression.data(); }
188 
190  {
191  return m_expression.coeff(row, col);
192  }
193 
195  {
196  return m_expression.const_cast_derived().coeffRef(row, col);
197  }
198 
199  inline const Scalar& coeffRef(Index row, Index col) const
200  {
201  return m_expression.derived().coeffRef(row, col);
202  }
203 
204  inline CoeffReturnType coeff(Index index) const
205  {
206  return m_expression.coeff(index);
207  }
208 
209  inline Scalar& coeffRef(Index index)
210  {
211  return m_expression.const_cast_derived().coeffRef(index);
212  }
213 
214  inline const Scalar& coeffRef(Index index) const
215  {
216  return m_expression.const_cast_derived().coeffRef(index);
217  }
218 
219  template<int LoadMode>
220  inline const PacketScalar packet(Index row, Index col) const
221  {
222  return m_expression.template packet<LoadMode>(row, col);
223  }
224 
225  template<int LoadMode>
226  inline void writePacket(Index row, Index col, const PacketScalar& x)
227  {
228  m_expression.const_cast_derived().template writePacket<LoadMode>(row, col, x);
229  }
230 
231  template<int LoadMode>
232  inline const PacketScalar packet(Index index) const
233  {
234  return m_expression.template packet<LoadMode>(index);
235  }
236 
237  template<int LoadMode>
238  inline void writePacket(Index index, const PacketScalar& x)
239  {
240  m_expression.const_cast_derived().template writePacket<LoadMode>(index, x);
241  }
242 
243  const typename internal::remove_all<NestedExpressionType>::type&
245  {
246  return m_expression;
247  }
248 
249  protected:
251 };
252 
253 } // end namespace Eigen
254 
255 #endif // EIGEN_ARRAYWRAPPER_H