Swap.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) 2006-2008 Benoit Jacob <jacob.benoit.1@gmail.com>
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_SWAP_H
26 #define EIGEN_SWAP_H
27 
28 namespace Eigen {
29 
37 namespace internal {
38 template<typename ExpressionType>
39 struct traits<SwapWrapper<ExpressionType> > : traits<ExpressionType> {};
40 }
41 
42 template<typename ExpressionType> class SwapWrapper
43  : public internal::dense_xpr_base<SwapWrapper<ExpressionType> >::type
44 {
45  public:
46 
47  typedef typename internal::dense_xpr_base<SwapWrapper>::type Base;
49  typedef typename internal::packet_traits<Scalar>::type Packet;
50 
51  inline SwapWrapper(ExpressionType& xpr) : m_expression(xpr) {}
52 
53  inline Index rows() const { return m_expression.rows(); }
54  inline Index cols() const { return m_expression.cols(); }
55  inline Index outerStride() const { return m_expression.outerStride(); }
56  inline Index innerStride() const { return m_expression.innerStride(); }
57 
58  typedef typename internal::conditional<
59  internal::is_lvalue<ExpressionType>::value,
60  Scalar,
61  const Scalar
63 
64  inline ScalarWithConstIfNotLvalue* data() { return m_expression.data(); }
65  inline const Scalar* data() const { return m_expression.data(); }
66 
67  inline Scalar& coeffRef(Index row, Index col)
68  {
69  return m_expression.const_cast_derived().coeffRef(row, col);
70  }
71 
72  inline Scalar& coeffRef(Index index)
73  {
74  return m_expression.const_cast_derived().coeffRef(index);
75  }
76 
77  inline Scalar& coeffRef(Index row, Index col) const
78  {
79  return m_expression.coeffRef(row, col);
80  }
81 
82  inline Scalar& coeffRef(Index index) const
83  {
84  return m_expression.coeffRef(index);
85  }
86 
87  template<typename OtherDerived>
88  void copyCoeff(Index row, Index col, const DenseBase<OtherDerived>& other)
89  {
90  OtherDerived& _other = other.const_cast_derived();
91  eigen_internal_assert(row >= 0 && row < rows()
92  && col >= 0 && col < cols());
93  Scalar tmp = m_expression.coeff(row, col);
94  m_expression.coeffRef(row, col) = _other.coeff(row, col);
95  _other.coeffRef(row, col) = tmp;
96  }
97 
98  template<typename OtherDerived>
99  void copyCoeff(Index index, const DenseBase<OtherDerived>& other)
100  {
101  OtherDerived& _other = other.const_cast_derived();
102  eigen_internal_assert(index >= 0 && index < m_expression.size());
103  Scalar tmp = m_expression.coeff(index);
104  m_expression.coeffRef(index) = _other.coeff(index);
105  _other.coeffRef(index) = tmp;
106  }
107 
108  template<typename OtherDerived, int StoreMode, int LoadMode>
109  void copyPacket(Index row, Index col, const DenseBase<OtherDerived>& other)
110  {
111  OtherDerived& _other = other.const_cast_derived();
112  eigen_internal_assert(row >= 0 && row < rows()
113  && col >= 0 && col < cols());
114  Packet tmp = m_expression.template packet<StoreMode>(row, col);
115  m_expression.template writePacket<StoreMode>(row, col,
116  _other.template packet<LoadMode>(row, col)
117  );
118  _other.template writePacket<LoadMode>(row, col, tmp);
119  }
120 
121  template<typename OtherDerived, int StoreMode, int LoadMode>
122  void copyPacket(Index index, const DenseBase<OtherDerived>& other)
123  {
124  OtherDerived& _other = other.const_cast_derived();
125  eigen_internal_assert(index >= 0 && index < m_expression.size());
126  Packet tmp = m_expression.template packet<StoreMode>(index);
127  m_expression.template writePacket<StoreMode>(index,
128  _other.template packet<LoadMode>(index)
129  );
130  _other.template writePacket<LoadMode>(index, tmp);
131  }
132 
133  ExpressionType& expression() const { return m_expression; }
134 
135  protected:
136  ExpressionType& m_expression;
137 };
138 
139 } // end namespace Eigen
140 
141 #endif // EIGEN_SWAP_H