Main Page   Class Hierarchy   Compound List   File List   Compound Members  

mtable.h

00001 #ifndef __TABLE_H__
00002 #define __TABLE_H__
00003 
00004 #include<magic/mobject.h>
00005 #include<magic/mstring.h>
00006 #include<magic/mmagisupp.h>
00007 
00009 //                                                                          //
00010 //        ----              |   -----             |         /    \          //
00011 //        |   )  ___   ___  |     |    ___  |     |  ___   /      \         //
00012 //        |---   ___| |   \ | /   |    ___| |---  | /   ) <        >        //
00013 //        |     (   | |     |/    |   (   | |   ) | |---   \      /         //
00014 //        |      \__|  \__/ | \   |    \__| |__/  |  \__    \    /          //
00015 //                                                                          //
00017 
00018 // Obey the global no-bounds-checking option
00019 #ifndef NOCHECKBOUNDS
00020 #define PACKTABLE_CHECKBOUNDS 1
00021 #endif
00022 
00023 
00029 template <class TYPE>
00030 class PackTable : public Object {
00031   protected:
00032     TYPE*   data;
00033   public:
00037     PackTable   () {
00038         data = NULL;
00039         rows = 0,
00040         cols = 0;
00041     }
00042 
00048     PackTable   (int rs, int cs) {
00049         data = NULL;
00050         rows = 0,
00051         cols = 0;
00052         make (rs, cs);
00053     }
00054 
00063     PackTable   (const PackTable& orig, bool deep=false) {
00064         data = NULL;
00065         rows = cols = 0;
00066         if (orig.data) {
00067             if (deep) {
00068                 // Deep copy requested
00069                 make (orig.rows, orig.cols);
00070                 for (int i=0; i<rows; i++)
00071                     for (int j=0; j<cols; j++)
00072                         get(i,j) = orig.get(i,j);
00073             } else {
00074                 // Default fast but shallow copy
00075                 rows = orig.rows;
00076                 cols = orig.cols;
00077                 data = new TYPE[rows*cols];
00078                 memcpy (data, orig.data, rows*cols);
00079             }
00080         }
00081     }
00082     
00083     ~PackTable  () {
00084         destroy ();
00085     }
00086 
00090     void    destroy () {
00091         delete data;
00092         data = NULL;
00093         rows = 0,
00094         cols = 0;
00095     }
00096 
00103     virtual void    make    (int nrows, int ncols) {
00104         ASSERT (nrows>=0 && ncols>=0);
00105         if (data)
00106             destroy ();
00107         rows = nrows,
00108         cols = ncols;
00109         if (rows>0 && cols>0) {
00110             // Luodaan uusi tietorakenne valmiiksi
00111             newComment (format ("PackTable<> data, %d rows, %d cols", rows, cols));
00112             data = new TYPE [rows*cols];
00113         }
00114     }
00115 
00122     const TYPE& get (int row, int col) const {
00123 #ifdef PACKTABLE_CHECKBOUNDS
00124         if (!(row>=0 && row<rows && col>=0 && col<cols))
00125             throw out_of_bounds (strformat ("Table range overflow: %d,%d out of %d,%d",
00126                                             row, col, rows, cols));
00127 #endif
00128         return data[row*cols+col];
00129     }
00130 
00137     TYPE&   get (int row, int col) {
00138 #ifdef PACKTABLE_CHECKBOUNDS
00139         if (!(row>=0 && row<rows && col>=0 && col<cols))
00140             throw out_of_bounds (strformat ("Table range overflow: %d,%d out of %d,%d",
00141                                             row, col, rows, cols));
00142 #endif
00143         return data[row*cols+col];
00144     }
00145 
00148     /*
00149     virtual CArchive&   operator>>  (CArchive& arc) const {
00150         arc << rows << cols;
00151         for (int i=0; i<rows; i++)
00152             for (int j=0; j<cols; j++)
00153                 arc << get (i, j);
00154         return arc;
00155     }
00156     */
00157 
00160     /*
00161     IStream&    operator<<  (IStream& arc) {
00162         int nrows, ncols;
00163         arc >> nrows >> ncols;
00164         make (nrows, ncols);
00165         for (int i=0; i<rows; i++)
00166             for (int j=0; j<cols; j++)
00167                 arc >> get (i, j);
00168         return arc;
00169     }
00170     */
00171 
00173     int rows;
00174 
00176     int cols;
00177     
00178   private:
00179     void operator=  (const PackTable& other) {} 
00180 };
00181 
00182 #endif

Generated at Tue Dec 4 19:53:28 2001 for MagiC++ by doxygen1.2.6 written by Dimitri van Heesch, © 1997-2001