2008-01-15 13:55:47 +00:00
|
|
|
// This file is part of Eigen, a lightweight C++ template library
|
2009-05-22 20:25:33 +02:00
|
|
|
// for linear algebra.
|
2008-01-15 13:55:47 +00:00
|
|
|
//
|
2008-11-24 13:40:43 +00:00
|
|
|
// Copyright (C) 2006-2008 Benoit Jacob <jacob.benoit.1@gmail.com>
|
2008-01-15 13:55:47 +00:00
|
|
|
//
|
2008-02-28 15:44:45 +00:00
|
|
|
// Eigen is free software; you can redistribute it and/or
|
|
|
|
|
// modify it under the terms of the GNU Lesser General Public
|
2008-06-02 22:58:36 +00:00
|
|
|
// License as published by the Free Software Foundation; either
|
2008-02-28 15:44:45 +00:00
|
|
|
// version 3 of the License, or (at your option) any later version.
|
|
|
|
|
//
|
|
|
|
|
// Alternatively, you can redistribute it and/or
|
|
|
|
|
// modify it under the terms of the GNU General Public License as
|
2008-06-02 22:58:36 +00:00
|
|
|
// published by the Free Software Foundation; either version 2 of
|
2008-02-28 15:44:45 +00:00
|
|
|
// the License, or (at your option) any later version.
|
2008-01-15 13:55:47 +00:00
|
|
|
//
|
|
|
|
|
// Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
|
|
|
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
2008-02-28 15:44:45 +00:00
|
|
|
// FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
|
|
|
|
|
// GNU General Public License for more details.
|
2008-01-15 13:55:47 +00:00
|
|
|
//
|
2008-06-02 22:58:36 +00:00
|
|
|
// You should have received a copy of the GNU Lesser General Public
|
2008-02-28 15:44:45 +00:00
|
|
|
// License and a copy of the GNU General Public License along with
|
|
|
|
|
// Eigen. If not, see <http://www.gnu.org/licenses/>.
|
2008-01-15 13:55:47 +00:00
|
|
|
|
|
|
|
|
#ifndef EIGEN_SWAP_H
|
|
|
|
|
#define EIGEN_SWAP_H
|
|
|
|
|
|
2008-08-05 21:55:57 +00:00
|
|
|
/** \class SwapWrapper
|
2010-07-06 13:10:08 +01:00
|
|
|
* \ingroup Core_Module
|
2008-08-05 21:55:57 +00:00
|
|
|
*
|
2008-08-07 04:31:05 +00:00
|
|
|
* \internal
|
2008-08-05 21:55:57 +00:00
|
|
|
*
|
2008-08-07 04:31:05 +00:00
|
|
|
* \brief Internal helper class for swapping two expressions
|
2008-08-05 21:55:57 +00:00
|
|
|
*/
|
|
|
|
|
template<typename ExpressionType>
|
2009-11-17 16:04:19 +01:00
|
|
|
struct ei_traits<SwapWrapper<ExpressionType> > : ei_traits<ExpressionType> {};
|
2008-08-05 21:55:57 +00:00
|
|
|
|
|
|
|
|
template<typename ExpressionType> class SwapWrapper
|
2010-04-23 12:16:30 -04:00
|
|
|
: public ei_dense_xpr_base<SwapWrapper<ExpressionType> >::type
|
2008-08-05 21:55:57 +00:00
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
|
2010-04-23 12:16:30 -04:00
|
|
|
typedef typename ei_dense_xpr_base<SwapWrapper>::type Base;
|
2010-01-22 10:15:41 +01:00
|
|
|
EIGEN_DENSE_PUBLIC_INTERFACE(SwapWrapper)
|
2008-08-05 21:55:57 +00:00
|
|
|
typedef typename ei_packet_traits<Scalar>::type Packet;
|
|
|
|
|
|
2008-08-09 04:37:09 +00:00
|
|
|
inline SwapWrapper(ExpressionType& xpr) : m_expression(xpr) {}
|
2008-08-05 21:55:57 +00:00
|
|
|
|
2010-05-30 16:00:58 -04:00
|
|
|
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(); }
|
2008-08-05 21:55:57 +00:00
|
|
|
|
2010-05-30 16:00:58 -04:00
|
|
|
inline Scalar& coeffRef(Index row, Index col)
|
2008-08-09 18:41:24 +00:00
|
|
|
{
|
|
|
|
|
return m_expression.const_cast_derived().coeffRef(row, col);
|
|
|
|
|
}
|
|
|
|
|
|
2010-05-30 16:00:58 -04:00
|
|
|
inline Scalar& coeffRef(Index index)
|
2008-08-09 18:41:24 +00:00
|
|
|
{
|
|
|
|
|
return m_expression.const_cast_derived().coeffRef(index);
|
|
|
|
|
}
|
|
|
|
|
|
2008-08-05 21:55:57 +00:00
|
|
|
template<typename OtherDerived>
|
2010-05-30 16:00:58 -04:00
|
|
|
void copyCoeff(Index row, Index col, const DenseBase<OtherDerived>& other)
|
2008-08-05 21:55:57 +00:00
|
|
|
{
|
|
|
|
|
OtherDerived& _other = other.const_cast_derived();
|
|
|
|
|
ei_internal_assert(row >= 0 && row < rows()
|
|
|
|
|
&& col >= 0 && col < cols());
|
|
|
|
|
Scalar tmp = m_expression.coeff(row, col);
|
|
|
|
|
m_expression.coeffRef(row, col) = _other.coeff(row, col);
|
|
|
|
|
_other.coeffRef(row, col) = tmp;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<typename OtherDerived>
|
2010-05-30 16:00:58 -04:00
|
|
|
void copyCoeff(Index index, const DenseBase<OtherDerived>& other)
|
2008-08-05 21:55:57 +00:00
|
|
|
{
|
|
|
|
|
OtherDerived& _other = other.const_cast_derived();
|
|
|
|
|
ei_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;
|
|
|
|
|
}
|
|
|
|
|
|
2008-08-09 18:41:24 +00:00
|
|
|
template<typename OtherDerived, int StoreMode, int LoadMode>
|
2010-05-30 16:00:58 -04:00
|
|
|
void copyPacket(Index row, Index col, const DenseBase<OtherDerived>& other)
|
2008-08-05 21:55:57 +00:00
|
|
|
{
|
|
|
|
|
OtherDerived& _other = other.const_cast_derived();
|
|
|
|
|
ei_internal_assert(row >= 0 && row < rows()
|
|
|
|
|
&& col >= 0 && col < cols());
|
2008-08-09 18:41:24 +00:00
|
|
|
Packet tmp = m_expression.template packet<StoreMode>(row, col);
|
|
|
|
|
m_expression.template writePacket<StoreMode>(row, col,
|
|
|
|
|
_other.template packet<LoadMode>(row, col)
|
2008-08-05 21:55:57 +00:00
|
|
|
);
|
2008-08-09 18:41:24 +00:00
|
|
|
_other.template writePacket<LoadMode>(row, col, tmp);
|
2008-08-05 21:55:57 +00:00
|
|
|
}
|
|
|
|
|
|
2008-08-09 18:41:24 +00:00
|
|
|
template<typename OtherDerived, int StoreMode, int LoadMode>
|
2010-05-30 16:00:58 -04:00
|
|
|
void copyPacket(Index index, const DenseBase<OtherDerived>& other)
|
2008-08-05 21:55:57 +00:00
|
|
|
{
|
|
|
|
|
OtherDerived& _other = other.const_cast_derived();
|
|
|
|
|
ei_internal_assert(index >= 0 && index < m_expression.size());
|
2008-08-09 18:41:24 +00:00
|
|
|
Packet tmp = m_expression.template packet<StoreMode>(index);
|
|
|
|
|
m_expression.template writePacket<StoreMode>(index,
|
|
|
|
|
_other.template packet<LoadMode>(index)
|
2008-08-05 21:55:57 +00:00
|
|
|
);
|
2008-08-09 18:41:24 +00:00
|
|
|
_other.template writePacket<LoadMode>(index, tmp);
|
2008-08-05 21:55:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected:
|
2008-08-09 04:37:09 +00:00
|
|
|
ExpressionType& m_expression;
|
2008-08-05 21:55:57 +00:00
|
|
|
};
|
2008-06-02 22:58:36 +00:00
|
|
|
|
2008-03-26 08:48:04 +00:00
|
|
|
/** swaps *this with the expression \a other.
|
|
|
|
|
*
|
2008-08-07 04:31:05 +00:00
|
|
|
* \note \a other is only marked for internal reasons, but of course
|
|
|
|
|
* it gets const-casted. One reason is that one will often call swap
|
|
|
|
|
* on temporary objects (hence non-const references are forbidden).
|
|
|
|
|
* Another reason is that lazyAssign takes a const argument anyway.
|
2008-03-26 08:48:04 +00:00
|
|
|
*/
|
2008-03-10 17:23:11 +00:00
|
|
|
template<typename Derived>
|
2008-01-15 13:55:47 +00:00
|
|
|
template<typename OtherDerived>
|
2010-01-04 19:00:16 +01:00
|
|
|
void DenseBase<Derived>::swap(DenseBase<OtherDerived> EIGEN_REF_TO_TEMPORARY other)
|
2008-01-15 13:55:47 +00:00
|
|
|
{
|
2008-08-21 13:17:21 +00:00
|
|
|
(SwapWrapper<Derived>(derived())).lazyAssign(other);
|
2008-01-15 13:55:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endif // EIGEN_SWAP_H
|