/* * Matrix.cpp * * Created on: Mar 2, 2010 * Author: metzler */ /** * Gets the value as string. */ string FieldContent::getAsString() { return value; } /** * Gets the value as integer. */ int FieldContent::getAsInt() { return value.intVal(); } /** * Gets the value as double. */ double FieldContent::getAsDouble() { return value.getAsdouble(); } /** * Sets the value as string. */ void FieldContent::set(string valueToSet) { value = valueToSet; } /** * Sets the value as string. */ void FieldContent::set(int valueToSet) { value = string(valueToSet); } /** * Sets the value as string. */ void FieldContent::set(double valueToSet) { value = string(valueToSet); } /** * Matrix constructor. * * \param number of rows to initialize the matrix with * \param number of columns to initialize the matrix with */ Matrix::Matrix() { fields = new vector >(); } /** * Matrix destructor. */ Matrix::~Matrix() { vector >::iterator row; for (row = fields.begin(); row < fields.end(); it++ ) delete(*row); delete(fields); delete(header); } /** * Sets the matrix header. * * \param char* matrix header */ void setHeader(const string text) { header = text; } /** * Gets the header. */ string getHeader() { return header; } /** * Gets the number of rows. */ int getNumberOfRows() { return numberOfRows; } /** * Gets the number of columns. */ int getNumberOfColumns() { return numberOfColumns; } /** * Sets the value of a field. * * \param row index * \param column index * \param value to set */ void setField(int rowIndex, int columnIndex, FieldContent value) { fields[rowIndex][columnIndex] = value; } /** * Gets the value of a field. * * \param row index * \param column index */ FieldContent getField(int rowIndex, int columnIndex) { return fields[rowIndex][columnIndex]; } /** * Constructor which initializes the reader with default delimiters. */ MatrixReader::MatrixReader() : skipBegin('#'), skipEnd('\n'), columnSeparator('\t'), rowBegin(''), rowEnd('\n') {} /** * Constructor for customized delimiters. * * \param column separating character * \param row begin indicator * \param row end indicator * \param comment begin indicator * \param comment end indicator */ MatrixReader::MatrixReader(char columnSep, char rowInitializer, char rowTerminator, char skipInitiator, char skipTerminator) : skipBegin(skipInitiator), skipEnd(skipTerminator), columnSeparator(columnSep), rowBegin(rowInitializer), rowEnd(rowTerminator) {} /** * Determines the number of columns assuming that the number of columns found in * the first data line is appropriate for all other lines. * * \return number of columns */ int MatrixReader::determineNumberOfColumns() { } /** * Reads a matrix from a given file using the default delimiter set consisting * of \t (tab) separating columns and \n (newline) separating rows if not * initialized differently * * \param file name where to read the matrix from * \param number of initial rows to skip * \param number of initial columns to skip */ Matrix* MatrixReader::read(char* fileName, int rowOffset, int columnOffset) { } /** * Writes a matrix to a file. * * \param file name * \param matrix to write * \param column separating character * \param row begin indicating character * \param row end indicating character */ void write(char* fileName, Matrix* matrix, char columnSeparator, char rowInitializer, char rowTerminator) { ofstream output; stringstream line; line << fileName; output.open(line.str().c_str(), ios::out); if (output == NULL) { throw new Exception("Unable to open matrix output file %s.\n", line.str()); } output << matrix->getHeader() << endl; for(int i = 0; i < matrix->getNumberOfRows(); i++) { output << rowInitializer; for(int j = 0; j < matrix->getNumberOfColumns(); j++) { output << scientific << matrix->getField(j, k) << columnSeparator; } output << rowTerminator; } output.close(); }