00001
00002 #include<cstdlib>
00003 #include<string>
00004 #include<iostream>
00005
00006 #include "CACHE.hpp"
00007
00008 #ifndef _H_CS_MATRIX
00009 #define _H_CS_MATRIX
00010
00012 template < typename T > class CS_MATRIX {
00013 private:
00015 CS_MATRIX() {}
00016
00017 protected:
00019 T** _matrix;
00020
00022 unsigned long int _cols;
00023
00025 unsigned long int _rows;
00026
00027 public:
00028
00029
00030
00035 static std::string fDim( const CS_MATRIX& obj ) {
00036 std::ostringstream oss;
00037 oss << "( " << obj.rows() << " x " << obj.cols() << " )";
00038 return oss.str();
00039 }
00040
00041
00042
00043
00045 std::string _descr;
00046
00048 CS_MATRIX( unsigned long int rows, unsigned long int cols, std::string descr ) {
00049 if ( descr == "" ) {
00050 std::cerr << "Parameter description is mandatory during CS_MATRIX initialisation!" << std::endl;
00051 exit( 1 );
00052 }
00053
00054 if ( cols == 0 || rows == 0 ) {
00055 std::cerr << "Size cannot be 0 during allocation of " << descr << std::endl;
00056 exit( 1 );
00057 }
00058
00059 typedef T* T_POINTER;
00060 _matrix = new T_POINTER[ rows ];
00061 for ( unsigned long int i=0; i<rows; i++ )
00062 _matrix[ i ] = new T[ cols ];
00063 _cols = cols;
00064 _rows = rows;
00065 _descr = descr;
00066 }
00067
00069 unsigned long int cols() const { return _cols; }
00070
00072 unsigned long int rows() const { return _rows; }
00073
00075 inline
00076 T& operator() ( unsigned long int i, unsigned long int j ) {
00077 return access( i, j );
00078 }
00079
00081 T& access( unsigned long int i, unsigned long int j ) {
00082 if ( i < _rows && j < _cols ) {
00083 CACHE::getInstance()->access( &( _matrix[ i ][ j ] ), sizeof( T ) );
00084 return _matrix[ i ][ j ];
00085 } else {
00086 std::cerr << _descr << ": Index out of bounds! (" << i << "," << j << ") is not in [0," << _rows << "] x [0," << _cols << "] !" << std::endl;
00087 exit( !EXIT_SUCCESS );
00088 }
00089 }
00090
00092 const T& const_access( unsigned long int i, unsigned long int j ) const {
00093 if ( i < _rows && j < _cols ) {
00094 CACHE::getInstance()->access( &( _matrix[ i ][ j ] ), sizeof( T ) );
00095 return _matrix[ i ][ j ];
00096 } else {
00097 std::cerr << _descr << ": Index out of bounds! (" << i << "," << j << ") is not in [0," << _rows << "] x [0," << _cols << "] !" << std::endl;
00098 exit( !EXIT_SUCCESS );
00099 }
00100 }
00101
00102 ~CS_MATRIX() {
00103 for ( unsigned int i = 0; i < _rows; i++ )
00104 delete [] _matrix[ i ];
00105 delete [] _matrix;
00106 }
00107
00108 };
00109
00110 #endif
00111