00001
00035 #ifndef Array2D_H
00036 #define Array2D_H
00037
00038 #include <assert.h>
00039
00040 namespace pfs
00041 {
00042
00054 class Array2D
00055 {
00056
00057 public:
00058
00062 virtual int getCols() const = 0;
00063
00067 virtual int getRows() const = 0;
00068
00069
00082 virtual float& operator()( int col, int row ) = 0;
00083
00096 virtual const float& operator()( int col, int row ) const = 0;
00097
00115 virtual float& operator()( int index ) = 0;
00116
00134 virtual const float& operator()( int index ) const = 0;
00135
00139 virtual ~Array2D()
00140 {
00141 }
00142
00143 };
00144
00145
00152 class Array2DImpl: public Array2D
00153 {
00154 float *data;
00155 int cols, rows;
00156
00157 public:
00158
00159 Array2DImpl( int cols, int rows ) : cols( cols ), rows( rows )
00160 {
00161 data = new float[cols*rows];
00162 }
00163
00164 ~Array2DImpl()
00165 {
00166 delete[] data;
00167 }
00168
00169 inline int getCols() const { return cols; }
00170 inline int getRows() const { return rows; }
00171
00172 inline float& operator()( int col, int row ) {
00173 assert( col >= 0 && col < cols );
00174 assert( row >= 0 && row < rows );
00175 return data[ col+row*cols ];
00176 }
00177 inline const float& operator()( int col, int row ) const {
00178 assert( col >= 0 && col < cols );
00179 assert( row >= 0 && row < rows );
00180 return data[ col+row*cols ];
00181 }
00182
00183 inline float& operator()( int index ) {
00184 assert( index >= 0 && index < rows*cols );
00185 return data[index];
00186 }
00187 inline const float& operator()( int index ) const {
00188 assert( index >= 0 && index <= rows*cols );
00189 return data[index];
00190 }
00191
00192 float* getRawData() {
00193 return data;
00194 }
00195
00196
00197 };
00198
00205 inline void copyArray(const Array2D *from, Array2D *to)
00206 {
00207 assert( from->getRows() == to->getRows() );
00208 assert( from->getCols() == to->getCols() );
00209
00210 const int elements = from->getRows()*from->getCols();
00211 for( int i = 0; i < elements; i++ )
00212 (*to)(i) = (*from)(i);
00213 }
00214
00221 inline void setArray(Array2D *array, const float value )
00222 {
00223 const int elements = array->getRows()*array->getCols();
00224 for( int i = 0; i < elements; i++ )
00225 (*array)(i) = value;
00226 }
00227
00235 inline void multiplyArray(Array2D *z, const Array2D *x, const Array2D *y)
00236 {
00237 assert( x->getRows() == y->getRows() );
00238 assert( x->getCols() == y->getCols() );
00239 assert( x->getRows() == z->getRows() );
00240 assert( x->getCols() == z->getCols() );
00241
00242 const int elements = x->getRows()*x->getCols();
00243 for( int i = 0; i < elements; i++ )
00244 (*z)(i) = (*x)(i) * (*y)(i);
00245 }
00246
00247 }
00248
00249 #endif