Swap.h 4.29 KB
Newer Older
LM's avatar
LM committed
1 2 3 4 5
// This file is part of Eigen, a lightweight C++ template library
// for linear algebra.
//
// Copyright (C) 2006-2008 Benoit Jacob <jacob.benoit.1@gmail.com>
//
Don Gagne's avatar
Don Gagne committed
6 7 8
// This Source Code Form is subject to the terms of the Mozilla
// Public License v. 2.0. If a copy of the MPL was not distributed
// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
LM's avatar
LM committed
9 10 11 12

#ifndef EIGEN_SWAP_H
#define EIGEN_SWAP_H

Don Gagne's avatar
Don Gagne committed
13 14
namespace Eigen { 

LM's avatar
LM committed
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
/** \class SwapWrapper
  * \ingroup Core_Module
  *
  * \internal
  *
  * \brief Internal helper class for swapping two expressions
  */
namespace internal {
template<typename ExpressionType>
struct traits<SwapWrapper<ExpressionType> > : traits<ExpressionType> {};
}

template<typename ExpressionType> class SwapWrapper
  : public internal::dense_xpr_base<SwapWrapper<ExpressionType> >::type
{
  public:

    typedef typename internal::dense_xpr_base<SwapWrapper>::type Base;
    EIGEN_DENSE_PUBLIC_INTERFACE(SwapWrapper)
    typedef typename internal::packet_traits<Scalar>::type Packet;

    inline SwapWrapper(ExpressionType& xpr) : m_expression(xpr) {}

    inline Index rows() const { return m_expression.rows(); }
    inline Index cols() const { return m_expression.cols(); }
    inline Index outerStride() const { return m_expression.outerStride(); }
    inline Index innerStride() const { return m_expression.innerStride(); }
Don Gagne's avatar
Don Gagne committed
42 43 44 45 46 47 48 49 50 51 52
    
    typedef typename internal::conditional<
                       internal::is_lvalue<ExpressionType>::value,
                       Scalar,
                       const Scalar
                     >::type ScalarWithConstIfNotLvalue;
                     
    inline ScalarWithConstIfNotLvalue* data() { return m_expression.data(); }
    inline const Scalar* data() const { return m_expression.data(); }

    inline Scalar& coeffRef(Index rowId, Index colId)
LM's avatar
LM committed
53
    {
Don Gagne's avatar
Don Gagne committed
54
      return m_expression.const_cast_derived().coeffRef(rowId, colId);
LM's avatar
LM committed
55 56 57 58 59 60 61
    }

    inline Scalar& coeffRef(Index index)
    {
      return m_expression.const_cast_derived().coeffRef(index);
    }

Don Gagne's avatar
Don Gagne committed
62
    inline Scalar& coeffRef(Index rowId, Index colId) const
LM's avatar
LM committed
63
    {
Don Gagne's avatar
Don Gagne committed
64
      return m_expression.coeffRef(rowId, colId);
LM's avatar
LM committed
65 66 67 68 69 70 71 72
    }

    inline Scalar& coeffRef(Index index) const
    {
      return m_expression.coeffRef(index);
    }

    template<typename OtherDerived>
Don Gagne's avatar
Don Gagne committed
73
    void copyCoeff(Index rowId, Index colId, const DenseBase<OtherDerived>& other)
LM's avatar
LM committed
74 75
    {
      OtherDerived& _other = other.const_cast_derived();
Don Gagne's avatar
Don Gagne committed
76 77 78 79 80
      eigen_internal_assert(rowId >= 0 && rowId < rows()
                         && colId >= 0 && colId < cols());
      Scalar tmp = m_expression.coeff(rowId, colId);
      m_expression.coeffRef(rowId, colId) = _other.coeff(rowId, colId);
      _other.coeffRef(rowId, colId) = tmp;
LM's avatar
LM committed
81 82 83 84 85 86 87 88 89 90 91 92 93
    }

    template<typename OtherDerived>
    void copyCoeff(Index index, const DenseBase<OtherDerived>& other)
    {
      OtherDerived& _other = other.const_cast_derived();
      eigen_internal_assert(index >= 0 && index < m_expression.size());
      Scalar tmp = m_expression.coeff(index);
      m_expression.coeffRef(index) = _other.coeff(index);
      _other.coeffRef(index) = tmp;
    }

    template<typename OtherDerived, int StoreMode, int LoadMode>
Don Gagne's avatar
Don Gagne committed
94
    void copyPacket(Index rowId, Index colId, const DenseBase<OtherDerived>& other)
LM's avatar
LM committed
95 96
    {
      OtherDerived& _other = other.const_cast_derived();
Don Gagne's avatar
Don Gagne committed
97 98 99 100 101
      eigen_internal_assert(rowId >= 0 && rowId < rows()
                        && colId >= 0 && colId < cols());
      Packet tmp = m_expression.template packet<StoreMode>(rowId, colId);
      m_expression.template writePacket<StoreMode>(rowId, colId,
        _other.template packet<LoadMode>(rowId, colId)
LM's avatar
LM committed
102
      );
Don Gagne's avatar
Don Gagne committed
103
      _other.template writePacket<LoadMode>(rowId, colId, tmp);
LM's avatar
LM committed
104 105 106 107 108 109 110 111 112 113 114 115 116 117
    }

    template<typename OtherDerived, int StoreMode, int LoadMode>
    void copyPacket(Index index, const DenseBase<OtherDerived>& other)
    {
      OtherDerived& _other = other.const_cast_derived();
      eigen_internal_assert(index >= 0 && index < m_expression.size());
      Packet tmp = m_expression.template packet<StoreMode>(index);
      m_expression.template writePacket<StoreMode>(index,
        _other.template packet<LoadMode>(index)
      );
      _other.template writePacket<LoadMode>(index, tmp);
    }

Don Gagne's avatar
Don Gagne committed
118 119
    ExpressionType& expression() const { return m_expression; }

LM's avatar
LM committed
120 121 122 123
  protected:
    ExpressionType& m_expression;
};

Don Gagne's avatar
Don Gagne committed
124 125
} // end namespace Eigen

LM's avatar
LM committed
126
#endif // EIGEN_SWAP_H