1 | package de.ugoe.cs.autoquest.tasktrees.alignment.matrix; |
---|
2 | |
---|
3 | import java.io.Serializable; |
---|
4 | |
---|
5 | public class StaticTriangleMatrix implements ITriangleMatrix, Serializable { |
---|
6 | |
---|
7 | /** |
---|
8 | * |
---|
9 | */ |
---|
10 | private static final long serialVersionUID = 7599542322424894866L; |
---|
11 | private final float[] matrix; |
---|
12 | protected int size; |
---|
13 | |
---|
14 | public StaticTriangleMatrix(int size) { |
---|
15 | this.size = size; |
---|
16 | matrix = new float[(size * (size + 1)) / 2]; |
---|
17 | } |
---|
18 | |
---|
19 | @Override |
---|
20 | public float get(int first, int second) { |
---|
21 | final int row = Math.min(first, second); |
---|
22 | final int col = Math.max(first, second); |
---|
23 | return matrix[(row * size) - (((row * (row + 1)) / 2) - (size - col))]; |
---|
24 | |
---|
25 | } |
---|
26 | |
---|
27 | @Override |
---|
28 | public void increaseSize(int count) throws Exception { |
---|
29 | throw new Exception( |
---|
30 | "Cannot call this function on this implementation of ITriangle Matrix"); |
---|
31 | |
---|
32 | } |
---|
33 | |
---|
34 | @Override |
---|
35 | public void initialize(float value) { |
---|
36 | for (int i = 0; i < matrix.length; i++) { |
---|
37 | matrix[i] = value; |
---|
38 | } |
---|
39 | } |
---|
40 | |
---|
41 | @Override |
---|
42 | public void set(int first, int second, float value) { |
---|
43 | final int row = Math.min(first, second); |
---|
44 | final int col = Math.max(first, second); |
---|
45 | matrix[(row * size) - (((row * (row + 1)) / 2) - (size - col))] = value; |
---|
46 | } |
---|
47 | |
---|
48 | @Override |
---|
49 | public int size() { |
---|
50 | return size; |
---|
51 | } |
---|
52 | |
---|
53 | @Override |
---|
54 | public String toString() { |
---|
55 | String result = ""; |
---|
56 | for (int i = 0; i < size; i++) { |
---|
57 | for (int j = 0; j < size; j++) { |
---|
58 | if (i < j) { |
---|
59 | if (Float.isInfinite(this.get(i, j))) { |
---|
60 | result = result + " -------"; |
---|
61 | } else { |
---|
62 | result = result |
---|
63 | + String.format("%+8.2f", this.get(i, j)); |
---|
64 | } |
---|
65 | } else { |
---|
66 | result = result + (" "); |
---|
67 | } |
---|
68 | } |
---|
69 | result = result + "\n"; |
---|
70 | } |
---|
71 | return result; |
---|
72 | } |
---|
73 | } |
---|