00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef __MBASE_HH
00021 #define __MBASE_HH
00022 #include <assert.h>
00023
00024
00025 template <class Accessor>
00026 class MatrixBase : public Accessor {
00027 public:
00028 const double* get_data_ptr()const{return this->my_values;}
00029 double* get_data_ptr(){return this->my_values;}
00030
00031 DynamicMatrix<Accessor>& down_cast() {return reinterpret_cast<DynamicMatrix<Accessor>&> (*this);}
00032 const DynamicMatrix<Accessor>& down_cast() const {return reinterpret_cast<const DynamicMatrix<Accessor>&> (*this);}
00033 };
00034
00035
00036 template <class Accessor>
00037 std::ostream& operator << (std::ostream& os, const MatrixBase<Accessor>& m){
00038 for(int r=0; r<m.num_rows(); r++){
00039 os << m[r] << std::endl;
00040 }
00041 return os;
00042 }
00043
00044
00045 template <class Accessor>
00046 std::istream& operator >> (std::istream& is, MatrixBase<Accessor>& m){
00047 for(int r=0; r<m.num_rows(); r++){
00048 is >> m[r];
00049 }
00050 return is;
00051 }
00052
00053
00054
00055 template <class Accessor1, class Accessor2>
00056 struct MatrixCopy {
00057 inline static void eval(MatrixBase<Accessor1>& to, const MatrixBase<Accessor2>& from){
00058 for(int r=0; r<from.num_rows(); r++){
00059 for(int c=0; c<from.num_cols(); c++){
00060 to(r,c)=from(r,c);
00061 }
00062 }
00063 }
00064 };
00065
00066 template<int Rows, int Cols, class Layout, class Zone1, class Zone2>
00067 struct MatrixCopy<FixedMAccessor<Rows,Cols,Layout,Zone1>,FixedMAccessor<Rows,Cols,Layout,Zone2> > {
00068 inline static void eval(MatrixBase<FixedMAccessor<Rows,Cols,Layout,Zone1> >& to,
00069 const MatrixBase<FixedMAccessor<Rows,Cols,Layout,Zone2> >& from) {
00070 memcpy(to.get_data_ptr(), from.get_data_ptr(), Rows*Cols*sizeof(double));
00071 }
00072 };
00073
00074 template<class Layout>
00075 struct MatrixCopy<DynamicMAccessor<Layout>,DynamicMAccessor<Layout> > {
00076 inline static void eval(MatrixBase<DynamicMAccessor<Layout> >& to,
00077 const MatrixBase<DynamicMAccessor<Layout> >& from){
00078 memcpy(to.get_data_ptr(), from.get_data_ptr(), from.num_rows()*from.num_cols()*sizeof(double));
00079 }
00080 };
00081
00082
00084
00085
00086
00087
00088
00090
00091 template<int Rows, int Cols, class Accessor>
00092 struct FixedMatrix : public MatrixBase<Accessor> {
00093
00094 template<class Accessor2>
00095 inline FixedMatrix& operator=(const FixedMatrix<Rows,Cols,Accessor2>& from){
00096 MatrixCopy<Accessor, Accessor2>::eval(*this,from);
00097 return *this;
00098 }
00099
00100
00101 inline FixedMatrix& operator=(const FixedMatrix& from){
00102 MatrixCopy<Accessor,Accessor>::eval(*this,from);
00103 return *this;
00104 }
00105
00106
00107 template<class Accessor2>
00108 inline FixedMatrix& operator=(const DynamicMatrix<Accessor2>& from){
00109 assert(from.num_rows() == Rows && from.num_cols() == Cols);
00110 MatrixCopy<Accessor,Accessor2>::eval(*this,from);
00111 return *this;
00112 }
00113 static void dummy() {}
00114 };
00115
00116 template<class Accessor>
00117 struct DynamicMatrix : public MatrixBase<Accessor> {
00118
00119 template<class Accessor2>
00120 DynamicMatrix& operator=(const MatrixBase<Accessor2>& from){
00121 assert(from.num_rows() == this->num_rows() && from.num_cols() == this->num_cols());
00122 MatrixCopy<Accessor,Accessor2>::eval(*this,from);
00123 return *this;
00124 }
00125
00126
00127
00128 DynamicMatrix& operator=(const DynamicMatrix& from){
00129 assert(from.num_rows() == this->num_rows() && from.num_cols() == this->num_cols());
00130 MatrixCopy<Accessor,Accessor>::eval(*this,from);
00131 return *this;
00132 }
00133
00134 operator DynamicMatrix& () { return *this; }
00135 operator const DynamicMatrix& () const { return *this; }
00136
00137 };
00138
00139
00140 #endif