-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathGenericTabulator.java
More file actions
43 lines (34 loc) · 1.48 KB
/
GenericTabulator.java
File metadata and controls
43 lines (34 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package expression.generic;
import java.util.Map;
import expression.ExpressionException;
import expression.TripleExpression;
import expression.parser.ExpressionParser;
import expression.type.*;
public class GenericTabulator implements Tabulator {
private static final Map<String, Type<?>> TYPES = Map.of(
"i", new CheckedIntegerType(),
"d", new DoubleType(),
"bi", new BigIntegerType()
);
@Override
public Object[][][] tabulate(String mode, String expression, int x1, int x2, int y1, int y2, int z1, int z2) throws ExpressionException {
Type<?> type = TYPES.get(mode);
return tabulateImpl(type, expression, x1, x2, y1, y2, z1, z2);
}
private <T> Object[][][] tabulateImpl(Type<T> type, String expression, int x1, int x2, int y1, int y2, int z1, int z2) throws ExpressionException {
Object[][][] result = new Object[x2 - x1 + 1][y2 - y1 + 1][z2 - z1 + 1];
ExpressionParser<T> parser = new ExpressionParser<>(type);
TripleExpression<T> parsedExpression = parser.parse(expression);
for (int i = x1; i <= x2; i++) {
for (int j = y1; j <= y2; j++) {
for (int k = z1; k <= z2; k++) {
try {
result[i][j][k] = parsedExpression.evaluate(type.castInt(i), type.castInt(j), type.castInt(k));
} catch (ArithmeticException ignored) {
}
}
}
}
return result;
}
}