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 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 final String line = OctaveIO.readerReadLine(reader);
69
70 if (line.startsWith(NROWS)) {
71
72
73
74 return read2dmatrix(reader, line);
75 } else if (line.startsWith(NDIMS)) {
76 return readVectorizedMatrix(reader, line);
77 } else {
78 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 int[] size = readSizeVectorizedMatrix(reader, dimsLine);
87 T res = createOctaveValue(size);
88 String line;
89
90
91 for (int idx = 0; idx < res.dataSize(); idx++) {
92 line = OctaveIO.readerReadLine(reader);
93 res.setPlain(line, idx);
94 }
95 return res;
96 }
97
98
99
100
101
102
103
104 private int[] readSizeVectorizedMatrix(BufferedReader reader,
105 String ndimsLine) {
106 String line = ndimsLine;
107 if (!line.startsWith(NDIMS)) {
108 throw new OctaveParseException
109 ("Expected <" + NDIMS + ">, but got <" + line + ">. ");
110 }
111 final int ndims = Integer.parseInt(line.substring(NDIMS.length()));
112
113 line = OctaveIO.readerReadLine(reader);
114 final String[] split = line.substring(1).split(" ");
115 if (split.length != ndims) {
116 throw new OctaveParseException
117 ("Expected <" + ndims + "> dimension, but got <" +
118 split.length + "> (line was <" + line + ">). ");
119 }
120 final int[] size = new int[split.length];
121 for (int dim = 0; dim < split.length; dim++) {
122 size[dim] = Integer.parseInt(split[dim]);
123 }
124 return size;
125 }
126
127
128
129 private T read2dmatrix(BufferedReader reader,
130 String rowsLine) {
131 int[] size = readSize2dmatrix(reader, rowsLine);
132 T res = createOctaveValue(size);
133
134 int rows = size[0];
135 int columns = size[1];
136 String line;
137
138 for (int r = 1; r <= rows; ++r) {
139 line = OctaveIO.readerReadLine(reader);
140 final String[] split = line.split(" ");
141 if (split.length != columns + 1) {
142 throw new OctaveParseException
143 ("Error in matrix-format: '" + line + "'");
144 }
145 for (int c = 1; c < split.length; c++) {
146 res.setPlain(split[c], (r - 1) + (c - 1) * rows);
147 }
148 }
149 return res;
150 }
151
152
153
154
155
156 protected int[] readSize2dmatrix(BufferedReader reader,
157 String rowsLine) {
158
159 String line = rowsLine;
160 if (!line.startsWith(NROWS)) {
161 throw new OctaveParseException
162 ("Expected <" + NROWS + "> got <" + line + ">. ");
163 }
164 final int rows = Integer.parseInt(line.substring(NROWS.length()));
165
166 line = OctaveIO.readerReadLine(reader);
167 if (!line.startsWith(NCOLUMNS)) {
168 throw new OctaveParseException
169 ("Expected <" + NCOLUMNS + "> got <" + line + ">. ");
170 }
171 final int columns = Integer.parseInt(line.substring(NCOLUMNS.length()));
172
173
174
175
176 return new int[] {rows, columns};
177 }
178 }