00001
00029 #ifndef __VETLIB_VETMATRIX_H__
00030 #define __VETLIB_VETMATRIX_H__
00031
00032 #include "../vetDefs.h"
00033 #include <stdio.h>
00034 #include <fstream>
00035
00036
00037 template<class T>
00038 class vetMatrix
00039 {
00040
00041 protected:
00042
00043 unsigned int rows;
00044 unsigned int columns;
00045
00046 T* data;
00047 T* dataSum;
00048
00049
00050 public:
00051
00052 vetMatrix()
00053 {
00054 rows = 0;
00055 columns = 0;
00056 data = NULL;
00057 dataSum = NULL;
00058 }
00059
00060 vetMatrix(unsigned int colCount, unsigned int rowCount)
00061 {
00062 data = NULL;
00063 dataSum = NULL;
00064 setDim(colCount, rowCount);
00065 }
00066
00067 vetMatrix(const char* filename)
00068 {
00069 data = NULL;
00070 dataSum = NULL;
00071 loadFromFile(filename);
00072 }
00073
00074 vetMatrix(unsigned int colCount, unsigned int rowCount, T* data_ptr)
00075 {
00076 rows = rowCount;
00077 columns = colCount;
00078
00079 data = data_ptr;
00080
00081 dataSum = new T;;
00082 for (int is=0; is < columns * rows; is++)
00083 dataSum += data[is];
00084
00085 }
00086
00087 ~vetMatrix()
00088 {
00089 if ( data != NULL )
00090 delete [] data;
00091
00092 if ( dataSum != NULL )
00093 delete dataSum;
00094 }
00095
00096 void setDim(unsigned int colCount, unsigned int rowCount)
00097 {
00098 if ( data != NULL )
00099 delete [] data;
00100
00101 if ( dataSum != NULL )
00102 delete dataSum;
00103
00104 rows = rowCount;
00105 columns = colCount;
00106
00107 data = new T [rows*columns];
00108 dataSum = new T;
00109 }
00110
00111 unsigned int getRowsCount() { return rows; };
00112 unsigned int getColumnsCount() { return columns; };
00113 unsigned int getItemsCount() { return columns*rows; };
00114 T getItemsSum() { return *dataSum; };
00115
00116 T* dump_data() { return data; };
00117 T* dump_dataSum() { return dataSum; };
00118
00119 VETRESULT setValue(unsigned int x, unsigned int y, T& value)
00120 {
00121 if ( x < 0 || y < 0 || x >= columns || y >= rows )
00122 return VETRET_PARAM_ERR;
00123
00124 if ( data == NULL )
00125 return VETRET_ILLEGAL_USE;
00126
00127 dataSum -= data[ y * rows + x ];
00128 data[ y * rows + x ] = value;
00129 dataSum += value;
00130
00131 return VETRET_OK;
00132 }
00133
00134 T& getValue(unsigned int x, unsigned int y)
00135 {
00136 if ( x < 0 || y < 0 || x >= columns || y >= rows )
00137 throw "Out of bounds.";
00138
00139 if ( data == NULL )
00140 throw "Empty table.";
00141
00142 return data[ y * rows + x ];
00143 }
00144
00145 T& getValueMax()
00146 {
00147 if ( data == NULL )
00148 throw "Empty table.";
00149
00150 T curMax = data[0];
00151
00152 for (int i=0; i < columns * rows; i++)
00153 if ( data[i] > curMax )
00154 curMax = data[i];
00155
00156 return curMax;
00157 }
00158
00159 T& getValueMin()
00160 {
00161 if ( data == NULL )
00162 throw "Empty table.";
00163
00164 T curMin = data[0];
00165
00166 for (int i=0; i < columns * rows; i++)
00167 if ( data[i] < curMax )
00168 curMax = data[i];
00169
00170 return curMax;
00171 }
00172
00173 void clearWith(T& value)
00174 {
00175 if ( data == NULL )
00176 throw "Empty table.";
00177
00178 for (int i=0; i < columns * rows; i++)
00179 data[i] = value;
00180
00181 dataSum = columns * rows * value;
00182
00183 }
00184
00185
00186
00187
00188
00189
00190
00191 VETRESULT saveToFile(const char* filename)
00192 {
00193 FILE *fp;
00194 int ret = VETRET_OK;
00195
00196 if ( data == NULL )
00197 return VETRET_ILLEGAL_USE;
00198
00199 if ( (fp = fopen(filename, "w")) == NULL )
00200 return VETRET_PARAM_ERR;
00201
00202
00203 if( fprintf(fp, "%d %d\n", rows, columns) == EOF )
00204 {
00205 fclose(fp);
00206 return VETRET_INTERNAL_ERR;
00207 }
00208
00209 for (int i=0; i < columns * rows; i++)
00210 if ( fprintf(fp, "%f ", (float)data[i]) == EOF)
00211 ret = VETRET_INTERNAL_ERR;
00212
00213 if ( fprintf(fp, "\n") == EOF )
00214 ret = VETRET_INTERNAL_ERR;
00215
00216 fclose(fp);
00217 return ret ;
00218 }
00219
00220
00221
00222 VETRESULT loadFromFile(const char* filename)
00223 {
00224 FILE *fp;
00225 int ret = VETRET_OK;
00226
00227 if ( (fp=fopen(filename,"r")) == NULL )
00228 return VETRET_PARAM_ERR;
00229
00230 unsigned int newRows = 0;
00231 unsigned int newCols = 0;
00232
00233 if ( fscanf(fp,"%u",&newRows) == EOF )
00234 {
00235 fclose(fp);
00236 return VETRET_INTERNAL_ERR;
00237 }
00238
00239 if ( fscanf(fp,"%u",&newCols) == EOF )
00240 {
00241 fclose(fp);
00242 return VETRET_INTERNAL_ERR;
00243 }
00244
00245 if ( newRows < 0 || newCols < 0 )
00246 {
00247 fclose(fp);
00248 return VETRET_INTERNAL_ERR;
00249 }
00250
00251 setDim(newRows, newCols);
00252
00253
00254
00255 float tmpValue;
00256
00257 for (int i=0; i < newRows * newCols; i++)
00258 if( fscanf(fp, "%f", &tmpValue) == EOF )
00259 ret = VETRET_INTERNAL_ERR;
00260 else
00261 data[i] = (T)tmpValue;
00262
00263 for (int is=0; is < columns * rows; is++)
00264 dataSum += data[is];
00265
00266 return ret;
00267 }
00268
00269
00270 };
00271
00272 #endif //__VETLIB_VETMATRIX_H__
00273