Swap.h
Go to the documentation of this file.
00001 // This file is part of Eigen, a lightweight C++ template library
00002 // for linear algebra.
00003 //
00004 // Copyright (C) 2006-2008 Benoit Jacob <jacob.benoit.1@gmail.com>
00005 //
00006 // Eigen is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 3 of the License, or (at your option) any later version.
00010 //
00011 // Alternatively, you can redistribute it and/or
00012 // modify it under the terms of the GNU General Public License as
00013 // published by the Free Software Foundation; either version 2 of
00014 // the License, or (at your option) any later version.
00015 //
00016 // Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
00017 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00018 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
00019 // GNU General Public License for more details.
00020 //
00021 // You should have received a copy of the GNU Lesser General Public
00022 // License and a copy of the GNU General Public License along with
00023 // Eigen. If not, see <http://www.gnu.org/licenses/>.
00024 
00025 #ifndef EIGEN_SWAP_H
00026 #define EIGEN_SWAP_H
00027 
00028 namespace Eigen { 
00029 
00037 namespace internal {
00038 template<typename ExpressionType>
00039 struct traits<SwapWrapper<ExpressionType> > : traits<ExpressionType> {};
00040 }
00041 
00042 template<typename ExpressionType> class SwapWrapper
00043   : public internal::dense_xpr_base<SwapWrapper<ExpressionType> >::type
00044 {
00045   public:
00046 
00047     typedef typename internal::dense_xpr_base<SwapWrapper>::type Base;
00048     EIGEN_DENSE_PUBLIC_INTERFACE(SwapWrapper)
00049     typedef typename internal::packet_traits<Scalar>::type Packet;
00050 
00051     inline SwapWrapper(ExpressionType& xpr) : m_expression(xpr) {}
00052 
00053     inline Index rows() const { return m_expression.rows(); }
00054     inline Index cols() const { return m_expression.cols(); }
00055     inline Index outerStride() const { return m_expression.outerStride(); }
00056     inline Index innerStride() const { return m_expression.innerStride(); }
00057     
00058     typedef typename internal::conditional<
00059                        internal::is_lvalue<ExpressionType>::value,
00060                        Scalar,
00061                        const Scalar
00062                      >::type ScalarWithConstIfNotLvalue;
00063                      
00064     inline ScalarWithConstIfNotLvalue* data() { return m_expression.data(); }
00065     inline const Scalar* data() const { return m_expression.data(); }
00066 
00067     inline Scalar& coeffRef(Index row, Index col)
00068     {
00069       return m_expression.const_cast_derived().coeffRef(row, col);
00070     }
00071 
00072     inline Scalar& coeffRef(Index index)
00073     {
00074       return m_expression.const_cast_derived().coeffRef(index);
00075     }
00076 
00077     inline Scalar& coeffRef(Index row, Index col) const
00078     {
00079       return m_expression.coeffRef(row, col);
00080     }
00081 
00082     inline Scalar& coeffRef(Index index) const
00083     {
00084       return m_expression.coeffRef(index);
00085     }
00086 
00087     template<typename OtherDerived>
00088     void copyCoeff(Index row, Index col, const DenseBase<OtherDerived>& other)
00089     {
00090       OtherDerived& _other = other.const_cast_derived();
00091       eigen_internal_assert(row >= 0 && row < rows()
00092                          && col >= 0 && col < cols());
00093       Scalar tmp = m_expression.coeff(row, col);
00094       m_expression.coeffRef(row, col) = _other.coeff(row, col);
00095       _other.coeffRef(row, col) = tmp;
00096     }
00097 
00098     template<typename OtherDerived>
00099     void copyCoeff(Index index, const DenseBase<OtherDerived>& other)
00100     {
00101       OtherDerived& _other = other.const_cast_derived();
00102       eigen_internal_assert(index >= 0 && index < m_expression.size());
00103       Scalar tmp = m_expression.coeff(index);
00104       m_expression.coeffRef(index) = _other.coeff(index);
00105       _other.coeffRef(index) = tmp;
00106     }
00107 
00108     template<typename OtherDerived, int StoreMode, int LoadMode>
00109     void copyPacket(Index row, Index col, const DenseBase<OtherDerived>& other)
00110     {
00111       OtherDerived& _other = other.const_cast_derived();
00112       eigen_internal_assert(row >= 0 && row < rows()
00113                         && col >= 0 && col < cols());
00114       Packet tmp = m_expression.template packet<StoreMode>(row, col);
00115       m_expression.template writePacket<StoreMode>(row, col,
00116         _other.template packet<LoadMode>(row, col)
00117       );
00118       _other.template writePacket<LoadMode>(row, col, tmp);
00119     }
00120 
00121     template<typename OtherDerived, int StoreMode, int LoadMode>
00122     void copyPacket(Index index, const DenseBase<OtherDerived>& other)
00123     {
00124       OtherDerived& _other = other.const_cast_derived();
00125       eigen_internal_assert(index >= 0 && index < m_expression.size());
00126       Packet tmp = m_expression.template packet<StoreMode>(index);
00127       m_expression.template writePacket<StoreMode>(index,
00128         _other.template packet<LoadMode>(index)
00129       );
00130       _other.template writePacket<LoadMode>(index, tmp);
00131     }
00132 
00133     ExpressionType& expression() const { return m_expression; }
00134 
00135   protected:
00136     ExpressionType& m_expression;
00137 };
00138 
00139 } // end namespace Eigen
00140 
00141 #endif // EIGEN_SWAP_H