RowRemapView.h

00001 /*
00002  * Expression Template Matrix Library
00003  *
00004  * Copyright (C) 2004 - 2006 Ricky Lung <mtlung@users.sourceforge.net>
00005  *
00006  * This library 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 2.1 of the License, or (at your option) any later version.
00010  *
00011  * This library is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Lesser General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU Lesser General Public
00017  * License along with this library; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019  *
00020  */
00021 
00022 
00024 struct RowReMapTag : public UnaExpTag, public ViewTag {
00025     EXMAT_INLINE2 RowReMapTag() {}
00026     static char* name()
00027     {   return "Row remapped";  }
00028 };
00029 
00031 template<class Rep>
00032 class RowReMapView : public RowReMapTag
00033 {
00034 public:
00035     typedef RowReMapView<Rep>   self_type;
00036     typedef RowReMapTag         exp_tag;
00037     typedef NullTag             extra_param_type;
00038     COMMON_VIEW_TYPEDEFS();
00039 
00040     enum {
00041         ROWS = super_type::COLS,
00042         COLS = super_type::ROWS,
00043         IsLinear = false,
00044         IsLinearRecursive = IsLinear && super_type::IsLinearRecursive,
00045         TNOP = 0,
00046         ENOP = 0,
00047         EXPLevel = Rep::EXPLevel + 1
00048     };
00049 
00051     typedef Mat<DenseMatCon<value_type,ROWS,COLS>, EmptyErrorChecker> temp_type;
00052 
00053     /************************************************/
00054     // Constructors
00055     EXMAT_INLINE2 RowReMapView(super_type& mat)
00056         : rep(mat)
00057     {
00058         InitTable();
00059     }
00061     template<class Rep2> EXMAT_INLINE2
00062     RowReMapView(const Rep2& self, super_type& mat)
00063         : rep(mat)
00064     {
00065         memcpy(rowMap, self.rowMap, sizeof(rowMap));
00066     }
00067 
00069     EXMAT_INLINE2 self_type operator=(const_param_type s) {
00070         rep = s;
00071         return *this;
00072     }
00073     /************************************************/
00074     // Dimension information
00075     EXMAT_INLINE2 size_t size() const { return rep.size(); }
00076     EXMAT_INLINE2 size_t rows() const { return rep.cols(); }
00077     EXMAT_INLINE2 size_t cols() const { return rep.rows(); }
00078     /************************************************/
00079     // Index function (for internal use, no error checking applied)
00080     EXMAT_INLINE2 reference setAt(index_type i) {
00081         if(ROWS == 1)
00082             return rep.setAt(i);
00083         else
00084             return rep.setAt(rowMap[i/rep.cols()], i%rep.cols());
00085     }
00086     EXMAT_INLINE2 const_reference getAt(index_type i) const {
00087         if(ROWS == 1)
00088             return rep.getAt(i);
00089         else
00090             return rep.getAt(rowMap[i/rep.cols()], i%rep.cols());
00091     }
00092     EXMAT_INLINE2 reference setAt(index_type i, index_type j) {
00093         return rep.setAt(rowMap[i], j);
00094     }
00095     EXMAT_INLINE2 const_reference getAt(index_type i, index_type j) const
00096     {   return rep.getAt(rowMap[i], j); }
00097 
00098     // Functions for re-mapping the row index
00099     // Do exchange of index
00100     EXMAT_INLINE2 void exchange(index_type r1, index_type r2) {
00101         // Or use bitwise tricks to do swaping
00102         index_type tmp = rowMap[r2];
00103         rowMap[r2] = rowMap[r1];
00104         rowMap[r1] = tmp;
00105     }
00106 
00107 private:
00108     EXMAT_INLINE2 void InitTable() {
00109         for(index_type i=0; i<ROWS; ++i)
00110             rowMap[i] = i;
00111     }
00112 private:
00113     index_type rowMap[ROWS];
00114 };
00115 
00116 template<class Rep, class ErrChk> EXMAT_INLINE2 static
00117 Mat<ExpMat<RowReMapView<typename Rep::rep_type> >, ErrChk>
00118 rowremap(Mat<Rep, ErrChk>& mat) {
00119     return ExpMat<RowReMapView<TTYPENAME Rep::rep_type> >(mat);
00120 }

Generated on Sat May 6 23:12:00 2006 for Exmat by  doxygen 1.4.6-NO