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
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
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
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
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
00176
00177
00178
00179
00180
00181
00182
00183
00186
00187
00188
00189
00190
00191
00192
00193
00194
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