VisitorOfParamData.hh 6.08 KB
/**
 * VisitorOfParamData.hh
 *
 *  Created on: 17 oct. 2012
 *      Author: AKKA IS
 */

#ifndef VISITOROFPARAMDATA_HH_
#define VISITOROFPARAMDATA_HH_

#include <vector>
#include <cstddef>
#include <iostream>

namespace AMDA {
namespace Parameters {

template <typename Type>
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<Type> toVector()
	{
		std::vector<Type> res(_data, _data + _size);
		return res;
	}

	const std::vector<Type> toVector() const
	{
		std::vector<Type> res(_data, _data + _size);
		return res;
	}
	
	inline TabRow& operator = (std::vector<Type>& vector)
	{
		if (vector.size() != _size)
			throw;
		int crtIndex = 0;
		for (typename std::vector<Type>::const_iterator it = vector.begin(); it != vector.end(); ++it) {
			_data[crtIndex] = (*it);
			++crtIndex;
		}
		return (*this);
	}

private:
	int _size;

	Type *_data;
};

template <typename Type>
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<Type>[_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<Type>& operator[](const int row)
	{
		if (row < 0 || row >= _dim1Size)
			throw;
		return _rows[row];
	}

	const TabRow<Type>& 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<Type> getTotalLine(void)
	{
		std::vector<Type> 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<Type> getTotalColumn(void)
	{
		std::vector<Type> 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<Type>* _rows;
};

enum LogicalData{
	False = 0,
	True = 1,
	NaN
};

template <typename Type>
class ParamDataSpec;

//scalar data
typedef class ParamDataSpec<short> ParamDataScalaireShort;
typedef class ParamDataSpec<float> ParamDataScalaireFloat;
typedef class ParamDataSpec<double> ParamDataScalaireDouble;
typedef class ParamDataSpec<long double> ParamDataScalaireLongDouble;
typedef class ParamDataSpec<int> ParamDataScalaireInt;
typedef class ParamDataSpec<LogicalData> ParamDataLogicalData;

//tab 1D data
typedef class ParamDataSpec<std::vector<short> > ParamDataTab1DShort;
typedef class ParamDataSpec<std::vector<float> > ParamDataTab1DFloat;
typedef class ParamDataSpec<std::vector<double> > ParamDataTab1DDouble;
typedef class ParamDataSpec<std::vector<long double> > ParamDataTab1DLongDouble;
typedef class ParamDataSpec<std::vector<int> > ParamDataTab1DInt;
typedef class ParamDataSpec<std::vector<LogicalData> > ParamDataTab1DLogicalData;

//tab 2D data
typedef class ParamDataSpec<Tab2DData<short>> ParamDataTab2DShort;
typedef class ParamDataSpec<Tab2DData<float>> ParamDataTab2DFloat;
typedef class ParamDataSpec<Tab2DData<double>> ParamDataTab2DDouble;
typedef class ParamDataSpec<Tab2DData<long double>> ParamDataTab2DLongDouble;
typedef class ParamDataSpec<Tab2DData<int>> ParamDataTab2DInt;
typedef class ParamDataSpec<Tab2DData<LogicalData>> 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_ */