1 | /* |
---|
2 | * |
---|
3 | */ |
---|
4 | package de.ugoe.cs.autoquest.tasktrees.alignment.matrix; |
---|
5 | |
---|
6 | import java.io.Serializable; |
---|
7 | |
---|
8 | // TODO: Auto-generated Javadoc |
---|
9 | /** |
---|
10 | * The Class StaticTriangleMatrix. |
---|
11 | */ |
---|
12 | public class StaticTriangleMatrix implements ITriangleMatrix, Serializable { |
---|
13 | |
---|
14 | /** The Constant serialVersionUID. */ |
---|
15 | private static final long serialVersionUID = 7599542322424894866L; |
---|
16 | |
---|
17 | /** The matrix. */ |
---|
18 | private final float[] matrix; |
---|
19 | |
---|
20 | /** The size. */ |
---|
21 | protected int size; |
---|
22 | |
---|
23 | /** |
---|
24 | * Instantiates a new static triangle matrix. |
---|
25 | * |
---|
26 | * @param size the size |
---|
27 | */ |
---|
28 | public StaticTriangleMatrix(int size) { |
---|
29 | this.size = size; |
---|
30 | matrix = new float[(size * (size + 1)) / 2]; |
---|
31 | } |
---|
32 | |
---|
33 | /* (non-Javadoc) |
---|
34 | * @see de.ugoe.cs.autoquest.tasktrees.alignment.matrix.ITriangleMatrix#get(int, int) |
---|
35 | */ |
---|
36 | @Override |
---|
37 | public float get(int first, int second) { |
---|
38 | final int row = Math.min(first, second); |
---|
39 | final int col = Math.max(first, second); |
---|
40 | return matrix[(row * size) - (((row * (row + 1)) / 2) - (size - col))]; |
---|
41 | |
---|
42 | } |
---|
43 | |
---|
44 | /* (non-Javadoc) |
---|
45 | * @see de.ugoe.cs.autoquest.tasktrees.alignment.matrix.ITriangleMatrix#increaseSize(int) |
---|
46 | */ |
---|
47 | @Override |
---|
48 | public void increaseSize(int count) throws Exception { |
---|
49 | throw new Exception( |
---|
50 | "Cannot call this function on this implementation of ITriangle Matrix"); |
---|
51 | |
---|
52 | } |
---|
53 | |
---|
54 | /* (non-Javadoc) |
---|
55 | * @see de.ugoe.cs.autoquest.tasktrees.alignment.matrix.ITriangleMatrix#initialize(float) |
---|
56 | */ |
---|
57 | @Override |
---|
58 | public void initialize(float value) { |
---|
59 | for (int i = 0; i < matrix.length; i++) { |
---|
60 | matrix[i] = value; |
---|
61 | } |
---|
62 | } |
---|
63 | |
---|
64 | /* (non-Javadoc) |
---|
65 | * @see de.ugoe.cs.autoquest.tasktrees.alignment.matrix.ITriangleMatrix#set(int, int, float) |
---|
66 | */ |
---|
67 | @Override |
---|
68 | public void set(int first, int second, float value) { |
---|
69 | final int row = Math.min(first, second); |
---|
70 | final int col = Math.max(first, second); |
---|
71 | matrix[(row * size) - (((row * (row + 1)) / 2) - (size - col))] = value; |
---|
72 | } |
---|
73 | |
---|
74 | /* (non-Javadoc) |
---|
75 | * @see de.ugoe.cs.autoquest.tasktrees.alignment.matrix.ITriangleMatrix#size() |
---|
76 | */ |
---|
77 | @Override |
---|
78 | public int size() { |
---|
79 | return size; |
---|
80 | } |
---|
81 | |
---|
82 | /* (non-Javadoc) |
---|
83 | * @see java.lang.Object#toString() |
---|
84 | */ |
---|
85 | @Override |
---|
86 | public String toString() { |
---|
87 | String result = ""; |
---|
88 | for (int i = 0; i < size; i++) { |
---|
89 | for (int j = 0; j < size; j++) { |
---|
90 | if (i < j) { |
---|
91 | if (Float.isInfinite(this.get(i, j))) { |
---|
92 | result = result + " -------"; |
---|
93 | } else { |
---|
94 | result = result |
---|
95 | + String.format("%+8.2f", this.get(i, j)); |
---|
96 | } |
---|
97 | } else { |
---|
98 | result = result + (" "); |
---|
99 | } |
---|
100 | } |
---|
101 | result = result + "\n"; |
---|
102 | } |
---|
103 | return result; |
---|
104 | } |
---|
105 | } |
---|