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 PreallocatedDynamicTriangleMatrix implements ITriangleMatrix, Serializable { |
---|
13 | |
---|
14 | /** The Constant serialVersionUID. */ |
---|
15 | private static final long serialVersionUID = 7599542322424894866L; |
---|
16 | |
---|
17 | /** The matrix. */ |
---|
18 | private float[] matrix; |
---|
19 | |
---|
20 | /** The size. */ |
---|
21 | protected int size; |
---|
22 | |
---|
23 | private int actualsize; |
---|
24 | private float initalizationValue; |
---|
25 | |
---|
26 | /** |
---|
27 | * Instantiates a new static triangle matrix. |
---|
28 | * |
---|
29 | * @param size the size |
---|
30 | */ |
---|
31 | public PreallocatedDynamicTriangleMatrix(int size) { |
---|
32 | this.size = size; |
---|
33 | this.actualsize = size*10; |
---|
34 | matrix = new float[(actualsize * (actualsize + 1)) / 2]; |
---|
35 | } |
---|
36 | |
---|
37 | /* (non-Javadoc) |
---|
38 | * @see de.ugoe.cs.autoquest.tasktrees.alignment.matrix.ITriangleMatrix#get(int, int) |
---|
39 | */ |
---|
40 | @Override |
---|
41 | public float get(int first, int second) { |
---|
42 | final int row = Math.min(first, second); |
---|
43 | final int col = Math.max(first, second); |
---|
44 | return matrix[(row * actualsize) - (((row * (row + 1)) / 2) - (actualsize - col))]; |
---|
45 | |
---|
46 | } |
---|
47 | |
---|
48 | /* (non-Javadoc) |
---|
49 | * @see de.ugoe.cs.autoquest.tasktrees.alignment.matrix.ITriangleMatrix#increaseSize(int) |
---|
50 | */ |
---|
51 | @Override |
---|
52 | public void increaseSize(int count) throws Exception { |
---|
53 | final int oldsize = size; |
---|
54 | this.size += count; |
---|
55 | if(size>actualsize) { |
---|
56 | //int newElements = ((oldsize * count) + ((count * (count + 1)) / 2)); |
---|
57 | actualsize*=2; |
---|
58 | float[] newmatrix = new float[actualsize]; |
---|
59 | System.arraycopy(this.matrix, 0, newmatrix, 0,matrix.length); |
---|
60 | for(int i=oldsize;i<actualsize;i++) { |
---|
61 | newmatrix[i]=this.initalizationValue; |
---|
62 | } |
---|
63 | matrix = newmatrix; |
---|
64 | } |
---|
65 | } |
---|
66 | |
---|
67 | /* (non-Javadoc) |
---|
68 | * @see de.ugoe.cs.autoquest.tasktrees.alignment.matrix.ITriangleMatrix#initialize(float) |
---|
69 | */ |
---|
70 | @Override |
---|
71 | public void initialize(float value) { |
---|
72 | this.initalizationValue = value; |
---|
73 | for (int i = 0; i < matrix.length; i++) { |
---|
74 | matrix[i] = value; |
---|
75 | } |
---|
76 | } |
---|
77 | |
---|
78 | /* (non-Javadoc) |
---|
79 | * @see de.ugoe.cs.autoquest.tasktrees.alignment.matrix.ITriangleMatrix#set(int, int, float) |
---|
80 | */ |
---|
81 | @Override |
---|
82 | public void set(int first, int second, float value) { |
---|
83 | final int row = Math.min(first, second); |
---|
84 | final int col = Math.max(first, second); |
---|
85 | matrix[(row * actualsize) - (((row * (row + 1)) / 2) - (actualsize - col))] = value; |
---|
86 | } |
---|
87 | |
---|
88 | /* (non-Javadoc) |
---|
89 | * @see de.ugoe.cs.autoquest.tasktrees.alignment.matrix.ITriangleMatrix#size() |
---|
90 | */ |
---|
91 | @Override |
---|
92 | public int size() { |
---|
93 | return size; |
---|
94 | } |
---|
95 | |
---|
96 | /* (non-Javadoc) |
---|
97 | * @see java.lang.Object#toString() |
---|
98 | */ |
---|
99 | @Override |
---|
100 | public String toString() { |
---|
101 | String result = ""; |
---|
102 | for (int i = 0; i < actualsize; i++) { |
---|
103 | for (int j = 0; j < actualsize; j++) { |
---|
104 | if (i < j) { |
---|
105 | if (Float.isInfinite(this.get(i, j))) { |
---|
106 | result = result + " -------"; |
---|
107 | } else { |
---|
108 | result = result |
---|
109 | + String.format("%+8.2f", this.get(i, j)); |
---|
110 | } |
---|
111 | } else { |
---|
112 | result = result + (" "); |
---|
113 | } |
---|
114 | } |
---|
115 | result = result + "\n"; |
---|
116 | } |
---|
117 | return result; |
---|
118 | } |
---|
119 | } |
---|