| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
package eu.simuline.octave.io.impl; |
| 19 | |
|
| 20 | |
import java.io.BufferedReader; |
| 21 | |
|
| 22 | |
import eu.simuline.octave.exception.OctaveParseException; |
| 23 | |
import eu.simuline.octave.io.OctaveIO; |
| 24 | |
import eu.simuline.octave.io.spi.OctaveDataReader; |
| 25 | |
import eu.simuline.octave.type.matrix.AbstractGenericMatrix; |
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | 562 | abstract class AbstractPrimitiveMatrixReader |
| 35 | |
<T extends AbstractGenericMatrix<?,?>> |
| 36 | |
extends OctaveDataReader { |
| 37 | |
|
| 38 | |
|
| 39 | |
protected static final String NDIMS = "# ndims: "; |
| 40 | |
protected static final String NROWS = "# rows: "; |
| 41 | |
protected static final String NCOLUMNS = "# columns: "; |
| 42 | |
|
| 43 | |
|
| 44 | |
abstract T createOctaveValue(int[] size); |
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
@Override |
| 67 | |
public T read(final BufferedReader reader) { |
| 68 | 554 | final String line = OctaveIO.readerReadLine(reader); |
| 69 | |
|
| 70 | 554 | if (line.startsWith(NROWS)) { |
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | 164 | return read2dmatrix(reader, line); |
| 75 | 390 | } else if (line.startsWith(NDIMS)) { |
| 76 | 390 | return readVectorizedMatrix(reader, line); |
| 77 | |
} else { |
| 78 | 0 | throw new OctaveParseException |
| 79 | |
("Expected <" + NROWS + "> or <" + NDIMS + |
| 80 | |
">, but got <" + line + ">. "); |
| 81 | |
} |
| 82 | |
} |
| 83 | |
|
| 84 | |
private T readVectorizedMatrix(BufferedReader reader, |
| 85 | |
String dimsLine) { |
| 86 | 390 | int[] size = readSizeVectorizedMatrix(reader, dimsLine); |
| 87 | 390 | T res = createOctaveValue(size); |
| 88 | |
String line; |
| 89 | |
|
| 90 | |
|
| 91 | 6362 | for (int idx = 0; idx < res.dataSize(); idx++) { |
| 92 | 5972 | line = OctaveIO.readerReadLine(reader); |
| 93 | 5972 | res.setPlain(line, idx); |
| 94 | |
} |
| 95 | 390 | return res; |
| 96 | |
} |
| 97 | |
|
| 98 | |
|
| 99 | |
|
| 100 | |
|
| 101 | |
|
| 102 | |
|
| 103 | |
|
| 104 | |
private int[] readSizeVectorizedMatrix(BufferedReader reader, |
| 105 | |
String ndimsLine) { |
| 106 | 390 | String line = ndimsLine; |
| 107 | 390 | if (!line.startsWith(NDIMS)) { |
| 108 | 0 | throw new OctaveParseException |
| 109 | |
("Expected <" + NDIMS + ">, but got <" + line + ">. "); |
| 110 | |
} |
| 111 | 390 | final int ndims = Integer.parseInt(line.substring(NDIMS.length())); |
| 112 | |
|
| 113 | 390 | line = OctaveIO.readerReadLine(reader); |
| 114 | 390 | final String[] split = line.substring(1).split(" "); |
| 115 | 390 | if (split.length != ndims) { |
| 116 | 0 | throw new OctaveParseException |
| 117 | |
("Expected <" + ndims + "> dimension, but got <" + |
| 118 | |
split.length + "> (line was <" + line + ">). "); |
| 119 | |
} |
| 120 | 390 | final int[] size = new int[split.length]; |
| 121 | 1772 | for (int dim = 0; dim < split.length; dim++) { |
| 122 | 1382 | size[dim] = Integer.parseInt(split[dim]); |
| 123 | |
} |
| 124 | 390 | return size; |
| 125 | |
} |
| 126 | |
|
| 127 | |
|
| 128 | |
|
| 129 | |
private T read2dmatrix(BufferedReader reader, |
| 130 | |
String rowsLine) { |
| 131 | 164 | int[] size = readSize2dmatrix(reader, rowsLine); |
| 132 | 164 | T res = createOctaveValue(size); |
| 133 | |
|
| 134 | 164 | int rows = size[0]; |
| 135 | 164 | int columns = size[1]; |
| 136 | |
String line; |
| 137 | |
|
| 138 | 674 | for (int r = 1; r <= rows; ++r) { |
| 139 | 510 | line = OctaveIO.readerReadLine(reader); |
| 140 | 510 | final String[] split = line.split(" "); |
| 141 | 510 | if (split.length != columns + 1) { |
| 142 | 0 | throw new OctaveParseException |
| 143 | |
("Error in matrix-format: '" + line + "'"); |
| 144 | |
} |
| 145 | 1406 | for (int c = 1; c < split.length; c++) { |
| 146 | 896 | res.setPlain(split[c], (r - 1) + (c - 1) * rows); |
| 147 | |
} |
| 148 | |
} |
| 149 | 164 | return res; |
| 150 | |
} |
| 151 | |
|
| 152 | |
|
| 153 | |
|
| 154 | |
|
| 155 | |
|
| 156 | |
protected int[] readSize2dmatrix(BufferedReader reader, |
| 157 | |
String rowsLine) { |
| 158 | |
|
| 159 | 164 | String line = rowsLine; |
| 160 | 164 | if (!line.startsWith(NROWS)) { |
| 161 | 0 | throw new OctaveParseException |
| 162 | |
("Expected <" + NROWS + "> got <" + line + ">. "); |
| 163 | |
} |
| 164 | 164 | final int rows = Integer.parseInt(line.substring(NROWS.length())); |
| 165 | |
|
| 166 | 164 | line = OctaveIO.readerReadLine(reader); |
| 167 | 164 | if (!line.startsWith(NCOLUMNS)) { |
| 168 | 0 | throw new OctaveParseException |
| 169 | |
("Expected <" + NCOLUMNS + "> got <" + line + ">. "); |
| 170 | |
} |
| 171 | 164 | final int columns = Integer.parseInt(line.substring(NCOLUMNS.length())); |
| 172 | |
|
| 173 | |
|
| 174 | |
|
| 175 | |
|
| 176 | 164 | return new int[] {rows, columns}; |
| 177 | |
} |
| 178 | |
} |