[6bc51d] | 1 | /*
|
---|
| 2 | * Matrix.cpp
|
---|
| 3 | *
|
---|
| 4 | * Created on: Mar 2, 2010
|
---|
| 5 | * Author: metzler
|
---|
| 6 | */
|
---|
| 7 |
|
---|
| 8 | /**
|
---|
| 9 | * Gets the value as string.
|
---|
| 10 | */
|
---|
| 11 | string FieldContent::getAsString() {
|
---|
| 12 | return value;
|
---|
| 13 | }
|
---|
| 14 |
|
---|
| 15 | /**
|
---|
| 16 | * Gets the value as integer.
|
---|
| 17 | */
|
---|
| 18 | int FieldContent::getAsInt() {
|
---|
| 19 | return value.intVal();
|
---|
| 20 | }
|
---|
| 21 |
|
---|
| 22 | /**
|
---|
| 23 | * Gets the value as double.
|
---|
| 24 | */
|
---|
| 25 | double FieldContent::getAsDouble() {
|
---|
| 26 | return value.getAsdouble();
|
---|
| 27 | }
|
---|
| 28 |
|
---|
| 29 | /**
|
---|
| 30 | * Sets the value as string.
|
---|
| 31 | */
|
---|
| 32 | void FieldContent::set(string valueToSet) {
|
---|
| 33 | value = valueToSet;
|
---|
| 34 | }
|
---|
| 35 |
|
---|
| 36 | /**
|
---|
| 37 | * Sets the value as string.
|
---|
| 38 | */
|
---|
| 39 | void FieldContent::set(int valueToSet) {
|
---|
| 40 | value = string(valueToSet);
|
---|
| 41 | }
|
---|
| 42 |
|
---|
| 43 | /**
|
---|
| 44 | * Sets the value as string.
|
---|
| 45 | */
|
---|
| 46 | void FieldContent::set(double valueToSet) {
|
---|
| 47 | value = string(valueToSet);
|
---|
| 48 | }
|
---|
| 49 |
|
---|
| 50 | /**
|
---|
| 51 | * Matrix constructor.
|
---|
| 52 | *
|
---|
| 53 | * \param number of rows to initialize the matrix with
|
---|
| 54 | * \param number of columns to initialize the matrix with
|
---|
| 55 | */
|
---|
| 56 | Matrix::Matrix() {
|
---|
| 57 | fields = new vector<vector<FieldContent> >();
|
---|
| 58 | }
|
---|
| 59 |
|
---|
| 60 | /**
|
---|
| 61 | * Matrix destructor.
|
---|
| 62 | */
|
---|
| 63 | Matrix::~Matrix() {
|
---|
| 64 | vector<vector<FieldContent> >::iterator row;
|
---|
| 65 | for (row = fields.begin(); row < fields.end(); it++ )
|
---|
| 66 | delete(*row);
|
---|
| 67 |
|
---|
| 68 | delete(fields);
|
---|
| 69 | delete(header);
|
---|
| 70 | }
|
---|
| 71 |
|
---|
| 72 | /**
|
---|
| 73 | * Sets the matrix header.
|
---|
| 74 | *
|
---|
| 75 | * \param char* matrix header
|
---|
| 76 | */
|
---|
| 77 | void setHeader(const string text) {
|
---|
| 78 | header = text;
|
---|
| 79 | }
|
---|
| 80 |
|
---|
| 81 | /**
|
---|
| 82 | * Gets the header.
|
---|
| 83 | */
|
---|
| 84 | string getHeader() {
|
---|
| 85 | return header;
|
---|
| 86 | }
|
---|
| 87 |
|
---|
| 88 | /**
|
---|
| 89 | * Gets the number of rows.
|
---|
| 90 | */
|
---|
| 91 | int getNumberOfRows() {
|
---|
| 92 | return numberOfRows;
|
---|
| 93 | }
|
---|
| 94 |
|
---|
| 95 | /**
|
---|
| 96 | * Gets the number of columns.
|
---|
| 97 | */
|
---|
| 98 | int getNumberOfColumns() {
|
---|
| 99 | return numberOfColumns;
|
---|
| 100 | }
|
---|
| 101 |
|
---|
| 102 | /**
|
---|
| 103 | * Sets the value of a field.
|
---|
| 104 | *
|
---|
| 105 | * \param row index
|
---|
| 106 | * \param column index
|
---|
| 107 | * \param value to set
|
---|
| 108 | */
|
---|
| 109 | void setField(int rowIndex, int columnIndex, FieldContent value) {
|
---|
| 110 | fields[rowIndex][columnIndex] = value;
|
---|
| 111 | }
|
---|
| 112 |
|
---|
| 113 | /**
|
---|
| 114 | * Gets the value of a field.
|
---|
| 115 | *
|
---|
| 116 | * \param row index
|
---|
| 117 | * \param column index
|
---|
| 118 | */
|
---|
| 119 | FieldContent getField(int rowIndex, int columnIndex) {
|
---|
| 120 | return fields[rowIndex][columnIndex];
|
---|
| 121 | }
|
---|
| 122 |
|
---|
| 123 | /**
|
---|
| 124 | * Constructor which initializes the reader with default delimiters.
|
---|
| 125 | */
|
---|
| 126 | MatrixReader::MatrixReader() : skipBegin('#'), skipEnd('\n'),
|
---|
| 127 | columnSeparator('\t'), rowBegin(''), rowEnd('\n') {}
|
---|
| 128 |
|
---|
| 129 | /**
|
---|
| 130 | * Constructor for customized delimiters.
|
---|
| 131 | *
|
---|
| 132 | * \param column separating character
|
---|
| 133 | * \param row begin indicator
|
---|
| 134 | * \param row end indicator
|
---|
| 135 | * \param comment begin indicator
|
---|
| 136 | * \param comment end indicator
|
---|
| 137 | */
|
---|
| 138 | MatrixReader::MatrixReader(char columnSep, char rowInitializer, char rowTerminator, char skipInitiator, char skipTerminator) :
|
---|
| 139 | skipBegin(skipInitiator), skipEnd(skipTerminator), columnSeparator(columnSep),
|
---|
| 140 | rowBegin(rowInitializer), rowEnd(rowTerminator) {}
|
---|
| 141 |
|
---|
| 142 | /**
|
---|
| 143 | * Determines the number of columns assuming that the number of columns found in
|
---|
| 144 | * the first data line is appropriate for all other lines.
|
---|
| 145 | *
|
---|
| 146 | * \return number of columns
|
---|
| 147 | */
|
---|
| 148 | int MatrixReader::determineNumberOfColumns() {
|
---|
| 149 |
|
---|
| 150 | }
|
---|
| 151 |
|
---|
| 152 | /**
|
---|
| 153 | * Reads a matrix from a given file using the default delimiter set consisting
|
---|
| 154 | * of \t (tab) separating columns and \n (newline) separating rows if not
|
---|
| 155 | * initialized differently
|
---|
| 156 | *
|
---|
| 157 | * \param file name where to read the matrix from
|
---|
| 158 | * \param number of initial rows to skip
|
---|
| 159 | * \param number of initial columns to skip
|
---|
| 160 | */
|
---|
| 161 | Matrix* MatrixReader::read(char* fileName, int rowOffset, int columnOffset) {
|
---|
| 162 |
|
---|
| 163 | }
|
---|
| 164 |
|
---|
| 165 | /**
|
---|
| 166 | * Writes a matrix to a file.
|
---|
| 167 | *
|
---|
| 168 | * \param file name
|
---|
| 169 | * \param matrix to write
|
---|
| 170 | * \param column separating character
|
---|
| 171 | * \param row begin indicating character
|
---|
| 172 | * \param row end indicating character
|
---|
| 173 | */
|
---|
| 174 | void write(char* fileName, Matrix* matrix, char columnSeparator, char rowInitializer, char rowTerminator) {
|
---|
| 175 | ofstream output;
|
---|
| 176 | stringstream line;
|
---|
| 177 |
|
---|
| 178 | line << fileName;
|
---|
| 179 | output.open(line.str().c_str(), ios::out);
|
---|
| 180 | if (output == NULL) {
|
---|
| 181 | throw new Exception("Unable to open matrix output file %s.\n", line.str());
|
---|
| 182 | }
|
---|
| 183 | output << matrix->getHeader() << endl;
|
---|
| 184 | for(int i = 0; i < matrix->getNumberOfRows(); i++) {
|
---|
| 185 | output << rowInitializer;
|
---|
| 186 | for(int j = 0; j < matrix->getNumberOfColumns(); j++) {
|
---|
| 187 | output << scientific << matrix->getField(j, k) << columnSeparator;
|
---|
| 188 | }
|
---|
| 189 | output << rowTerminator;
|
---|
| 190 | }
|
---|
| 191 | output.close();
|
---|
| 192 | }
|
---|