Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members   File Members  

mtable.h

Go to the documentation of this file.
00001 
00025 #ifndef __TABLE_H__
00026 #define __TABLE_H__
00027 
00028 #include<magic/mobject.h>
00029 #include<magic/mstring.h>
00030 #include<magic/mexception.h>
00031 
00032 BEGIN_NAMESPACE (MagiC);
00033 
00035 //                                                                          //
00036 //        ----              |   -----             |         /    \          //
00037 //        |   )  ___   ___  |     |    ___  |     |  ___   /      \         //
00038 //        |---   ___| |   \ | /   |    ___| |---  | /   ) <        >        //
00039 //        |     (   | |     |/    |   (   | |   ) | |---   \      /         //
00040 //        |      \__|  \__/ | \   |    \__| |__/  |  \__    \    /          //
00041 //                                                                          //
00043 
00044 // Obey the global no-bounds-checking option
00045 #ifndef NOCHECKBOUNDS
00046 #define PACKTABLE_CHECKBOUNDS 1
00047 #endif
00048 
00049 
00055 template <class TYPE>
00056 class PackTable : public Object {
00057   protected:
00058     TYPE*   mData;
00059   public:
00063     PackTable   () {
00064         mData = NULL;
00065         rows   = 0,
00066         cols   = 0;
00067     }
00068 
00074     PackTable   (int rs, int cs) {
00075         mData = NULL;
00076         rows   = 0,
00077         cols   = 0;
00078         make (rs, cs);
00079     }
00080 
00089     PackTable   (const PackTable& orig, bool deep=false) {
00090         mData = NULL;
00091         rows   = cols = 0;
00092         if (orig.mData) {
00093             if (deep) {
00094                 // Deep copy requested
00095                 make (orig.rows, orig.cols);
00096                 for (int i=0; i<rows; i++)
00097                     for (int j=0; j<cols; j++)
00098                         get(i,j) = orig.get(i,j);
00099             } else {
00100                 // Default fast but shallow copy
00101                 rows = orig.rows;
00102                 cols = orig.cols;
00103                 mData = new TYPE[rows*cols];
00104                 memcpy (mData, orig.mData, rows*cols*sizeof(double));
00105             }
00106         }
00107     }
00108     
00109     ~PackTable  () {
00110         destroy ();
00111     }
00112 
00116     void    destroy () {
00117         delete mData;
00118         mData = NULL;
00119         rows   = 0,
00120         cols   = 0;
00121     }
00122 
00129     virtual void    make    (int nrows, int ncols) {
00130         ASSERT (nrows>=0 && ncols>=0);
00131         if (mData)
00132             destroy ();
00133         rows = nrows,
00134         cols = ncols;
00135         if (rows>0 && cols>0) {
00136             // Luodaan uusi tietorakenne valmiiksi
00137             newComment (format ("PackTable<> data, %d rows, %d cols", rows, cols));
00138             mData = new TYPE [rows*cols];
00139         }
00140     }
00141 
00148     const TYPE& get (int row, int col) const {
00149 #ifdef PACKTABLE_CHECKBOUNDS
00150         if (!(row>=0 && row<rows && col>=0 && col<cols))
00151             throw out_of_bounds (strformat ("Table range overflow: %d,%d out of %d,%d",
00152                                             row, col, rows, cols));
00153 #endif
00154         return mData[row*cols+col];
00155     }
00156 
00163     TYPE&   get (int row, int col) {
00164 #ifdef PACKTABLE_CHECKBOUNDS
00165         if (!(row>=0 && row<rows && col>=0 && col<cols))
00166             throw out_of_bounds (strformat ("Table range overflow: %d,%d out of %d,%d",
00167                                             row, col, rows, cols));
00168 #endif
00169         return mData[row*cols+col];
00170     }
00171 
00174     /*
00175     virtual CArchive&   operator>>  (CArchive& arc) const {
00176         arc << rows << cols;
00177         for (int i=0; i<rows; i++)
00178             for (int j=0; j<cols; j++)
00179                 arc << get (i, j);
00180         return arc;
00181     }
00182     */
00183 
00186     /*
00187     IStream&    operator<<  (IStream& arc) {
00188         int nrows, ncols;
00189         arc >> nrows >> ncols;
00190         make (nrows, ncols);
00191         for (int i=0; i<rows; i++)
00192             for (int j=0; j<cols; j++)
00193                 arc >> get (i, j);
00194         return arc;
00195     }
00196     */
00197 
00199     int rows;
00200 
00202     int cols;
00203     
00204   private:
00205     void operator=  (const PackTable& other) {} 
00206 };
00207 
00208 END_NAMESPACE;
00209 
00210 #endif
00211 

Generated on Thu Feb 10 20:06:42 2005 for LibMagiC by doxygen1.2.18