/** * VisitorOfParamData.hh * * Created on: 17 oct. 2012 * Author: AKKA IS */ #ifndef VISITOROFPARAMDATA_HH_ #define VISITOROFPARAMDATA_HH_ #include #include #include namespace AMDA { namespace Parameters { template class TabRow { public: TabRow() : _size(0), _data(NULL) { } ~TabRow() { free(); } void reallocate(int size) { free(); if (size > 0) { _data = new Type[size]; _size = size; } } void free() { if (_data != NULL) delete[] _data; _data = NULL; _size = 0; } Type& operator[](const int col) { if (col < 0 || col >= _size) throw; return _data[col]; } const Type& operator[](const int col) const { if (col < 0 || col >= _size) throw; return _data[col]; } std::vector toVector() { std::vector res(_data, _data + _size); return res; } const std::vector toVector() const { std::vector res(_data, _data + _size); return res; } inline TabRow& operator = (std::vector& vector) { if ((int)vector.size() != _size) throw; int crtIndex = 0; for (typename std::vector::const_iterator it = vector.begin(); it != vector.end(); ++it) { _data[crtIndex] = (*it); ++crtIndex; } return (*this); } private: int _size; Type *_data; }; template class Tab2DData { public : Tab2DData(void) : _dim1Size(0), _dim2Size(0), _rows(NULL) { } Tab2DData(int dim1Size, int dim2Size) : _dim1Size(dim1Size), _dim2Size(dim2Size), _rows(NULL) { reallocate(dim1Size,dim2Size); } Tab2DData(const Tab2DData& data) : Tab2DData() { reallocate(data.getDim1Size(),data.getDim2Size()); for (int i = 0; i < data.getDim1Size(); ++i) for (int j = 0; j < data.getDim2Size(); ++j) { _rows[i][j] = data[i][j]; } } ~Tab2DData(void) { free(); } void reallocate(int dim1Size, int dim2Size) { free(); _dim1Size = dim1Size; _dim2Size = dim2Size; if ((_dim1Size <= 0) || (_dim2Size <= 0)) return; _rows = new TabRow[_dim1Size]; for (int i = 0; i < _dim1Size; ++i) _rows[i].reallocate(dim2Size); } void free() { if (_rows != NULL) delete[] _rows; _rows = NULL; _dim1Size = 0; _dim2Size = 0; } TabRow& operator[](const int row) { if (row < 0 || row >= _dim1Size) throw; return _rows[row]; } const TabRow& operator[](const int row) const { if (row < 0 || row >= _dim1Size) throw; return _rows[row]; } int getDim1Size(void) const { return _dim1Size; } int getDim2Size(void) const { return _dim2Size; } int size(void) const { return _dim1Size*_dim2Size; } Type getTotal(void) { Type r; /* << ElemNull(); for (int i = 0; i < _dim1Size*_dim2Size; ++i) r += _data[i];*/ return r; } std::vector getTotalLine(void) { std::vector r; /*for (int i = 0; i < _dim1Size; ++i) { //for each line, compute sum of column Type sum << ElemNull(); for (int j = 0; j < _dim2Size; ++j) sum += _data[i*_dim2Size+j]; r.push_back(sum); }*/ return r; } std::vector getTotalColumn(void) { std::vector r; /*for (int i = 0; i < _dim2Size; ++i) { //for each column, compute sum of line Type sum << ElemNull(); for (int j = 0; j < _dim1Size; ++j) sum += _data[j*_dim2Size+i]; r.push_back(sum); }*/ return r; } inline Tab2DData& operator = (const Tab2DData& data) { reallocate(data.getDim1Size(),data.getDim2Size()); for (int i = 0; i < data.getDim1Size(); ++i) for (int j = 0; j < data.getDim2Size(); ++j) { _rows[i][j] = data[i][j]; } return (*this); } private: int _dim1Size; int _dim2Size; TabRow* _rows; }; enum LogicalData{ False = 0, True = 1, NaN }; template class ParamDataSpec; //scalar data typedef class ParamDataSpec ParamDataScalaireShort; typedef class ParamDataSpec ParamDataScalaireFloat; typedef class ParamDataSpec ParamDataScalaireDouble; typedef class ParamDataSpec ParamDataScalaireLongDouble; typedef class ParamDataSpec ParamDataScalaireInt; typedef class ParamDataSpec ParamDataLogicalData; //tab 1D data typedef class ParamDataSpec > ParamDataTab1DShort; typedef class ParamDataSpec > ParamDataTab1DFloat; typedef class ParamDataSpec > ParamDataTab1DDouble; typedef class ParamDataSpec > ParamDataTab1DLongDouble; typedef class ParamDataSpec > ParamDataTab1DInt; typedef class ParamDataSpec > ParamDataTab1DLogicalData; //tab 2D data typedef class ParamDataSpec> ParamDataTab2DShort; typedef class ParamDataSpec> ParamDataTab2DFloat; typedef class ParamDataSpec> ParamDataTab2DDouble; typedef class ParamDataSpec> ParamDataTab2DLongDouble; typedef class ParamDataSpec> ParamDataTab2DInt; typedef class ParamDataSpec> ParamDataTab2DLogicalData; /** * AMDA::Parameters::VisitorOfParamData */ class VisitorOfParamData { public: VisitorOfParamData() {} virtual ~VisitorOfParamData() {} virtual void visit(ParamDataScalaireShort *) = 0; virtual void visit(ParamDataScalaireFloat *) = 0; virtual void visit(ParamDataScalaireDouble *) = 0; virtual void visit(ParamDataScalaireLongDouble *) = 0; virtual void visit(ParamDataScalaireInt *) = 0; virtual void visit(ParamDataLogicalData *) = 0; virtual void visit(ParamDataTab1DShort *) = 0; virtual void visit(ParamDataTab1DFloat *) = 0; virtual void visit(ParamDataTab1DDouble *) = 0; virtual void visit(ParamDataTab1DLongDouble *) = 0; virtual void visit(ParamDataTab1DInt *) = 0; virtual void visit(ParamDataTab1DLogicalData *) = 0; virtual void visit(ParamDataTab2DShort *) = 0; virtual void visit(ParamDataTab2DFloat *) = 0; virtual void visit(ParamDataTab2DDouble *) = 0; virtual void visit(ParamDataTab2DLongDouble *) = 0; virtual void visit(ParamDataTab2DInt *) = 0; virtual void visit(ParamDataTab2DLogicalData *) = 0; }; } /* namespace Parameters */ } /* namespace AMDA */ #endif /* VISITOROFPARAMDATA_HH_ */